Add support for size_t to wxConfig

Allow writing and reading size_t values directly too, there were not
previously accepted unlike all the other arithmetic types.

Closes #19091.
This commit is contained in:
Vadim Zeitlin
2021-03-09 21:33:30 +01:00
parent fce8780297
commit 0c837e5310
4 changed files with 65 additions and 1 deletions

View File

@@ -192,6 +192,9 @@ public:
bool Read(const wxString& key, wxLongLong_t *pl, wxLongLong_t defVal) const;
#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
bool Read(const wxString& key, size_t* val) const;
bool Read(const wxString& key, size_t* val, size_t defVal) const;
#if wxUSE_BASE64
// read a binary data block
bool Read(const wxString& key, wxMemoryBuffer* data) const

View File

@@ -571,7 +571,29 @@ public:
@endWxPerlOnly
*/
bool Read(const wxString& key, wxLongLong_t* ll,
wxLongLong_t defaultVal) const;
/**
Reads a size_t value, returning @true if the value was found.
If the value was not found, @a value is not changed.
@since 3.1.5
@beginWxPerlOnly
Not supported by wxPerl.
@endWxPerlOnly
*/
bool Read(const wxString& key, size_t* value) const;
/**
Reads a size_t value, returning @true if the value was found.
If the value was not found, @a defaultVal is used instead.
@since 3.1.5
@beginWxPerlOnly
Not supported by wxPerl.
@endWxPerlOnly
*/
bool Read(const wxString& key, size_t* value,
size_t defaultVal) const;
/**
Reads a double value, returning @true if the value was found. If the
value was not found, @a d is not changed.

View File

@@ -177,6 +177,40 @@ bool wxConfigBase::Read(const wxString& key, int *pi, int defVal) const
return r;
}
// size_t is stored either as long or long long (Win64)
#if SIZEOF_SIZE_T == SIZEOF_LONG
typedef long SizeSameSizeAsSizeT;
#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
typedef wxLongLong_t SizeSameSizeAsSizeT;
#else
#error Unexpected sizeof(size_t)
#endif
bool wxConfigBase::Read(const wxString& key, size_t* val) const
{
wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") );
SizeSameSizeAsSizeT tmp;
if ( !Read(key, &tmp) )
return false;
*val = static_cast<size_t>(tmp);
return true;
}
bool wxConfigBase::Read(const wxString& key, size_t* val, size_t defVal) const
{
wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") );
if ( !Read(key, val) )
{
*val = defVal;
return false;
}
return true;
}
// Read floats as doubles then just type cast it down.
bool wxConfigBase::Read(const wxString& key, float* val) const
{

View File

@@ -53,6 +53,7 @@ TEST_CASE("wxConfig::ReadWriteLocal", "[config]")
const wxULongLong_t uval64 = wxULL(0x9000000000000009);
config->Write("ull", uval64);
config->Write("size", size_t(UINT_MAX));
#ifdef wxHAS_CONFIG_TEMPLATE_RW
config->Write("color1", wxColour(11,22,33,44));
#endif // wxHAS_CONFIG_TEMPLATE_RW
@@ -110,6 +111,10 @@ TEST_CASE("wxConfig::ReadWriteLocal", "[config]")
CHECK( ll == static_cast<wxLongLong_t>(uval64) );
CHECK( config->ReadLongLong("ull", 0) == static_cast<wxLongLong_t>(uval64) );
size_t size;
CHECK( config->Read("size", &size) );
CHECK( size == UINT_MAX );
#ifdef wxHAS_CONFIG_TEMPLATE_RW
wxColour color1;
r = config->Read("color1", &color1);