libmusicbrainz4  4.0.0
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: Entity.h 13259 2011-08-10 12:02:50Z adhawkins $
00022 
00023 ----------------------------------------------------------------------------*/
00024 
00025 #ifndef _MUSICBRAINZ4_ENTITY_H
00026 #define _MUSICBRAINZ4_ENTITY_H
00027 
00028 #include <iostream>
00029 #include <string>
00030 #include <sstream>
00031 #include <map>
00032 
00033 #include "musicbrainz4/xmlParser.h"
00034 
00035 namespace MusicBrainz4
00036 {
00037         class CEntityPrivate;
00038 
00039         class CEntity
00040         {
00041         public:
00042                 CEntity();
00043                 CEntity(const CEntity& Other);
00044                 CEntity& operator =(const CEntity& Other);
00045                 virtual ~CEntity();
00046 
00047                 virtual CEntity *Clone()=0;
00048 
00049                 bool Parse(const XMLNode& Node);
00050 
00051                 std::map<std::string,std::string> ExtAttributes() const;
00052                 std::map<std::string,std::string> ExtElements() const;
00053 
00054                 virtual std::ostream& Serialise(std::ostream& os) const;
00055                 static std::string GetElementName();
00056 
00057         protected:
00058                 template<typename T>
00059                 bool ProcessItem(const XMLNode& Node, T* & RetVal)
00060                 {
00061                         RetVal=new T(Node);
00062 
00063                         return true;
00064                 }
00065 
00066                 template<class T>
00067                 bool ProcessItem(const XMLNode& Node, T& RetVal)
00068                 {
00069                         bool Ret=true;
00070 
00071                         std::stringstream os;
00072                         if (Node.getText())
00073                                 os << (const char *)Node.getText();
00074 
00075                         os >> RetVal;
00076                         if (os.fail())
00077                         {
00078                                 Ret=false;
00079                                 std::cerr << "Error parsing value '";
00080                                 if (Node.getText())
00081                                         std::cerr << Node.getText();
00082                                 std::cerr << "'" << std::endl;
00083                         }
00084 
00085                         return Ret;
00086                 }
00087 
00088                 template<typename T>
00089                 bool ProcessItem(const std::string& Text, T& RetVal)
00090                 {
00091                         bool Ret=true;
00092 
00093                         std::stringstream os;
00094                         os << Text;
00095 
00096                         os >> RetVal;
00097                         if (os.fail())
00098                         {
00099                                 Ret=false;
00100                                 std::cerr << "Error parsing value '" << Text << "'" << std::endl;
00101                         }
00102 
00103                         return Ret;
00104                 }
00105 
00106                 bool ProcessItem(const XMLNode& Node, std::string& RetVal)
00107                 {
00108                         if (Node.getText())
00109                                 RetVal=Node.getText();
00110 
00111                         return true;
00112 
00113                 }
00114 
00115                 virtual bool ParseAttribute(const std::string& Name, const std::string& Value)=0;
00116                 virtual bool ParseElement(const XMLNode& Node)=0;
00117 
00118         private:
00119                 CEntityPrivate *m_d;
00120 
00121                 void Cleanup();
00122 
00123         };
00124 }
00125 
00126 std::ostream& operator << (std::ostream& os, const MusicBrainz4::CEntity& Entity);
00127 
00128 #endif