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:
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 #ifdef _MB5_DEBUG_
80  std::cerr << "Error parsing value '";
81  if (Node.getText())
82  std::cerr << Node.getText();
83  std::cerr << "'" << std::endl;
84 #endif
85  }
86  }
87 
88  template<typename T>
89  void ProcessItem(const std::string& Text, T& RetVal)
90  {
91  std::stringstream os;
92  os << Text;
93 
94  os >> RetVal;
95  if (os.fail())
96  {
97 #ifdef _MB5_DEBUG_
98  std::cerr << "Error parsing value '" << Text << "'" << std::endl;
99 #endif
100  }
101  }
102 
103  void ProcessItem(const XMLNode& Node, std::string& RetVal)
104  {
105  if (Node.getText())
106  RetVal=Node.getText();
107  }
108 
109  virtual void ParseAttribute(const std::string& Name, const std::string& Value)=0;
110  virtual void ParseElement(const XMLNode& Node)=0;
111 
112  private:
113  CEntityPrivate *m_d;
114 
115  void Cleanup();
116  };
117 }
118 
119 std::ostream& operator << (std::ostream& os, const MusicBrainz5::CEntity& Entity);
120 
121 #endif
std::ostream & operator<<(std::ostream &os, const MusicBrainz5::CEntity &Entity)
Definition: Entity.h:43
void ProcessItem(const XMLNode &Node, std::string &RetVal)
Definition: Entity.h:103
void ProcessItem(const XMLNode &Node, T &RetVal)
Definition: Entity.h:70
void ProcessRelationList(const XMLNode &Node, CRelationListList *&RetVal)
virtual void ParseElement(const XMLNode &Node)=0
std::map< std::string, std::string > ExtAttributes() const
static std::string GetElementName()
void Parse(const XMLNode &Node)
void ProcessItem(const XMLNode &Node, T *&RetVal)
Definition: Entity.h:64
virtual std::ostream & Serialise(std::ostream &os) const
CEntity(const CEntity &Other)
std::map< std::string, std::string > ExtElements() const
void ProcessItem(const std::string &Text, T &RetVal)
Definition: Entity.h:89
virtual CEntity * Clone()=0
CEntity & operator=(const CEntity &Other)
virtual void ParseAttribute(const std::string &Name, const std::string &Value)=0
Definition: RelationListList.h:40
Definition: Alias.h:37