libmusicbrainz4  4.0.3
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Macros Pages
ListImpl.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_LIST_IMPL_H
26 #define _MUSICBRAINZ4_LIST_IMPL_H
27 
28 #include "musicbrainz4/List.h"
29 
30 namespace MusicBrainz4
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