File: Getter.hpp

package info (click to toggle)
reflect-cpp 0.21.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,128 kB
  • sloc: cpp: 50,336; python: 139; makefile: 30; sh: 3
file content (87 lines) | stat: -rw-r--r-- 2,767 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef RFL_INTERNAL_GETTER_HPP_
#define RFL_INTERNAL_GETTER_HPP_

#include <tuple>
#include <variant>

#include "../Tuple.hpp"
#include "StringLiteral.hpp"
#include "find_index.hpp"

namespace rfl::internal {

// ----------------------------------------------------------------------------

template <class NamedTupleType>
struct Getter;

// ----------------------------------------------------------------------------

/// Default case - anything that cannot be explicitly matched.
template <class NamedTupleType>
struct Getter {
 public:
  /// Retrieves the indicated value from the tuple.
  template <int _index>
  static inline auto& get(NamedTupleType& _tup) {
    return rfl::get<_index>(_tup.values());
  }

  /// Gets a field by name.
  template <StringLiteral _field_name>
  static inline auto& get(NamedTupleType& _tup) {
    constexpr auto index =
        find_index<_field_name, typename NamedTupleType::Fields>();
    return Getter<NamedTupleType>::template get<index>(_tup);
  }

  /// Gets a field by the field type.
  template <class Field>
  static inline auto& get(NamedTupleType& _tup) {
    constexpr auto index =
        find_index<Field::name_, typename NamedTupleType::Fields>();
    static_assert(
        std::is_same<typename tuple_element_t<
                         index, typename NamedTupleType::Fields>::Type,
                     typename Field::Type>(),
        "If two fields have the same name, "
        "their type must be the same as "
        "well.");
    return Getter<NamedTupleType>::template get<index>(_tup);
  }

  /// Retrieves the indicated value from the tuple.
  template <int _index>
  static inline const auto& get_const(const NamedTupleType& _tup) {
    return rfl::get<_index>(_tup.values());
  }

  /// Gets a field by name.
  template <StringLiteral _field_name>
  static inline const auto& get_const(const NamedTupleType& _tup) {
    constexpr auto index =
        find_index<_field_name, typename NamedTupleType::Fields>();
    return Getter<NamedTupleType>::template get_const<index>(_tup);
  }

  /// Gets a field by the field type.
  template <class Field>
  static inline const auto& get_const(const NamedTupleType& _tup) {
    constexpr auto index =
        find_index<Field::name_, typename NamedTupleType::Fields>();
    static_assert(
        std::is_same<typename tuple_element_t<
                         index, typename NamedTupleType::Fields>::Type,
                     typename Field::Type>(),
        "If two fields have the same name, "
        "their type must be the same as "
        "well.");
    return Getter<NamedTupleType>::template get_const<index>(_tup);
  }
};

// ----------------------------------------------------------------------------

}  // namespace rfl::internal

#endif