Atlas-C++
Element.h
1 // This file may be redistributed and modified only under the terms of
2 // the GNU Lesser General Public License (See COPYING for details).
3 // Copyright (C) 2000-2001 Stefanus Du Toit, Karsten-O. Laux and Al Riddoch
4 
5 // $Id$
6 
7 #ifndef ATLAS_MESSAGE_ELEMENT_H
8 #define ATLAS_MESSAGE_ELEMENT_H
9 
10 #include <Atlas/Exception.h>
11 #include <Atlas/float.h>
12 
13 #include <string>
14 #include <map>
15 #include <vector>
16 
17 namespace Atlas { namespace Message {
18 
21 {
22  public:
23  WrongTypeException() : Atlas::Exception("Wrong Message::Element type") { }
24 };
25 
26 class Element;
27 
28 typedef long IntType;
29 typedef double FloatType;
30 typedef void * PtrType;
31 typedef std::string StringType;
32 typedef std::map<std::string, Element> MapType;
33 typedef std::vector<Element> ListType;
34 
60 class Element
61 {
62 public:
63  enum Type {
64  TYPE_NONE,
65  TYPE_INT,
66  TYPE_FLOAT,
67  TYPE_PTR,
68  TYPE_STRING,
69  TYPE_MAP,
70  TYPE_LIST
71  };
72 
73 private:
74  // These are now legacy typedefs. New code should use the
75  // Atlas::Message::*Type versions.
76  typedef Atlas::Message::IntType IntType;
77  typedef Atlas::Message::FloatType FloatType;
78  typedef Atlas::Message::PtrType PtrType;
79  typedef Atlas::Message::StringType StringType;
80  typedef Atlas::Message::MapType MapType;
81  typedef Atlas::Message::ListType ListType;
82 
84  void clear(Type new_type = TYPE_NONE);
85 
86 public:
89  : t(TYPE_NONE)
90  {
91  }
92 
94  ~Element()
95  {
96  clear();
97  }
98 
100  Element(const Element& obj);
101 
103  Element(int v)
104  : t(TYPE_INT), i(v)
105  {
106  }
107 
109  Element(bool v)
110  : t(TYPE_INT), i(v)
111  {
112  }
113 
115  Element(IntType v)
116  : t(TYPE_INT), i(v)
117  {
118  }
119 
121  Element(float v)
122  : t(TYPE_FLOAT), f(v)
123  {
124  }
125 
127  Element(FloatType v)
128  : t(TYPE_FLOAT), f(v)
129  {
130  }
131 
133  Element(PtrType v)
134  : t(TYPE_PTR), p(v)
135  {
136  }
137 
139  Element(const char* v)
140  : t(TYPE_STRING)
141  {
142  if(v)
143  s = new DataType<StringType>(v);
144  else
145  s = new DataType<StringType>();
146  }
147 
149  Element(const StringType& v)
150  : t(TYPE_STRING)
151  {
152  s = new DataType<StringType>(v);
153  }
155  Element(const MapType& v)
156  : t(TYPE_MAP)
157  {
158  m = new DataType<MapType>(v);
159  }
161  Element(const ListType& v)
162  : t(TYPE_LIST)
163  {
164  l = new DataType<ListType>(v);
165  }
166 
168  Element& operator=(const Element& obj);
169 
170  Element& operator=(int v)
171  {
172  if (TYPE_INT != t)
173  {
174  clear(TYPE_INT);
175  }
176  i = v;
177  return *this;
178  }
179 
180  Element& operator=(bool v)
181  {
182  if (TYPE_INT != t)
183  {
184  clear(TYPE_INT);
185  }
186  i = v;
187  return *this;
188  }
189 
190  Element& operator=(IntType v)
191  {
192  if (TYPE_INT != t)
193  {
194  clear(TYPE_INT);
195  }
196  i = v;
197  return *this;
198  }
199 
200  Element& operator=(float v)
201  {
202  if (TYPE_FLOAT != t)
203  {
204  clear(TYPE_FLOAT);
205  }
206  f = v;
207  return *this;
208  }
209 
210  Element& operator=(FloatType v)
211  {
212  if (TYPE_FLOAT != t)
213  {
214  clear(TYPE_FLOAT);
215  }
216  f = v;
217  return *this;
218  }
219 
220  Element& operator=(PtrType v)
221  {
222  if (TYPE_PTR != t)
223  {
224  clear(TYPE_PTR);
225  }
226  p = v;
227  return *this;
228  }
229 
230  Element& operator=(const char * v)
231  {
232  if (TYPE_STRING != t || !s->unique())
233  {
234  clear(TYPE_STRING);
235  s = new DataType<StringType>(v);
236  } else {
237  *s = StringType(v);
238  }
239  return *this;
240  }
241 
242  Element& operator=(const StringType & v)
243  {
244  if (TYPE_STRING != t || !s->unique())
245  {
246  clear(TYPE_STRING);
247  s = new DataType<StringType>(v);
248  } else {
249  *s = v;
250  }
251  return *this;
252  }
253 
254  Element& operator=(const MapType & v)
255  {
256  if (TYPE_MAP != t || !m->unique())
257  {
258  clear(TYPE_MAP);
259  m = new DataType<MapType>(v);
260  } else {
261  *m = v;
262  }
263  return *this;
264  }
265 
266  Element& operator=(const ListType & v)
267  {
268  if (TYPE_LIST != t || !l->unique())
269  {
270  clear(TYPE_LIST);
271  l = new DataType<ListType>(v);
272  } else {
273  *l = v;
274  }
275  return *this;
276  }
277 
279  bool operator==(const Element& o) const;
280 
281 #if defined(__GNUC__) && __GNUC__ < 3
282  bool operator!=(const Element& o) const
283  {
284  return !(*this == o);
285  }
286 #endif // defined(__GNUC__) && __GNUC__ < 3
287 
289  template<class C>
290  bool operator!=(C c) const
291  {
292  return !(*this == c);
293  }
294 
296  bool operator==(IntType v) const
297  {
298  return (t == TYPE_INT && i == v);
299  }
300 
302  bool operator==(FloatType v) const
303  {
304  return t == TYPE_FLOAT && Equal(f, v);
305  }
306 
308  bool operator==(PtrType v) const
309  {
310  return t == TYPE_PTR && p == v;
311  }
312 
314  bool operator==(const char * v) const
315  {
316  if(t == TYPE_STRING)
317  return (*s == v);
318  return false;
319  }
320 
322  bool operator==(const StringType& v) const
323  {
324  if(t == TYPE_STRING)
325  return (*s == v);
326  return false;
327  }
328 
330  bool operator==(const MapType& v) const
331  {
332  if(t == TYPE_MAP)
333  return (*m == v);
334  return false;
335  }
336 
338  bool operator==(const ListType& v) const
339  {
340  if (t == TYPE_LIST)
341  return (*l == v);
342  return false;
343  }
344 
346  Type getType() const { return t; }
348  bool isNone() const { return (t == TYPE_NONE); }
350  bool isInt() const { return (t == TYPE_INT); }
352  bool isFloat() const { return (t == TYPE_FLOAT); }
354  bool isPtr() const { return (t == TYPE_PTR); }
356  bool isNum() const { return ((t == TYPE_FLOAT) || (t == TYPE_INT)); }
358  bool isString() const { return (t == TYPE_STRING); }
360  bool isMap() const { return (t == TYPE_MAP); }
362  bool isList() const { return (t == TYPE_LIST); }
363 
365  IntType asInt() const
366  {
367  if (t == TYPE_INT) return i;
368  throw WrongTypeException();
369  }
370  IntType Int() const
371  {
372  return i;
373  }
375  FloatType asFloat() const
376  {
377  if (t == TYPE_FLOAT) return f;
378  throw WrongTypeException();
379  }
380  FloatType Float() const
381  {
382  return f;
383  }
385  PtrType asPtr() const
386  {
387  if (t == TYPE_PTR) return p;
388  throw WrongTypeException();
389  }
390  PtrType Ptr() const
391  {
392  return p;
393  }
395  FloatType asNum() const
396  {
397  if (t == TYPE_FLOAT) return f;
398  if (t == TYPE_INT) return FloatType(i);
399  throw WrongTypeException();
400  }
402  const std::string& asString() const
403  {
404  if (t == TYPE_STRING) return *s;
405  throw WrongTypeException();
406  }
408  std::string& asString()
409  {
410  if (t == TYPE_STRING) return *(s = s->makeUnique());
411  throw WrongTypeException();
412  }
413  const StringType& String() const
414  {
415  return *s;
416  }
417  StringType& String()
418  {
419  return *(s = s->makeUnique());
420  }
422  const MapType& asMap() const
423  {
424  if (t == TYPE_MAP) return *m;
425  throw WrongTypeException();
426  }
428  MapType& asMap()
429  {
430  if (t == TYPE_MAP) return *(m = m->makeUnique());
431  throw WrongTypeException();
432  }
433  const MapType& Map() const
434  {
435  return *m;
436  }
437  MapType& Map()
438  {
439  return *(m = m->makeUnique());
440  }
442  const ListType& asList() const
443  {
444  if (t == TYPE_LIST) return *l;
445  throw WrongTypeException();
446  }
448  ListType& asList()
449  {
450  if (t == TYPE_LIST) return *(l = l->makeUnique());
451  throw WrongTypeException();
452  }
453  const ListType& List() const
454  {
455  return *l;
456  }
457  ListType& List()
458  {
459  return *(l = l->makeUnique());
460  }
461 
462  static const char * typeName(Type);
463 
464 protected:
465 
466  template<class C>
467  class DataType
468  {
469  public:
470  DataType() : _refcount(1), _data(0) {}
471  DataType(const C& c) : _refcount(1), _data(c) {}
472 
473  DataType& operator=(const C& c) {_data = c; return *this;}
474 
475  bool operator==(const C& c) const {return _data == c;}
476 
477  void ref() {++_refcount;}
478  void unref() {if(--_refcount == 0) delete this;}
479 
480  bool unique() const {return _refcount == 1;}
481  DataType* makeUnique()
482  {
483  if(unique())
484  return this;
485  unref(); // _refcount > 1, so this is fine
486  return new DataType(_data);
487  }
488 
489  operator C&() {return _data;}
490 // operator const C&() const {return _data;}
491 
492  private:
493  DataType(const DataType&); // unimplemented
494  DataType& operator=(const DataType&); // unimplemented
495 
496  unsigned long _refcount;
497  C _data;
498  };
499 
500  Type t;
501  union {
502  IntType i;
503  FloatType f;
504  void* p;
508  };
509 };
510 
511 } } // namespace Atlas::Message
512 
513 
514 #endif // ATLAS_MESSAGE_ELEMENT_H
Atlas::Message::Element::Element
Element(IntType v)
Set type to int, and value to v.
Definition: Element.h:115
Atlas::Message::Element::operator==
bool operator==(PtrType v) const
Check for equality with a pointer.
Definition: Element.h:308
Atlas::Message::Element::isMap
bool isMap() const
Check whether the current type is MapType.
Definition: Element.h:360
Atlas::Message::Element::asList
const ListType & asList() const
Retrieve the current value as a const ListType reference.
Definition: Element.h:442
Atlas::Message::Element::operator==
bool operator==(IntType v) const
Check for equality with a int.
Definition: Element.h:296
Atlas::Message::Element::operator==
bool operator==(FloatType v) const
Check for equality with a double.
Definition: Element.h:302
Atlas::Message::Element::operator=
Element & operator=(const Element &obj)
overload assignment operator !
Atlas::Message::Element::operator==
bool operator==(const ListType &v) const
Check for equality with a ListType.
Definition: Element.h:338
Atlas::Message::Element::asMap
const MapType & asMap() const
Retrieve the current value as a const MapType reference.
Definition: Element.h:422
Atlas::Message::Element::Element
Element(const MapType &v)
Set type to MapType, and value to v.
Definition: Element.h:155
Atlas::Message::Element
Multi-type container.
Definition: Element.h:61
Atlas::Message::Element::isPtr
bool isPtr() const
Check whether the current type is pointer.
Definition: Element.h:354
Atlas::Message::Element::asString
std::string & asString()
Retrieve the current value as a non-const std::string reference.
Definition: Element.h:408
Atlas::Message::Element::getType
Type getType() const
Get the current type.
Definition: Element.h:346
Atlas::Message::Element::asPtr
PtrType asPtr() const
Retrieve the current value as a pointer.
Definition: Element.h:385
Atlas::Message::Element::isFloat
bool isFloat() const
Check whether the current type is double.
Definition: Element.h:352
Atlas::Message::Element::operator==
bool operator==(const MapType &v) const
Check for equality with a MapType.
Definition: Element.h:330
Atlas::Exception
Base class for all exceptions thrown by Atlas-C++.
Definition: Exception.h:18
Atlas::Message::Element::operator==
bool operator==(const Element &o) const
Check for equality with another Element.
Atlas::Message::Element::asMap
MapType & asMap()
Retrieve the current value as a non-const MapType reference.
Definition: Element.h:428
Atlas::Message::Element::Element
Element(const char *v)
Set type to std::string, and value to v.
Definition: Element.h:139
Atlas::Message::Element::Element
Element(const Element &obj)
Copy an existing object.
Atlas::Message::Element::DataType< StringType >
Atlas::Message::Element::Element
Element(float v)
Set type to double, and value to v.
Definition: Element.h:121
Atlas::Message::Element::Element
Element(const ListType &v)
Set type to ListType, and value to v.
Definition: Element.h:161
Atlas::Message::Element::isList
bool isList() const
Check whether the current type is ListType.
Definition: Element.h:362
Atlas::Message::Element::Element
Element(const StringType &v)
Set type to std::string, and value to v.
Definition: Element.h:149
Atlas::Message::Element::asInt
IntType asInt() const
Retrieve the current value as a int.
Definition: Element.h:365
Atlas::Message::Element::asFloat
FloatType asFloat() const
Retrieve the current value as a double.
Definition: Element.h:375
Atlas::Message::Element::isNum
bool isNum() const
Check whether the current type is numeric.
Definition: Element.h:356
Atlas::Message::Element::Element
Element(FloatType v)
Set type to double, and value to v.
Definition: Element.h:127
Atlas::Message::Element::isNone
bool isNone() const
Check whether the current type is nothing.
Definition: Element.h:348
Atlas::Message::Element::Element
Element(int v)
Set type to int, and value to v.
Definition: Element.h:103
Atlas::Message::Element::operator==
bool operator==(const StringType &v) const
Check for equality with a std::string.
Definition: Element.h:322
Atlas::Message::Element::asList
ListType & asList()
Retrieve the current value as a non-const ListType reference.
Definition: Element.h:448
Atlas
The Atlas namespace.
Definition: Bridge.h:20
Atlas::Message::Element::Element
Element(PtrType v)
Set type to PtrType, and value to v.
Definition: Element.h:133
Atlas::Message::Element::Element
Element()
Construct an empty object.
Definition: Element.h:88
Atlas::Message::WrongTypeException
An exception class issued when the wrong type is requested in as().
Definition: Element.h:21
Atlas::Message::Element::Element
Element(bool v)
Set type to int, and value to v.
Definition: Element.h:109
Atlas::Message::Element::asNum
FloatType asNum() const
Retrieve the current value as a number.
Definition: Element.h:395
Atlas::Message::Element::asString
const std::string & asString() const
Retrieve the current value as a const std::string reference.
Definition: Element.h:402
Atlas::Message::Element::operator==
bool operator==(const char *v) const
Check for equality with a const char *.
Definition: Element.h:314
Atlas::Message::Element::operator!=
bool operator!=(C c) const
Check for inequality with anything we can check equality with.
Definition: Element.h:290
Atlas::Message::Element::isInt
bool isInt() const
Check whether the current type is int.
Definition: Element.h:350
Atlas::Message::Element::isString
bool isString() const
Check whether the current type is std::string.
Definition: Element.h:358

Copyright 2000-2004 the respective authors.

This document can be licensed under the terms of the GNU Free Documentation License or the GNU General Public License and may be freely distributed under the terms given by one of these licenses.