diff --git a/include/wx/confbase.h b/include/wx/confbase.h index 88e4c641ba..9073d602d5 100644 --- a/include/wx/confbase.h +++ b/include/wx/confbase.h @@ -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 diff --git a/interface/wx/config.h b/interface/wx/config.h index 6d579480c0..4c3f71dae5 100644 --- a/interface/wx/config.h +++ b/interface/wx/config.h @@ -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. diff --git a/src/common/config.cpp b/src/common/config.cpp index 22ff3fa5d1..93d6442d2d 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -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(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 { diff --git a/tests/config/config.cpp b/tests/config/config.cpp index 1b60d1ccdd..e5f13d03a8 100644 --- a/tests/config/config.cpp +++ b/tests/config/config.cpp @@ -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(uval64) ); CHECK( config->ReadLongLong("ull", 0) == static_cast(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);