SSP21-CPP
EnumField.h
1 #ifndef SSP21_ENUMFIELD_H
2 #define SSP21_ENUMFIELD_H
3 
4 #include "ssp21/crypto/gen/ParseError.h"
5 #include "ssp21/crypto/gen/FormatError.h"
6 #include "ssp21/crypto/IMessagePrinter.h"
7 
8 #include "ser4cpp/serialization/BigEndian.h"
9 
10 namespace ssp21
11 {
12 
13  template <typename Spec>
14  class EnumField final
15  {
16  typedef typename Spec::enum_type_t enum_t;
17 
18  public:
19 
20  size_t size() const
21  {
22  return 1;
23  }
24 
25  operator enum_t () const
26  {
27  return value;
28  }
29 
30  EnumField()
31  {}
32 
33  explicit EnumField(enum_t value) : value(value)
34  {}
35 
36  ParseError read(seq32_t& input)
37  {
38  uint8_t raw_value;
39  if (!ser4cpp::BigEndian::read(input, raw_value)) return ParseError::insufficient_bytes;
40 
41  auto enum_value = Spec::from_type(raw_value);
42 
43  if (enum_value == Spec::enum_type_t::undefined)
44  {
46  }
47 
48  this->value = enum_value;
49 
50  return ParseError::ok;
51  }
52 
53  FormatError write(wseq32_t& output) const
54  {
55  return ser4cpp::BigEndian::write(output, Spec::to_type(value)) ? FormatError::ok : FormatError::insufficient_space;
56  }
57 
58  void print(const char* name, IMessagePrinter& printer) const
59  {
60  printer.print(name, Spec::to_string(this->value));
61  }
62 
63  enum_t value = enum_t::undefined;
64 
65  };
66 
67 
68 }
69 
70 #endif
SSP21-cpp main namespace.
Definition: BufferTypes.h:12
an enumeration value was undefined
parser ran out of bytes before completion
ParseError
Definition: ParseError.h:27
not enough output buffer space
FormatError
Definition: FormatError.h:28