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