source: trunk/src/object.cc @ 57

Last change on this file since 57 was 57, checked in by epyon, 12 years ago
  • nv/object - object class added with implementation and type registration
  • nv/types - container support (currently only storage and detection)
File size: 3.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/types.hh"
10
11using namespace nv;
12
13object::object()
14        : m_id(), m_name(), m_uid(0), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
15{
16//      m_uid = uid_store::get_free_uid();
17//      uid_store::register_object( this, m_uid );
18}
19
20object::object( string aid, uid auid )
21        : m_id(aid), m_name(), m_uid( auid ), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
22{
23        //      uid_store::register_object( this, auid );
24}
25
26void object::add_child( object* child )
27{
28        if (child)
29        {
30                if (child->m_parent)
31                {
32                        child->detach();
33                }
34                child->m_parent = this;
35                m_children.push_back( child );
36                m_child_count++;
37        }
38}
39
40void object::remove_child( object* child )
41{
42        if ( child->m_parent != this )
43        {
44                return; // signal error?
45        }
46        list::iterator it = std::find( m_children.begin(), m_children.end(), child );
47        if ( it != m_children.end() )
48        {
49                (*it)->m_parent = nullptr;
50                m_children.erase(it);
51        }       
52}
53
54void object::detach()
55{
56        if (m_parent)
57        {
58                m_parent->remove_child( this );
59        }
60}
61
62void object::change_parent( object* new_parent )
63{
64        if (m_parent) detach();
65        if (new_parent) new_parent->add_child( this );
66}
67
68void object::destroy_children()
69{
70        while ( !m_children.empty() )
71        {
72                delete m_children.front();
73        }
74}
75
76object::~object()
77{
78//      if ( m_lua_index != lua::ref_none )
79//      {
80//              lua::state::get()->unregister_object( this );
81//      }
82//      uid_store::unregister_object( m_uid );
83        detach();
84        destroy_children();
85}
86
87object* object::find( object* child, bool recursive /*= false */ )
88{
89        list::iterator it = std::find( m_children.begin(), m_children.end(), child );
90        if ( it != m_children.end() )
91        {
92                return *it;
93        }
94        if ( recursive )
95        {
96                for ( object* i : m_children )
97                {
98                        object* r = i->find( child, recursive );
99                        if (r) return r;
100                }
101        }
102        return nullptr;
103}
104
105object* object::find( uid child, bool recursive /*= false */ )
106{
107        for ( object* i : m_children )
108        {
109                if (i->m_uid == child) return i;
110        }
111        if ( recursive )
112        {
113                for ( object* i : m_children )
114                {
115                        object* r = i->find( child, recursive );
116                        if (r) return r;
117                }
118        }
119        return nullptr;
120}
121
122object* object::find( string child, bool recursive /*= false */ )
123{
124        for ( object* i : m_children )
125        {
126                if (i->m_id == child) return i;
127        }
128        if ( recursive )
129        {
130                for ( object* i : m_children )
131                {
132                        object* r = i->find( child, recursive );
133                        if (r) return r;
134                }
135        }
136        return nullptr;
137}
138
139void object::register_type( type_database* db )
140{
141        type_field fields[] = {
142                type_field("id",          &object::m_id),
143                type_field("uid",         &object::m_uid).flag( TF_READONLY ),
144                type_field("lua_index",   &object::m_lua_index).flag( TF_READONLY | TF_NOSERIALIZE ),
145                type_field("parent",      &object::m_parent).flag( TF_READONLY | TF_NOSERIALIZE ),
146                type_field("child_count", &object::m_child_count).flag( TF_READONLY ),
147                type_field("children"   , &object::m_children).flag( TF_READONLY ),
148        };
149        db->create_type<object>().fields(fields);
150}
Note: See TracBrowser for help on using the repository browser.