Click here to Skip to main content
15,896,118 members
Articles / Programming Languages / C++

Simple Stack-Based Wrapper for Windows and XML Registries

Rate me:
Please Sign up or sign in to vote.
4.19/5 (14 votes)
9 Aug 20049 min read 73.8K   796   19  
Left to my own devices, I tend to leak handles, leave keys open too long, close and re-open keys too often, and generally make a mess of the whole thing. And frankly, so do most of you (no insult intended).
////////////////////////////////////////////////////////////////////////////////
// forwards
//

class RegKeyImpl;
class RegValueImpl;

class RegKey;
class RegValue;


////////////////////////////////////////////////////////////////////////////////
// class RegKey
//

class RegKey
{
public:
	RegKey(const RegKey& other);
	~RegKey();

public:
	RegKey& operator=(const RegKey& rhs);

public:
	static
	RegKey		FromHKEY(HKEY root, bool& result);

public:
	RegKey		Key(LPCTSTR subKeyName) const;
	RegValue	Value(LPCTSTR valueName) const;

private:
	RegKey(RegKeyImpl*);

private:
	RegKeyImpl*	m_pImpl;
};



////////////////////////////////////////////////////////////////////////////////
// class RegValue
//

class RegValue
{
public:
	RegValue(const RegValue& other);
	~RegValue();

public:
	RegValue& operator=(const RegValue& rhs);

public:
	bool Get(bool& out);
	bool Set(const bool& in);

	bool Get(int& out);
	bool Set(const int& in);

	bool Get(double& out);
	bool Set(const double& in);

	bool Get(LPTSTR out, int size);
	bool Set(LPCTSTR in);

	bool Get(void* out, int size, unsigned long& out_count);
	bool Set(void* in,  int size);

	template <class T>
	bool GetT(T& out)
	{ unsigned long out_count; return (Get((void*)&out, sizeof(T), out_count) && (out_count == sizeof(T))); }

	template <class T>
	bool SetT(const T& in)
	{ return Set((void*)&in, sizeof(T)); }

private:
	RegValue(RegValueImpl*);

private:
	RegValueImpl* m_pImpl;
	friend RegKey;
};


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
A compiler warns of bogasity, ignore it at your peril. Unless you've done the compiler's job yourself, don't criticize it.

Comments and Discussions