Main Page   Class Hierarchy   Compound List   File List   Compound Members  

SOMEClassCatalog.h

00001 /********************************************************************
00002 ** Copyright (C) 2000 SOMELib Project
00003 **
00004 ** This library is free software; you can redistribute it and/or
00005 ** modify it under the terms of the GNU Library General Public
00006 ** License as published by the Free Software Foundation; either
00007 ** version 2 of the License, or (at your option) any later version.
00008 **
00009 ** This library is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 ** Library General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU Library General Public
00015 ** License along with this library; if not, write to the
00016 ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017 ** Boston, MA  02111-1307, USA.
00018 **
00019 *******************************************************************/
00020 
00021 #ifndef SOME_class_properties_HEADER
00022 #define SOME_class_properties_HEADER
00023 
00024 #include "UserConfig.h"
00025 #include <string>
00026 #include <cassert>
00027 #include <map>
00028 #ifdef _MSC_VER //should we just go with some_string_map?  probably not
00029 #include "SOMEStringMap.h"
00030 #endif
00031 
00032 namespace SOME
00033 {
00042 class ClassCatalog
00043 {
00044 public:
00045     typedef void* (*ConstructFunc)();   //since our construct func's are extern "C"d they appear to have no params and must be cast
00046 
00047     ClassCatalog(const std::string& classname,
00048                  const std::string& the_category,
00049                  ConstructFunc def = NULL,
00050                  ConstructFunc one = NULL,
00051              ConstructFunc two = NULL):
00052     ctor0_func(def),
00053     ctor1_func(one),
00054     ctor2_func(two)
00055     {
00056         assert(ctor0_func != NULL || ctor1_func != NULL || ctor2_func != NULL);
00057         class_properties["classname"] = classname;
00058         class_properties["category"] = the_category;
00059     }
00060 
00062     void addCategory( std::string key, std::string value )
00063     {
00064         //you can only add class info if it does not already exist
00065         if (class_properties[key] == "")
00066             class_properties[key] = value;
00067     }
00068 
00070     std::map < std::string, std::string > getProperties()
00071     {
00072 #ifdef _MSC_VER
00073         return class_properties.getStdMap();
00074 #else
00075 return class_properties;
00076 #endif
00077     }
00078     std::string getCategory(std::string key)
00079     {
00080         return class_properties[key];
00081     }
00082 
00087     bool infoEquals( std::string key, std::string value )
00088     {
00089         return class_properties[key] == value;
00090     }
00091 
00092 
00093 
00094     const std::string& getClassName()
00095     {
00096         return class_properties["classname"];
00097     }
00099     const std::string& getCategory()
00100     {
00101         return class_properties["category"];
00102     }
00103     ConstructFunc getCtor0() const
00104     {
00105         return ctor0_func;
00106     }
00107     ConstructFunc getCtor1() const
00108     {
00109         return ctor1_func;
00110     }
00111     ConstructFunc getCtor2() const
00112     {
00113         return ctor2_func;
00114     }
00115 
00116     //here is a way to get an object without using SOMEObj
00118 
00129     template < class BaseClass >
00130     BaseClass* construct()
00131     {
00132         BaseClass * ret_ptr = 0;
00133         BaseClass* (*ctor0)() = (BaseClass * (*)())ctor0_func;
00134         if (ctor0 != NULL)
00135         {
00136             ret_ptr = (*ctor0)();
00137         }
00138         return ret_ptr;
00139     }
00140 
00151     template < class BaseClass >
00152     void deduce_construct(BaseClass** dest_ptr)
00153     {
00154         BaseClass* (*ctor0)() = (BaseClass * (*)())ctor0_func;
00155         if (ctor0 != NULL)
00156             *dest_ptr = (*ctor0)();
00157         else
00158             *dest_ptr = NULL;
00159     }
00160 
00161 
00163 
00174     template < class BaseClass, typename ParamType1 >
00175     BaseClass* construct(ParamType1& a)
00176     {
00177         BaseClass* ret_ptr = 0;
00178         BaseClass* (*ctor1)(ParamType1&) = (BaseClass * (*)(ParamType1&))ctor1_func;
00179         if (ctor1 != NULL)
00180         {
00181             ret_ptr = (*ctor1)(a);
00182         }
00183         return ret_ptr;
00184     }
00185 
00187 
00198     template < class BaseClass, typename ParamType1, typename ParamType2 >
00199     BaseClass* construct(ParamType1& a, ParamType2& b)
00200     {
00201         BaseClass* ret_ptr = 0;
00202         BaseClass* (*ctor2)(ParamType1&, ParamType2&) = (BaseClass * (*)(ParamType1&, ParamType2&))ctor2_func;
00203         if (ctor2 != NULL)
00204         {
00205             ret_ptr = (*ctor2)(a, b);
00206         }
00207         return ret_ptr;
00208     }
00209 
00210 private:
00211 #ifdef _MSC_VER
00212     StringMap class_properties;
00213 #else
00214     std::map < std::string, std::string > class_properties;
00215 #endif
00216 
00217     ConstructFunc ctor0_func;
00218     ConstructFunc ctor1_func;
00219     ConstructFunc ctor2_func;
00220 
00221 };
00222 
00223 } //namespace SOME
00224 
00225 #endif //SOME_class_properties_HEADER

Generated at Fri Dec 8 14:24:48 2000 for SOMELib by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000