ClassInfo.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 #ifndef SOFA_CORE_OBJECTMODEL_CLASSINFO_H
00028 #define SOFA_CORE_OBJECTMODEL_CLASSINFO_H
00029 
00030 #include <sofa/helper/system/config.h>
00031 #include <sofa/helper/FnDispatcher.h>
00032 #include <sofa/core/objectmodel/DataPtr.h>
00033 #include <sofa/core/objectmodel/Data.h>
00034 #include <string>
00035 #include <map>
00036 
00037 using sofa::core::objectmodel::DataPtr;
00038 using sofa::core::objectmodel::Data;
00039 
00040 namespace sofa
00041 {
00042 
00043 namespace core
00044 {
00045 
00046 namespace objectmodel
00047 {
00048 
00049 class Base;
00050 
00057 class ClassInfo
00058 {
00059 protected:
00060     const std::type_info* pt;
00061     ClassInfo(const std::type_info* ti)
00062     : pt(ti)
00063     {
00064         classes[sofa::helper::TypeInfo(*ti)] = this;
00065     }
00066 
00067     virtual ~ClassInfo()
00068     {
00069     }
00070 public:
00071     
00072     std::string name() const { return pt->name(); }
00073     operator const std::type_info&() const { return *pt; }
00074     helper::TypeInfo type() const { return sofa::helper::TypeInfo(*pt); }
00075     bool operator==(const ClassInfo& t) const { return *pt == *t.pt; }
00076     bool operator!=(const ClassInfo& t) const { return *pt != *t.pt; }
00077 #ifdef _MSC_VER
00078     bool operator<(const ClassInfo& t) const { return (pt->before(*t.pt)!=0); }
00079 #else
00080     bool operator<(const ClassInfo& t) const { return pt->before(*t.pt); }
00081 #endif
00082     
00083     virtual void* dynamicCast(Base* obj) const = 0;
00084     
00085     virtual bool isInstance(Base* obj) const
00086     {
00087         return dynamicCast(obj) != NULL;
00088     }
00089 protected:
00090     static std::map<sofa::helper::TypeInfo, ClassInfo*> classes;
00091 };
00092 
00093 template<class T>
00094 class TClassInfo : public ClassInfo
00095 {
00096 protected:
00097     TClassInfo()
00098     : ClassInfo(&typeid(T))
00099     {
00100     }
00101 
00102 public:
00103     static TClassInfo<T>& get()
00104     {
00105         static TClassInfo<T> inst;
00106         return inst;
00107     }
00108 
00109     virtual void* dynamicCast(Base* obj) const
00110     {
00111         return dynamic_cast<T*>(obj);
00112     }
00113 };
00114 
00115 template<class T>
00116 const ClassInfo& classidT() { return TClassInfo<T>::get(); }
00117 
00118 #define classid(T) sofa::core::objectmodel::classidT<T>()
00119 
00120 } // namespace objectmodel
00121 
00122 } // namespace core
00123 
00124 } // namespace sofa
00125 
00126 #endif

Generated on Sun Nov 23 02:29:25 2008 for Sofa by  doxygen 1.5.7.1