| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- //
- // 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"
- 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 get(std::string_view id) const { return data_.at(id); }
- template <typename C,
- typename = std::enable_if_t<std::is_constructible_v<
- std::string_view, decltype(*std::begin(std::declval<C>()))>>>
- Object get(C const & container) const {
- return Object::get(get(*std::begin(container)), ++std::begin(container),
- std::end(container));
- }
- private:
- std::unordered_map<std::string_view, Object> data_;
- };
- }
|