File: Skip.hpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (146 lines) | stat: -rw-r--r-- 4,375 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef RFL_INTERNAL_SKIP_HPP_
#define RFL_INTERNAL_SKIP_HPP_

#include <optional>

namespace rfl::internal {

template <class T, bool _skip_serialization, bool _skip_deserialization>
class Skip {
 private:
  using SelfType = Skip<T, _skip_serialization, _skip_deserialization>;

 public:
  static constexpr bool skip_serialization_ = _skip_serialization;
  static constexpr bool skip_deserialization_ = _skip_deserialization;

  /// The underlying type.
  using Type = T;
  using ReflectionType = std::optional<T>;

  Skip() : value_(Type()) {}

  Skip(const Type& _value) : value_(_value) {}

  Skip(ReflectionType&& _value) noexcept
      : value_(_value ? std::move(*_value) : Type()) {}

  Skip(const ReflectionType& _value) : value_(_value ? *_value : Type()) {}

  Skip(Type&& _value) noexcept : value_(std::move(_value)) {}

  Skip(SelfType&& _skip) noexcept = default;

  Skip(const SelfType& _skip) = default;

  template <class U, bool _skip_s, bool _skip_d>
  Skip(const Skip<U, _skip_s, _skip_d>& _other) : value_(_other.get()) {}

  template <class U, bool _skip_s, bool _skip_d>
  Skip(Skip<U, _skip_s, _skip_d>&& _other) : value_(_other.get()) {}

  template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
                                             bool>::type = true>
  Skip(const U& _value) : value_(_value) {}

  template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
                                             bool>::type = true>
  Skip(U&& _value) noexcept : value_(std::forward<U>(_value)) {}

  template <class U, bool _skip_s, bool _skip_d,
            typename std::enable_if<std::is_convertible_v<U, Type>,
                                    bool>::type = true>
  Skip(const Skip<U, _skip_s, _skip_d>& _skip) : value_(_skip.value()) {}

  /// Assigns the underlying object to its default value.
  template <class U = Type,
            typename std::enable_if<std::is_default_constructible_v<U>,
                                    bool>::type = true>
  Skip(const Default&) : value_(Type()) {}

  ~Skip() = default;

  /// Returns the underlying object.
  Type& get() { return value_; }

  /// Returns the underlying object.
  const Type& get() const { return value_; }

  /// Returns the underlying object.
  Type& operator()() { return value_; }

  /// Returns the underlying object.
  const Type& operator()() const { return value_; }

  /// Assigns the underlying object.
  auto& operator=(const Type& _value) {
    value_ = _value;
    return *this;
  }

  /// Assigns the underlying object.
  auto& operator=(Type&& _value) noexcept {
    value_ = std::move(_value);
    return *this;
  }

  /// Assigns the underlying object.
  template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
                                             bool>::type = true>
  auto& operator=(const U& _value) {
    value_ = _value;
    return *this;
  }

  /// Assigns the underlying object to its default value.
  template <class U = Type,
            typename std::enable_if<std::is_default_constructible_v<U>,
                                    bool>::type = true>
  auto& operator=(const Default&) {
    value_ = Type();
    return *this;
  }

  /// Assigns the underlying object.
  SelfType& operator=(const SelfType& _other) = default;

  /// Assigns the underlying object.
  SelfType& operator=(SelfType&& _other) = default;

  /// Assigns the underlying object.
  template <class U, bool _skip_s, bool _skip_d>
  auto& operator=(const Skip<U, _skip_s, _skip_d>& _skip) {
    value_ = _skip.get();
    return *this;
  }

  /// Assigns the underlying object.
  template <class U, bool _skip_s, bool _skip_d>
  auto& operator=(Skip<U, _skip_s, _skip_d>&& _skip) {
    value_ = std::forward<T>(_skip.value_);
    return *this;
  }

  /// Returns the ReflectionType - necessary for the serialization to work.
  ReflectionType reflection() const { return value_; }

  /// Assigns the underlying object.
  void set(const Type& _value) { value_ = _value; }

  /// Assigns the underlying object.
  void set(Type&& _value) { value_ = std::move(_value); }

  /// Returns the underlying object.
  Type& value() { return value_; }

  /// Returns the underlying object.
  const Type& value() const { return value_; }

 private:
  /// The underlying value
  T value_;
};

}  // namespace rfl::internal

#endif