source: trunk/src/object.cc @ 116

Last change on this file since 116 was 107, checked in by epyon, 12 years ago
  • proper initialization for root object
File size: 4.2 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 "nv/root.hh"
10#include "nv/types.hh"
11#include "nv/lua/lua_state.hh"
12#include "nv/uid.hh"
13
14using namespace nv;
15
16object::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}
20
21object::object( root* aroot, const string& aid )
22        : m_root( aroot ), m_id(aid), m_name(), m_uid( 0 ), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
23{
24        if ( m_root )
25        {
26                uid_store*  store = get_root()->get_uid_store();
27                lua::state* state = get_root()->get_lua_state();
28                if (store)
29                {
30                        m_uid = store->insert( this );
31                }
32                if (state)
33                {
34                        m_lua_index = state->register_object( this );
35                }
36        }
37
38}
39
40void object::add_child( object* child )
41{
42        if (child)
43        {
44                if (child->m_parent)
45                {
46                        child->detach();
47                }
48                child->m_parent = this;
49                m_children.push_back( child );
50                m_child_count++;
51                m_root->child_added( child );
52        }
53}
54
55void object::remove_child( object* child )
56{
57        if ( child->m_parent != this )
58        {
59                return; // signal error?
60        }
61        list::iterator it = std::find( m_children.begin(), m_children.end(), child );
62        if ( it != m_children.end() )
63        {
64                (*it)->m_parent = nullptr;
65                m_children.erase(it);
66                m_root->child_removed( child );
67        }       
68}
69
70void object::detach()
71{
72        if (m_parent)
73        {
74                m_parent->remove_child( this );
75        }
76}
77
78void object::change_parent( object* new_parent )
79{
80        if (m_parent) detach();
81        if (new_parent) new_parent->add_child( this );
82}
83
84void object::destroy_children()
85{
86        while ( !m_children.empty() )
87        {
88                delete m_children.front();
89        }
90}
91
92object::~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
108object* 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
126object* 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
143object* 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
160bool 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
172bool 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
184void 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>("object").fields(fields);
195}
Note: See TracBrowser for help on using the repository browser.