libmusicbrainz4  4.0.3
Entity.h
Go to the documentation of this file.
1 /* --------------------------------------------------------------------------
2 
3  libmusicbrainz4 - Client library to access MusicBrainz
4 
5  Copyright (C) 2011 Andrew Hawkins
6 
7  This file is part of libmusicbrainz4.
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of v2 of the GNU Lesser General Public
11  License as published by the Free Software Foundation.
12 
13  libmusicbrainz4 is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this library. If not, see <http://www.gnu.org/licenses/>.
20 
21  $Id$
22 
23 ----------------------------------------------------------------------------*/
24 
25 #ifndef _MUSICBRAINZ4_ENTITY_H
26 #define _MUSICBRAINZ4_ENTITY_H
27 
28 #include <iostream>
29 #include <string>
30 #include <sstream>
31 #include <map>
32 
33 #include "musicbrainz4/xmlParser.h"
34 
35 namespace MusicBrainz4
36 {
37  class CEntityPrivate;
38 
39  class CRelationListList;
40 
41  class CEntity
42  {
43  public:
44  CEntity();
45  CEntity(const CEntity& Other);
46  CEntity& operator =(const CEntity& Other);
47  virtual ~CEntity();
48 
49  virtual CEntity *Clone()=0;
50 
51  void Parse(const XMLNode& Node);
52 
53  std::map<std::string,std::string> ExtAttributes() const;
54  std::map<std::string,std::string> ExtElements() const;
55 
56  virtual std::ostream& Serialise(std::ostream& os) const;
57  static std::string GetElementName();
58 
59  protected:
60  void ProcessRelationList(const XMLNode& Node, CRelationListList* & RetVal);
61 
62  template<typename T>
63  void ProcessItem(const XMLNode& Node, T* & RetVal)
64  {
65  RetVal=new T(Node);
66  }
67 
68  template<class T>
69  void ProcessItem(const XMLNode& Node, T& RetVal)
70  {
71  std::stringstream os;
72  if (Node.getText())
73  os << (const char *)Node.getText();
74 
75  os >> RetVal;
76  if (os.fail())
77  {
78  std::cerr << "Error parsing value '";
79  if (Node.getText())
80  std::cerr << Node.getText();
81  std::cerr << "'" << std::endl;
82  }
83  }
84 
85  template<typename T>
86  void ProcessItem(const std::string& Text, T& RetVal)
87  {
88  std::stringstream os;
89  os << Text;
90 
91  os >> RetVal;
92  if (os.fail())
93  {
94  std::cerr << "Error parsing value '" << Text << "'" << std::endl;
95  }
96  }
97 
98  void ProcessItem(const XMLNode& Node, std::string& RetVal)
99  {
100  if (Node.getText())
101  RetVal=Node.getText();
102  }
103 
104  virtual void ParseAttribute(const std::string& Name, const std::string& Value)=0;
105  virtual void ParseElement(const XMLNode& Node)=0;
106 
107  private:
108  CEntityPrivate *m_d;
109 
110  void Cleanup();
111  };
112 }
113 
114 std::ostream& operator << (std::ostream& os, const MusicBrainz4::CEntity& Entity);
115 
116 #endif
std::map< std::string, std::string > ExtAttributes() const
void ProcessItem(const std::string &Text, T &RetVal)
Definition: Entity.h:86
Definition: Entity.h:41
void ProcessItem(const XMLNode &Node, std::string &RetVal)
Definition: Entity.h:98
void ProcessItem(const XMLNode &Node, T *&RetVal)
Definition: Entity.h:63
virtual void ParseAttribute(const std::string &Name, const std::string &Value)=0
virtual void ParseElement(const XMLNode &Node)=0
void ProcessItem(const XMLNode &Node, T &RetVal)
Definition: Entity.h:69
void Parse(const XMLNode &Node)
virtual CEntity * Clone()=0
std::map< std::string, std::string > ExtElements() const
static std::string GetElementName()
Definition: RelationListList.h:38
void ProcessRelationList(const XMLNode &Node, CRelationListList *&RetVal)
Definition: Alias.h:35
virtual std::ostream & Serialise(std::ostream &os) const
CEntity & operator=(const CEntity &Other)
std::ostream & operator<<(std::ostream &os, const MusicBrainz4::CEntity &Entity)