From: Vadim Zeitlin Date: Wed, 15 Aug 2007 20:23:01 +0000 (+0000) Subject: added ReadType convenience functions (patch 1764160) X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/56601ff2db94ccc874107eb60c4564ceb47f6d02?hp=bc1dab7e1f1234abf404a8d055d0cd87b426a995 added ReadType convenience functions (patch 1764160) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48117 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/latex/wx/config.tex b/docs/latex/wx/config.tex index 61c2136e6a..83c1e01824 100644 --- a/docs/latex/wx/config.tex +++ b/docs/latex/wx/config.tex @@ -53,7 +53,7 @@ Here is how you would typically use this class: // another example: using default values and the full path instead of just // key name: if the key is not found , the value 17 is returned - long value = config->Read("/LastRun/CalculatedValues/MaxValue", 17); + long value = config->ReadLong("/LastRun/CalculatedValues/MaxValue", 17); ... ... ... @@ -141,10 +141,10 @@ sensible!): conf->Write("../GroupEntry", 2); conf->SetPath(".."); - wxASSERT( conf->Read("Subgroup/SubgroupEntry", 0l) == 3 ); + wxASSERT( conf->ReadLong("Subgroup/SubgroupEntry", 0) == 3 ); // use absolute path: it is allowed, too - wxASSERT( conf->Read("/RootEntry", 0l) == 1 ); + wxASSERT( conf->ReadLong("/RootEntry", 0) == 1 ); \end{verbatim} {\it Warning}: it is probably a good idea to always restore the path to its @@ -174,7 +174,7 @@ doesn't save and restore the path): foo(config); // we're reading "/Foo/Data/Test" here! -1 will probably be returned... - wxASSERT( config->Read("Test", -1) == 17 ); + wxASSERT( config->ReadLong("Test", -1) == 17 ); } \end{verbatim} @@ -670,28 +670,6 @@ not found, {\it l} is not changed. Reads a long value, returning \true if the value was found. If the value was not found, {\it defaultVal} is used instead. -\constfunc{long }{Read}{\param{const wxString\& }{key}, \param{long}{ defaultVal}} - -Reads a long value from the key and returns it. {\it defaultVal} is returned -if the key is not found. - -NB: writing - -{\small -\begin{verbatim} - conf->Read("key", 0); -\end{verbatim} -} - -won't work because the call is ambiguous: compiler can not choose between two -{\it Read} functions. Instead, write: - -{\small -\begin{verbatim} - conf->Read("key", 0l); -\end{verbatim} -} - \constfunc{bool}{Read}{\param{const wxString\& }{ key}, \param{double*}{ d}} Reads a double value, returning \true if the value was found. If the value was @@ -755,6 +733,39 @@ implements the following methods:\par }} +\membersection{wxConfigBase::ReadBool}\label{wxconfigbasereadbool} + +\constfunc{long }{ReadBool}{\param{const wxString\& }{key}, \param{bool}{ defaultVal}} + +Reads a bool value from the key and returns it. {\it defaultVal} is returned +if the key is not found. + + +\membersection{wxConfigBase::ReadDouble}\label{wxconfigbasereaddouble} + +\constfunc{long }{ReadDouble}{\param{const wxString\& }{key}, \param{double}{ defaultVal}} + +Reads a double value from the key and returns it. {\it defaultVal} is returned +if the key is not found. + + +\membersection{wxConfigBase::ReadLong}\label{wxconfigbasereadlong} + +\constfunc{long }{ReadLong}{\param{const wxString\& }{key}, \param{long}{ defaultVal}} + +Reads a long value from the key and returns it. {\it defaultVal} is returned +if the key is not found. + + +\membersection{wxConfigBase::ReadObject}\label{wxconfigbasereadobject} + +\constfunc{T }{ReadObject}{\param{const wxString\& }{key}, \param{T const&}{ defaultVal}} + +Reads a value of type T, for which function +\helpref{wxFromString}{wxfromstring} is defined, from the key and returns it. +{\it defaultVal} is returned if the key is not found. + + \membersection{wxConfigBase::RenameEntry}\label{wxconfigbaserenameentry} \func{bool}{RenameEntry}{\param{const wxString\& }{ oldName}, \param{const wxString\& }{ newName}} diff --git a/include/wx/confbase.h b/include/wx/confbase.h index 9d97e49680..30d436b3e4 100644 --- a/include/wx/confbase.h +++ b/include/wx/confbase.h @@ -211,8 +211,7 @@ public: } #endif - // convenience functions returning directly the value (we don't have them for - // int/double/bool as there would be ambiguities with the long one then) + // convenience functions returning directly the value wxString Read(const wxString& key, const wxString& defVal = wxEmptyString) const { wxString s; (void)Read(key, &s, defVal); return s; } @@ -226,10 +225,25 @@ public: { return Read(key, wxString(defVal)); } #endif - long Read(const wxString& key, long defVal) const + long ReadLong(const wxString& key, long defVal) const { long l; (void)Read(key, &l, defVal); return l; } - // write the value (return true on success) + double ReadDouble(const wxString& key, double defVal) const + { double d; (void)Read(key, &d, defVal); return d; } + + bool ReadBool(const wxString& key, bool defVal) const + { bool b; (void)Read(key, &b, defVal); return b; } + + template + T ReadObject(const wxString& key, T const& defVal) const + { T t; (void)Read(key, &t, defVal); return t; } + + // for compatibility with wx 2.8 + long Read(const wxString& key, long defVal) const + { return ReadLong(key, defVal); } + + + // write the value (return true on success) bool Write(const wxString& key, const wxString& value) { return DoWriteString(key, value); } diff --git a/tests/config/config.cpp b/tests/config/config.cpp index e02693454d..408d4d7b11 100644 --- a/tests/config/config.cpp +++ b/tests/config/config.cpp @@ -95,19 +95,32 @@ void ConfigTestCase::ReadWriteLocalTest() CPPUNIT_ASSERT( r ); CPPUNIT_ASSERT_EQUAL( long1, 234L ); + CPPUNIT_ASSERT( config->ReadLong(wxT("long1"), 0) == 234 ); + + double double1; + r = config->Read(wxT("double1"), &double1); + CPPUNIT_ASSERT( r ); + CPPUNIT_ASSERT_EQUAL( double1, 345.67 ); + + CPPUNIT_ASSERT( config->ReadDouble(wxT("double1"), 0) == double1 ); + bool bool1; - r = config->Read(wxT("foo"), &bool1); + r = config->Read(wxT("foo"), &bool1); // there is no "foo" key CPPUNIT_ASSERT( !r ); r = config->Read(wxT("bool1"), &bool1); CPPUNIT_ASSERT( r ); CPPUNIT_ASSERT_EQUAL( bool1, true ); + CPPUNIT_ASSERT( config->ReadBool(wxT("bool1"), false) == bool1 ); + wxColour color1; r = config->Read(wxT("color1"), &color1); CPPUNIT_ASSERT( r ); CPPUNIT_ASSERT( color1 == wxColour(11,22,33,44) ); + CPPUNIT_ASSERT( config->ReadObject(wxT("color1"), wxNullColour) == color1 ); + config->DeleteAll(); delete config; }