[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 |
|
---|
[120] | 9 | #include <algorithm>
|
---|
[64] | 10 | #include "nv/root.hh"
|
---|
[57] | 11 | #include "nv/types.hh"
|
---|
[77] | 12 | #include "nv/lua/lua_state.hh"
|
---|
| 13 | #include "nv/uid.hh"
|
---|
[57] | 14 |
|
---|
| 15 | using namespace nv;
|
---|
| 16 |
|
---|
| 17 | object::object()
|
---|
[217] | 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)
|
---|
[57] | 27 | {
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[77] | 30 | object::object( root* aroot, const string& aid )
|
---|
[217] | 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)
|
---|
[57] | 40 | {
|
---|
[77] | 41 | if ( m_root )
|
---|
| 42 | {
|
---|
[256] | 43 | m_root->object_created( this );
|
---|
[77] | 44 | }
|
---|
[57] | 45 | }
|
---|
| 46 |
|
---|
| 47 | void 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++;
|
---|
[105] | 58 | m_root->child_added( child );
|
---|
[57] | 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | void 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);
|
---|
[105] | 73 | m_root->child_removed( child );
|
---|
[57] | 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void object::detach()
|
---|
| 78 | {
|
---|
| 79 | if (m_parent)
|
---|
| 80 | {
|
---|
| 81 | m_parent->remove_child( this );
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | void object::change_parent( object* new_parent )
|
---|
| 86 | {
|
---|
| 87 | if (m_parent) detach();
|
---|
| 88 | if (new_parent) new_parent->add_child( this );
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | void object::destroy_children()
|
---|
| 92 | {
|
---|
| 93 | while ( !m_children.empty() )
|
---|
| 94 | {
|
---|
| 95 | delete m_children.front();
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | object::~object()
|
---|
| 100 | {
|
---|
[256] | 101 | if ( m_root )
|
---|
[77] | 102 | {
|
---|
[256] | 103 | m_root->object_destroyed( this );
|
---|
[77] | 104 | }
|
---|
[57] | 105 | detach();
|
---|
| 106 | destroy_children();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | object* 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 |
|
---|
| 127 | object* 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 |
|
---|
| 144 | object* 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 |
|
---|
[66] | 161 | bool 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 |
|
---|
| 173 | bool 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 |
|
---|
[256] | 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 | // }
|
---|
[187] | 197 |
|
---|
[217] | 198 | void nv::object::register_with_lua( const char* lua_name, const char* storage )
|
---|
[187] | 199 | {
|
---|
| 200 | lua::state* state = get_root()->get_lua_state();
|
---|
| 201 | if (state)
|
---|
| 202 | {
|
---|
| 203 | if ( lua_name != nullptr )
|
---|
| 204 | {
|
---|
[217] | 205 | m_lua_index = state->register_object( this, lua_name );
|
---|
[187] | 206 | }
|
---|
[217] | 207 | if ( storage != nullptr )
|
---|
[187] | 208 | {
|
---|
[217] | 209 | m_lua_proto_index = state->register_proto( this, storage );
|
---|
[187] | 210 | }
|
---|
| 211 | }
|
---|
| 212 | }
|
---|