//
// DynamicStruct.h
//
// $Id: //poco/Main/Foundation/include/Poco/DynamicStruct.h#9 $
//
// Library: Foundation
// Package: Core
// Module:  DynamicStruct
//
// Definition of the DynamicStruct class.
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// 
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//


#ifndef Foundation_DynamicStruct_INCLUDED
#define Foundation_DynamicStruct_INCLUDED


#include "Poco/Foundation.h"
#include "Poco/DynamicAny.h"
#include "Poco/DynamicAnyHolder.h"
#include <map>
#include <set>


namespace Poco {


class Foundation_API DynamicStruct
	/// DynamicStruct allows to define a named collection of DynamicAny objects.
{
public:
	typedef std::map<std::string, DynamicAny> Data;
	typedef Data::iterator Iterator;
	typedef Data::const_iterator ConstIterator;

	DynamicStruct();
		/// Creates an empty DynamicStruct

	DynamicStruct(const Data &val);
		/// Creates the DynamicStruct from the given value.
	
	virtual ~DynamicStruct();
		/// Destroys the DynamicStruct.

	DynamicAny& operator [] (const std::string& name);
		/// Returns the DynamicAny with the given name, creates an entry if not found.

	const DynamicAny& operator [] (const std::string& name) const;
		/// Returns the DynamicAny with the given name, throws a
		/// NotFoundException if the data member is not found.

	bool contains(const std::string& name) const;
		/// Returns true if the DynamicStruct contains a member with the given 
		/// name

	Iterator find(const std::string& name);
		/// Returns an iterator, pointing to the <name,DynamicAny> pair containing
		/// the element, or it returns end() if the member was not found

	ConstIterator find(const std::string& name) const;
		/// Returns a const iterator, pointing to the <name,DynamicAny> pair containing
		/// the element, or it returns end() if the member was not found

	Iterator end();
		/// Returns the end iterator for the DynamicStruct

	ConstIterator end() const;
		/// Returns the end const iterator for the DynamicStruct

	Iterator begin();
		/// Returns the begin iterator for the DynamicStruct

	ConstIterator begin() const;
		/// Returns the begin const iterator for the DynamicStruct

	std::pair<DynamicStruct::Iterator, bool> insert(const std::string& key, const DynamicAny& value);
		/// Inserts a <name, DynamicAny> pair into the DynamicStruct,
		/// returns a pair containing the iterator and a boolean which
		/// indicates success or not (is true, when insert succeeded, false,
		/// when already another element was present, in this case Iterator
		/// points to that other element)

	std::pair<DynamicStruct::Iterator, bool> insert(const DynamicStruct::Data::value_type& aPair);
		/// Inserts a <name, DynamicAny> pair into the DynamicStruct,
		/// returns a pair containing the iterator and a boolean which
		/// indicates success or not (is true, when insert succeeded, false,
		/// when already another element was present, in this case Iterator
		/// points to that other element)

	DynamicStruct::Data::size_type erase(const std::string& key);
		/// Erases the element if found, returns number of elements deleted

	void erase(DynamicStruct::Iterator it);
		/// Erases the element at the given position

	bool empty() const;
		/// Returns true if the DynamicStruct doesn't contain any members

	DynamicStruct::Data::size_type size() const;
		/// Returns the number of members the DynamicStruct contains

	std::set<std::string> members() const;
		/// Returns a sorted collection containing all member names

private:
	Data _data;
};


//
// inlines
//
inline DynamicAny& DynamicStruct::operator [] (const std::string& name)
{
	return _data[name];
}


inline bool DynamicStruct::contains(const std::string& name) const
{
	return find(name) != end();
}


inline DynamicStruct::Iterator DynamicStruct::find(const std::string& name)
{
	return _data.find(name);
}


inline DynamicStruct::ConstIterator DynamicStruct::find(const std::string& name) const
{
	return _data.find(name);
}


inline DynamicStruct::Iterator DynamicStruct::end()
{
	return _data.end();
}


inline DynamicStruct::ConstIterator DynamicStruct::end() const
{
	return _data.end();
}


inline DynamicStruct::Iterator DynamicStruct::begin()
{
	return _data.begin();
}


inline DynamicStruct::ConstIterator DynamicStruct::begin() const
{
	return _data.begin();
}


inline std::pair<DynamicStruct::Iterator, bool> DynamicStruct::insert(const std::string& key, const DynamicAny& value)
{
	return insert(std::make_pair(key, value));
}


inline std::pair<DynamicStruct::Iterator, bool> DynamicStruct::insert(const DynamicStruct::Data::value_type& aPair)
{
	return _data.insert(aPair);
}


inline DynamicStruct::Data::size_type DynamicStruct::erase(const std::string& key)
{
	return _data.erase(key);
}


inline void DynamicStruct::erase(DynamicStruct::Iterator it)
{
	_data.erase(it);
}


inline bool DynamicStruct::empty() const
{
	return _data.empty();
}


inline DynamicStruct::Data::size_type DynamicStruct::size() const
{
	return _data.size();
}


template <>
class DynamicAnyHolderImpl<DynamicStruct>: public DynamicAnyHolder
{
public:
	DynamicAnyHolderImpl(const DynamicStruct& val): _val(val)
	{
	}

