Skip to main content
GameDev.net gamedev.net
🔒 Locked

Exercise: Variant class

Started by Enigma Oct 11, 2004 at 10:23 AM 0 replies 3k views
Original Post
Enigma
Enigma
Hi, I've been interested in "best" use of C++ for a while now. Seeing a couple of questions on Variant datatypes on this forum and rereading a GOTW about it recently I thought it would be interesting to see how I would write such a class. I've included my effort below. My challenge to you is this: How would you implement a Variant class if you didn't have access to boost::any or any other existing Variant implementation. I'll have to trust you to not to peek at the sources to existing Variant implementations while writing your own. Syntax wise I have used an equivalent to the boost::any syntax:
write: Variant variant = TYPE();
read: TYPE value = cast_name<TYPE>(variant);
but you may prefer a different syntax. Finally, if you can't be bothered to try writing your own Variant class, any (constructive) critism of my own effort would be appreciated. I'll be interested to see what other people come up with.
/*
	Write your own version
	before looking at mine!
*/











#include <list>
#include <iostream>
#include <string>

class Variant
{

	private:

		class VariantImplementationBase
		{

			public:

				virtual ~VariantImplementationBase() = 0;
				virtual VariantImplementationBase* clone() const = 0;

		};

		template <typename TYPE>
		class VariantImplementation
			:
			public VariantImplementationBase
		{

			public:

				VariantImplementation(const TYPE& value)
					:
					value_(value)
				{
				}

				VariantImplementationBase* clone() const
				{
					return new VariantImplementation<TYPE>(value_);
				}

				TYPE value_;

		};

	public:

		Variant()
			:
			value_(0)
		{
		}

		template <typename TYPE>
		Variant(TYPE& value)
			:
			value_(new VariantImplementation<TYPE>(value))
		{
		}

		/* buggy
		Variant(const Variant& variant)
			:
			value_(variant.value_->clone())
		{
		}*/

		Variant(const Variant& variant)
			:
			value_(variant.value_ ? variant.value_->clone() : 0)
		{
		}

		~Variant()
		{
			delete value_;
		}

		template <typename TYPE>
		Variant& operator=(const TYPE& value)
		{
			VariantImplementationBase* newValue = new VariantImplementation<TYPE>(value);
			delete value_;
			value_ = newValue;
			return *this;
		}

		/* buggy
		Variant& operator=(const Variant& variant)
		{
			return operator=(variant.value_);
		}*/

		Variant& operator=(const Variant& variant)
		{
			if (variant.value_)
			{
				value_ = variant.value_->clone();
			}
			else
			{
				value_ = 0;
			}
			return *this;
		}

		template <typename TYPE>
		operator TYPE&() const
		{
			VariantImplementation<TYPE>* typedValue = dynamic_cast<VariantImplementation<TYPE>*>(value_);
			if (!typedValue)
			{
				throw "Typecast Exception";
			}
			return typedValue->value_;
		}

	private:

		VariantImplementationBase* value_;

};

Variant::VariantImplementationBase::~VariantImplementationBase()
{
}

template <typename TYPE>
TYPE variant_cast(Variant& v)
{
	return v;
}

void func(int i)
{
	std::cout << "Func: " << i << '\n';
}

int main()
{
	Variant v;

	// store and retrieve an int
	v = int(7);
	std::cout << variant_cast<int>(v) << '\n';

	// pass a variant to a function as if it was a variable
	// of the correct type
	func(v);

	// store and retrieve a string
	v = std::string("test");
	std::cout << variant_cast<std::string>(v) << '\n';

	// store a list
	v = std::list<int>();

	// operate on the actual list by using references
	std::list<int>& l = variant_cast<std::list<int>&>(v);
	l.push_back(8);
	for (std::list<int>::iterator i = variant_cast<std::list<int>&>(v).begin(); i != variant_cast<std::list<int>&>(v).end(); ++i)
	{
		std::cout << *i << '\n';
	}

	// retrive a copy of the list and operate only on the
	// copy
	std::list<int> l2 = variant_cast<std::list<int> >(v);
	l2.push_back(9);
	for (std::list<int>::iterator i = l2.begin(); i != l2.end(); ++i)
	{
		std::cout << *i << '\n';
	}

	// prove that the previous section only affected the
	// copy by printing out the original list
	for (std::list<int>::iterator i = variant_cast<std::list<int>&>(v).begin(); i != variant_cast<std::list<int>&>(v).end(); ++i)
	{
		std::cout << *i << '\n';
	}

	// cause an exception to be thrown by attempting to cast
	// to the wrong type
	std::cout << variant_cast<std::string>(v) << '\n';
}

Enigma EDIT: *whistle* Spotted a couple of bugs in my implementation. [Edited by - Enigma on October 11, 2004 11:49:01 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.