Commits

Don Williamson  committed 4e88967 Merge

Merge

  • Participants
  • Parent commits f45d05b, 98dd08c

Comments (0)

Files changed (2)

File inc/clutl/Objects.h

 
 
 	//
+	// Create an object of the given type by allocating and constructing it.
+	// This function has 3 possible modes of operation, based on which parameters you specify:
+	//
+	//    1. Create an anonymous object.
+	//    2. Create a named object.
+	//    3. Create a named object that is also tracked in an object group.
+	//
+	Object* CreateObject(const clcpp::Type* type, unsigned int unique_id = 0, ObjectGroup* object_group = 0);
+
+	void DestroyObject(const Object* object);
+
+
+	//
 	// Hash table based storage of collections of objects.
 	// The ObjectGroup is an object itself, allowing groups to be nested within other groups.
 	//
 		ObjectGroup();
 		~ObjectGroup();
 
-		// Create a nested group within this one
-		ObjectGroup* CreateObjectGroup(unsigned int unique_id);
-
-		// Create an anonymous object which doesn't get tracked by the database
-		Object* CreateObject(unsigned int type_hash);
-
-		// Create a named object that is internally tracked by name and can be found at a later point
-		Object* CreateObject(unsigned int type_hash, unsigned int unique_id);
-
-		// Destroy named/anonymous object or an object group
-		void DestroyObject(const Object* object);
-
 		// Find a created object by unique ID
 		Object* FindObject(unsigned int unique_id) const;
 
 		// and more adaptable.
 		void AllowFindInParent(bool allow) { m_AllowFindInParent = allow; }
 
-		const clcpp::Database* GetReflectionDB() const { return m_ReflectionDB; }
+		friend Object* clutl::CreateObject(const clcpp::Type*, unsigned int, ObjectGroup*);
+		friend void clutl::DestroyObject(const Object*);
 
 	private:
 		struct HashEntry;
 
+		void AddObject(Object* object);
+		void RemoveObject(const Object* object);
 		void AddHashEntry(Object* object);
-		void RemoveHashEntry(const Object* object);
+		void RemoveHashEntry(unsigned int hash);
 		void Resize(bool increase);
 
-		// Reflection database to use for type access
-		const clcpp::Database* m_ReflectionDB;
-
 		// An open-addressed hash table with linear probing - good cache behaviour for storing
 		// hashes of pointers that may suffer from clustering.
 		unsigned int m_MaxNbObjects;
 	class ObjectDatabase
 	{
 	public:
-		ObjectDatabase(const clcpp::Database* reflection_db);
+		ObjectDatabase();
 		~ObjectDatabase();
 
 		ObjectGroup* GetRootGroup() const { return m_RootGroup; }
 	{
 		if (object != 0)
 		{
-			clcpp::internal::Assert(object->object_group != 0);
-			object->object_group->DestroyObject(object);
+			DestroyObject(object);
 			object = 0;
 		}
 	}
 
 
 //
-// Helpers for creating typed objects in object groups.
+// Helpers for creating typed objects.
 // Exposed publically as Koenig lookup doesn't apply to template parameters.
 //
 template <typename TYPE>
-inline TYPE* New(clutl::ObjectGroup* group)
+inline TYPE* New()
 {
-	return static_cast<TYPE*>(group->CreateObject(clcpp::GetTypeNameHash<TYPE>()));
+	return static_cast<TYPE*>(clutl::CreateObject(clcpp::GetType<TYPE>()));
 }
 template <typename TYPE>
-inline TYPE* New(clutl::ObjectGroup* group, unsigned int unique_id)
+inline TYPE* New(clutl::ObjectGroup* group, unsigned int unique_id = 0)
 {
-	return static_cast<TYPE*>(group->CreateObject(clcpp::GetTypeNameHash<TYPE>(), unique_id));
+	return static_cast<TYPE*>(clutl::CreateObject(clcpp::GetType<TYPE>(), unique_id, group));
 }

File src/clReflectUtil/Objects.cpp

 
 
 
-clutl::ObjectGroup::ObjectGroup()
-	: m_ReflectionDB(0)
-	, m_MaxNbObjects(8)
-	, m_NbObjects(0)
-	, m_NbOccupiedEntries(0)
-	, m_NamedObjects(0)
-	, m_AllowFindInParent(true)
+
+clutl::Object* clutl::CreateObject(const clcpp::Type *type, unsigned int unique_id, ObjectGroup* object_group)
 {
-	// Allocate the hash table
-	m_NamedObjects = new HashEntry[m_MaxNbObjects];
-}
-
-
-clutl::ObjectGroup::~ObjectGroup()
-{
-	if (m_NamedObjects != 0)
-		delete [] m_NamedObjects;
-}
-
-
-clutl::ObjectGroup* clutl::ObjectGroup::CreateObjectGroup(unsigned int unique_id)
-{
-	return (ObjectGroup*)CreateObject(clcpp::GetTypeNameHash<ObjectGroup>(), unique_id);
-}
-
-
-clutl::Object* clutl::ObjectGroup::CreateObject(unsigned int type_hash)
-{
-	// Can the type be located?
-	const clcpp::Type* type = m_ReflectionDB->GetType(type_hash);
 	if (type == 0)
 		return 0;
 
 	// The object group has no registered constructor so construct manually
 	// if it comes through
 	Object* object = 0;
-	if (type_hash == clcpp::GetTypeNameHash<ObjectGroup>())
+	if (type->name.hash == clcpp::GetTypeNameHash<ObjectGroup>())
 	{
 		object = new ObjectGroup();
 	}
 		CallFunction(class_type->constructor, object);
 	}
 
-	// Construct the object and pass on any reflection DB pointer to derivers
-	// of the object group type
-	object->object_group = this;
+	// Construct the object and add to its object group
 	object->type = type;
-	if (class_type->flag_attributes & FLAG_ATTR_IS_OBJECT_GROUP)
-		((ObjectGroup*)object)->m_ReflectionDB = m_ReflectionDB;
+	object->unique_id = unique_id;
+	if (object_group)
+		object_group->AddObject(object);
 
 	return object;
 }
 
 
