[57] | 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 |
|
---|
[64] | 9 | #include "nv/root.hh"
|
---|
[57] | 10 | #include "nv/types.hh"
|
---|
| 11 |
|
---|
| 12 | using namespace nv;
|
---|
| 13 |
|
---|
| 14 | object::object()
|
---|
[64] | 15 | : m_root( nullptr ), m_id(), m_name(), m_uid(0), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
|
---|
[57] | 16 | {
|
---|
| 17 | // m_uid = uid_store::get_free_uid();
|
---|
| 18 | // uid_store::register_object( this, m_uid );
|
---|
| 19 | }
|
---|
| 20 |
|
---|
[64] | 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)
|
---|
[57] | 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 | void object::register_type( type_database* db )
|
---|
| 141 | {
|
---|
| 142 | type_field fields[] = {
|
---|
| 143 | type_field("id", &object::m_id),
|
---|
| 144 | type_field("uid", &object::m_uid).flag( TF_READONLY ),
|
---|
| 145 | type_field("lua_index", &object::m_lua_index).flag( TF_READONLY | TF_NOSERIALIZE ),
|
---|
| 146 | type_field("parent", &object::m_parent).flag( TF_READONLY | TF_NOSERIALIZE ),
|
---|
| 147 | type_field("child_count", &object::m_child_count).flag( TF_READONLY ),
|
---|
| 148 | type_field("children" , &object::m_children).flag( TF_READONLY ),
|
---|
| 149 | };
|
---|
| 150 | db->create_type<object>().fields(fields);
|
---|
| 151 | }
|
---|