Click here to Skip to main content
15,886,873 members
Articles / Programming Languages / Visual C++ 9.0

Protean Charm or Binding Objects in C++

Rate me:
Please Sign up or sign in to vote.
4.85/5 (16 votes)
22 Mar 2009CPOL5 min read 29.6K   83   18  
The article describes a smart binding of C++ objects.
#ifndef	_Conversion_H
#define	_Conversion_H

#include	"Reflection.h"

#include	<string>
#include	<vector>
using	namespace	std;

string	Object2String	(Double*	object);
string	Object2String	(Int*		object);
string	Object2String	(Long*		object);
string	Object2String	(Bool*		object);
string	Object2String	(String*	object);

bool	String2Object	(Double*	object, string& value);
bool	String2Object	(Int*		object, string& value);
bool	String2Object	(Long*		object, string& value);
bool	String2Object	(Bool*		object, string& value);
bool	String2Object	(String*	object, string& value);

class	ConversionHelper;

class	IConversion
{
public:
	IConversion	(const string& name);
	virtual			~IConversion	();
	virtual	string	Object2String	(object_ptr& objectPtr)					= 0;
	virtual	void	String2Object	(string& value, object_ptr& objectPtr)	= 0;

//	Property
	string&	Name()	{ return name_; }

private:
	string				name_;
	ConversionHelper*	conversionHelper_;
};

template<typename T>
class	ConversionBase : public IConversion
{
public:
	ConversionBase<T>	() : IConversion(typeid(T).name()) {}

	virtual	string	Object2String(object_ptr& objectPtr)
	{
		return	::Object2String(reinterpret_cast<T*>(objectPtr.value()));
	}

	virtual	void	String2Object	(string& value, object_ptr& objectPtr)
	{
		::String2Object(reinterpret_cast<T*>(objectPtr.value()),value);
	}
};

#endif

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Israel Israel
MSc in System Engineering from Tallinn Technical University, Estonia. Currently, I work in a hitech enterprise in Israel.

Comments and Discussions