	~DynamicAnyHolderImpl()
	{
	}
	
	const std::type_info& type() const
	{
		return typeid(DynamicStruct);
	}

	void convert(Int8& val) const
	{
		throw BadCastException("Cannot cast DynamicStruct type to Int8");
	}

	void convert(Int16& val) const
	{
		throw BadCastException("Cannot cast DynamicStruct type to Int16");
	}
	
	void convert(Int32& val) const
	{
		throw BadCastException("Cannot cast DynamicStruct type to Int32");
	}

	void convert(Int64& val) const
	{
		throw BadCastException("Cannot cast DynamicStruct type to Int64");
	}

	void convert(UInt8& val) const
	{
		throw BadCastException("Cannot cast DynamicStruct type to UInt8");
	}

	void convert(UInt16& val) const
	{
		throw BadCastException("Cannot cast DynamicStruct type to UInt16");
	}
	
	void convert(UInt32& val) const
	{
		throw BadCastException("Cannot cast DynamicStruct type to UInt32");
	}

	void convert(UInt64& val) const
	{
		throw BadCastException("Cannot cast DynamicStruct type to UInt64");
	}

	void convert(bool& val) const
	{
		throw BadCastException("Cannot cast DynamicStruct type to bool");
	}

	void convert(float& val) const
	{
		throw BadCastException("Cannot cast DynamicStruct type to float");
	}

	void convert(double& val) const
	{
		throw BadCastException("Cannot cast DynamicStruct type to double");
	}

	void convert(char& val) const
	{
		throw BadCastException("Cannot cast DynamicStruct type to char");
	}

	void convert(std::string& val) const
	{
		// Serialize in JSON format: equals an object

		// JSON format definition: { string ':' value } string:value pair n-times, sep. by ','
		val.append("{ ");
		DynamicStruct::ConstIterator it = _val.begin();
		DynamicStruct::ConstIterator itEnd = _val.end();
		if (!_val.empty())
		{
			DynamicAny key(it->first);
			appendJSONString(val, key);
			val.append(" : ");
			appendJSONString(val, it->second);
			++it;
		}
		for (; it != itEnd; ++it)
		{
			val.append(", ");
			DynamicAny key(it->first);
			appendJSONString(val, key);
			val.append(" : ");
			appendJSONString(val, it->second);
		}
		val.append(" }");	
	}

	void convert(Poco::DateTime&) const
	{
		throw BadCastException("DynamicStruct -> Poco::DateTime");
	}

	void convert(Poco::LocalDateTime&) const
	{
		throw BadCastException("DynamicStruct -> Poco::LocalDateTime");
	}

	void convert(Poco::Timestamp&) const
	{
		throw BadCastException("DynamicStruct -> Poco::Timestamp");
	}

	DynamicAnyHolder* clone() const
	{
		return new DynamicAnyHolderImpl(_val);
	}
	
	const DynamicStruct& value() const
	{
		return _val;
	}

	bool isArray() const
	{
		return false;
	}

	bool isStruct() const
	{
		return true;
	}

	bool isInteger() const
	{
		return false;
	}

	bool isSigned() const
	{
		return false;
	}

	bool isNumeric() const
	{
		return false;
	}

	bool isString() const
	{
		return false;
	}

	DynamicAny& operator [] (const std::string& name)
	{
		return _val[name];
	}

	const DynamicAny& operator [] (const std::string& name) const
	{
		return _val[name];
	}

private:
	DynamicStruct _val;
};


} // namespace Poco


#endif // Foundation_DynamicStruct_INCLUDED
