context.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. if (scope.begin() == scope.end()) { return get(scope.context()); }
  28. return get(scope.context()).get(static_cast<Scope const &>(scope));
  29. }
  30. private:
  31. std::unordered_map<std::string_view, Object> data_;
  32. };
  33. }