Main Page | Modules | Class Hierarchy | Class List | File List | Class Members | Related Pages

Object.h

00001 // This file may be redistributed and modified only under the terms of
00002 // the GNU Lesser General Public License (See COPYING for details).
00003 // Copyright (C) 2000-2001 Stefanus Du Toit, Karsten-O. Laux, Alistair Riddoch
00004 
00005 #ifndef ATLAS_MESSAGE_OBJECT_H
00006 #define ATLAS_MESSAGE_OBJECT_H
00007 
00008 #include <string>
00009 #include <map>
00010 #include <vector>
00011 
00012 namespace Atlas { namespace Message {
00013 
00015 class WrongTypeException { };
00016 
00037 class Object
00038 {
00039 public:
00040     typedef long IntType;
00041     typedef double FloatType;
00042     typedef std::string StringType;
00043     typedef std::map<std::string, Object> MapType;
00044     typedef std::vector<Object> ListType;
00045 
00046     enum Type {
00047         TYPE_NONE,
00048         TYPE_INT,
00049         TYPE_FLOAT,
00050         TYPE_STRING,
00051         TYPE_MAP,
00052         TYPE_LIST
00053     };
00054 
00056     Object()
00057       : t(TYPE_NONE)
00058     {
00059     }
00060 
00062     void clear()
00063     {
00064      switch(t) 
00065         {
00066       case TYPE_NONE:
00067       case TYPE_INT:
00068       case TYPE_FLOAT:
00069         break;
00070       case TYPE_STRING:
00071         delete s;
00072         break;
00073       case TYPE_MAP:
00074         delete m;
00075         break;
00076       case TYPE_LIST:
00077         delete l;
00078         break;
00079       }
00080      
00081      t = TYPE_NONE;
00082     }
00083 
00085     virtual ~Object()
00086       {
00087         clear();
00088       }
00089 
00091     Object(const Object& obj)
00092       : t(obj.t)
00093     {
00094       switch(t) 
00095         {
00096       case TYPE_NONE:
00097         break;
00098       case TYPE_INT:
00099         i = obj.i;
00100         break;
00101       case TYPE_FLOAT:
00102         f = obj.f;
00103         break;
00104       case TYPE_STRING:
00105         s = new StringType(*obj.s);
00106         break;
00107       case TYPE_MAP:
00108         m = new MapType(*obj.m);
00109         break;
00110       case TYPE_LIST:
00111         l = new ListType(*obj.l);
00112         break;
00113       }
00114         
00115     }
00116 
00118     Object(int v)
00119       : t(TYPE_INT), i(v)
00120     {
00121     }
00122 
00124     Object(bool v)
00125       : t(TYPE_INT), i(v)
00126     {
00127     }
00128 
00130     Object(IntType v)
00131       : t(TYPE_INT), i(v)
00132     {
00133     }
00134 
00136     Object(float v)
00137       : t(TYPE_FLOAT), f(v)
00138     {
00139     }   
00140 
00142     Object(FloatType v)
00143       : t(TYPE_FLOAT), f(v)
00144     {
00145     }
00146 
00148     Object(const char* v)
00149       : t(TYPE_STRING)
00150     {
00151       if(v)
00152         s = new StringType(v);
00153       else
00154         s = new StringType();
00155     }
00156 
00158     Object(const StringType& v)
00159       : t(TYPE_STRING)
00160     {
00161       s = new StringType(v);
00162     }
00164     Object(const MapType& v)
00165       : t(TYPE_MAP)
00166     {
00167       m = new MapType(v);
00168     }
00170     Object(const ListType& v)
00171       : t(TYPE_LIST)
00172     {
00173       l = new ListType(v);
00174     }
00175 
00177     Object& operator=(const Object& obj) 
00178     {
00179       //check for self assignment
00180       if(&obj == this)
00181         return *this;
00182 
00183       //first clear
00184       clear();
00185     
00186       // then perform actual assignment of members
00187       t =  obj.t;
00188       
00189       switch(t) 
00190         {
00191         case TYPE_NONE:
00192           break;
00193         case TYPE_INT:
00194           i = obj.i;
00195           break;
00196         case TYPE_FLOAT:
00197           f = obj.f;
00198         break;
00199         case TYPE_STRING:
00200           s = new StringType(*obj.s);
00201           break;
00202         case TYPE_MAP:
00203           m = new MapType(*obj.m);
00204           break;
00205         case TYPE_LIST:
00206           l = new ListType(*obj.l);
00207           break;
00208         }
00209 
00210       return *this;
00211     }
00212 
00214     bool operator==(const Object& o) const
00215     {
00216         if (t != o.t) return false;
00217         switch(t) {
00218             case TYPE_NONE: return true;
00219             case TYPE_INT: return i == o.i;
00220             case TYPE_FLOAT: return f == o.f;
00221             case TYPE_STRING: return *s == *o.s;
00222             case TYPE_MAP: return *m == *o.m;
00223             case TYPE_LIST: return *l == *o.l;
00224         }
00225         return false;
00226     }
00227 
00229     bool operator!=(const Object m) const
00230     {
00231         return !(*this == m);
00232     }
00233 
00235     bool operator==(IntType v) const
00236     {
00237       return (t == TYPE_INT && i == v);
00238     }
00239 
00241     bool operator!=(IntType v) const { return !(*this == v); }
00242 
00244     bool operator==(FloatType v) const
00245     {
00246       return t == TYPE_FLOAT && f == v;
00247     }
00248 
00250     bool operator!=(FloatType v) const { return !(*this == v); }
00251 
00253     bool operator==(const StringType& v) const
00254     {
00255       if(t == TYPE_STRING)
00256         return (*s == v);
00257       return false;
00258     }
00259 
00261     bool operator!=(const StringType& v) const { return !(*this == v); }
00262 
00264     bool operator==(const MapType& v) const
00265     {
00266       if(t == TYPE_MAP)
00267         return (*m == v);
00268       return false;
00269     }
00270 
00272     bool operator!=(const MapType& v) const { return !(*this == v); }
00273 
00275     bool operator==(const ListType& v) const
00276     {
00277       if (t == TYPE_LIST)
00278         return (*l == v);
00279       return false;
00280     }
00281 
00283     bool operator!=(const ListType& v) const { return !(*this == v); }
00284 
00286     Type GetType() const { return t; }
00288     bool IsNone() const { return (t == TYPE_NONE); }
00290     bool IsInt() const { return (t == TYPE_INT); }
00292     bool IsFloat() const { return (t == TYPE_FLOAT); }
00294     bool IsNum() const { return ((t == TYPE_FLOAT) || (t == TYPE_INT)); }
00296     bool IsString() const { return (t == TYPE_STRING); }
00298     bool IsMap() const { return (t == TYPE_MAP); }
00300     bool IsList() const { return (t == TYPE_LIST); }
00301 
00303     long AsInt() const throw (WrongTypeException)
00304     {
00305         if (t == TYPE_INT) return i;
00306         throw WrongTypeException();
00307     }
00309     FloatType AsFloat() const throw (WrongTypeException)
00310     {
00311         if (t == TYPE_FLOAT) return f;
00312         throw WrongTypeException();
00313     }
00315     FloatType AsNum() const throw (WrongTypeException)
00316     {
00317         if (t == TYPE_FLOAT) return f;
00318         if (t == TYPE_INT) return FloatType(i);
00319         throw WrongTypeException();
00320     }
00322     const std::string& AsString() const throw (WrongTypeException)
00323     {
00324         if (t == TYPE_STRING) return *s;
00325         throw WrongTypeException();
00326     }
00328     std::string& AsString() throw (WrongTypeException)
00329     {
00330         if (t == TYPE_STRING) return *s;
00331         throw WrongTypeException();
00332     }
00334     const MapType& AsMap() const throw (WrongTypeException)
00335     {
00336         if (t == TYPE_MAP) return *m;
00337         throw WrongTypeException();
00338     }
00340     MapType& AsMap() throw (WrongTypeException)
00341     {
00342         if (t == TYPE_MAP) return *m;
00343         throw WrongTypeException();
00344     }
00346     const ListType& AsList() const throw (WrongTypeException)
00347     {
00348         if (t == TYPE_LIST) return *l;
00349         throw WrongTypeException();
00350     }
00352     ListType& AsList() throw (WrongTypeException)
00353     {
00354         if (t == TYPE_LIST) return *l;
00355         throw WrongTypeException();
00356     }
00357 
00358 protected:
00359 
00360     Type t;
00361     union {
00362       IntType i;
00363       FloatType f;
00364       StringType* s;
00365       MapType* m;
00366       ListType* l;
00367       void* n;
00368     };
00369 };
00370 
00371 } } // namespace Atlas::Message
00372 
00373 
00374 #endif

Copyright 2000 the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.