Last active
February 19, 2017 21:35
-
-
Save alepez/43072c214705f0a7f674204923616600 to your computer and use it in GitHub Desktop.
This file contains 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 "Services.hpp" | |
#include "../common/log.hpp" | |
#include <typeinfo> | |
#include "../modules/pluggit/Pluggit.hpp" | |
#include "../modules/openmotics/OpenMotics.hpp" | |
namespace myhome { | |
Services::Services() { | |
/* | |
* Keep running other services even after a service has an exception | |
*/ | |
#define TRY(expr) \ | |
try { \ | |
expr; \ | |
} catch (const std::exception& err) { \ | |
log::err("Error: {}", err.what()); \ | |
} catch (...) { \ | |
log::err("Error: unknown"); \ | |
} | |
/* | |
* Just a shortcut | |
*/ | |
#define INIT_SERVICE(T) TRY(services_[typeid(T)] = makeConstPtr<T>()); | |
INIT_SERVICE(Pluggit); | |
INIT_SERVICE(OpenMotics); | |
#undef INIT_SERVICE | |
#undef TRY | |
} | |
} /* myhome */ |
This file contains 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
#ifndef SERVICES_HPP_QDPIHW6F | |
#define SERVICES_HPP_QDPIHW6F | |
#include "../myhome_fwd.hpp" | |
#include <map> | |
#include <typeindex> | |
namespace myhome { | |
class Services { | |
public: | |
Services(); | |
template <typename T> ConstPtr<T> get() const { | |
auto index = std::type_index{typeid(T)}; | |
if (!services_.count(index)) { | |
// TODO: Log something... service unavailable? | |
return ConstPtr<T>(); | |
} | |
return std::static_pointer_cast<const T>(services_.at(index)); | |
} | |
private: | |
std::map<std::type_index, ConstPtr<void>> services_; | |
}; | |
} /* myhome */ | |
#endif /* end of include guard: SERVICES_HPP_QDPIHW6F */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment