|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <map> |
| 4 | +#include "../interfaces/Encoder.h" |
| 5 | +#include "CBOR.h" |
| 6 | +#include "../interfaces/message.h" |
| 7 | +#include "./tinycbor/cbor-lib.h" |
| 8 | + |
| 9 | +class CBORMessageEncoderClass; |
| 10 | + |
| 11 | + |
| 12 | +// TODO find a better name |
| 13 | +// TODO maybe a template<CBORTag tag, MessageId id> ? |
| 14 | +// TODO maybe template<resultStruct> that is also the parameter of encode |
| 15 | +// TODO in order to make this more extensible we should not pass Message* as a parameter, templated function may be better (or void*) |
| 16 | +// providing both id and tag gives the ability to convert and avoid using a conversion function |
| 17 | +class CBORMessageEncoderInterface { |
| 18 | +public: |
| 19 | + CBORMessageEncoderInterface(const CBORTag tag, const MessageId id); |
| 20 | + virtual ~CBORMessageEncoderInterface() {} |
| 21 | + |
| 22 | +protected: |
| 23 | + virtual Encoder::Status encode(CborEncoder* encoder, Message *msg) = 0; |
| 24 | + |
| 25 | +private: |
| 26 | + const CBORTag tag; |
| 27 | + const MessageId id; |
| 28 | + |
| 29 | + friend CBORMessageEncoderClass; |
| 30 | + |
| 31 | + // wrapper for encode function that for the time being only writes the tag in the buffer |
| 32 | + Encoder::Status _encode(CborEncoder* encoder, Message *msg); |
| 33 | +}; |
| 34 | + |
| 35 | +// TODO make a private constructor? |
| 36 | +class CBORMessageEncoderClass: public Encoder { |
| 37 | +public: |
| 38 | + static CBORMessageEncoderClass& getInstance() { |
| 39 | + static CBORMessageEncoderClass singleton; |
| 40 | + |
| 41 | + return singleton; |
| 42 | + } |
| 43 | + |
| 44 | + void append(CBORTag id, CBORMessageEncoderInterface* encoder) { |
| 45 | + // auto pair = encoders.try_emplace(id, encoder); |
| 46 | + encoders[id] = encoder; |
| 47 | + } |
| 48 | + |
| 49 | + Encoder::Status encode(Message* message, uint8_t * data, size_t& len); |
| 50 | +private: |
| 51 | + std::map<MessageId, CBORMessageEncoderInterface*> encoders; |
| 52 | +}; |
| 53 | + |
| 54 | +extern CBORMessageEncoderClass& CBORMessageEncoder; |
0 commit comments