Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
xorz57 committed Jul 9, 2024
1 parent 63b428e commit 498921e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ public:
};

int main() {
ServiceLocator serviceLocator;
service_locator_t sl;

serviceLocator.SetInstance<A>();
serviceLocator.SetInstance<B>();
sl.set_instance<A>();
sl.set_instance<B>();

std::cout << serviceLocator.GetInstance<A>() << std::endl;
std::cout << serviceLocator.GetInstance<B>() << std::endl;
std::cout << sl.get_instance<A>() << std::endl;
std::cout << sl.get_instance<B>() << std::endl;

std::cout << serviceLocator.GetInstance<A>() << std::endl;
std::cout << serviceLocator.GetInstance<B>() << std::endl;
std::cout << sl.get_instance<A>() << std::endl;
std::cout << sl.get_instance<B>() << std::endl;

serviceLocator.Clear();
sl.clear();

std::cout << serviceLocator.GetInstance<A>() << std::endl;
std::cout << serviceLocator.GetInstance<B>() << std::endl;
std::cout << sl.get_instance<A>() << std::endl;
std::cout << sl.get_instance<B>() << std::endl;

return 0;
}
Expand Down
20 changes: 10 additions & 10 deletions examples/Example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ class B {
};

int main() {
ServiceLocator serviceLocator;
service_locator_t sl;

serviceLocator.SetInstance<A>();
serviceLocator.SetInstance<B>();
sl.set_instance<A>();
sl.set_instance<B>();

std::cout << serviceLocator.GetInstance<A>() << std::endl;
std::cout << serviceLocator.GetInstance<B>() << std::endl;
std::cout << sl.get_instance<A>() << std::endl;
std::cout << sl.get_instance<B>() << std::endl;

std::cout << serviceLocator.GetInstance<A>() << std::endl;
std::cout << serviceLocator.GetInstance<B>() << std::endl;
std::cout << sl.get_instance<A>() << std::endl;
std::cout << sl.get_instance<B>() << std::endl;

serviceLocator.Clear();
sl.clear();

std::cout << serviceLocator.GetInstance<A>() << std::endl;
std::cout << serviceLocator.GetInstance<B>() << std::endl;
std::cout << sl.get_instance<A>() << std::endl;
std::cout << sl.get_instance<B>() << std::endl;

return 0;
}
24 changes: 12 additions & 12 deletions include/ServiceLocator/ServiceLocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,32 @@
#include <unordered_map>
#include <memory>

class ServiceLocator {
class service_locator_t {
public:
ServiceLocator() = default;
~ServiceLocator() = default;
service_locator_t() = default;
~service_locator_t() = default;

template<typename T, typename... Args>
void SetInstance(Args... args) {
void set_instance(Args... args) {
const size_t hash = typeid(T).hash_code();
std::shared_ptr<T> instance = std::make_shared<T>(args...);
mInstances.emplace(hash, std::shared_ptr<void>(instance));
m_instances.emplace(hash, std::shared_ptr<void>(instance));
}

template<typename T>
std::shared_ptr<T> GetInstance() {
std::shared_ptr<T> get_instance() {
const size_t hash = typeid(T).hash_code();
auto it = mInstances.find(hash);
if (it != mInstances.end()) {
return std::static_pointer_cast<T>(mInstances.at(hash));
auto it = m_instances.find(hash);
if (it != m_instances.end()) {
return std::static_pointer_cast<T>(m_instances.at(hash));
}
return nullptr;
}

void Clear() {
mInstances.clear();
void clear() {
m_instances.clear();
}

private:
std::unordered_map<size_t, std::shared_ptr<void>> mInstances;
std::unordered_map<size_t, std::shared_ptr<void>> m_instances;
};

0 comments on commit 498921e

Please sign in to comment.