nux-1.16.0
|
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*- 00002 /* 00003 * Copyright 2010 Inalogic® Inc. 00004 * 00005 * This program is free software: you can redistribute it and/or modify it 00006 * under the terms of the GNU Lesser General Public License, as 00007 * published by the Free Software Foundation; either version 2.1 or 3.0 00008 * of the License. 00009 * 00010 * This program is distributed in the hope that it will be useful, but 00011 * WITHOUT ANY WARRANTY; without even the implied warranties of 00012 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 00013 * PURPOSE. See the applicable version of the GNU Lesser General Public 00014 * License for more details. 00015 * 00016 * You should have received a copy of both the GNU Lesser General Public 00017 * License along with this program. If not, see <http://www.gnu.org/licenses/> 00018 * 00019 * Authored by: Jay Taoko <jaytaoko@inalogic.com> 00020 * 00021 */ 00022 00023 #ifndef NOBJECTTYPE_H 00024 #define NOBJECTTYPE_H 00025 00026 #include <string> 00027 00028 namespace nux 00029 { 00030 // TODO: write a nice is_instance (and is_derived_instance) 00031 00032 //template <typename T, typename I> 00033 //bool is_instance(T const& 00034 00035 struct NObjectType 00036 { 00037 const char* name; 00038 NObjectType* super; 00039 00040 static const NObjectType Null_Type; 00041 00042 NObjectType() 00043 : name("Null_Type") 00044 , super(nullptr) 00045 { 00046 } 00047 00048 NObjectType(const char* type_name, NObjectType* super_type) 00049 : name(type_name) 00050 , super(super_type) 00051 { 00052 } 00053 00055 inline bool operator == (const NObjectType &Type) const 00056 { 00057 return IsObjectType (Type); 00058 } 00059 00061 inline bool operator != (const NObjectType &Type) const 00062 { 00063 return !IsObjectType (Type); 00064 } 00065 00067 inline bool IsObjectType (const NObjectType &Type) const 00068 { 00069 return this == &Type; 00070 } 00071 00073 inline bool IsDerivedFromType (const NObjectType &Type) const 00074 { 00075 const NObjectType *current_type = this; 00076 00077 while (current_type) 00078 { 00079 if (current_type == &Type) 00080 return true; 00081 00082 current_type = current_type->super; 00083 } 00084 00085 return false; 00086 } 00087 00088 inline unsigned int SubClassDepth() const 00089 { 00090 const NObjectType* current_type = this; 00091 unsigned int depth = 0; 00092 00093 while (current_type) 00094 { 00095 depth++; 00096 current_type = current_type->super; 00097 } 00098 00099 return depth; 00100 } 00101 }; 00102 00103 #define NUX_DECLARE_OBJECT_TYPE(TypeName, SuperType) \ 00104 public: \ 00105 typedef SuperType SuperObject; \ 00106 static ::nux::NObjectType StaticObjectType; \ 00107 public: \ 00108 virtual ::nux::NObjectType& Type() const { return StaticObjectType; } \ 00109 ::nux::NObjectType& GetTypeInfo() const { return StaticObjectType; } 00110 00111 00112 #define NUX_IMPLEMENT_OBJECT_TYPE(TypeName) \ 00113 ::nux::NObjectType TypeName::StaticObjectType(#TypeName, &TypeName::SuperObject::StaticObjectType); 00114 00115 #define NUX_DECLARE_ROOT_OBJECT_TYPE(TypeName) \ 00116 public: \ 00117 typedef ::nux::NObjectType SuperObject; \ 00118 static ::nux::NObjectType StaticObjectType; \ 00119 public: \ 00120 virtual ::nux::NObjectType& Type() const { return StaticObjectType; } \ 00121 ::nux::NObjectType& GetTypeInfo() const { return StaticObjectType; } 00122 00123 #define NUX_IMPLEMENT_ROOT_OBJECT_TYPE(TypeName) \ 00124 ::nux::NObjectType TypeName::StaticObjectType(#TypeName, 0); 00125 00126 } // namespace nux 00127 00128 #endif // NOBJECTTYPE_H 00129