Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

prop_registry.hh

Go to the documentation of this file.
00001 /*
00002  *  File:       cucu.c
00003  *              $Id: prop_registry.hh,v 1.4 2002/06/26 20:49:47 alec Exp $
00004  *
00005  *  Author:     Alec Panoviciu (alecu@email.com)
00006  *
00007  *  Comments:
00008  *
00009  *  Revision history:
00010  *
00011  *  $Log: prop_registry.hh,v $
00012  *  Revision 1.4  2002/06/26 20:49:47  alec
00013  *  g++ 3.x happy
00014  *
00015  *  Revision 1.3  2002/06/23 23:29:33  alec
00016  *  profile-based optimization stuff
00017  *
00018  *  Revision 1.2  2002/04/29 09:34:10  alec
00019  *  scanner ptree building compiles
00020  *
00021  */
00022 
00023 /* 
00024   Copyright (C) 2002 Alexandru Panoviciu (alecu@email.com)
00025 
00026   This program is free software; you can redistribute it and/or modify
00027   it under the terms of the GNU General Public License as published by
00028   the Free Software Foundation; either version 2 of the License, or
00029   (at your option) any later version.
00030 
00031   This program is distributed in the hope that it will be useful,
00032   but WITHOUT ANY WARRANTY; without even the implied warranty of
00033   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00034   GNU General Public License for more details.
00035 
00036   You should have received a copy of the GNU General Public License
00037   along with this program; if not, write to the Free Software
00038   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00039 
00040  */
00041 
00042 
00043 #ifndef __PROP_REGISTRY_HH__
00044 #define __PROP_REGISTRY_HH__
00045 
00046 #include <string>
00047 #include <vector>
00048 #include <exception>
00049 #include <typeinfo>
00050 #include <map>
00051 using namespace std;
00052 
00053 #include "debug.h"
00054 
00055 
00061 class PropRegistry
00062 {
00063 public:
00064   
00069   class PropData
00070   {
00071   public:
00072 
00073     enum {k_string, k_bool, k_int, k_float, k_undef} tag;
00074 
00075     vector<string> s;
00076     vector<bool> b;
00077     vector<int> i;
00078     vector<float> f;
00079 
00080     PropData () : tag(k_undef) {}
00081     
00082     PropData (const PropData &o) :
00083       tag(o.tag), s(o.s), b(o.b), i(o.i), f(o.f)
00084     {}
00085 
00086     PropData& operator = (const PropData &o)
00087     {
00088       if (this  == &o) return *this;
00089       tag = o.tag;
00090       s = o.s;
00091       b = o.b;
00092       i = o.i;
00093       f = o.f;
00094     }
00095 
00096 
00097     explicit PropData (const char *s_) :
00098       tag(k_string), s(1, string(s_))
00099     {}
00100     
00101     explicit PropData (const string &s_) :
00102       tag(k_string), s(1, s_)
00103     {}
00104 
00105     explicit PropData (const vector<string> &s_) :
00106       tag(k_string), s(s_)
00107     {}
00108 
00109     explicit PropData (const bool b_) :
00110       tag(k_bool), b(1, b_)
00111     {}
00112 
00113     explicit PropData (const vector<bool> &b_) :
00114       tag(k_bool), b(b_)
00115     {}
00116     
00117     explicit PropData (const int i_) :
00118       tag(k_int), i(1, i_)
00119     {}
00120 
00121     explicit PropData (const vector<int> &i_) :
00122       tag(k_int), i(i_)
00123     {}
00124     
00125     explicit PropData (const float f_) :
00126       tag(k_float), f(1, f_)
00127     {}
00128 
00129     explicit PropData (const vector<float> &f_) :
00130       tag(k_float), f(f_)
00131     {}
00132 
00133     PropData& operator = (const char *s_)
00134     {
00135       return *this = PropData(s_);
00136     }
00137 
00138     PropData& operator = (const string &s_)
00139     {
00140       return *this = PropData(s_);
00141     }
00142 
00143     PropData& operator = (const bool b_)
00144     {
00145       return *this = PropData(b_);
00146     }
00147 
00148     PropData& operator = (const vector<bool> &b_)
00149     {
00150       return *this = PropData(b_);
00151     }
00152 
00153     PropData& operator = (const int i_)
00154     {
00155       return *this = PropData(i_);
00156     }
00157 
00158     PropData& operator = (const vector<int> &i_)
00159     {
00160       return *this = PropData(i_);
00161     }
00162 
00163     PropData& operator = (const float f_)
00164     {
00165       return *this = PropData(f_);
00166     }
00167 
00168     PropData& operator = (const vector<float> &f_)
00169     {
00170       return *this = PropData(f_);
00171     }
00172 
00173     operator string () throw (bad_cast)
00174     {
00175       if ((tag != k_string) || (s.size() != 1)) throw bad_cast();
00176       return s[0];
00177     }
00178 
00179     operator vector<string>& () throw (bad_cast)
00180     {
00181       if (tag != k_string) throw bad_cast();
00182       return s;
00183     }
00184 
00185     operator bool () throw (bad_cast)
00186     {
00187       if ((tag != k_bool) || (b.size() != 1)) throw bad_cast();
00188       return b[0];
00189     }
00190 
00191     operator vector<bool>& () throw (bad_cast)
00192     {
00193       if (tag != k_bool) throw bad_cast();
00194       return b;
00195     }
00196 
00197     operator int () throw (bad_cast)
00198     {
00199       if ((tag != k_int) || (i.size() != 1)) throw bad_cast();
00200       return i[0];
00201     }
00202 
00203     operator vector<int>& () throw (bad_cast)
00204     {
00205       if (tag != k_int) throw bad_cast();
00206       return i;
00207     }
00208 
00209     operator float () throw (bad_cast)
00210     {
00211       if ((tag != k_float) || (i.size() != 1)) throw bad_cast();
00212       return f[0];
00213     }
00214 
00215     operator vector<float>& () throw (bad_cast)
00216     {
00217       if (tag != k_float) throw bad_cast();
00218       return f;
00219     }
00220 
00225     PropData& operator << (const PropData &o) throw (bad_cast);
00226 
00227     PropData& operator << (const string &o) throw (bad_cast)
00228     {
00229       return *this << PropData(o);
00230     }
00231     
00232     PropData& operator << (const int &o) throw (bad_cast)
00233     {
00234       return *this << PropData(o);
00235     }
00236     
00237     PropData& operator << (const float &o) throw (bad_cast)
00238     {
00239       return *this << PropData(o);
00240     }
00241     
00242     PropData& operator << (const bool &o) throw (bad_cast)
00243     {
00244       return *this << PropData(o);
00245     }
00246 
00247     PropData& operator << (const char *o) throw (bad_cast)
00248     {
00249       return *this << PropData(o);
00250     }
00251 
00252   };
00253     
00257   bool contains (const string &key) const
00258   {
00259     return data.find(key) != data.end();
00260   }
00261 
00265   bool empty () const
00266   {
00267     return data.empty();
00268   }
00269 
00270   typedef map<string, PropData, less<string> > StorageType;
00271 
00272   typedef StorageType::iterator iterator;
00273   typedef StorageType::const_iterator const_iterator;
00274 
00275   iterator begin ()
00276   {
00277     return data.begin();
00278   }
00279 
00280   iterator end ()
00281   {
00282     return data.end();
00283   }
00284 
00285   const_iterator begin () const
00286   {
00287     return data.begin();
00288   }
00289 
00290   const_iterator end () const
00291   {
00292     return data.end();
00293   }
00294 
00332   PropData& operator [] (const string &key)
00333   {
00334     return data[key];
00335   }
00336 
00337 private:
00338 
00339   StorageType data;
00340 
00341 };
00342 
00343 
00344 inline string operator + (const string &a, PropRegistry::PropData &b)
00345 {
00346   return a + (string) b;
00347 }
00348 
00349 inline int operator + (const int &a, PropRegistry::PropData &b)
00350 {
00351   return a + (int) b;
00352 }
00353 
00354 inline bool operator + (const bool &a, PropRegistry::PropData &b)
00355 {
00356   return a + (bool) b;
00357 }
00358 
00359 inline float operator + (const float &a, PropRegistry::PropData &b)
00360 {
00361   return a + (float) b;
00362 }
00363 
00364 
00365 inline string operator + (PropRegistry::PropData &b, const string &a)
00366 {
00367   return a + (string) b;
00368 }
00369 
00370 inline int operator + (PropRegistry::PropData &b, const int &a)
00371 {
00372   return a + (int) b;
00373 }
00374 
00375 inline bool operator + (PropRegistry::PropData &b, const bool &a)
00376 {
00377   return a + (bool) b;
00378 }
00379 
00380 inline float operator + (PropRegistry::PropData &b, const float &a)
00381 {
00382   return a + (float) b;
00383 }
00384 
00385 
00394 ostream& operator << (ostream &os, const PropRegistry::PropData &o);
00395 
00400 ostream& operator << (ostream &os, const PropRegistry  &o);
00401 
00405 ostream& operator >> (ostream &os, PropRegistry::PropData &o);
00406 
00411 ostream& operator >> (ostream &os, PropRegistry &o);
00412 
00413 
00414 #endif /* #ifndef __PROP_REGISTRY_HH__ */

Generated at Tue Jul 9 21:05:45 2002 for CppCC by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001