| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <set> |
| 4 |
#include "inode.h" |
| 5 |
#include "imodule.h" |
| 6 |
|
| 7 |
// Forward declaration (is declared in inameobserver.h) |
| 8 |
class NameObserver; |
| 9 |
|
| 10 |
/** |
| 11 |
* greebo: The Namespace is the managing class which keeps track |
| 12 |
* of all the names in the map. It gets notified on name changes |
| 13 |
* and broadcasts this event to all registered NameObservers. |
| 14 |
* |
| 15 |
* Additionally, it provides algorithms to merge elements from |
| 16 |
* another namespace into this one, preserving all links between |
| 17 |
* Namespaced objects. |
| 18 |
*/ |
| 19 |
class INamespace |
| 20 |
{ |
| 21 |
public: |
| 22 |
|
| 23 |
virtual ~INamespace() {} |
| 24 |
|
| 25 |
/** |
| 26 |
* greebo: Call this to put the given scene::Node and all its |
| 27 |
* children into this Namespace. This will search for Namespaced |
| 28 |
* items and tell them to add their relevant "names" to this space. |
| 29 |
*/ |
| 30 |
virtual void connect(const scene::INodePtr& root) = 0; |
| 31 |
|
| 32 |
/** |
| 33 |
* greebo: The counter-part of the connect() method above. This will |
| 34 |
* remove the given node and all its children from this namespace. |
| 35 |
*/ |
| 36 |
virtual void disconnect(const scene::INodePtr& root) = 0; |
| 37 |
|
| 38 |
/** |
| 39 |
* greebo: Returns true if the given name already exists in this namespace. |
| 40 |
*/ |
| 41 |
virtual bool nameExists(const std::string& name) = 0; |
| 42 |
|
| 43 |
/** |
| 44 |
* greebo: Inserts the given name into this namespace. The name |
| 45 |
* must not exist yet, it won't get inserted otherwise. |
| 46 |
* |
| 47 |
* @returns: TRUE if the insertion was successful, FALSE if the name already exists. |
| 48 |
*/ |
| 49 |
virtual bool insert(const std::string& name) = 0; |
| 50 |
|
| 51 |
/** |
| 52 |
* greebo: Removes the given name from this namespace. The name |
| 53 |
* is available again afterwards. |
| 54 |
* |
| 55 |
* @returns: TRUE if the removal was successful, FALSE if the name did not exist. |
| 56 |
*/ |
| 57 |
virtual bool erase(const std::string& name) = 0; |
| 58 |
|
| 59 |
/** |
| 60 |
* \brief |
| 61 |
* Add a new name to the namespace, ensuring that it is unique by adding or |
| 62 |
* changing a numeric prefix if necessary. |
| 63 |
* |
| 64 |
* \return |
| 65 |
* The actual name that was inserted, unique in this namespace. |
| 66 |
*/ |
| 67 |
virtual std::string addUniqueName(const std::string& originalName) = 0; |
| 68 |
|
| 69 |
/** |
| 70 |
* greebo: Registers/de-registers a Observer. The Observer will get notified |
| 71 |
* as soon as the name changes. |
| 72 |
*/ |
| 73 |
virtual void addNameObserver(const std::string& name, NameObserver& observer) = 0; |
| 74 |
virtual void removeNameObserver(const std::string& name, NameObserver& observer) = 0; |
| 75 |
|
| 76 |
/** |
| 77 |
* greebo: Tells the namespace to notify the nameobservers about a name change. |
| 78 |
* This usually gets called by the entity itself, whose name has been altered. |
| 79 |
*/ |
| 80 |
virtual void nameChanged(const std::string& oldName, const std::string& newName) = 0; |
| 81 |
|
| 82 |
/** |
| 83 |
* \brief |
| 84 |
* Prepares the given scene graph for import into this namespace by ensuring |
| 85 |
* that none of its names conflict with those in this namespace. |
| 86 |
* |
| 87 |
* The nodes below <root> should have been added to a different namespace |
| 88 |
* prior to this call, so that links are preserved during name changes. |
| 89 |
* |
| 90 |
* After this call, the imported nodes are renamed to fit into this |
| 91 |
* namespace and can be safely connected to this Namespace. This method does |
| 92 |
* \em not actually import the given scene graph's names into this |
| 93 |
* namespace. |
| 94 |
*/ |
| 95 |
virtual void ensureNoConflicts(const scene::INodePtr& foreignRoot) = 0; |
| 96 |
|
| 97 |
/** |
| 98 |
* Specialised variant of the above ensureNoConflicts(foreignRoot): |
| 99 |
* Prepares only the given list of nodes (that are member of the foreign root) such that |
| 100 |
* their names don't conflict with any name in this namespace. |
| 101 |
* |
| 102 |
* Every Namespaced node will get a new unique name assigned, but only if it's |
| 103 |
* actually colliding with a local name, otherwise it will remain unchanged. |
| 104 |
*/ |
| 105 |
virtual void ensureNoConflicts(const scene::INodePtr& foreignRoot, const std::set<scene::INodePtr>& foreignNodes) = 0; |
| 106 |
}; |
| 107 |
typedef std::shared_ptr<INamespace> INamespacePtr; |
| 108 |
|
| 109 |
/** |
| 110 |
* greebo: A Namespaced object is the primary communication |
| 111 |
* partner for the Namespace. This object provides methods |
| 112 |
* to connect/disconnect items from a map Namespace. |
| 113 |
* |
| 114 |
* Note: the standard implementation in D3 maps is |
| 115 |
* the NamespaceManager class in the entity module. |
| 116 |
* |
| 117 |
* Note: I chose to use raw INamespace* pointers in the interface |
| 118 |
* to allow the Namespace calling these functions without maintaining |
| 119 |
* weak_ptrs to itself. This can of course be changed in the |
| 120 |
* future, but I didn't want to bother with "self" weak_ptrs right now. |
| 121 |
*/ |
| 122 |
class Namespaced |
| 123 |
{ |
| 124 |
public: |
| 125 |
/** |
| 126 |
* Destructor |
| 127 |
*/ |
| 128 |
virtual ~Namespaced() {} |
| 129 |
|
| 130 |
/** |
| 131 |
* Returns the name of this object. |
| 132 |
*/ |
| 133 |
virtual std::string getName() const = 0; |
| 134 |
|
| 135 |
// Gets/sets the namespace of this named object |
| 136 |
virtual void setNamespace(INamespace* space) = 0; |
| 137 |
virtual INamespace* getNamespace() const = 0; |
| 138 |
|
| 139 |
/** |
| 140 |
* Tells the Namespaced object to register all relevant |
| 141 |
* names with the associated Namespace. |
| 142 |
*/ |
| 143 |
virtual void attachNames() = 0; |
| 144 |
|
| 145 |
// Counter-part of the registerNames() method above. |
| 146 |
virtual void detachNames() = 0; |
| 147 |
|
| 148 |
/** |
| 149 |
* Tells the object to change its name. Invoked by the |
| 150 |
* INamespace class itself during import. |
| 151 |
*/ |
| 152 |
virtual void changeName(const std::string& newName) = 0; |
| 153 |
|
| 154 |
virtual void connectNameObservers() = 0; |
| 155 |
virtual void disconnectNameObservers() = 0; |
| 156 |
}; |
| 157 |
typedef std::shared_ptr<Namespaced> NamespacedPtr; |
| 158 |
|
| 159 |
inline NamespacedPtr Node_getNamespaced(scene::INodePtr node) |
| 160 |
{ |
| 161 |
return std::dynamic_pointer_cast<Namespaced>(node); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* greebo: Use the namespace factory to create new Namespace objects. |
| 166 |
*/ |
| 167 |
class INamespaceFactory : |
| 168 |
public RegisterableModule |
| 169 |
{ |
| 170 |
public: |
| 171 |
/** |
| 172 |
* Creates and returns a new Namespace. |
| 173 |
*/ |
| 174 |
virtual INamespacePtr createNamespace() = 0; |
| 175 |
}; |
| 176 |
|
| 177 |
const char* const MODULE_NAMESPACE_FACTORY("NamespaceFactory"); |
| 178 |
|
| 179 |
// Factory accessor |
| 180 |
inline INamespaceFactory& GlobalNamespaceFactory() |
| 181 |
{ |
| 182 |
static module::InstanceReference<INamespaceFactory> _reference(MODULE_NAMESPACE_FACTORY); |
| 183 |
return _reference; |
| 184 |
} |