1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef OBJECT_H
4 #define OBJECT_H
5 
6 #include <map>
7 #include <set>
8 #include "ObjectDependenceTypes.h"
9 #include "System/Platform/Threading.h"
10 #include "System/creg/creg_cond.h"
11 
12 
13 class CObject
14 {
15 public:
16 	CR_DECLARE(CObject)
17 
18 	CObject();
19 	virtual ~CObject();
20 	void Serialize(creg::ISerializer* ser);
21 	void PostLoad();
22 	virtual void Detach();
23 
24 	/// Request to not inform this when obj dies
25 	virtual void DeleteDeathDependence(CObject* obj, DependenceType dep);
26 	/// Request to inform this when obj dies
27 	virtual void AddDeathDependence(CObject* obj, DependenceType dep);
28 	/// Called when an object died, that this is interested in
29 	virtual void DependentDied(CObject* obj);
30 
31 /*
32 	// Possible future replacement for dynamic_cast (10x faster)
33 	// Identifier bits for classes that have subclasses
34 	enum {WORLDOBJECT_BIT=8,SOLIDOBJECT_BIT,UNIT_BIT,BUILDING_BIT,MOVETYPE_BIT,AAIRMOVETYPE_BIT,
35 		COMMANDAI_BIT,EXPGENSPAWNABLE_BIT,PROJECTILE_BIT,SMOKEPROJECTILE_BIT,WEAPON_BIT};
36 	// Class hierarchy for the relevant classes
37 	enum {
38 		OBJECT=0,
39 			WORLDOBJECT=(1<<WORLDOBJECT_BIT),
40 				SOLIDOBJECT=(1<<SOLIDOBJECT_BIT)|WORLDOBJECT,
41 					FEATURE,
42 					UNIT=(1<<UNIT_BIT)|SOLIDOBJECT,
43 						BUILDER,TRANSPORTUNIT,
44 						BUILDING=(1<<BUILDING_BIT)|UNIT,
45 							FACTORY,EXTRACTORBUILDING,
46 			MOVETYPE=(1<<MOVETYPE_BIT),
47 				GROUNDMOVETYPE,
48 				AAIRMOVETYPE=(1<<AAIRMOVETYPE_BIT)|MOVETYPE,
49 					AIRMOVETYPE,
50 					TAAIRMOVETYPE,
51 			COMMANDAI=(1<<COMMANDAI_BIT),
52 				FACTORYCAI,TRANSPORTCAI,MOBILECAI,
53 			EXPGENSPAWNABLE=(1<<EXPGENSPAWNABLE_BIT),
54 				PROJECTILE=(1<<PROJECTILE_BIT)|EXPGENSPAWNABLE,
55 					SHIELDPARTPROJECTILE,
56 					SMOKEPROJECTILE=(1<<SMOKEPROJECTILE_BIT)|PROJECTILE,
57 						GEOTHERMSMOKEPROJECTILE,
58 			WEAPON=(1<<WEAPON_BIT),
59 				DGUNWEAPON,BEAMLASER
60 	};
61 	// Must also set objType in the contstructors of all classes that need to use this feature
62 	unsigned objType;
63 #define INSTANCE_OF_SUBCLASS_OF(type,obj) ((obj->objType & kind) == kind) // exact class or any subclass of it
64 #define INSTANCE_OF(type,obj) (obj->objType == type) // exact class only, saves one instruction yay :)
65 */
66 
67 private:
68 	// Note, this has nothing to do with the UnitID, FeatureID, ...
69 	// It's only purpose is to make the sorting in TSyncSafeSet syncsafe
70 	boost::int64_t sync_id;
71 	static Threading::AtomicCounterInt64 cur_sync_id;
72 
73 	// makes std::set<T*> syncsafe (else iteration order depends on the pointer's address, which is not syncsafe)
74 	struct syncsafe_compare
75 	{
operatorsyncsafe_compare76 		bool operator() (const CObject* const& a1, const CObject* const& a2) const
77 		{
78 			// I don't think the default less-op is sync-safe
79 			//return a1 < a2;
80 			return a1->sync_id < a2->sync_id;
81 		}
82 	};
83 
84 public:
85 	typedef std::set<CObject*, syncsafe_compare> TSyncSafeSet;
86 	typedef std::map<DependenceType, TSyncSafeSet> TDependenceMap;
87 
88 protected:
GetListeners(const DependenceType dep)89 	const TSyncSafeSet& GetListeners(const DependenceType dep) { return listeners[dep]; }
GetAllListeners()90 	const TDependenceMap& GetAllListeners() const { return listeners; }
GetListening(const DependenceType dep)91 	const TSyncSafeSet& GetListening(const DependenceType dep)  { return listening[dep]; }
GetAllListening()92 	const TDependenceMap& GetAllListening() const { return listening; }
93 
94 protected:
95 	bool detached;
96 	TDependenceMap listeners;
97 	TDependenceMap listening;
98 };
99 
100 
101 
102 
103 #endif /* OBJECT_H */
104