context.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // context.h
  3. // reflection
  4. //
  5. // Created by Sam Jaffe on 8/6/22.
  6. // Copyright © 2022 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <initializer_list>
  10. #include <utility>
  11. #include "reflection/forward.h"
  12. #include "reflection/object.h"
  13. #include "reflection/scope.h"
  14. namespace reflection {
  15. class Context {
  16. public:
  17. struct Arg : std::pair<std::string_view, Object> {
  18. template <typename T> Arg(std::string_view id, T &&value)
  19. : pair(id, Object(std::forward<T>(value), std::string(id))) {}
  20. Arg(Object &&obj) : pair("", std::move(obj)) { first = second.name(); }
  21. };
  22. public:
  23. explicit Context(std::initializer_list<Arg> data) : data_(data.begin(), data.end()) {}
  24. Object const & get(std::string_view id) const { return data_.at(id); }
  25. bool has(std::string_view id) const { return data_.count(id); }
  26. Object get(ContextScope const &scope) const {
  27. return get(scope.context()).get(static_cast<Scope const &>(scope));
  28. }
  29. private:
  30. std::unordered_map<std::string_view, Object> data_;
  31. };
  32. }