Jump to content
Sign in to follow this  
  • entries
  • comments
  • views

In case you need to deal with environment variables

Sign in to follow this  



[source lang="cpp"]
#ifndef EnvironmentVariableMap_h___
#define EnvironmentVariableMap_h___


class EnvironmentVariableMap; // forward declaration

class EnvironmentVariable
friend class EnvironmentVariableMap;
std::string m_key;
EnvironmentVariable (const std::string key = "") : m_key(key) { }
EnvironmentVariable& operator= (std::string& value);
operator std::string() const;

bool operator== (EnvironmentVariable& lhs, std::string& rhs);
bool operator== (std::string& lhs, EnvironmentVariable& rhs);

class EnvironmentVariableMap
EnvironmentVariableMap() { }
static EnvironmentVariableMap* sm_this;
static EnvironmentVariableMap& getSingleton();
static EnvironmentVariableMap* getSingletonPtr() { return sm_this; }
std::string get(const std::string& key);
void set(const std::string& key, const std::string& value);
EnvironmentVariable& operator[] (const std::string& key);

#endif // EnvironmentVariableMap_h___


[source lang="cpp"]
#include "EnvironmentVariableMap.h"

#if defined(__WIN32__) || defined(_WIN32)
# define putenv _putenv

using namespace std;

EnvironmentVariableMap* EnvironmentVariableMap::sm_this = 0;

EnvironmentVariableMap& EnvironmentVariableMap::getSingleton ()
if (0 == sm_this)
sm_this = new EnvironmentVariableMap();

return *sm_this;

string EnvironmentVariableMap::get(const string &key)
return string(getenv(key.c_str()));

void EnvironmentVariableMap::set(const string &key, const string &value)
string setline(key);
setline += "=";
setline += value;


EnvironmentVariable& EnvironmentVariableMap::operator[] (const string& key)
EnvironmentVariable* tmp = new EnvironmentVariable(key);
return *tmp;

EnvironmentVariable& EnvironmentVariable::operator= (string &value)
EnvironmentVariableMap::getSingleton().set(m_key, value);
return *this;

EnvironmentVariable::operator string () const
return EnvironmentVariableMap::getSingleton().get(this->m_key);

bool operator== (EnvironmentVariable &lhs, string &rhs)
return string(lhs) == rhs;

bool operator== (string &lhs, EnvironmentVariable &rhs)
return lhs == string(rhs);

This code can possibly be improved by adding a cache for environment variables, but then any environment changes need to be made through the class. Use of EnvironmentVariableMap is easy. But it doesn't like C-style strings, since I never bothered to deal with them. You can add that, too, if you want
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!