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

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.