Pol  Revision:cb584c9
uobject.cpp
Go to the documentation of this file.
1 
10 #include "uobject.h"
11 
12 #include <atomic>
13 #include <iosfwd>
14 #include <stddef.h>
15 #include <string>
16 
17 #include "../clib/cfgelem.h"
18 #include "../clib/logfacility.h"
19 #include "../clib/passert.h"
20 #include "../clib/rawtypes.h"
21 #include "../clib/refptr.h"
22 #include "../clib/streamsaver.h"
23 #include "../plib/systemstate.h"
24 #include "baseobject.h"
25 #include "clidata.h"
26 #include "dynproperties.h"
27 #include "globals/state.h"
28 #include "item/itemdesc.h"
29 #include "objtype.h"
30 #include "polcfg.h"
31 #include "proplist.h"
32 #include "realms.h"
33 #include "realms/realm.h"
34 #include "tooltips.h"
35 #include "uconst.h"
36 #include "uobjcnt.h"
37 
38 namespace Pol
39 {
40 namespace Core
41 {
42 std::set<UObject*> unreaped_orphan_instances;
43 std::ofstream orphans_txt( "orphans.txt", std::ios::out | std::ios::trunc );
44 
46 {
48  Clib::OFStreamWriter sw_orphan( &orphans_txt );
49  sw() << o->name() << ", " << o->ref_counted_count() << '\n';
50  o->printOn( sw );
51  o->printOnDebug( sw_orphan );
52  INFO_PRINT << sw().str();
53  // ref_ptr<UObject>::display_referers( o->as_ref_counted() );
54 
55  return 0;
56 }
58 {
59  // orphans_txt.open( "orphans.txt", ios::out|ios::trunc );
60 
61  for ( auto& obj : unreaped_orphan_instances )
62  {
63  display_orphan( obj );
64  }
65  // for( std::set<UObject*>::iterator itr = unreaped_orphan_instances.begin();
66 }
67 
68 
69 std::atomic<unsigned int> UObject::dirty_writes;
70 std::atomic<unsigned int> UObject::clean_writes;
71 
72 UObject::UObject( u32 objtype, UOBJ_CLASS i_uobj_class )
73  : ref_counted(),
74  ULWObject( i_uobj_class ),
76  serial_ext( 0 ),
77  objtype_( objtype ),
78  color( 0 ),
79  facing( FACING_N ),
80  _rev( 0 ),
81  name_( "" ),
82  flags_(),
83  proplist_( CPropProfiler::class_to_type( i_uobj_class ) )
84 {
85  graphic = Items::getgraphic( objtype );
90 }
91 
93 {
94  if ( ref_counted::count() != 0 )
95  {
96  POLLOG_INFO << "Ouch! UObject::~UObject() with count()==" << ref_counted::count() << "\n";
97  }
98  passert( ref_counted::count() == 0 );
99  if ( serial == 0 )
100  {
102  }
104 }
105 
107 {
108  size_t size = sizeof( UObject ) + proplist_.estimatedSize();
109  size += estimateSizeDynProps();
110  return size;
111 }
112 
113 //
114 // Every UObject is registered with the objecthash after creation.
115 // (This can't happen in the ctor since the object isn't fully created yet)
116 //
117 // Scripts may still reference any object, so rather than outright delete,
118 // we set its serial to 0 (marking it "orphan", though "zombie" would probably be a better term).
119 // Later, when all _other_ references to the object have been deleted,
120 // objecthash.Reap() will remove its reference to this object, deleting it.
121 //
123 {
124  if ( serial != 0 )
125  {
126  if ( ref_counted::count() < 1 )
127  {
128  POLLOG_INFO << "Ouch! UObject::destroy() with count()==" << ref_counted::count() << "\n";
129  }
130 
131  set_dirty(); // we will have to write a 'object deleted' directive once
132 
133  serial =
134  0; // used to set serial_ext to 0. This way, if debugging, one can find out the old serial
135  passert( ref_counted::count() >= 1 );
136 
138  }
139 }
140 
141 bool UObject::dirty() const
142 {
143  return flags_.get( OBJ_FLAGS::DIRTY );
144 }
145 
147 {
148  if ( dirty() )
149  ++dirty_writes;
150  else
151  ++clean_writes;
153 }
154 
155 bool UObject::getprop( const std::string& propname, std::string& propval ) const
156 {
157  return proplist_.getprop( propname, propval );
158 }
159 
160 void UObject::setprop( const std::string& propname, const std::string& propvalue )
161 {
162  if ( propname[0] != '#' )
163  set_dirty();
164  proplist_.setprop( propname, propvalue ); // VOID_RETURN
165 }
166 
167 void UObject::eraseprop( const std::string& propname )
168 {
169  if ( propname[0] != '#' )
170  set_dirty();
171  proplist_.eraseprop( propname ); // VOID_RETURN
172 }
173 
174 void UObject::copyprops( const UObject& from )
175 {
176  set_dirty();
177  proplist_.copyprops( from.proplist_ );
178 }
179 
180 void UObject::copyprops( const PropertyList& proplist )
181 {
182  set_dirty();
183  proplist_.copyprops( proplist );
184 }
185 
186 void UObject::getpropnames( std::vector<std::string>& propnames ) const
187 {
188  proplist_.getpropnames( propnames );
189 }
190 
192 {
193  return proplist_;
194 }
195 
196 std::string UObject::name() const
197 {
198  return name_;
199 }
200 
201 std::string UObject::description() const
202 {
203  return name_;
204 }
205 
206 void UObject::setname( const std::string& newname )
207 {
208  set_dirty();
209  increv();
211  name_ = newname;
212 }
213 
215 {
216  return nullptr;
217 }
218 
219 const UObject* UObject::owner() const
220 {
221  return nullptr;
222 }
223 
225 {
226  return this;
227 }
228 
230 {
231  return this;
232 }
233 
235 {
236  return this;
237 }
238 
240 {
241  return this;
242 }
243 
244 
246 {
247  using namespace fmt;
248 
249  if ( !name_.get().empty() )
250  sw() << "\tName\t" << name_.get() << pf_endl;
251 
252  sw() << "\tSerial\t0x" << hex( serial ) << pf_endl;
253  sw() << "\tObjType\t0x" << hex( objtype_ ) << pf_endl;
254  sw() << "\tGraphic\t0x" << hex( graphic ) << pf_endl;
255 
256  if ( color != 0 )
257  sw() << "\tColor\t0x" << hex( color ) << pf_endl;
258 
259  sw() << "\tX\t" << x << pf_endl;
260  sw() << "\tY\t" << y << pf_endl;
261  sw() << "\tZ\t" << (int)z << pf_endl;
262 
263  if ( facing )
264  sw() << "\tFacing\t" << static_cast<int>( facing ) << pf_endl;
265 
266  sw() << "\tRevision\t" << rev() << pf_endl;
267  if ( realm == nullptr )
268  sw() << "\tRealm\tbritannia" << pf_endl;
269  else
270  sw() << "\tRealm\t" << realm->name() << pf_endl;
271 
273 }
274 
276 {
277  sw() << "# uobj_class: " << (int)uobj_class_ << pf_endl;
278 }
279 
282 {
283  if ( graphic > ( Plib::systemstate.config.max_tile_id ) )
285 }
286 
288 {
289  name_ = elem.remove_string( "NAME", "" );
290 
291  // serial, objtype extracted by caller
292  graphic = elem.remove_ushort( "GRAPHIC", static_cast<u16>( objtype_ ) );
294 
296 
297  color = elem.remove_ushort( "COLOR", 0 );
298 
299 
300  std::string realmstr = elem.remove_string( "Realm", "britannia" );
301  realm = find_realm( realmstr );
302  if ( !realm )
303  {
304  ERROR_PRINT.Format( "{} '{}' (0x{:X}): has an invalid realm property '{}'.\n" )
305  << classname() << name() << serial << realmstr;
306  throw std::runtime_error( "Data integrity error" );
307  }
308  x = elem.remove_ushort( "X" );
309  y = elem.remove_ushort( "Y" );
310  z = static_cast<s8>( elem.remove_int( "Z" ) );
311  if ( !realm->valid( x, y, z ) )
312  {
313  x = static_cast<u16>( realm->width() ) - 1;
314  y = static_cast<u16>( realm->height() ) - 1;
315  z = 0;
316  }
317 
318  unsigned short tmp = elem.remove_ushort( "FACING", 0 );
319  setfacing( static_cast<unsigned char>( tmp ) );
320 
321  _rev = elem.remove_ulong( "Revision", 0 );
322 
323 
324  proplist_.readProperties( elem );
325 }
326 
328 {
329  printOn( sw );
330 }
331 
333 {
334  sw() << classname() << pf_endl;
335  sw() << "{" << pf_endl;
336  printProperties( sw );
337  sw() << "}" << pf_endl;
338  sw() << pf_endl;
339  // sw.flush();
340 }
341 
343 {
344  sw() << classname() << pf_endl;
345  sw() << "{" << pf_endl;
346  printProperties( sw );
347  printDebugProperties( sw );
348  sw() << "}" << pf_endl;
349  sw() << pf_endl;
350  // sw.flush();
351 }
352 
354 {
355  obj.printOn( writer );
356  return writer;
357 }
358 
359 bool UObject::setgraphic( u16 /*newgraphic*/ )
360 {
361  ERROR_PRINT.Format(
362  "UOBject::SetGraphic used, object class does not have a graphic member! Object Serial: "
363  "0x{:X}\n" )
364  << serial;
365  return false;
366 }
367 
368 bool UObject::setcolor( u16 newcolor )
369 {
370  set_dirty();
371 
372  if ( color != newcolor )
373  {
374  color = newcolor;
376  }
377 
378  return true;
379 }
380 
382 
384 
386 {
388 }
389 
390 void UObject::saveonexit( bool newvalue )
391 {
393 }
394 
395 const char* UObject::target_tag() const
396 {
397  return "object";
398 }
399 }
400 }
void printProperties(Clib::StreamWriter &sw) const
Definition: proplist.cpp:429
void display_unreaped_orphan_instances()
Definition: uobject.cpp:57
static std::atomic< unsigned int > dirty_writes
Definition: uobject.h:224
std::string remove_string(const char *propname)
Definition: cfgfile.cpp:381
SystemState systemstate
Definition: systemstate.cpp:12
void setprop(const std::string &propname, const std::string &propvalue)
Definition: uobject.cpp:160
size_t estimateSizeDynProps() const
virtual bool setgraphic(u16 newobjtype)
Definition: uobject.cpp:359
UObject(u32 objtype, UOBJ_CLASS uobj_class)
Definition: uobject.cpp:72
#define POLLOG_INFO
Definition: logfacility.h:213
unsigned int remove_ulong(const char *propname)
Definition: cfgfile.cpp:461
void set(ENUM flag)
Definition: uobject.h:113
bool get(ENUM flag) const
Definition: uobject.h:108
int remove_int(const char *propname)
Definition: cfgfile.cpp:340
virtual UObject * owner()
Definition: uobject.cpp:214
size_t estimatedSize() const
Definition: proplist.cpp:362
virtual void fixInvalidGraphic()
Fixes invalid graphic, moving here to allow it to be overridden in subclass (see Multi) ...
Definition: uobject.cpp:281
void getpropnames(std::vector< std::string > &propnames) const
Definition: uobject.cpp:186
virtual ~UObject()
Definition: uobject.cpp:92
virtual void setfacing(u8 newfacing)=0
unsigned ref_counted_count() const
Definition: uobject.h:306
void eraseprop(const std::string &propname)
Definition: proplist.cpp:395
virtual size_t estimatedSize() const
Definition: uobject.cpp:106
PropertyList proplist_
Definition: uobject.h:277
const std::string name() const
Definition: realm.cpp:124
virtual void on_color_changed()
Definition: uobject.cpp:381
bool dirty() const
Definition: uobject.cpp:141
void set_dirty()
Definition: uobject.h:291
unsigned short getgraphic(u32 objtype)
Definition: itemdesc.cpp:906
unsigned short u16
Definition: rawtypes.h:26
unsigned int u32
Definition: rawtypes.h:27
virtual const char * target_tag() const
Definition: uobject.cpp:395
bool getprop(const std::string &propname, std::string &propvalue) const
Definition: uobject.cpp:155
static std::atomic< unsigned int > clean_writes
Definition: uobject.h:225
std::ofstream orphans_txt("orphans.txt", std::ios::out|std::ios::trunc)
virtual void destroy()
Definition: uobject.cpp:122
AttributeFlags< OBJ_FLAGS > flags_
Definition: uobject.h:274
virtual UObject * self_as_owner()
Definition: uobject.cpp:224
void eraseprop(const std::string &propname)
Definition: uobject.cpp:167
unsigned short height() const
Definition: realm.h:245
void setprop(const std::string &propname, const std::string &propvalue)
Definition: proplist.cpp:387
std::set< UObject * > unreaped_orphan_instances
Definition: uobject.cpp:42
virtual void printSelfOn(Clib::StreamWriter &sw) const
Definition: uobject.cpp:327
virtual const char * classname() const =0
char tileheight(unsigned short tilenum)
Definition: polfile2.cpp:34
void copyprops(const PropertyList &proplist)
Definition: proplist.cpp:403
#define passert(exp)
Definition: passert.h:62
void remove(ENUM flag)
Definition: uobject.h:114
const PropertyList & getprops() const
Definition: uobject.cpp:191
signed char s8
Definition: rawtypes.h:29
void readProperties(Clib::ConfigElem &elem)
Definition: proplist.cpp:464
virtual UObject * toplevel_owner()
Definition: uobject.cpp:234
const u32 objtype_
Definition: uobject.h:249
void setname(const std::string &)
Definition: uobject.cpp:206
void getpropnames(std::vector< std::string > &propnames) const
Definition: proplist.cpp:421
unsigned int count() const
Definition: refptr.h:130
bool saveonexit() const
Definition: uobject.cpp:385
StateManager stateManager
Definition: state.cpp:8
Clib::StreamWriter & operator<<(Clib::StreamWriter &writer, const UObject &obj)
Definition: uobject.cpp:353
virtual void printOnDebug(Clib::StreamWriter &sw) const
Definition: uobject.cpp:342
u32 rev() const
Definition: uobject.h:220
virtual void on_facing_changed()
Definition: uobject.cpp:383
virtual void printDebugProperties(Clib::StreamWriter &sw) const
Definition: uobject.cpp:275
const UOBJ_CLASS uobj_class_
Definition: baseobject.h:65
virtual void readProperties(Clib::ConfigElem &elem)
Definition: uobject.cpp:287
Realms::Realm * realm
Definition: baseobject.h:56
void clear_dirty() const
Definition: uobject.cpp:146
virtual void printOn(Clib::StreamWriter &) const
Definition: uobject.cpp:332
unsigned short width() const
Definition: realm.h:241
unsigned short remove_ushort(const char *propname)
Definition: cfgfile.cpp:318
bool getprop(const std::string &propname, std::string &propvalue) const
Definition: proplist.cpp:371
Realms::Realm * find_realm(const std::string &name)
Definition: realms.cpp:64
void change(ENUM flag, bool value)
Definition: uobject.h:115
#define ERROR_PRINT
Definition: logfacility.h:230
virtual void printProperties(Clib::StreamWriter &sw) const
Definition: uobject.cpp:245
bool valid(unsigned short x, unsigned short y, short z) const
Definition: realm.cpp:119
virtual bool setcolor(u16 newcolor)
Definition: uobject.cpp:368
void send_object_cache_to_inrange(const UObject *obj)
Definition: tooltips.cpp:77
#define INFO_PRINT
Definition: logfacility.h:223
virtual std::string description() const
Definition: uobject.cpp:201
virtual std::string name() const
Definition: uobject.cpp:196
#define pf_endl
Definition: proplist.cpp:25
Definition: berror.cpp:12
boost_utils::object_name_flystring name_
Definition: uobject.h:272
void copyprops(const UObject &obj)
Definition: uobject.cpp:174
#define GRAPHIC_NODRAW
Definition: objtype.h:19
int display_orphan(UObject *o)
Definition: uobject.cpp:45
UObjCount uobjcount
Definition: state.h:49