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 |
|
---|
12 | using namespace nv;
|
---|
13 |
|
---|
14 | object::object()
|
---|
15 | : m_root( nullptr ), m_id(), m_name(), m_uid(0), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
|
---|
16 | {
|
---|
17 | // m_uid = uid_store::get_free_uid();
|
---|
18 | // uid_store::register_object( this, m_uid );
|
---|
19 | }
|
---|
20 |
|
---|
21 | object::object( root* aroot, const string& aid, uid auid )
|
---|
22 | : m_root( aroot ), m_id(aid), m_name(), m_uid( auid ), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
|
---|
23 | {
|
---|
24 | // uid_store::register_object( this, auid );
|
---|
25 | }
|
---|
26 |
|
---|
27 | void object::add_child( object* child )
|
---|
28 | {
|
---|
29 | if (child)
|
---|
30 | {
|
---|
31 | if (child->m_parent)
|
---|
32 | {
|
---|
33 | child->detach();
|
---|
34 | }
|
---|
35 | child->m_parent = this;
|
---|
36 | m_children.push_back( child );
|
---|
37 | m_child_count++;
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | void object::remove_child( object* child )
|
---|
42 | {
|
---|
43 | if ( child->m_parent != this )
|
---|
44 | {
|
---|
45 | return; // signal error?
|
---|
46 | }
|
---|
47 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
48 | if ( it != m_children.end() )
|
---|
49 | {
|
---|
50 | (*it)->m_parent = nullptr;
|
---|
51 | m_children.erase(it);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | void object::detach()
|
---|
56 | {
|
---|
57 | if (m_parent)
|
---|
58 | {
|
---|
59 | m_parent->remove_child( this );
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | void object::change_parent( object* new_parent )
|
---|
64 | {
|
---|
65 | if (m_parent) detach();
|
---|
66 | if (new_parent) new_parent->add_child( this );
|
---|
67 | }
|
---|
68 |
|
---|
69 | void object::destroy_children()
|
---|
70 | {
|
---|
71 | while ( !m_children.empty() )
|
---|
72 | {
|
---|
73 | delete m_children.front();
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | object::~object()
|
---|
78 | {
|
---|
79 | // if ( m_lua_index != lua::ref_none )
|
---|
80 | // {
|
---|
81 | // lua::state::get()->unregister_object( this );
|
---|
82 | // }
|
---|
83 | // uid_store::unregister_object( m_uid );
|
---|
84 | detach();
|
---|
85 | destroy_children();
|
---|
86 | }
|
---|
87 |
|
---|
88 | object* object::find( object* child, bool recursive /*= false */ )
|
---|
89 | {
|
---|
90 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
91 | if ( it != m_children.end() )
|
---|
92 | {
|
---|
93 | return *it;
|
---|
94 | }
|
---|
95 | if ( recursive )
|
---|
96 | {
|
---|
97 | for ( object* i : m_children )
|
---|
98 | {
|
---|
99 | object* r = i->find( child, recursive );
|
---|
100 | if (r) return r;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | return nullptr;
|
---|
104 | }
|
---|
105 |
|
---|
106 | object* object::find( uid child, bool recursive /*= false */ )
|
---|
107 | {
|
---|
108 | for ( object* i : m_children )
|
---|
109 | {
|
---|
110 | if (i->m_uid == child) return i;
|
---|
111 | }
|
---|
112 | if ( recursive )
|
---|
113 | {
|
---|
114 | for ( object* i : m_children )
|
---|
115 | {
|
---|
116 | object* r = i->find( child, recursive );
|
---|
117 | if (r) return r;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | return nullptr;
|
---|
121 | }
|
---|
122 |
|
---|
123 | object* object::find( string child, bool recursive /*= false */ )
|
---|
124 | {
|
---|
125 | for ( object* i : m_children )
|
---|
126 | {
|
---|
127 | if (i->m_id == child) return i;
|
---|
128 | }
|
---|
129 | if ( recursive )
|
---|
130 | {
|
---|
131 | for ( object* i : m_children )
|
---|
132 | {
|
---|
133 | object* r = i->find( child, recursive );
|
---|
134 | if (r) return r;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | return nullptr;
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool object::move_to_top( object* child )
|
---|
141 | {
|
---|
142 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
143 | if ( it != m_children.end() )
|
---|
144 | {
|
---|
145 | m_children.erase( it );
|
---|
146 | m_children.push_back( child );
|
---|
147 | return true;
|
---|
148 | }
|
---|
149 | return false;
|
---|
150 | }
|
---|
151 |
|
---|
152 | bool object::move_to_bottom( object* child )
|
---|
153 | {
|
---|
154 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
155 | if ( it != m_children.end() )
|
---|
156 | {
|
---|
157 | m_children.erase( it );
|
---|
158 | m_children.push_front( child );
|
---|
159 | return true;
|
---|
160 | }
|
---|
161 | return false;
|
---|
162 | }
|
---|
163 |
|
---|
164 | void object::register_type( type_database* db )
|
---|
165 | {
|
---|
166 | type_field fields[] = {
|
---|
167 | type_field("id", &object::m_id),
|
---|
168 | type_field("uid", &object::m_uid).flag( TF_READONLY ),
|
---|
169 | type_field("lua_index", &object::m_lua_index).flag( TF_READONLY | TF_NOSERIALIZE ),
|
---|
170 | type_field("parent", &object::m_parent).flag( TF_READONLY | TF_NOSERIALIZE ),
|
---|
171 | type_field("child_count", &object::m_child_count).flag( TF_READONLY ),
|
---|
172 | type_field("children" , &object::m_children).flag( TF_READONLY ),
|
---|
173 | };
|
---|
174 | db->create_type<object>().fields(fields);
|
---|
175 | }
|
---|