-clutl::Object* clutl::ObjectGroup::CreateObject(unsigned int type_hash, unsigned int unique_id)
+void clutl::DestroyObject(const Object* object)
 {
-	// Create the object
-	Object* object = CreateObject(type_hash);
-	if (object == 0)
-		return 0;
-
-	// Add to the hash table if there is a unique ID assigned
-	if (unique_id != 0)
-	{
-		object->unique_id = unique_id;
-		AddHashEntry(object);
-	}
-
-	return object;
-}
-
-
-void clutl::ObjectGroup::DestroyObject(const Object* object)
-{
-	// Remove from the hash table if it's named
-	if (object->unique_id != 0)
-		RemoveHashEntry(object);
-
 	// These represent fatal code errors
 	clcpp::internal::Assert(object != 0);
 	clcpp::internal::Assert(object->type != 0);
 
+	// Remove from any attached object group
+	if (object->object_group != 0)
+		object->object_group->RemoveObject(object);
+
 	if (object->type->name.hash == clcpp::GetTypeNameHash<ObjectGroup>())
 	{
 		// ObjecGroup class does not have a registered destructor
 }
 
 
+
+
+clutl::ObjectGroup::ObjectGroup()
+	: m_MaxNbObjects(8)
+	, m_NbObjects(0)
+	, m_NbOccupiedEntries(0)
+	, m_NamedObjects(0)
+	, m_AllowFindInParent(true)
+{
+	// Allocate the hash table
+	m_NamedObjects = new HashEntry[m_MaxNbObjects];
+}
+
+
+clutl::ObjectGroup::~ObjectGroup()
+{
+	if (m_NamedObjects != 0)
+		delete [] m_NamedObjects;
+}
+
+
+void clutl::ObjectGroup::AddObject(Object* object)
+{
+	object->object_group = this;
+	if (object->unique_id != 0)
+		AddHashEntry(object);
+}
+
+
+void clutl::ObjectGroup::RemoveObject(const Object* object)
+{
+	// Remove from the hash table if it's named
+	if (object->unique_id != 0)
+		RemoveHashEntry(object->unique_id);
+}
+
+
 clutl::Object* clutl::ObjectGroup::FindObject(unsigned int unique_id) const
 {
 	// Search up through the object group hierarchy
 }
 
 
-void clutl::ObjectGroup::RemoveHashEntry(const Object* object)
+void clutl::ObjectGroup::RemoveHashEntry(unsigned int hash)
 {
 	// Linear probe from the natural hash location for matching hash
-	unsigned int hash = object->unique_id;
 	const unsigned int index_mask = m_MaxNbObjects - 1;
 	unsigned int index = hash & index_mask;
 	while (m_NamedObjects[index].hash && m_NamedObjects[index].hash != hash)
 }
 
 
-clutl::ObjectDatabase::ObjectDatabase(const clcpp::Database* reflection_db)
+clutl::ObjectDatabase::ObjectDatabase()
 	: m_RootGroup(0)
 {
 	m_RootGroup = new ObjectGroup();
-	m_RootGroup->m_ReflectionDB = reflection_db;
 }