File: variant.h

package info (click to toggle)
libnop 0.0~git20200728.45dfe0f-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,452 kB
  • sloc: cpp: 13,946; ansic: 3,537; makefile: 100; python: 73
file content (134 lines) | stat: -rw-r--r-- 4,072 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright 2017 The Native Object Protocols Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef LIBNOP_INCLUDE_NOP_BASE_VARIANT_H_
#define LIBNOP_INCLUDE_NOP_BASE_VARIANT_H_

#include <nop/base/encoding.h>
#include <nop/types/variant.h>

namespace nop {

//
// Variant<Ts...> encoding format:
//
// +-----+---------+-----------+
// | VAR | INT32:I | ELEMENT I |
// +-----+---------+-----------+
//
// Elements are expected to be valid encodings for their element type.
//
// EmptyVariant encoding format:
//
// +-----+
// | NIL |
// +-----+
//
// Therefore a Variant in the empty state has this specific encoding:
//
// +-----+----+-----+
// | VAR | -1 | NIL |
// +-----+----+-----+
//

template <>
struct Encoding<EmptyVariant> : EncodingIO<EmptyVariant> {
  static constexpr EncodingByte Prefix(EmptyVariant /*value*/) {
    return EncodingByte::Nil;
  }

  static constexpr std::size_t Size(EmptyVariant value) {
    return BaseEncodingSize(Prefix(value));
  }

  static constexpr bool Match(EncodingByte prefix) {
    return prefix == EncodingByte::Nil;
  }

  template <typename Writer>
  static constexpr Status<void> WritePayload(EncodingByte /*prefix*/,
                                             EmptyVariant /*value*/,
                                             Writer* /*writer*/) {
    return {};
  }

  template <typename Reader>
  static constexpr Status<void> ReadPayload(EncodingByte /*prefix*/,
                                            EmptyVariant* /*value*/,
                                            Reader* /*reader*/) {
    return {};
  }
};

template <typename... Ts>
struct Encoding<Variant<Ts...>> : EncodingIO<Variant<Ts...>> {
  using Type = Variant<Ts...>;

  static constexpr EncodingByte Prefix(const Type& /*value*/) {
    return EncodingByte::Variant;
  }

  static constexpr std::size_t Size(const Type& value) {
    return BaseEncodingSize(Prefix(value)) +
           Encoding<std::int32_t>::Size(value.index()) +
           value.Visit([](const auto& element) {
             using Element = typename std::decay<decltype(element)>::type;
             return Encoding<Element>::Size(element);
           });
  }

  static constexpr bool Match(EncodingByte prefix) {
    return prefix == EncodingByte::Variant;
  }

  template <typename Writer>
  static constexpr Status<void> WritePayload(EncodingByte /*prefix*/,
                                             const Type& value,
                                             Writer* writer) {
    auto status = Encoding<std::int32_t>::Write(value.index(), writer);
    if (!status)
      return status;

    return value.Visit([writer](const auto& element) {
      using Element = typename std::decay<decltype(element)>::type;
      return Encoding<Element>::Write(element, writer);
    });
  }

  template <typename Reader>
  static constexpr Status<void> ReadPayload(EncodingByte /*prefix*/,
                                            Type* value, Reader* reader) {
    std::int32_t type = 0;
    auto status = Encoding<std::int32_t>::Read(&type, reader);
    if (!status) {
      return status;
    } else if (type < Type::kEmptyIndex ||
               type >= static_cast<std::int32_t>(sizeof...(Ts))) {
      return ErrorStatus::UnexpectedVariantType;
    }

    value->Become(type);

    return value->Visit([reader](auto&& element) {
      using Element = typename std::decay<decltype(element)>::type;
      return Encoding<Element>::Read(&element, reader);
    });
  }
};

}  // namespace nop

#endif  // LIBNOP_INCLUDE_NOP_BASE_VARIANT_H_