libmusicbrainz5  5.0.1
ListImpl.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 v2 of the GNU Lesser General Public
11  License as published by the Free Software Foundation.
12 
13  libmusicbrainz5 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 _MUSICBRAINZ5_LIST_IMPL_H
26 #define _MUSICBRAINZ5_LIST_IMPL_H
27 
28 #include "musicbrainz5/List.h"
29 
30 namespace MusicBrainz5
31 {
32  template <class T>
33  class CListImpl: public CList
34  {
35  public:
36  CListImpl(const XMLNode& Node=XMLNode::emptyNode())
37  : CList()
38  {
39  if (!Node.isEmpty())
40  {
41  //std::cout << T::GetElementName() << " List node: " << std::endl << Node.createXMLString(true) << std::endl;
42 
43  Parse(Node);
44  }
45  }
46 
47  CListImpl(const CListImpl<T>& Other)
48  : CList()
49  {
50  *this=Other;
51  }
52 
54  {
55  if (this!=&Other)
56  {
57  CList::operator =(Other);
58  }
59 
60  return *this;
61  }
62 
63  virtual ~CListImpl()
64  {
65  }
66 
68  {
69  return new CListImpl<T>(*this);
70  }
71 
72  virtual std::ostream& Serialise(std::ostream& os) const
73  {
74  os << T::GetElementName() << " List (impl):" << std::endl;
75 
76  CList::Serialise(os);
77 
78  for (int count=0;count<NumItems();count++)
79  {
80  T *ThisItem=Item(count);
81 
82  os << *ThisItem << std::endl;
83  }
84 
85  return os;
86  }
87 
88  static std::string GetElementName()
89  {
90  return "";
91  }
92 
93  T *Item(int Item) const
94  {
95  return dynamic_cast<T *>(CList::Item(Item));
96  }
97 
98  void AddItem(T *Item)
99  {
100  CList::AddItem(Item);
101  }
102 
103  protected:
104  void ParseElement(const XMLNode& Node)
105  {
106  std::string NodeName=Node.getName();
107 
108  if (T::GetElementName()==NodeName)
109  {
110  T *Item=0;
111 
112  ProcessItem(Node,Item);
113  AddItem(Item);
114  }
115  else
116  CList::ParseElement(Node);
117  }
118  };
119 }
120 
121 #endif
CListImpl(const XMLNode &Node=XMLNode::emptyNode())
Definition: ListImpl.h:36
CEntity * Item(int Item) const
Definition: Alias.h:35
void ProcessItem(const XMLNode &Node, T *&RetVal)
Definition: Entity.h:63
CList & operator=(const CList &Other)
CListImpl< T > * Clone()
Definition: ListImpl.h:67
T * Item(int Item) const
Definition: ListImpl.h:93
virtual std::ostream & Serialise(std::ostream &os) const
Definition: ListImpl.h:72
void ParseElement(const XMLNode &Node)
Definition: ListImpl.h:104
virtual void ParseElement(const XMLNode &Node)
int NumItems() const
Definition: ListImpl.h:33
MusicBrainz5::CListImpl< T > & operator=(const CListImpl< T > &Other)
Definition: ListImpl.h:53
virtual ~CListImpl()
Definition: ListImpl.h:63
void AddItem(T *Item)
Definition: ListImpl.h:98
void AddItem(CEntity *Item)
virtual std::ostream & Serialise(std::ostream &os) const
static std::string GetElementName()
Definition: ListImpl.h:88
void Parse(const XMLNode &Node)
CListImpl(const CListImpl< T > &Other)
Definition: ListImpl.h:47
Definition: List.h:36