1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of NV Libraries.
|
---|
5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
6 |
|
---|
7 | #include "nv/object.hh"
|
---|
8 |
|
---|
9 | #include "nv/root.hh"
|
---|
10 | #include "nv/types.hh"
|
---|
11 | #include "nv/lua/lua_state.hh"
|
---|
12 | #include "nv/uid.hh"
|
---|
13 |
|
---|
14 | using namespace nv;
|
---|
15 |
|
---|
16 | object::object()
|
---|
17 | : m_root( nullptr ), m_id(), m_name(), m_uid(0), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
|
---|
18 | {
|
---|
19 | // m_uid = uid_store::get_free_uid();
|
---|
20 | // uid_store::register_object( this, m_uid );
|
---|
21 | }
|
---|
22 |
|
---|
23 | object::object( root* aroot, const string& aid )
|
---|
24 | : m_root( aroot ), m_id(aid), m_name(), m_uid( 0 ), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
|
---|
25 | {
|
---|
26 | if ( m_root )
|
---|
27 | {
|
---|
28 | uid_store* store = get_root()->get_uid_store();
|
---|
29 | lua::state* state = get_root()->get_lua_state();
|
---|
30 | if (store)
|
---|
31 | {
|
---|
32 | m_uid = store->insert( this );
|
---|
33 | }
|
---|
34 | if (state)
|
---|
35 | {
|
---|
36 | m_lua_index = state->register_object( this );
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | }
|
---|
41 |
|
---|
42 | void object::add_child( object* child )
|
---|
43 | {
|
---|
44 | if (child)
|
---|
45 | {
|
---|
46 | if (child->m_parent)
|
---|
47 | {
|
---|
48 | child->detach();
|
---|
49 | }
|
---|
50 | child->m_parent = this;
|
---|
51 | m_children.push_back( child );
|
---|
52 | m_child_count++;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | void object::remove_child( object* child )
|
---|
57 | {
|
---|
58 | if ( child->m_parent != this )
|
---|
59 | {
|
---|
60 | return; // signal error?
|
---|
61 | }
|
---|
62 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
63 | if ( it != m_children.end() )
|
---|
64 | {
|
---|
65 | (*it)->m_parent = nullptr;
|
---|
66 | m_children.erase(it);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | void object::detach()
|
---|
71 | {
|
---|
72 | if (m_parent)
|
---|
73 | {
|
---|
74 | m_parent->remove_child( this );
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | void object::change_parent( object* new_parent )
|
---|
79 | {
|
---|
80 | if (m_parent) detach();
|
---|
81 | if (new_parent) new_parent->add_child( this );
|
---|
82 | }
|
---|
83 |
|
---|
84 | void object::destroy_children()
|
---|
85 | {
|
---|
86 | while ( !m_children.empty() )
|
---|
87 | {
|
---|
88 | delete m_children.front();
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | object::~object()
|
---|
93 | {
|
---|
94 | if ( m_lua_index != lua::ref_none )
|
---|
95 | {
|
---|
96 | lua::state* state = get_root()->get_lua_state();
|
---|
97 | state->unregister_object( this );
|
---|
98 | }
|
---|
99 | if ( m_uid != 0 && m_root )
|
---|
100 | {
|
---|
101 | uid_store* store = get_root()->get_uid_store();
|
---|
102 | if (store) store->remove( m_uid );
|
---|
103 | }
|
---|
104 | detach();
|
---|
105 | destroy_children();
|
---|
106 | }
|
---|
107 |
|
---|
108 | object* object::find( object* child, bool recursive /*= false */ )
|
---|
109 | {
|
---|
110 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
111 | if ( it != m_children.end() )
|
---|
112 | {
|
---|
113 | return *it;
|
---|
114 | }
|
---|
115 | if ( recursive )
|
---|
116 | {
|
---|
117 | for ( object* i : m_children )
|
---|
118 | {
|
---|
119 | object* r = i->find( child, recursive );
|
---|
120 | if (r) return r;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | return nullptr;
|
---|
124 | }
|
---|
125 |
|
---|
126 | object* object::find( uid child, bool recursive /*= false */ )
|
---|
127 | {
|
---|
128 | for ( object* i : m_children )
|
---|
129 | {
|
---|
130 | if (i->m_uid == child) return i;
|
---|
131 | }
|
---|
132 | if ( recursive )
|
---|
133 | {
|
---|
134 | for ( object* i : m_children )
|
---|
135 | {
|
---|
136 | object* r = i->find( child, recursive );
|
---|
137 | if (r) return r;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | return nullptr;
|
---|
141 | }
|
---|
142 |
|
---|
143 | object* object::find( string child, bool recursive /*= false */ )
|
---|
144 | {
|
---|
145 | for ( object* i : m_children )
|
---|
146 | {
|
---|
147 | if (i->m_id == child) return i;
|
---|
148 | }
|
---|
149 | if ( recursive )
|
---|
150 | {
|
---|
151 | for ( object* i : m_children )
|
---|
152 | {
|
---|
153 | object* r = i->find( child, recursive );
|
---|
154 | if (r) return r;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | return nullptr;
|
---|
158 | }
|
---|
159 |
|
---|
160 | bool object::move_to_top( object* child )
|
---|
161 | {
|
---|
162 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
163 | if ( it != m_children.end() )
|
---|
164 | {
|
---|
165 | m_children.erase( it );
|
---|
166 | m_children.push_back( child );
|
---|
167 | return true;
|
---|
168 | }
|
---|
169 | return false;
|
---|
170 | }
|
---|
171 |
|
---|
172 | bool object::move_to_bottom( object* child )
|
---|
173 | {
|
---|
174 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
175 | if ( it != m_children.end() )
|
---|
176 | {
|
---|
177 | m_children.erase( it );
|
---|
178 | m_children.push_front( child );
|
---|
179 | return true;
|
---|
180 | }
|
---|
181 | return false;
|
---|
182 | }
|
---|
183 |
|
---|
184 | void object::register_type( type_database* db )
|
---|
185 | {
|
---|
186 | type_field fields[] = {
|
---|
187 | type_field("id", &object::m_id),
|
---|
188 | type_field("uid", &object::m_uid).flag( TF_READONLY ),
|
---|
189 | type_field("lua_index", &object::m_lua_index).flag( TF_READONLY | TF_NOSERIALIZE ),
|
---|
190 | type_field("parent", &object::m_parent).flag( TF_READONLY | TF_NOSERIALIZE ),
|
---|
191 | type_field("child_count", &object::m_child_count).flag( TF_READONLY ),
|
---|
192 | type_field("children" , &object::m_children).flag( TF_READONLY ),
|
---|
193 | };
|
---|
194 | db->create_type<object>().fields(fields);
|
---|
195 | }
|
---|