Skip to content

Commit 25dc2b7

Browse files
<locale>: Hide some non-Standard functions of locale::id (#5067)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent 532670b commit 25dc2b7

File tree

7 files changed

+48
-18
lines changed

7 files changed

+48
-18
lines changed

stl/inc/locale

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ protected:
157157
_EXPORT_STD template <class _Facet>
158158
_NODISCARD bool has_facet(const locale& _Loc) noexcept {
159159
_BEGIN_LOCK(_LOCK_LOCALE) // the thread lock, make get atomic
160-
size_t _Id = _Facet::id;
160+
size_t _Id = _Facet::id._Get_index();
161161
return _Loc._Getfacet(_Id) || _Facet::_Getcat() != static_cast<size_t>(-1);
162162
_END_LOCK()
163163
}

stl/inc/xlocale

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,18 @@ public:
7474

7575
class _CRTIMP2_PURE_IMPORT id { // identifier stamp, unique for each distinct kind of facet
7676
public:
77+
#ifdef _CRTBLD // TRANSITION, ABI: preserved for binary compatibility
7778
__CLR_OR_THIS_CALL id(size_t _Val = 0) : _Id(_Val) {}
79+
#else // ^^^ defined(_CRTBLD) / !defined(_CRTBLD) vvv
80+
template <int = 0> // TRANSITION, ABI
81+
id() noexcept /* strengthened */ {}
82+
#endif // ^^^ !defined(_CRTBLD) ^^^
7883

7984
id(const id&) = delete;
8085
id& operator=(const id&) = delete;
8186

82-
__CLR_OR_THIS_CALL operator size_t() { // get stamp, with lazy allocation
87+
template <int = 0> // TRANSITION, ABI
88+
size_t _Get_index() { // get stamp, with lazy allocation
8389
if (_Id == 0) { // still zero, allocate stamp
8490
_BEGIN_LOCK(_LOCK_LOCALE)
8591
if (_Id == 0) {
@@ -90,8 +96,14 @@ public:
9096
return _Id;
9197
}
9298

99+
#ifdef _CRTBLD // TRANSITION, ABI: preserved for binary compatibility
100+
__CLR_OR_THIS_CALL operator size_t() {
101+
return _Get_index();
102+
}
103+
#endif // defined(_CRTBLD)
104+
93105
private:
94-
size_t _Id; // the identifier stamp
106+
size_t _Id = 0; // the identifier stamp
95107

96108
__PURE_APPDOMAIN_GLOBAL static int _Id_cnt;
97109
};
@@ -223,7 +235,7 @@ public:
223235
_CATCH_END
224236

225237
_Locimp* _Newimp = _Locimp::_New_Locimp(*_Ptr);
226-
_Newimp->_Addfac(_Facptr, _Facet::id);
238+
_Newimp->_Addfac(_Facptr, _Facet::id._Get_index());
227239
_Newimp->_Catmask = none;
228240
_Newimp->_Name = "*";
229241
return locale{_Secret_locale_construct_tag{}, _Newimp};
@@ -232,7 +244,7 @@ public:
232244
template <class _Facet>
233245
locale(const locale& _Loc, const _Facet* _Facptr) : _Ptr(_Locimp::_New_Locimp(*_Loc._Ptr)) {
234246
if (_Facptr) { // replace facet
235-
_Ptr->_Addfac(const_cast<_Facet*>(_Facptr), _Facet::id);
247+
_Ptr->_Addfac(const_cast<_Facet*>(_Facptr), _Facet::id._Get_index());
236248
_Ptr->_Catmask = none;
237249
_Ptr->_Name = "*";
238250
}
@@ -421,7 +433,7 @@ const _Facet& __CRTDECL use_facet(const locale& _Loc) { // get facet reference f
421433
_BEGIN_LOCK(_LOCK_LOCALE) // the thread lock, make get atomic
422434
const locale::facet* _Psave = _Facetptr<_Facet>::_Psave; // static pointer to lazy facet
423435

424-
const size_t _Id = _Facet::id;
436+
const size_t _Id = _Facet::id._Get_index();
425437
const locale::facet* _Pf = _Loc._Getfacet(_Id);
426438

427439
if (!_Pf) {

stl/src/locale.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ _MRTIMP2_PURE locale __CLRCALL_PURE_OR_CDECL locale::global(const locale& loc) {
4747
if ((_CATMASK(Facet::_Getcat()) & cat) == 0) { \
4848
; \
4949
} else if (ptrloc == nullptr) { \
50-
ptrimp->_Addfac(new Facet(lobj), Facet::id); \
50+
ptrimp->_Addfac(new Facet(lobj), Facet::id._Get_index()); \
5151
} else { \
5252
ptrimp->_Addfac( \
5353
const_cast<locale::facet*>(static_cast<const locale::facet*>(&_STD use_facet<Facet>(*ptrloc))), \
54-
Facet::id); \
54+
Facet::id._Get_index()); \
5555
}
5656

5757
using _Tc1 = ctype<char>;

stl/src/locale0.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ __PURE_APPDOMAIN_GLOBAL locale::_Locimp* locale::_Locimp::_Clocptr = nullptr; //
125125

126126
__PURE_APPDOMAIN_GLOBAL int locale::id::_Id_cnt = 0; // unique id counter for facets
127127

128-
__PURE_APPDOMAIN_GLOBAL locale::id ctype<char>::id(0);
128+
__PURE_APPDOMAIN_GLOBAL locale::id ctype<char>::id{};
129129

130-
__PURE_APPDOMAIN_GLOBAL locale::id ctype<wchar_t>::id(0);
130+
__PURE_APPDOMAIN_GLOBAL locale::id ctype<wchar_t>::id{};
131131

132-
__PURE_APPDOMAIN_GLOBAL locale::id codecvt<wchar_t, char, mbstate_t>::id(0);
132+
__PURE_APPDOMAIN_GLOBAL locale::id codecvt<wchar_t, char, mbstate_t>::id{};
133133

134-
__PURE_APPDOMAIN_GLOBAL locale::id ctype<unsigned short>::id(0);
134+
__PURE_APPDOMAIN_GLOBAL locale::id ctype<unsigned short>::id{};
135135

136-
__PURE_APPDOMAIN_GLOBAL locale::id codecvt<unsigned short, char, mbstate_t>::id(0);
136+
__PURE_APPDOMAIN_GLOBAL locale::id codecvt<unsigned short, char, mbstate_t>::id{};
137137

138138
_MRTIMP2_PURE const locale& __CLRCALL_PURE_OR_CDECL locale::classic() { // get reference to "C" locale
139139
#if !defined(_M_CEE_PURE)

stl/src/wlocale.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ _STD_BEGIN
1515
if ((_CATMASK(Facet::_Getcat()) & cat) == 0) { \
1616
; \
1717
} else if (ptrloc == nullptr) { \
18-
ptrimp->_Addfac(new Facet(lobj), Facet::id); \
18+
ptrimp->_Addfac(new Facet(lobj), Facet::id._Get_index()); \
1919
} else { \
2020
ptrimp->_Addfac( \
2121
const_cast<locale::facet*>(static_cast<const locale::facet*>(&_STD use_facet<Facet>(*ptrloc))), \
22-
Facet::id); \
22+
Facet::id._Get_index()); \
2323
}
2424

2525
// moved from locale to ease subsetting
@@ -36,7 +36,7 @@ using _Tw10 = moneypunct<wchar_t, true>;
3636
using _Tw11 = time_get<wchar_t>;
3737
using _Tw12 = time_put<wchar_t>;
3838
using _Tw13 = codecvt<wchar_t, char, _Mbstatet>;
39-
__PURE_APPDOMAIN_GLOBAL locale::id time_put<wchar_t>::id(0);
39+
__PURE_APPDOMAIN_GLOBAL locale::id time_put<wchar_t>::id{};
4040

4141
void __CLRCALL_OR_CDECL locale::_Locimp::_Makewloc(const _Locinfo& lobj, locale::category cat, _Locimp* ptrimp,
4242
const locale* ptrloc) { // setup wide part of a new locale

stl/src/xlocale.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ _STD_BEGIN
1515
if ((_CATMASK(Facet::_Getcat()) & cat) == 0) { \
1616
; \
1717
} else if (ptrloc == nullptr) { \
18-
ptrimp->_Addfac(new Facet(lobj), Facet::id); \
18+
ptrimp->_Addfac(new Facet(lobj), Facet::id._Get_index()); \
1919
} else { \
2020
ptrimp->_Addfac( \
2121
const_cast<locale::facet*>(static_cast<const locale::facet*>(&_STD use_facet<Facet>(*ptrloc))), \
22-
Facet::id); \
22+
Facet::id._Get_index()); \
2323
}
2424

2525
// moved from locale to ease subsetting

tests/std/tests/Dev09_056375_locale_cleanup/test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
#include <cassert>
88
#include <cstdio>
9+
#include <cwchar>
910
#include <locale>
11+
#include <type_traits>
1012

1113
#pragma warning(push) // TRANSITION, OS-23694920
1214
#pragma warning(disable : 4668) // 'MEOW' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
@@ -47,6 +49,22 @@ STATIC_ASSERT(!is_implicitly_default_constructible<locale::facet>);
4749
STATIC_ASSERT(!is_implicitly_default_constructible<ctype<char>>);
4850
STATIC_ASSERT(!is_implicitly_default_constructible<ctype<wchar_t>>);
4951

52+
// Test mandatory locale::id properties and strengthened exception specification.
53+
STATIC_ASSERT(is_nothrow_default_constructible_v<locale::id>); // strengthened
54+
STATIC_ASSERT(!is_copy_constructible_v<locale::id>);
55+
STATIC_ASSERT(!is_move_constructible_v<locale::id>);
56+
STATIC_ASSERT(!is_copy_assignable_v<locale::id>);
57+
STATIC_ASSERT(!is_move_assignable_v<locale::id>);
58+
STATIC_ASSERT(is_nothrow_destructible_v<locale::id>);
59+
60+
// Test that non-Standard locale::id constructor and conversion function are not user-visible.
61+
STATIC_ASSERT(!is_constructible_v<locale::id, size_t>);
62+
STATIC_ASSERT(!is_constructible_v<size_t, locale::id>);
63+
STATIC_ASSERT(!is_constructible_v<size_t, locale::id&>);
64+
STATIC_ASSERT(!is_convertible_v<size_t, locale::id>);
65+
STATIC_ASSERT(!is_convertible_v<locale::id, size_t>);
66+
STATIC_ASSERT(!is_convertible_v<locale::id&, size_t>);
67+
5068
void test_dll() {
5169
puts("Calling dll");
5270
#ifdef _M_CEE

0 commit comments

Comments
 (0)