RIT VEXU Core API
Loading...
Searching...
No Matches
types.hpp
1#pragma once
2#include "core/device/vdb/protocol.hpp"
3#include <string>
4namespace VDP {
9class Record : public Part {
10 friend PacketReader;
11 friend PacketWriter;
12
13 public:
14 using SizeT = uint32_t;
20 explicit Record(std::string name);
27 Record(std::string name, const std::vector<Part *> &parts);
34 Record(std::string name, std::vector<PartPtr> parts);
41 Record(std::string name, PacketReader &reader);
46 void set_fields(std::vector<PartPtr> fields);
47
48 std::vector<PartPtr> get_fields() const;
49
53 void fetch() override;
54
55 void response() override;
56
61 void read_data_from_message(PacketReader &reader) override;
62
63 PartPtr clone() override;
64
65 void Visit(Visitor *);
66
67 protected:
68 // Encode the schema itself for transmission on the wire
69 void write_schema(PacketWriter &sofar) const override;
70 // Encode the data currently held according to schema for transmission on the
71 // wire
72 void write_message(PacketWriter &sofar) const override;
73
74 private:
75 void pprint(std::stringstream &ss, size_t indent) const override;
76 void pprint_data(std::stringstream &ss, size_t indent) const override;
77
78 std::vector<PartPtr> fields;
79};
80
83class String : public Part {
84 friend PacketReader;
85 friend PacketWriter;
86
87 public:
88 using FetchFunc = std::function<std::string()>;
94 explicit String(std::string name, FetchFunc fetcher = []() { return "no value"; });
98 void fetch() override;
99
103 void response() override;
104
109 void set_value(std::string new_value);
110
114 std::string get_value();
115
116 PartPtr clone() override;
121 void read_data_from_message(PacketReader &reader) override;
128 void pprint(std::stringstream &ss, size_t indent) const override;
135 void pprint_data(std::stringstream &ss, size_t indent) const override;
136
137 void Visit(Visitor *);
138
139 protected:
140 void write_schema(PacketWriter &sofar) const override;
141 void write_message(PacketWriter &sofar) const override;
142
143 private:
144 FetchFunc fetcher;
145 std::string value;
146};
147
148// Template to reduce boiler plate for Schema wrappers for simple types
149// Fixed size, numeric types such as uin8_t, uint32, float, double
153template <typename NumT, Type schemaType> class Number : public Part {
154 friend PacketReader;
155 friend PacketWriter;
156
157 public:
158 using NumberType = NumT;
159 static constexpr Type SchemaType = schemaType;
160
161 // Checks to make sure this isn't misused
162 static_assert(
163 std::is_floating_point<NumberType>::value || std::is_integral<NumberType>::value,
164 "Number type this is instantiated with must be floating point "
165 "or integral"
166 );
170 using FetchFunc = std::function<NumberType()>;
175 explicit Number(
176 std::string field_name, FetchFunc fetcher = []() { return (NumberType)0; }
177 )
178 : Part(field_name), fetcher(fetcher) {}
179
182 void fetch() override { value = fetcher(); }
187 void set_value(NumberType val) { this->value = val; }
191 NumberType get_value() { return value; }
197 void pprint(std::stringstream &ss, size_t indent) const override {
198 add_indents(ss, indent);
199 ss << name << ":\t" << to_string(SchemaType);
200 }
201
206 void pprint_data(std::stringstream &ss, size_t indent) const override {
207 add_indents(ss, indent);
208 ss << name << ":\t";
209 if (sizeof(NumberType) == 1) {
210 ss << (int)value; // Otherwise, stringstream interprets uint8 as char and
211 // prints a char
212 } else {
213 ss << value;
214 }
215 }
216
220 void read_data_from_message(PacketReader &reader) override { value = reader.get_number<NumberType>(); }
221
222 protected:
227 void write_schema(PacketWriter &sofar) const override {
228 sofar.write_type(SchemaType); // Type
229 sofar.write_string(name); // Name
230 }
231
235 void write_message(PacketWriter &sofar) const override { sofar.write_number<NumberType>(value); }
236 FetchFunc fetcher;
237 NumberType value = (NumberType)0;
238};
239
240class Float : public Number<float, Type::Float> {
241public:
242 using NumT = Number<float, Type::Float>;
243 Float(
244 std::string name,
245 NumT::FetchFunc func = []() { return (NumT::NumberType)0; });
246 void Visit(Visitor *);
247 PartPtr clone() override;
248};
249class Double : public Number<double, Type::Double> {
250public:
251 using NumT = Number<double, Type::Double>;
252 Double(
253 std::string name,
254 NumT::FetchFunc func = []() { return (NumT::NumberType)0; });
255 void Visit(Visitor *);
256 PartPtr clone() override;
257};
258
259class Uint8 : public Number<uint8_t, Type::Uint8> {
260public:
261 using NumT = Number<uint8_t, Type::Uint8>;
262 Uint8(
263 std::string name,
264 NumT::FetchFunc func = []() { return (NumT::NumberType)0; });
265 void Visit(Visitor *);
266 PartPtr clone() override;
267};
268class Uint16 : public Number<uint16_t, Type::Uint16> {
269public:
271 Uint16(
272 std::string name,
273 NumT::FetchFunc func = []() { return (NumT::NumberType)0; });
274 void Visit(Visitor *);
275 PartPtr clone() override;
276};
277class Uint32 : public Number<uint32_t, Type::Uint32> {
278public:
280 Uint32(
281 std::string name,
282 NumT::FetchFunc func = []() { return (NumT::NumberType)0; });
283 void Visit(Visitor *);
284 PartPtr clone() override;
285};
286class Uint64 : public Number<uint64_t, Type::Uint64> {
287public:
289 Uint64(
290 std::string name,
291 NumT::FetchFunc func = []() { return (NumT::NumberType)0; });
292 void Visit(Visitor *);
293 PartPtr clone() override;
294};
295
296class Int8 : public Number<int8_t, Type::Int8> {
297public:
298 using NumT = Number<int8_t, Type::Int8>;
299 Int8(
300 std::string name,
301 NumT::FetchFunc func = []() { return (NumT::NumberType)0; });
302 void Visit(Visitor *);
303 PartPtr clone() override;
304};
305class Int16 : public Number<int16_t, Type::Int16> {
306public:
307 using NumT = Number<int16_t, Type::Int16>;
308 Int16(
309 std::string name,
310 NumT::FetchFunc func = []() { return (NumT::NumberType)0; });
311 void Visit(Visitor *);
312 PartPtr clone() override;
313};
314class Int32 : public Number<int32_t, Type::Int32> {
315public:
316 using NumT = Number<int32_t, Type::Int32>;
317 Int32(
318 std::string name,
319 NumT::FetchFunc func = []() { return (NumT::NumberType)0; });
320 void Visit(Visitor *);
321 PartPtr clone() override;
322};
323
324class Int64 : public Number<int64_t, Type::Int64> {
325public:
326 using NumT = Number<int64_t, Type::Int64>;
327 Int64(
328 std::string name,
329 NumT::FetchFunc func = []() { return (NumT::NumberType)0; });
330 void Visit(Visitor *);
331 PartPtr clone() override;
332};
336class Visitor {
337public:
338 virtual ~Visitor() {}
339
340 virtual void VisitRecord(Record *) = 0;
341
342 virtual void VisitString(String *) = 0;
343
344 virtual void VisitFloat(Float *) = 0;
345 virtual void VisitDouble(Double *) = 0;
346
347 virtual void VisitUint8(Uint8 *) = 0;
348 virtual void VisitUint16(Uint16 *) = 0;
349 virtual void VisitUint32(Uint32 *) = 0;
350 virtual void VisitUint64(Uint64 *) = 0;
351
352 virtual void VisitInt8(Int8 *) = 0;
353 virtual void VisitInt16(Int16 *) = 0;
354 virtual void VisitInt32(Int32 *) = 0;
355 virtual void VisitInt64(Int64 *) = 0;
356};
357
361public:
362 virtual void VisitAnyFloat(const std::string &name, double value,
363 const Part *) = 0;
364 virtual void VisitAnyInt(const std::string &name, int64_t value,
365 const Part *) = 0;
366 virtual void VisitAnyUint(const std::string &name, uint64_t value,
367 const Part *) = 0;
368
369 // Implemented to call Visitor::VisitAnyFloat
370 void VisitFloat(Float *) override;
371 void VisitDouble(Double *) override;
372
373 // Implemented to call Visitor::VisitAnyUint
374 void VisitUint8(Uint8 *) override;
375 void VisitUint16(Uint16 *) override;
376 void VisitUint32(Uint32 *) override;
377 void VisitUint64(Uint64 *) override;
378
379 // Implemented to call Visitor::VisitAnyInt
380 void VisitInt8(Int8 *) override;
381 void VisitInt16(Int16 *) override;
382 void VisitInt32(Int32 *) override;
383 void VisitInt64(Int64 *) override;
384};
385
386} // namespace VDP
Definition types.hpp:153
NumberType get_value()
Definition types.hpp:191
Number(std::string field_name, FetchFunc fetcher=[]() { return(NumberType) 0;})
Definition types.hpp:175
void pprint(std::stringstream &ss, size_t indent) const override
Definition types.hpp:197
std::function< NumberType()> FetchFunc
Definition types.hpp:170
void pprint_data(std::stringstream &ss, size_t indent) const override
Definition types.hpp:206
void set_value(NumberType val)
Definition types.hpp:187
void write_schema(PacketWriter &sofar) const override
Definition types.hpp:227
void write_message(PacketWriter &sofar) const override
Definition types.hpp:235
void read_data_from_message(PacketReader &reader) override
Definition types.hpp:220
void fetch() override
Definition types.hpp:182
void write_number(const Number &num)
Definition protocol.hpp:355
void write_string(const std::string &str)
Definition protocol.cpp:219
void write_type(Type t)
Definition protocol.cpp:214
Definition protocol.hpp:154
Part(std::string name)
Definition protocol.cpp:92
Definition types.hpp:9
void write_schema(PacketWriter &sofar) const override
Definition types.cpp:81
void read_data_from_message(PacketReader &reader) override
Definition types.cpp:73
void write_message(PacketWriter &sofar) const override
Definition types.cpp:93
Record(std::string name)
Definition types.cpp:8
void fetch() override
Definition types.cpp:63
void set_fields(std::vector< PartPtr > fields)
Definition types.cpp:47
Definition types.hpp:83
void fetch() override
Definition types.cpp:146
void response() override
Definition types.cpp:150
void write_message(PacketWriter &sofar) const override
Definition types.cpp:203
void pprint_data(std::stringstream &ss, size_t indent) const override
Definition types.cpp:187
String(std::string name, FetchFunc fetcher=[]() { return "no value";})
Definition types.cpp:141
void read_data_from_message(PacketReader &reader) override
Definition types.cpp:170
void pprint(std::stringstream &ss, size_t indent) const override
Definition types.cpp:177
void write_schema(PacketWriter &sofar) const override
Definition types.cpp:195
std::string get_value()
Definition types.cpp:159
void set_value(std::string new_value)
Definition types.cpp:155
Definition types.hpp:360
Definition types.hpp:336