File: NamespaceManager.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (105 lines) | stat: -rw-r--r-- 2,782 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#pragma once

#include "ientity.h"
#include "inamespace.h"

#include <map>
#include "SpawnArgs.h"
#include "KeyValueObserver.h"
#include "NameKeyObserver.h"
#include "util/Noncopyable.h"

namespace entity {

class NamespaceManager :
	public Entity::Observer,
	public Namespaced,
	public util::Noncopyable
{
private:
	INamespace* _namespace;

	// The attached entity
	SpawnArgs& _entity;

	// All the observed key values of the entity get remembered
	// This prevents having to traverse all the keyvalues again when changing namespaces
	typedef std::map<std::string, EntityKeyValue*> KeyValues;
	KeyValues _nameKeys;

	typedef std::map<EntityKeyValue*, NameKeyObserverPtr> NameKeyObserverMap;
	NameKeyObserverMap _nameKeyObservers;

	typedef std::map<EntityKeyValue*, KeyValueObserverPtr> KeyValueObserverMap;
	KeyValueObserverMap _keyValueObservers;

	// lock for this class to avoid double-updates
	bool _updateMutex;

	// The key defining a name spawnarg, usually "name"
	std::string _nameKey;

public:
	NamespaceManager(SpawnArgs& entity);

	~NamespaceManager();

	// Gets/sets the namespace of this named object
	void setNamespace(INamespace* space);
	INamespace* getNamespace() const;

	void attachNames();
	void detachNames();

	void connectNameObservers();
	void disconnectNameObservers();

	// Returns the name of this entity
	std::string getName() const;

	// Changes the name of this entity
	void changeName(const std::string& newName);

	/**
	 * greebo: This gets called as soon as a new entity key/value gets added
	 * to the attached entity.
	 *
	 * The routine saves all relevant keyvalues and attaches the
	 * "name keys" to the Namespace.
	 *
	 * Note: Entity::Observer implementation
	 */
	void onKeyInsert(const std::string& key, EntityKeyValue& value);

	/**
	 * greebo: Gets called by the observed Entity when a value is erased from
	 * the list of spawnargs.
	 *
	 * Note: Entity::Observer implementation
	 */
	void onKeyErase(const std::string& key, EntityKeyValue& value);

private:
	/**
	 * greebo: returns TRUE if the given key is recognised as "name" for the
	 * selected game type.
	 */
	bool keyIsName(const std::string& key);

	// Some keyvalues are not referring to names, but to entityDefs, those should not 
	// change themselves if an incidentially matching name is changed
	bool keyIsReferringToEntityDef(const std::string& key);

	void detachNameKeys();

	void attachKeyObservers();
	void detachKeyObservers();

	void attachKeyToNamespace(const std::string& key, EntityKeyValue& keyValue);
	void detachKeyFromNamespace(const std::string& key, EntityKeyValue& keyValue);

	void attachKeyObserver(const std::string& key, EntityKeyValue& keyValue);
	void detachKeyObserver(const std::string& key, EntityKeyValue& keyValue);
};

} // namespace entity