CLAM-Development  1.4.0
XMLArrayAdapter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
23 // Class XMLArrayAdapter
24 //
25 
26 #ifndef _XMLArrayAdapter_h
27 #define _XMLArrayAdapter_h
28 
29 #include "BasicXMLable.hxx"
30 #include <sstream>
31 
32 namespace CLAM {
33 
59 template <class T> class XMLArrayAdapter : public BasicXMLable {
60 // Internal Types
61 public:
63  typedef T t_adaptee;
64 // Attributes
65 private:
66  t_adaptee * mAdaptee;
67  unsigned int size;
68 // Construction/Destruction
69 public:
86  XMLArrayAdapter (t_adaptee * anAdaptee, unsigned int nElements, const char * name=NULL, bool isXMLElement=false)
87  : BasicXMLable(name, isXMLElement), mAdaptee(anAdaptee)
88  {
89  size = nElements;
90  }
91  XMLArrayAdapter (const t_adaptee * anAdaptee, unsigned int nElements,
92  const char * name=NULL, bool isXMLElement=false)
93  : BasicXMLable(name, isXMLElement), mAdaptee(const_cast<T*>(anAdaptee))
94  {
95  size = nElements;
96  }
97  virtual ~XMLArrayAdapter() {};
98 
99 // Accessors
100 public:
101  //* @return A string with the extracted XML content
102  std::string XMLContent() const
103  {
104  std::string s;
105  s.resize(size*15);
106  std::stringstream str(s);
107  for (unsigned int i=0; i<size; i++) {
108  str << mAdaptee[i];
109  if (i < size-1) str << " ";
110  }
111  str << std::ends;
112  return str.str();
113  }
114 
115  //* Extracts the content from the stream.
116  bool XMLContent(std::istream & str)
117  {
118  for (unsigned int i=0; i<size; i++) {
119  str >> mAdaptee[i];
120  }
121  return bool(str.good());
122  }
123 
124 // Testing
125 public:
126  //* Check the internal status for a class instance is valid
128  return super::FulfilsInvariant();
129  }
130 };
131 }
132 
133 
134 #endif//_XMLArrayAdapter_h
135