| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- //
- // context.h
- // reflection
- //
- // Created by Sam Jaffe on 8/6/22.
- // Copyright © 2022 Sam Jaffe. All rights reserved.
- //
- #pragma once
- #include <initializer_list>
- #include <utility>
- #include "reflection/forward.h"
- #include "reflection/object.h"
- #include "reflection/scope.h"
- namespace reflection {
- class Context {
- public:
- struct Arg : std::pair<std::string_view, Object> {
- template <typename T> Arg(std::string_view id, T &&value)
- : pair(id, Object(std::forward<T>(value), std::string(id))) {}
- Arg(Object &&obj) : pair("", std::move(obj)) { first = second.name(); }
- };
-
- public:
- explicit Context(std::initializer_list<Arg> data) : data_(data.begin(), data.end()) {}
- Object const & get(std::string_view id) const { return data_.at(id); }
- bool has(std::string_view id) const { return data_.count(id); }
- Object get(ContextScope const &scope) const {
- if (scope.begin() == scope.end()) { return get(scope.context()); }
- return get(scope.context()).get(static_cast<Scope const &>(scope));
- }
- private:
- std::unordered_map<std::string_view, Object> data_;
- };
- }
|