[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()
|
---|
[64] | 18 | : m_root( nullptr ), m_id(), m_name(), m_uid(0), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
|
---|
[57] | 19 | {
|
---|
| 20 | }
|
---|
| 21 |
|
---|
[77] | 22 | object::object( root* aroot, const string& aid )
|
---|
| 23 | : m_root( aroot ), m_id(aid), m_name(), m_uid( 0 ), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
|
---|
[57] | 24 | {
|
---|
[77] | 25 | if ( m_root )
|
---|
| 26 | {
|
---|
| 27 | uid_store* store = get_root()->get_uid_store();
|
---|
| 28 | lua::state* state = get_root()->get_lua_state();
|
---|
| 29 | if (store)
|
---|
| 30 | {
|
---|
| 31 | m_uid = store->insert( this );
|
---|
| 32 | }
|
---|
| 33 | if (state)
|
---|
| 34 | {
|
---|
| 35 | m_lua_index = state->register_object( this );
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[57] | 39 | }
|
---|
| 40 |
|
---|
| 41 | void object::add_child( object* child )
|
---|
| 42 | {
|
---|
| 43 | if (child)
|
---|
| 44 | {
|
---|
| 45 | if (child->m_parent)
|
---|
| 46 | {
|
---|
| 47 | child->detach();
|
---|
| 48 | }
|
---|
| 49 | child->m_parent = this;
|
---|
| 50 | m_children.push_back( child );
|
---|
| 51 | m_child_count++;
|
---|
[105] | 52 | m_root->child_added( child );
|
---|
[57] | 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void object::remove_child( object* child )
|
---|
| 57 | {
|
---|
| 58 | if ( child->m_parent != this )
|
---|
| 59 | {
|
---|
| 60 | return; // signal error?
|
---|
| 61 | }
|
---|
| 62 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
| 63 | if ( it != m_children.end() )
|
---|
| 64 | {
|
---|
| 65 | (*it)->m_parent = nullptr;
|
---|
| 66 | m_children.erase(it);
|
---|
[105] | 67 | m_root->child_removed( child );
|
---|
[57] | 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void object::detach()
|
---|
| 72 | {
|
---|
| 73 | if (m_parent)
|
---|
| 74 | {
|
---|
| 75 | m_parent->remove_child( this );
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | void object::change_parent( object* new_parent )
|
---|
| 80 | {
|
---|
| 81 | if (m_parent) detach();
|
---|
| 82 | if (new_parent) new_parent->add_child( this );
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | void object::destroy_children()
|
---|
| 86 | {
|
---|
| 87 | while ( !m_children.empty() )
|
---|
| 88 | {
|
---|
| 89 | delete m_children.front();
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | object::~object()
|
---|
| 94 | {
|
---|
[77] | 95 | if ( m_lua_index != lua::ref_none )
|
---|
| 96 | {
|
---|
| 97 | lua::state* state = get_root()->get_lua_state();
|
---|
| 98 | state->unregister_object( this );
|
---|
| 99 | }
|
---|
| 100 | if ( m_uid != 0 && m_root )
|
---|
| 101 | {
|
---|
| 102 | uid_store* store = get_root()->get_uid_store();
|
---|
| 103 | if (store) store->remove( m_uid );
|
---|
| 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 |
|
---|
[57] | 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 | };
|
---|
[78] | 195 | db->create_type<object>("object").fields(fields);
|
---|
[57] | 196 | }
|
---|