herald  2.0.0
extended_data.h
1 // Copyright 2020-2021 Herald Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #ifndef HERALD_EXTENDED_DATA_H
6 #define HERALD_EXTENDED_DATA_H
7 
8 #include "../../datatype/data.h"
9 #include "../../datatype/payload_data.h"
10 #include "../../datatype/payload_timestamp.h"
11 
12 #include <optional>
13 #include <cstdint>
14 
15 namespace herald {
16 namespace payload {
17 namespace extended {
18 
19 using namespace herald::datatype;
20 
21 using ExtendedDataSegmentCode = uint8_t;
22 
23 // Abstract class only
24 class ExtendedData {
25 public:
26  ExtendedData() = default;
27  virtual ~ExtendedData() = default;
28 
29  virtual bool hasData() const = 0;
30  virtual void addSection(ExtendedDataSegmentCode code, uint8_t value) = 0;
31  virtual void addSection(ExtendedDataSegmentCode code, uint16_t value) = 0;
32  virtual void addSection(ExtendedDataSegmentCode code, float value) = 0;
33  virtual void addSection(ExtendedDataSegmentCode code, const std::string value) = 0;
34  virtual void addSection(ExtendedDataSegmentCode code, const Data& value) = 0;
35 
36  virtual PayloadData payload() = 0;
37 };
38 
39 // V1 Concrete types
40 // enum class ExtendedDataSegmentCodesV1 : ExtendedDataSegmentCode {
41 // TextPremises = 0x10,
42 // TextLocation = 0x11,
43 // TextArea = 0x12,
44 // LocationUrl = 0x13
45 // };
46 
47 // operator ExtendedDataSegmentCode(const ExtendedDataSegmentCodesV1& from) {
48 // return static_cast<uint8_t>(from);
49 // };
51  uint8_t value;
52 
53  constexpr ExtendedDataSegmentCodesV1(uint8_t v = 0) : value(v) {}
54  constexpr operator uint8_t() const { return value; }
55 
56  static const ExtendedDataSegmentCodesV1 TextPremises, TextLocation, TextArea, LocationUrl;
57 };
58 
59 constexpr const ExtendedDataSegmentCodesV1
60 ExtendedDataSegmentCodesV1::TextPremises(0x10),
61 ExtendedDataSegmentCodesV1::TextLocation(0x11),
62 ExtendedDataSegmentCodesV1::TextArea(0x12),
63 ExtendedDataSegmentCodesV1::LocationUrl(0x13)
64 ;
65 
67  uint8_t code;
68  uint8_t length;
69  Data data;
70 
71  ConcreteExtendedDataSectionV1() : code(0), length(0), data() {}
72 
73  ConcreteExtendedDataSectionV1(uint8_t code,uint8_t length,const Data data)
74  : code(code), length(length), data(data)
75  {
76  ;
77  }
78 
79  ConcreteExtendedDataSectionV1(uint8_t code,const std::string& from)
80  : code(code), length(from.size()), data(from)
81  {
82  ;
83  }
84 
86  : code(other.code), length(other.length), data(std::move(other.data))
87  {
88  ;
89  }
90 
92  : code(other.code), length(other.length), data(other.data)
93  {
94  ;
95  }
96 
97  ConcreteExtendedDataSectionV1& operator=(const ConcreteExtendedDataSectionV1& other) = default;
98 };
99 
101 public:
103  ConcreteExtendedDataV1(const ConcreteExtendedDataV1& other); // copy ctor
104  ConcreteExtendedDataV1(ConcreteExtendedDataV1&& other); // move ctor
105  // ConcreteExtendedDataV1& operator=(const ConcreteExtendedDataV1& other); // copy assign
106  // ConcreteExtendedDataV1& operator=(ConcreteExtendedDataV1&& other); // move assign
108 
109  // Overrides
110  bool hasData() const override;
111  void addSection(ExtendedDataSegmentCode code, uint8_t value) override;
112  void addSection(ExtendedDataSegmentCode code, uint16_t value) override;
113  void addSection(ExtendedDataSegmentCode code, float value) override;
114  void addSection(ExtendedDataSegmentCode code, const std::string value) override;
115  void addSection(ExtendedDataSegmentCode code, const Data& value) override;
116  PayloadData payload() override;
117 
118  // V1 only methods
119  const std::size_t getSectionCount() const;
120  const ConcreteExtendedDataSectionV1& getSection(std::size_t index) const;
121 
122 private:
123  // bool mHasData;
124  std::array<ConcreteExtendedDataSectionV1,8> sections;
125  std::size_t inUse;
126 };
127 
128 }
129 }
130 }
131 
132 #endif
The main data workhorse class of the Herald API.
Definition: data.h:33
Definition: payload_data.h:13
Definition: extended_data.h:24
Contains all low-level Herald datatype implementations.
Definition: base64_string.h:14
Acts as a non-global memory arena for arbitrary classes.
Definition: aggregates.h:15