libmusicbrainz4  4.0.1
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Defines
Entity.h
Go to the documentation of this file.
00001 /* --------------------------------------------------------------------------
00002 
00003    libmusicbrainz4 - Client library to access MusicBrainz
00004 
00005    Copyright (C) 2011 Andrew Hawkins
00006 
00007    This file is part of libmusicbrainz4.
00008 
00009    This library is free software; you can redistribute it and/or
00010    modify it under the terms of v2 of the GNU Lesser General Public
00011    License as published by the Free Software Foundation.
00012 
00013    libmusicbrainz4 is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Lesser General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this library.  If not, see <http://www.gnu.org/licenses/>.
00020 
00021      $Id$
00022 
00023 ----------------------------------------------------------------------------*/
00024 
00025 #ifndef _MUSICBRAINZ4_ENTITY_H
00026 #define _MUSICBRAINZ4_ENTITY_H
00027 
00028 #if defined(__GNUC__) && __GNUC__ >= 4
00029 
00030 #define LIBMB4_DEPRECATED __attribute__ ((deprecated))
00031 
00032 #else
00033 
00034 #define LIBMB4_DEPRECATED
00035 
00036 #endif
00037 
00038 #include <iostream>
00039 #include <string>
00040 #include <sstream>
00041 #include <map>
00042 
00043 #include "musicbrainz4/xmlParser.h"
00044 
00045 namespace MusicBrainz4
00046 {
00047         class CEntityPrivate;
00048 
00049         class CRelationListList;
00050 
00051         class CEntity
00052         {
00053         public:
00054                 CEntity();
00055                 CEntity(const CEntity& Other);
00056                 CEntity& operator =(const CEntity& Other);
00057                 virtual ~CEntity();
00058 
00059                 virtual CEntity *Clone()=0;
00060 
00061                 bool Parse(const XMLNode& Node);
00062 
00063                 std::map<std::string,std::string> ExtAttributes() const;
00064                 std::map<std::string,std::string> ExtElements() const;
00065 
00066                 virtual std::ostream& Serialise(std::ostream& os) const;
00067                 static std::string GetElementName();
00068 
00069         protected:
00070                 bool ProcessRelationList(const XMLNode& Node, CRelationListList* & RetVal);
00071 
00072                 template<typename T>
00073                 bool ProcessItem(const XMLNode& Node, T* & RetVal)
00074                 {
00075                         RetVal=new T(Node);
00076 
00077                         return true;
00078                 }
00079 
00080                 template<class T>
00081                 bool ProcessItem(const XMLNode& Node, T& RetVal)
00082                 {
00083                         bool Ret=true;
00084 
00085                         std::stringstream os;
00086                         if (Node.getText())
00087                                 os << (const char *)Node.getText();
00088 
00089                         os >> RetVal;
00090                         if (os.fail())
00091                         {
00092                                 Ret=false;
00093                                 std::cerr << "Error parsing value '";
00094                                 if (Node.getText())
00095                                         std::cerr << Node.getText();
00096                                 std::cerr << "'" << std::endl;
00097                         }
00098 
00099                         return Ret;
00100                 }
00101 
00102                 template<typename T>
00103                 bool ProcessItem(const std::string& Text, T& RetVal)
00104                 {
00105                         bool Ret=true;
00106 
00107                         std::stringstream os;
00108                         os << Text;
00109 
00110                         os >> RetVal;
00111                         if (os.fail())
00112                         {
00113                                 Ret=false;
00114                                 std::cerr << "Error parsing value '" << Text << "'" << std::endl;
00115                         }
00116 
00117                         return Ret;
00118                 }
00119 
00120                 bool ProcessItem(const XMLNode& Node, std::string& RetVal)
00121                 {
00122                         if (Node.getText())
00123                                 RetVal=Node.getText();
00124 
00125                         return true;
00126 
00127                 }
00128 
00129                 virtual bool ParseAttribute(const std::string& Name, const std::string& Value)=0;
00130                 virtual bool ParseElement(const XMLNode& Node)=0;
00131 
00132         private:
00133                 CEntityPrivate *m_d;
00134 
00135                 void Cleanup();
00136 
00137         };
00138 }
00139 
00140 std::ostream& operator << (std::ostream& os, const MusicBrainz4::CEntity& Entity);
00141 
00142 #endif