Skip to content

Commit

Permalink
Handle uint32 values, avoiding ambiguity errors when compiling
Browse files Browse the repository at this point in the history
and using int64_t for storage so that the correct positive value
is retained.
  • Loading branch information
parnham committed Sep 28, 2022
1 parent daed756 commit 63d8a6c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions include/entity/codec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ namespace ent
virtual void item(os &dst, const string &name, const string &value, int depth) const = 0;
virtual void item(os &dst, const string &name, const vector<uint8_t> &value, int depth) const = 0;

// To avoid ambiguity and retain positive values cast unsigned integers to 64-bit longs
void item(os &dst, const string &name, uint32_t value, int depth) const { this->item(dst, name, (int64_t)value, depth); }


// Decoding functions
virtual bool validate(const string &data) const = 0;
Expand All @@ -50,6 +53,9 @@ namespace ent
virtual string get(const string &data, int &i, int type, const string def) const = 0;
virtual vector<uint8_t> get(const string &data, int &i, int type, const vector<uint8_t> def) const = 0;

// To avoid ambiguity and retain positive values cast unsigned integers to 64-bit longs
uint32_t get(const string &data, int &i, int type, uint32_t def) const { return this->get(data, i, type, (int64_t)def); }


// Encode dynamic type
void object(const tree &item, os &dst, const string &name, stack<int> &stack) const
Expand Down
6 changes: 6 additions & 0 deletions packages/debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
libentity0 (1.1.11) unstable; urgency=medium

* Handle uint32 values

-- Dan Parnham <[email protected]> Wed, 28 Sep 2022 14:27:53 +0100

libentity0 (1.1.10) unstable; urgency=medium

* Added hexadecimal and leading decimal point support to JSON parser
Expand Down
2 changes: 1 addition & 1 deletion packages/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.10
1.1.11
17 changes: 17 additions & 0 deletions src/test/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,20 @@ TEST_CASE("maps of entities can be decoded from JSON", "[entity]")
REQUIRE(e["first"].integer == 101);
REQUIRE(e["second"].integer == 42);
}


TEST_CASE("entities with uint values lower than 64-bit can be handled")
{
struct UnsignedEntity
{
uint16_t little = std::numeric_limits<uint16_t>::max();
uint32_t medium = std::numeric_limits<uint32_t>::max();

emap(eref(little), eref(medium))
};
UnsignedEntity uns;
auto e = ent::to_tree(uns);

REQUIRE(e["little"].as_long() == std::numeric_limits<uint16_t>::max());
REQUIRE(e["medium"].as_long() == std::numeric_limits<uint32_t>::max());
}

0 comments on commit 63d8a6c

Please sign in to comment.