Skip to content

Instantly share code, notes, and snippets.

@alepez
Last active February 19, 2017 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alepez/43072c214705f0a7f674204923616600 to your computer and use it in GitHub Desktop.
Save alepez/43072c214705f0a7f674204923616600 to your computer and use it in GitHub Desktop.
#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 */
#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