Skip to content

Commit 9666030

Browse files
fixup! implementing decoder interface
1 parent 87f45cb commit 9666030

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/cbor/CborDecoder.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
*/
1010
#include "CborDecoder.h"
1111

12-
static CBORMessageDecoderClass* singleton = nullptr;
13-
CBORMessageDecoderClass& CBORMessageDecoder = CBORMessageDecoderClass::getInstance();
14-
15-
Decoder::Status CBORMessageDecoderClass::decode(Message* msg, const uint8_t* const buf, size_t &len) { // TODO do we need to propagate the maximum length?
12+
Decoder::Status CBORMessageDecoderSingleton::decode(Message* msg, const uint8_t* const buf, size_t &len) { // TODO do we need to propagate the maximum length?
1613
// prepare cbor structure
1714
CborValue iter;
1815
CborTag tag;
@@ -49,17 +46,16 @@ Decoder::Status CBORMessageDecoderClass::decode(Message* msg, const uint8_t* con
4946
return Decoder::Status::Complete;
5047
}
5148

52-
CBORMessageDecoderClass& CBORMessageDecoderClass::getInstance() {
53-
if(singleton == nullptr) {
54-
singleton = new CBORMessageDecoderClass();
55-
}
56-
return *singleton;
49+
CBORMessageDecoderSingleton& CBORMessageDecoderSingleton::getInstance() {
50+
static CBORMessageDecoderSingleton singleton;
51+
52+
return singleton;
5753
}
5854

5955
CBORMessageDecoderInterface::CBORMessageDecoderInterface(const CBORTag tag, const MessageId id)
6056
: tag(tag), id(id) {
6157
// call singleton/global variable and insert this encoder
62-
CBORMessageDecoderClass::getInstance().append(tag, this);
58+
CBORMessageDecoderSingleton::getInstance().append(tag, this);
6359
}
6460

6561
Decoder::Status CBORMessageDecoderInterface::_decode(CborValue* iter, Message *msg) {

src/cbor/CborDecoder.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
#include "../interfaces/message.h"
1616
#include "./tinycbor/cbor-lib.h"
1717

18-
class CBORMessageDecoderClass;
18+
class CBORMessageDecoderSingleton;
1919

2020
// TODO find a better name
2121
// TODO maybe a template<CBORTag tag, MessageId id> ?
2222
// TODO maybe template<resultStruct> that is also the parameter of encode
2323
// TODO in order to make this more extensible we should not pass Message* as a parameter, templated function may be better (or void*)
24-
// providing both id and tag gives the ability to convert and avoid using a conversion function
2524
class CBORMessageDecoderInterface {
2625
public:
2726
CBORMessageDecoderInterface(const CBORTag tag, const MessageId id);
@@ -34,25 +33,31 @@ class CBORMessageDecoderInterface {
3433
const CBORTag tag;
3534
const MessageId id;
3635

37-
friend CBORMessageDecoderClass;
36+
friend CBORMessageDecoderSingleton;
3837

3938
// wrapper for encode function that for the time being only writes the tag in the buffer
4039
Decoder::Status _decode(CborValue* iter, Message *msg);
4140
};
4241

43-
// TODO make a private constructor?
44-
class CBORMessageDecoderClass: public Decoder {
42+
class CBORMessageDecoderSingleton: public Decoder {
4543
public:
46-
CBORMessageDecoderClass() {}
47-
static CBORMessageDecoderClass& getInstance();
44+
static CBORMessageDecoderSingleton& getInstance();
4845

4946
void append(CBORTag id, CBORMessageDecoderInterface* encoder) {
5047
decoders[id] = encoder;
5148
}
5249

5350
Decoder::Status decode(Message* msg, const uint8_t* const buf, size_t &len);
5451
private:
52+
CBORMessageDecoderSingleton() {}
53+
54+
static CBORMessageDecoderSingleton singleton;
5555
std::map<CBORTag, CBORMessageDecoderInterface*> decoders;
5656
};
5757

58-
extern CBORMessageDecoderClass& CBORMessageDecoder;
58+
class CBORMessageDecoder: public Decoder {
59+
public:
60+
inline Decoder::Status decode(Message* msg, const uint8_t* const buf, size_t &len) {
61+
return CBORMessageDecoderSingleton::getInstance().decode(msg, buf, len);
62+
}
63+
};

0 commit comments

Comments
 (0)