Created
April 23, 2025 20:27
-
-
Save EncodeTheCode/39a2854ac997336ce3188dc2fff06d65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <unordered_map> | |
#include <string> | |
#include <memory> | |
// Base class for tracked objects | |
class TrackedObject { | |
public: | |
TrackedObject(const std::string& customName); | |
virtual ~TrackedObject(); | |
void setName(const std::string& newName); | |
std::string getName() const; | |
private: | |
std::string name; | |
}; | |
// Singleton class for managing TrackedObjects | |
class ObjectRegistry { | |
public: | |
static ObjectRegistry& getInstance(); | |
void registerObject(TrackedObject* obj, const std::string& name); | |
void unregisterObject(TrackedObject* obj); | |
void renameObject(TrackedObject* obj, const std::string& newName); | |
TrackedObject* getByName(const std::string& name); | |
void printAll(); | |
private: | |
ObjectRegistry() = default; | |
std::unordered_map<std::string, TrackedObject*> nameMap; | |
std::unordered_map<TrackedObject*, std::string> pointerMap; | |
}; | |
// ---------- TrackedObject Implementation ---------- | |
TrackedObject::TrackedObject(const std::string& customName) : name(customName) { | |
ObjectRegistry::getInstance().registerObject(this, name); | |
} | |
TrackedObject::~TrackedObject() { | |
ObjectRegistry::getInstance().unregisterObject(this); | |
} | |
void TrackedObject::setName(const std::string& newName) { | |
ObjectRegistry::getInstance().renameObject(this, newName); | |
name = newName; | |
} | |
std::string TrackedObject::getName() const { | |
return name; | |
} | |
// ---------- ObjectRegistry Implementation ---------- | |
ObjectRegistry& ObjectRegistry::getInstance() { | |
static ObjectRegistry instance; | |
return instance; | |
} | |
void ObjectRegistry::registerObject(TrackedObject* obj, const std::string& name) { | |
nameMap[name] = obj; | |
pointerMap[obj] = name; | |
} | |
void ObjectRegistry::unregisterObject(TrackedObject* obj) { | |
auto it = pointerMap.find(obj); | |
if (it != pointerMap.end()) { | |
nameMap.erase(it->second); | |
pointerMap.erase(it); | |
} | |
} | |
void ObjectRegistry::renameObject(TrackedObject* obj, const std::string& newName) { | |
auto it = pointerMap.find(obj); | |
if (it != pointerMap.end()) { | |
nameMap.erase(it->second); | |
nameMap[newName] = obj; | |
it->second = newName; | |
} | |
} | |
TrackedObject* ObjectRegistry::getByName(const std::string& name) { | |
auto it = nameMap.find(name); | |
return it != nameMap.end() ? it->second : nullptr; | |
} | |
void ObjectRegistry::printAll() { | |
std::cout << "Tracked objects:\n"; | |
for (const auto& pair : nameMap) { | |
std::cout << " Name: " << pair.first << ", Address: " << pair.second << "\n"; | |
} | |
} | |
// ---------- Example Usage ---------- | |
class MyObject : public TrackedObject { | |
public: | |
MyObject(const std::string& name) : TrackedObject(name) {} | |
}; | |
int main() { | |
MyObject* obj1 = new MyObject("Alpha"); | |
MyObject* obj2 = new MyObject("Beta"); | |
ObjectRegistry::getInstance().printAll(); | |
obj1->setName("AlphaRenamed"); | |
ObjectRegistry::getInstance().printAll(); | |
delete obj1; | |
delete obj2; | |
ObjectRegistry::getInstance().printAll(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment