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 <algorithm>
|
---|
10 |
|
---|
11 | using namespace nv;
|
---|
12 |
|
---|
13 | object::object( const string& aid )
|
---|
14 | : m_id( aid )
|
---|
15 | , m_name()
|
---|
16 | , m_uid(0)
|
---|
17 | , m_parent( nullptr )
|
---|
18 | , m_children()
|
---|
19 | , m_child_count(0)
|
---|
20 | {
|
---|
21 | }
|
---|
22 |
|
---|
23 | void object::add_child( object* child )
|
---|
24 | {
|
---|
25 | if (child)
|
---|
26 | {
|
---|
27 | if (child->m_parent)
|
---|
28 | {
|
---|
29 | child->detach();
|
---|
30 | }
|
---|
31 | child->m_parent = this;
|
---|
32 | m_children.push_back( child );
|
---|
33 | m_child_count++;
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | void object::remove_child( object* child )
|
---|
38 | {
|
---|
39 | if ( child->m_parent != this )
|
---|
40 | {
|
---|
41 | return; // signal error?
|
---|
42 | }
|
---|
43 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
44 | if ( it != m_children.end() )
|
---|
45 | {
|
---|
46 | (*it)->m_parent = nullptr;
|
---|
47 | m_children.erase(it);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | void object::detach()
|
---|
52 | {
|
---|
53 | if (m_parent)
|
---|
54 | {
|
---|
55 | m_parent->remove_child( this );
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | void object::change_parent( object* new_parent )
|
---|
60 | {
|
---|
61 | if (m_parent) detach();
|
---|
62 | if (new_parent) new_parent->add_child( this );
|
---|
63 | }
|
---|
64 |
|
---|
65 | object::~object()
|
---|
66 | {
|
---|
67 | }
|
---|
68 |
|
---|
69 | object* object::find( object* child, bool recursive /*= false */ )
|
---|
70 | {
|
---|
71 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
72 | if ( it != m_children.end() )
|
---|
73 | {
|
---|
74 | return *it;
|
---|
75 | }
|
---|
76 | if ( recursive )
|
---|
77 | {
|
---|
78 | for ( object* i : m_children )
|
---|
79 | {
|
---|
80 | object* r = i->find( child, recursive );
|
---|
81 | if (r) return r;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | return nullptr;
|
---|
85 | }
|
---|
86 |
|
---|
87 | object* object::find( uid child, bool recursive /*= false */ )
|
---|
88 | {
|
---|
89 | for ( object* i : m_children )
|
---|
90 | {
|
---|
91 | if (i->m_uid == child) return i;
|
---|
92 | }
|
---|
93 | if ( recursive )
|
---|
94 | {
|
---|
95 | for ( object* i : m_children )
|
---|
96 | {
|
---|
97 | object* r = i->find( child, recursive );
|
---|
98 | if (r) return r;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | return nullptr;
|
---|
102 | }
|
---|
103 |
|
---|
104 | object* object::find( string child, bool recursive /*= false */ )
|
---|
105 | {
|
---|
106 | for ( object* i : m_children )
|
---|
107 | {
|
---|
108 | if (i->m_id == child) return i;
|
---|
109 | }
|
---|
110 | if ( recursive )
|
---|
111 | {
|
---|
112 | for ( object* i : m_children )
|
---|
113 | {
|
---|
114 | object* r = i->find( child, recursive );
|
---|
115 | if (r) return r;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | return nullptr;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | // void object::register_type( type_database* db )
|
---|
123 | // {
|
---|
124 | // type_field fields[] = {
|
---|
125 | // type_field("id", &object::m_id),
|
---|
126 | // type_field("uid", &object::m_uid).flag( TF_READONLY ),
|
---|
127 | // type_field("lua_index", &object::m_lua_index).flag( TF_READONLY | TF_NOSERIALIZE ),
|
---|
128 | // type_field("parent", &object::m_parent).flag( TF_READONLY | TF_NOSERIALIZE ),
|
---|
129 | // type_field("child_count", &object::m_child_count).flag( TF_READONLY ),
|
---|
130 | // type_field("children" , &object::m_children).flag( TF_READONLY ),
|
---|
131 | // };
|
---|
132 | // db->create_type<object>("object").fields(fields);
|
---|
133 | // }
|
---|