Skip to content

Instantly share code, notes, and snippets.

@schwehr
Created May 14, 2017 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schwehr/a51330a3634dbb4d64a53a08673f0dd4 to your computer and use it in GitHub Desktop.
Save schwehr/a51330a3634dbb4d64a53a08673f0dd4 to your computer and use it in GitHub Desktop.
My take on CPLConfigOptionSetter class from GDAL cpl_conv.{cpp,h}
// 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