Instance.cpp
Go to the documentation of this file.
1 #include "Instance.hpp"
2 
3 #include "instance_ptr.hpp"
5 #include "SystemContainer.hpp"
6 
7 #include "../DebugView/TextDebugView.hpp"
8 
9 namespace iv
10 {
11 
13  sc( sc ),
14  sc_dup( nullptr ),
15  os( nullptr ),
16  _instance_name(),
17  _parent( nullptr ),
18  _children(),
19  _root_client( nullptr ),
20  _clients(),
21  _logging_cnt( 0 ),
22  instance_pointers()
23 {
24 }
25 
27 {
28  // unregister
29  if( this->_root_client )
30  {
31  //
32  if( this->getOS() )
33  this->getOS()->unregister_instance( this );
34 
35  //
36  if( this->getOS() )
37  this->getOS()->debug_listeners().foreach(
38  [ & ]( DebugInstanceListener * const & listener )
39  {
40  listener->InstanceDestroyed( this );
41  }
42  );
43  }
44 
45  // remove from parent and children
46  if( this->_parent )
47  this->_parent->_children.erase( this );
48  for( Instance * inst : this->_children )
49  inst->_parent = nullptr;
50 
51  // delete duplicated system container
52  if( this->sc_dup )
53  delete this->sc_dup;
54 }
55 
56 unsigned Instance::frame_id() const
57 {
58  return this->getSystemContainer()->frame_id();
59 }
60 
61 void Instance::instance_finalize( std::string const & inst_name, ClientMarker const * marker )
62 {
63  if( this->_root_client )
64  if( this->getOS() )
65  this->getOS()->log( SRC_INFO, Defs::Log::Warning, "There already is a root ClientMarker in this Instance (Instance was probably already finalized)." );
66 
67  //
68  this->_instance_name = inst_name;
69  this->_root_client = marker;
70 
71  //-- finalize creation ---
72  if( this->getOS() )
73  this->getOS()->register_instance( this );
74 
75  if( this->getOS() )
76  this->getOS()->debug_listeners().foreach(
77  [ & ]( DebugInstanceListener * const & listener )
78  {
79  listener->InstanceCreated( this );
80  }
81  );
82 }
83 
85 {
86  if( this->_parent )
87  this->_parent->_children.erase( this );
88 
89  this->_parent = parent;
90 
91  if( this->_parent )
92  this->_parent->_children.insert( this );
93 }
94 
96 {
97  ClientMarker const * cm = this->Debug_RootClient();
98  if( cm )
99  cm->print_status_with_related( view );
100 }
101 
103 {
104  return this->sc;
105 }
106 
108 {
109  if( !this->sc_dup )
110  this->sc_dup = new SystemContainer( this->sc );
111 
112  return this->sc_dup;
113 }
114 
115 InstanceSystem * Instance::getOS( )
116 {
117  if( this->os )
118  {
119  return this->os;
120  }
121  else
122  {
123  this->os = sc->getSystem< InstanceSystem >( );
124  return this->os;
125  }
126 }
127 
129 {
130  this->_clients.insert( cm );
131 
132  if( this->getOS() )
133  this->getOS()->debug_listeners().foreach(
134  [ & ]( DebugInstanceListener * const & listener )
135  {
136  listener->ClientCreated( cm );
137  }
138  );
139 }
140 
142 {
143  this->_clients.erase( cm );
144 
145  if( this->getOS() )
146  this->getOS()->debug_listeners().foreach(
147  [ & ]( DebugInstanceListener * const & listener )
148  {
149  listener->ClientDestroyed( cm );
150  }
151  );
152 }
153 
154 void Instance::client_log( ClientMarker const * cm, SrcInfo const & info, LogId id, std::string const & message )
155 {
156  if( this->getOS() )
157  this->getOS()->ClientLog( cm, info, id, message );
158 
159 }
160 
162 {
163  if( this->getOS() )
164  return this->getOS()->ClientLogEnabled( cm, id );
165 
166  return false;
167 }
168 
169 std::string const & Instance::instance_name()
170 {
171  return this->_instance_name;
172 }
173 
175 {
176  return this->_parent;
177 }
178 
179 std::unordered_set< Instance * > const & Instance::Debug_Children()
180 {
181  return this->_children;
182 }
183 
185 {
186  return this->_root_client;
187 }
188 
189 std::unordered_set< ClientMarker const * > const & Instance::Debug_Clients()
190 {
191  return this->_clients;
192 }
193 
194 }