430
by Tim Penhey
Make the ObjectType.name a const char*. |
1 |
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
|
220.3.1
by Jay Taoko
*Fixes |
2 |
/*
|
307.1.2
by Jay Taoko
* Added LGPL v2.1 License to source code files. |
3 |
* Copyright 2010 Inalogic® Inc.
|
220.3.1
by Jay Taoko
*Fixes |
4 |
*
|
5 |
* This program is free software: you can redistribute it and/or modify it
|
|
307.1.2
by Jay Taoko
* Added LGPL v2.1 License to source code files. |
6 |
* under the terms of the GNU Lesser General Public License, as
|
7 |
* published by the Free Software Foundation; either version 2.1 or 3.0
|
|
8 |
* of the License.
|
|
220.3.1
by Jay Taoko
*Fixes |
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful, but
|
|
11 |
* WITHOUT ANY WARRANTY; without even the implied warranties of
|
|
12 |
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
|
|
13 |
* PURPOSE. See the applicable version of the GNU Lesser General Public
|
|
14 |
* License for more details.
|
|
15 |
*
|
|
16 |
* You should have received a copy of both the GNU Lesser General Public
|
|
307.1.2
by Jay Taoko
* Added LGPL v2.1 License to source code files. |
17 |
* License along with this program. If not, see <http://www.gnu.org/licenses/>
|
220.3.1
by Jay Taoko
*Fixes |
18 |
*
|
19 |
* Authored by: Jay Taoko <jaytaoko@inalogic.com>
|
|
20 |
*
|
|
21 |
*/
|
|
22 |
||
23 |
#ifndef NOBJECTTYPE_H
|
|
24 |
#define NOBJECTTYPE_H
|
|
25 |
||
430
by Tim Penhey
Make the ObjectType.name a const char*. |
26 |
#include <string> |
27 |
||
28 |
namespace nux |
|
29 |
{
|
|
442.1.1
by Tim Penhey
Remove two of the pointers in nux::Object, and refactor the weak pointer to use sigc to be notified when the object is destroyed. |
30 |
// TODO: write a nice is_instance (and is_derived_instance)
|
31 |
||
32 |
//template <typename T, typename I>
|
|
33 |
//bool is_instance(T const&
|
|
430
by Tim Penhey
Make the ObjectType.name a const char*. |
34 |
|
220.3.1
by Jay Taoko
*Fixes |
35 |
struct NObjectType |
36 |
{
|
|
430
by Tim Penhey
Make the ObjectType.name a const char*. |
37 |
const char* name; |
38 |
NObjectType* super; |
|
39 |
||
220.3.1
by Jay Taoko
*Fixes |
40 |
static const NObjectType Null_Type; |
41 |
||
42 |
NObjectType() |
|
430
by Tim Penhey
Make the ObjectType.name a const char*. |
43 |
: name("Null_Type") |
44 |
, super(nullptr) |
|
220.3.1
by Jay Taoko
*Fixes |
45 |
{
|
46 |
}
|
|
47 |
||
430
by Tim Penhey
Make the ObjectType.name a const char*. |
48 |
NObjectType(const char* type_name, NObjectType* super_type) |
49 |
: name(type_name) |
|
50 |
, super(super_type) |
|
220.3.1
by Jay Taoko
*Fixes |
51 |
{
|
52 |
}
|
|
53 |
||
54 |
//! Return true is this has the same type as the argument type.
|
|
55 |
inline bool operator == (const NObjectType &Type) const |
|
56 |
{
|
|
57 |
return IsObjectType (Type); |
|
58 |
}
|
|
59 |
||
60 |
//! Return true is this has is of a different type than the argument type.
|
|
61 |
inline bool operator != (const NObjectType &Type) const |
|
62 |
{
|
|
63 |
return !IsObjectType (Type); |
|
64 |
}
|
|
65 |
||
66 |
//! Return true is this has the same type as the argument type.
|
|
67 |
inline bool IsObjectType (const NObjectType &Type) const |
|
68 |
{
|
|
430
by Tim Penhey
Make the ObjectType.name a const char*. |
69 |
return this == &Type; |
220.3.1
by Jay Taoko
*Fixes |
70 |
}
|
71 |
||
72 |
//! Return true if this has the same type as the argument type or is derived from it.
|
|
73 |
inline bool IsDerivedFromType (const NObjectType &Type) const |
|
74 |
{
|
|
75 |
const NObjectType *current_type = this; |
|
76 |
||
77 |
while (current_type) |
|
78 |
{
|
|
79 |
if (current_type == &Type) |
|
80 |
return true; |
|
81 |
||
430
by Tim Penhey
Make the ObjectType.name a const char*. |
82 |
current_type = current_type->super; |
220.3.1
by Jay Taoko
*Fixes |
83 |
}
|
84 |
||
85 |
return false; |
|
86 |
}
|
|
87 |
||
88 |
inline unsigned int SubClassDepth() const |
|
89 |
{
|
|
430
by Tim Penhey
Make the ObjectType.name a const char*. |
90 |
const NObjectType* current_type = this; |
220.3.1
by Jay Taoko
*Fixes |
91 |
unsigned int depth = 0; |
92 |
||
93 |
while (current_type) |
|
94 |
{
|
|
95 |
depth++; |
|
430
by Tim Penhey
Make the ObjectType.name a const char*. |
96 |
current_type = current_type->super; |
220.3.1
by Jay Taoko
*Fixes |
97 |
}
|
98 |
||
99 |
return depth; |
|
100 |
}
|
|
101 |
};
|
|
102 |
||
103 |
#define NUX_DECLARE_OBJECT_TYPE(TypeName, SuperType) \
|
|
104 |
public: \
|
|
105 |
typedef SuperType SuperObject; \
|
|
430
by Tim Penhey
Make the ObjectType.name a const char*. |
106 |
static ::nux::NObjectType StaticObjectType; \
|
220.3.1
by Jay Taoko
*Fixes |
107 |
public: \
|
430
by Tim Penhey
Make the ObjectType.name a const char*. |
108 |
virtual ::nux::NObjectType& Type() const { return StaticObjectType; } \
|
109 |
::nux::NObjectType& GetTypeInfo() const { return StaticObjectType; }
|
|
220.3.1
by Jay Taoko
*Fixes |
110 |
|
111 |
||
112 |
#define NUX_IMPLEMENT_OBJECT_TYPE(TypeName) \
|
|
430
by Tim Penhey
Make the ObjectType.name a const char*. |
113 |
::nux::NObjectType TypeName::StaticObjectType(#TypeName, &TypeName::SuperObject::StaticObjectType);
|
220.3.1
by Jay Taoko
*Fixes |
114 |
|
115 |
#define NUX_DECLARE_ROOT_OBJECT_TYPE(TypeName) \
|
|
116 |
public: \
|
|
430
by Tim Penhey
Make the ObjectType.name a const char*. |
117 |
typedef ::nux::NObjectType SuperObject; \
|
118 |
static ::nux::NObjectType StaticObjectType; \
|
|
220.3.1
by Jay Taoko
*Fixes |
119 |
public: \
|
430
by Tim Penhey
Make the ObjectType.name a const char*. |
120 |
virtual ::nux::NObjectType& Type() const { return StaticObjectType; } \
|
121 |
::nux::NObjectType& GetTypeInfo() const { return StaticObjectType; }
|
|
220.3.1
by Jay Taoko
*Fixes |
122 |
|
123 |
#define NUX_IMPLEMENT_ROOT_OBJECT_TYPE(TypeName) \
|
|
430
by Tim Penhey
Make the ObjectType.name a const char*. |
124 |
::nux::NObjectType TypeName::StaticObjectType(#TypeName, 0);
|
125 |
||
126 |
} // namespace nux |
|
220.3.1
by Jay Taoko
*Fixes |
127 |
|
128 |
#endif // NOBJECTTYPE_H |
|
129 |