Created
May 14, 2017 22:29
-
-
Save schwehr/a51330a3634dbb4d64a53a08673f0dd4 to your computer and use it in GitHub Desktop.
My take on CPLConfigOptionSetter class from GDAL cpl_conv.{cpp,h}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Totally untested. | |
class CPL_DLL CPLConfigOptionSetter | |
{ | |
public: | |
CPLConfigOptionSetter(const std::string& pszKey, const std::string& osValue, | |
bool bSetOnlyIfUndefined) : | |
m_osKey(osKey), | |
m_osOldValue(CPLGetConfigOption(osKey.c_str(), NULL)), | |
m_bRestoreOldValue(false) | |
{ | |
if( (bSetOnlyIfUndefined && | |
CPLGetConfigOption(osKey.c_str(), NULL) == NULL) || | |
!bSetOnlyIfUndefined ) | |
{ | |
m_bRestoreOldValue = true; | |
CPLSetThreadLocalConfigOption(osKey.c_str(), osValue.c_str()); | |
} | |
} | |
~CPLConfigOptionSetter() | |
{ | |
if( !m_bRestoreOldValue ) return; | |
CPLSetThreadLocalConfigOption(m_osKey.c_str(), m_osOldValue.c_str()); | |
} | |
private: | |
const std::string m_osKey; | |
const std::string m_osOldValue; | |
bool m_bRestoreOldValue; | |
#if HAVE_CXX11 | |
CPLConfigOptionSetter(const CPLConfigOptionSetter&) = delete; | |
CPLConfigOptionSetter& operator=(const CPLConfigOptionSetter&) = delete; | |
#else | |
CPLConfigOptionSetter(const CPLConfigOptionSetter&); | |
CPLConfigOptionSetter& operator=(const CPLConfigOptionSetter&); | |
#endif | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment