source: trunk/src/object.cc @ 256

Last change on this file since 256 was 256, checked in by epyon, 11 years ago
  • various minor fixes
File size: 4.3 KB
Line 
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#include "nv/root.hh"
11#include "nv/types.hh"
12#include "nv/lua/lua_state.hh"
13#include "nv/uid.hh"
14
15using namespace nv;
16
17object::object()
18        : m_root( nullptr )
19        , m_id()
20        , m_name()
21        , m_uid(0)
22        , m_lua_index(lua::ref_none)
23        , m_lua_proto_index(lua::ref_none)
24        , m_parent( nullptr )
25        , m_children()
26        , m_child_count(0)
27{
28}
29
30object::object( root* aroot, const string& aid )
31        : m_root( aroot )
32        , m_id( aid )
33        , m_name()
34        , m_uid(0)
35        , m_lua_index(lua::ref_none)
36        , m_lua_proto_index(lua::ref_none)
37        , m_parent( nullptr )
38        , m_children()
39        , m_child_count(0)
40{
41        if ( m_root )
42        {
43                m_root->object_created( this );
44        }
45}
46
47void object::add_child( object* child )
48{
49        if (child)
50        {
51                if (child->m_parent)
52                {
53                        child->detach();
54                }
55                child->m_parent = this;
56                m_children.push_back( child );
57                m_child_count++;
58                m_root->child_added( child );
59        }
60}
61
62void object::remove_child( object* child )
63{
64        if ( child->m_parent != this )
65        {
66                return; // signal error?
67        }
68        list::iterator it = std::find( m_children.begin(), m_children.end(), child );
69        if ( it != m_children.end() )
70        {
71                (*it)->m_parent = nullptr;
72                m_children.erase(it);
73                m_root->child_removed( child );
74        }       
75}
76
77void object::detach()
78{
79        if (m_parent)
80        {
81                m_parent->remove_child( this );
82        }
83}
84
85void object::change_parent( object* new_parent )
86{
87        if (m_parent) detach();
88        if (new_parent) new_parent->add_child( this );
89}
90
91void object::destroy_children()
92{
93        while ( !m_children.empty() )
94        {
95                delete m_children.front();
96        }
97}
98
99object::~object()
100{
101        if ( m_root )
102        {
103                m_root->object_destroyed( this );
104        }
105        detach();
106        destroy_children();
107}
108
109object* object::find( object* child, bool recursive /*= false */ )
110{
111        list::iterator it = std::find( m_children.begin(), m_children.end(), child );
112        if ( it != m_children.end() )
113        {
114                return *it;
115        }
116        if ( recursive )
117        {
118                for ( object* i : m_children )
119                {
120                        object* r = i->find( child, recursive );
121                        if (r) return r;
122                }
123        }
124        return nullptr;
125}
126
127object* object::find( uid child, bool recursive /*= false */ )
128{
129        for ( object* i : m_children )
130        {
131                if (i->m_uid == child) return i;
132        }
133        if ( recursive )
134        {
135                for ( object* i : m_children )
136                {
137                        object* r = i->find( child, recursive );
138                        if (r) return r;
139                }
140        }
141        return nullptr;
142}
143
144object* object::find( string child, bool recursive /*= false */ )
145{
146        for ( object* i : m_children )
147        {
148                if (i->m_id == child) return i;
149        }
150        if ( recursive )
151        {
152                for ( object* i : m_children )
153                {
154                        object* r = i->find( child, recursive );
155                        if (r) return r;
156                }
157        }
158        return nullptr;
159}
160
161bool object::move_to_top( object* child )
162{
163        list::iterator it = std::find( m_children.begin(), m_children.end(), child );
164        if ( it != m_children.end() )
165        {
166                m_children.erase( it );
167                m_children.push_back( child );
168                return true;
169        }       
170        return false;
171}
172
173bool object::move_to_bottom( object* child )
174{
175        list::iterator it = std::find( m_children.begin(), m_children.end(), child );
176        if ( it != m_children.end() )
177        {
178                m_children.erase( it );
179                m_children.push_front( child );
180                return true;
181        }       
182        return false;
183}
184
185// void object::register_type( type_database* db )
186// {
187//      type_field fields[] = {
188//              type_field("id",          &object::m_id),
189//              type_field("uid",         &object::m_uid).flag( TF_READONLY ),
190//              type_field("lua_index",   &object::m_lua_index).flag( TF_READONLY | TF_NOSERIALIZE ),
191//              type_field("parent",      &object::m_parent).flag( TF_READONLY | TF_NOSERIALIZE ),
192//              type_field("child_count", &object::m_child_count).flag( TF_READONLY ),
193//              type_field("children"   , &object::m_children).flag( TF_READONLY ),
194//      };
195//      db->create_type<object>("object").fields(fields);
196// }
197
198void nv::object::register_with_lua( const char* lua_name, const char* storage )
199{
200        lua::state* state = get_root()->get_lua_state();
201        if (state)
202        {
203                if ( lua_name != nullptr )
204                {
205                        m_lua_index       = state->register_object( this, lua_name );
206                }
207                if ( storage != nullptr )
208                {
209                        m_lua_proto_index = state->register_proto( this, storage );
210                }
211        }
212}
Note: See TracBrowser for help on using the repository browser.