Blame include/musicbrainz5/Entity.h

Packit f70c98
/* --------------------------------------------------------------------------
Packit f70c98
Packit f70c98
   libmusicbrainz5 - Client library to access MusicBrainz
Packit f70c98
Packit f70c98
   Copyright (C) 2012 Andrew Hawkins
Packit f70c98
Packit f70c98
   This file is part of libmusicbrainz5.
Packit f70c98
Packit f70c98
   This library is free software; you can redistribute it and/or
Packit f70c98
   modify it under the terms of the GNU Lesser General Public
Packit f70c98
   License as published by the Free Software Foundation; either
Packit f70c98
   version 2.1 of the License, or (at your option) any later version.
Packit f70c98
Packit f70c98
   libmusicbrainz5 is distributed in the hope that it will be useful,
Packit f70c98
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit f70c98
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit f70c98
   Lesser General Public License for more details.
Packit f70c98
Packit f70c98
   You should have received a copy of the GNU General Public License
Packit f70c98
   along with this library.  If not, see <http://www.gnu.org/licenses/>.
Packit f70c98
Packit f70c98
     $Id$
Packit f70c98
Packit f70c98
----------------------------------------------------------------------------*/
Packit f70c98
Packit f70c98
#ifndef _MUSICBRAINZ5_ENTITY_H
Packit f70c98
#define _MUSICBRAINZ5_ENTITY_H
Packit f70c98
Packit f70c98
#include <iostream>
Packit f70c98
#include <string>
Packit f70c98
#include <sstream>
Packit f70c98
#include <map>
Packit f70c98
Packit f70c98
#include "musicbrainz5/xmlParser.h"
Packit f70c98
Packit f70c98
namespace MusicBrainz5
Packit f70c98
{
Packit f70c98
	class CEntityPrivate;
Packit f70c98
Packit f70c98
	class CRelationListList;
Packit f70c98
Packit f70c98
	class CEntity
Packit f70c98
	{
Packit f70c98
	public:
Packit f70c98
		CEntity();
Packit f70c98
		CEntity(const CEntity& Other);
Packit f70c98
		CEntity& operator =(const CEntity& Other);
Packit f70c98
		virtual ~CEntity();
Packit f70c98
Packit f70c98
		virtual CEntity *Clone()=0;
Packit f70c98
Packit f70c98
		void Parse(const XMLNode& Node);
Packit f70c98
Packit f70c98
		std::map<std::string,std::string> ExtAttributes() const;
Packit f70c98
		std::map<std::string,std::string> ExtElements() const;
Packit f70c98
Packit f70c98
		virtual std::ostream& Serialise(std::ostream& os) const;
Packit f70c98
		static std::string GetElementName();
Packit f70c98
Packit f70c98
	protected:
Packit f70c98
		void ProcessRelationList(const XMLNode& Node, CRelationListList* & RetVal);
Packit f70c98
Packit f70c98
		template<typename T>
Packit f70c98
		void ProcessItem(const XMLNode& Node, T* & RetVal)
Packit f70c98
		{
Packit f70c98
			RetVal=new T(Node);
Packit f70c98
		}
Packit f70c98
Packit f70c98
		template<class T>
Packit f70c98
		void ProcessItem(const XMLNode& Node, T& RetVal)
Packit f70c98
		{
Packit f70c98
			std::stringstream os;
Packit f70c98
			if (Node.getText())
Packit f70c98
				os << (const char *)Node.getText();
Packit f70c98
Packit f70c98
			os >> RetVal;
Packit f70c98
			if (os.fail())
Packit f70c98
			{
Packit 853e2d
#ifdef _MB5_DEBUG_
Packit f70c98
				std::cerr << "Error parsing value '";
Packit f70c98
				if (Node.getText())
Packit f70c98
					std::cerr << Node.getText();
Packit f70c98
				std::cerr << "'" << std::endl;
Packit 853e2d
#endif
Packit f70c98
			}
Packit f70c98
		}
Packit f70c98
Packit f70c98
		template<typename T>
Packit f70c98
		void ProcessItem(const std::string& Text, T& RetVal)
Packit f70c98
		{
Packit f70c98
			std::stringstream os;
Packit f70c98
			os << Text;
Packit f70c98
Packit f70c98
			os >> RetVal;
Packit f70c98
			if (os.fail())
Packit f70c98
			{
Packit 853e2d
#ifdef _MB5_DEBUG_
Packit f70c98
				std::cerr << "Error parsing value '" << Text << "'" << std::endl;
Packit 853e2d
#endif
Packit f70c98
			}
Packit f70c98
		}
Packit f70c98
Packit f70c98
		void ProcessItem(const XMLNode& Node, std::string& RetVal)
Packit f70c98
		{
Packit f70c98
			if (Node.getText())
Packit f70c98
				RetVal=Node.getText();
Packit f70c98
		}
Packit f70c98
Packit f70c98
		virtual void ParseAttribute(const std::string& Name, const std::string& Value)=0;
Packit f70c98
		virtual void ParseElement(const XMLNode& Node)=0;
Packit f70c98
Packit f70c98
	private:
Packit f70c98
		CEntityPrivate *m_d;
Packit f70c98
Packit f70c98
		void Cleanup();
Packit f70c98
	};
Packit f70c98
}
Packit f70c98
Packit f70c98
std::ostream& operator << (std::ostream& os, const MusicBrainz5::CEntity& Entity);
Packit f70c98
Packit f70c98
#endif