Skip to content

Commit

Permalink
Part of theheraldproject#113. Compilation partial fixes.
Browse files Browse the repository at this point in the history
Signed off by: Adam Fowler <[email protected]>
  • Loading branch information
adamfowleruk committed Apr 26, 2022
1 parent 58454d7 commit 2aee243
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 66 deletions.
60 changes: 20 additions & 40 deletions herald-tests/data-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TEST_CASE("datatypes-data-ctor-empty", "[datatypes][data][ctor][empty]") {
SECTION("datatypes-data-ctor-empty") {
herald::datatype::Data data;
REQUIRE(data.size() == 0);
REQUIRE(data.hexEncodedString() == "");
REQUIRE(herald::datatype::HexString::encode(data).encoded() == "");
}
}

Expand Down Expand Up @@ -48,7 +48,8 @@ TEST_CASE("datatypes-data-ctor-move", "[datatypes][data][ctor][move]") {
REQUIRE(d.at(3) == std::byte(3));

REQUIRE(orig.size() == 0);
REQUIRE(orig.hexEncodedString() == ""); // this will definitely try to 'use' the underlying data store
REQUIRE(herald::datatype::HexString::encode(orig).encoded() ==
""); // this will definitely try to 'use' the underlying data store
}
}

Expand All @@ -65,7 +66,7 @@ TEST_CASE("datatypes-data-assign-move", "[datatypes][data][assign][move]") {
REQUIRE(d.at(3) == std::byte(3));

REQUIRE(orig.size() == 0);
REQUIRE(orig.hexEncodedString() == ""); // this will definitely try to 'use' the underlying data store
REQUIRE(herald::datatype::HexString::encode(orig).encoded() == ""); // this will definitely try to 'use' the underlying data store
}
}

Expand Down Expand Up @@ -119,7 +120,7 @@ TEST_CASE("datatypes-data-from-uint8array", "[datatypes][data][ctor][from-uint8a
const uint8_t bytes[] = {0,1,2,3};
herald::datatype::Data d{bytes, 4};

std::string hs = d.hexEncodedString();
herald::data::String hs = herald::datatype::HexString::encode(d).encoded();
INFO("Data: uint8array as hexString: expected: 00010203, got: " << hs);

REQUIRE(d.size() == 4);
Expand Down Expand Up @@ -178,9 +179,12 @@ TEST_CASE("datatypes-data-ctor-repeat", "[datatypes][data][ctor][repeat]") {

TEST_CASE("datatypes-data-ctor-fromhexstring", "[datatypes][data][ctor][fromhexstring]") {
SECTION("datatypes-data-ctor-fromhexstring") {
const std::string hex = "00010ff0ffcc";
herald::datatype::Data d = herald::datatype::Data::fromHexEncodedString(hex);
const std::string finalhex = d.hexEncodedString();
const herald::data::String hex = "00010ff0ffcc";
herald::data::HexString hs;
bool ok = herald::data::HexString::from(hex,hs);
REQUIRE(ok);
herald::datatype::Data d = hs.decode();
const herald::data::String finalhex = hs.encoded();
INFO("Data: fromHexEncodedString: from: " << hex << ", to: " << finalhex);

REQUIRE(d.size() == 6);
Expand All @@ -197,9 +201,12 @@ TEST_CASE("datatypes-data-ctor-fromhexstring", "[datatypes][data][ctor][fromhexs

TEST_CASE("datatypes-data-ctor-fromhexstring-trimmed", "[datatypes][data][ctor][fromhexstring]") {
SECTION("datatypes-data-ctor-fromhexstring-trimmed") {
const std::string hex = "8010ff0ffcc";
herald::datatype::Data d = herald::datatype::Data::fromHexEncodedString(hex);
const std::string finalhex = d.hexEncodedString();
const herald::data::String hex = "8010ff0ffcc";
herald::data::HexString hs;
bool ok = herald::data::HexString::from(hex,hs);
REQUIRE(ok);
herald::datatype::Data d = hs.decode();
const herald::data::String finalhex = hs.encoded();
INFO("Data: fromHexEncodedString: from: " << hex << ", to: " << finalhex);

REQUIRE(d.size() == 6);
Expand Down Expand Up @@ -375,41 +382,14 @@ TEST_CASE("datatypes-data-changeendianness", "[datatypes][data][changeendianness

for (std::size_t i = 0;i < 5;++i) {
REQUIRE(rev.uint8(i, value));
INFO("Byte value is " << value << " with hex " << rev.subdata(i,1).hexEncodedString());
INFO("Byte value is "
<< value << " with hex "
<< herald::datatype::HexString::encode(rev.subdata(i, 1)).encoded());
REQUIRE(uintTwoFourty == value);
}
}
}

TEST_CASE("datatypes-data-description", "[datatypes][data][description]") {
SECTION("datatypes-data-description") {
const uint8_t bytes[] = {0,1,2,3};
herald::datatype::Data d{bytes, 4};

std::string hex = d.description();
INFO("Data: description output: " << hex);
REQUIRE(hex.size() > 0);
// NOTE: No requirements on format for this method - DO NOT rely on it
}
}

TEST_CASE("datatypes-data-hexencodedstring", "[datatypes][data][hexencodedstring]") {
SECTION("datatypes-data-hexencodedstring") {
const uint8_t bytes[] = {0,1,2,3};
herald::datatype::Data d{bytes, 4};

std::string hex = d.hexEncodedString();
INFO("Data: hexEncodedString (std::string) output: " << hex);
REQUIRE(8 == hex.size());
REQUIRE("00010203" == hex);

std::string hexrev = d.reversed().hexEncodedString();
INFO("Data: hexEncodedString (std::string) reversed output: " << hexrev);
REQUIRE(8 == hexrev.size());
REQUIRE("03020100" == hexrev);
}
}



TEST_CASE("datatypes-data-subdata-offset-valid", "[datatypes][data][subdata][offset][valid]") {
Expand Down
12 changes: 6 additions & 6 deletions herald-tests/datatypes-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,19 @@ TEST_CASE("datatypes-uuid-notblank", "[datatypes][uuid][notblank]") {
TEST_CASE("datatypes-uuid-charctordash", "[datatypes][uuid][charctordash]") {
SECTION("datatypes-uuid-charctordash") {
herald::datatype::UUID serviceUUID("428132af-4746-42d3-801e-4572d65bfd9b");
INFO("Service UUID " << serviceUUID.string());
INFO("Service UUID " << serviceUUID);
auto blankUUID = herald::datatype::UUID::fromString("");
INFO("Blank UUID " << blankUUID.string());
INFO("Blank UUID " << blankUUID);
REQUIRE(serviceUUID != blankUUID);
}
}

TEST_CASE("datatypes-uuid-charctor", "[datatypes][uuid][charctor]") {
SECTION("datatypes-uuid-charctor") {
herald::datatype::UUID serviceUUID("428132af474642d3801e4572d65bfd9b");
INFO("Service UUID " << serviceUUID.string());
INFO("Service UUID " << serviceUUID);
auto blankUUID = herald::datatype::UUID::fromString("");
INFO("Blank UUID " << blankUUID.string());
INFO("Blank UUID " << blankUUID);
REQUIRE(serviceUUID != blankUUID);
}
}
Expand All @@ -170,8 +170,8 @@ TEST_CASE("datatypes-uuid-charctormatch", "[datatypes][uuid][charctormatch]") {
SECTION("datatypes-uuid-charctormatch") {
herald::datatype::UUID serviceUUID("428132af-4746-42d3-801e-4572d65bfd9b");
herald::datatype::UUID serviceUUID2("428132af474642d3801e4572d65bfd9b");
INFO("Service UUID " << serviceUUID.string());
INFO("Service UUID2 " << serviceUUID2.string());
INFO("Service UUID " << serviceUUID);
INFO("Service UUID2 " << serviceUUID2);
REQUIRE(serviceUUID == serviceUUID2);
}
}
Expand Down
6 changes: 3 additions & 3 deletions herald-tests/randomuuid-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TEST_CASE("datatypes-uuid-notrandom","[randomness][uuid][basic][datatypes]") {
herald::datatype::AllZerosNotRandom rnd;
herald::datatype::RandomnessGenerator gen(std::move(rnd));
auto emptyV4 = herald::datatype::UUID::random(gen);
REQUIRE(emptyV4.string() == std::string("00000000-0000-4000-8000-000000000000")); // v4 variant 1
REQUIRE((herald::data::String)emptyV4 == "00000000-0000-4000-8000-000000000000"); // v4 variant 1
}
}

Expand All @@ -53,8 +53,8 @@ TEST_CASE("datatypes-uuid-random","[randomness][uuid][basic][datatypes]") {
herald::datatype::IntegerDistributedRandomSource rnd;
herald::datatype::RandomnessGenerator gen(std::move(rnd));
auto randomV4 = herald::datatype::UUID::random(gen);
std::string str = randomV4.string();
herald::data::String str = (herald::data::String)randomV4;
INFO("UUID v4 random value: " << str);
REQUIRE(str != std::string("00000000-0000-4000-8000-000000000000")); // v4 variant 1
REQUIRE(str != "00000000-0000-4000-8000-000000000000"); // v4 variant 1
}
}
2 changes: 1 addition & 1 deletion herald/include/herald/ble/ble_concrete_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class ConcreteBLEDatabase : public BLEDatabase, public BLEDeviceDelegate /*, pub
// HTDBG("Device for target identifier {} already exists",(std::string)targetIdentifier);
return results[0].value().get(); // TODO ensure we send back the latest, not just the first match
}
HTDBG("New target identified: {}",(herald::data::String)targetIdentifier);
HTDBG("New target identified: {}",targetIdentifier);
BLEDevice& newDevice = devices[indexAvailable()];
newDevice.reset(targetIdentifier,*this);

Expand Down
60 changes: 57 additions & 3 deletions herald/include/herald/data/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
#ifndef HERALD_STRING_UTILS_H
#define HERALD_STRING_UTILS_H

#ifndef CONFIG_HERALD_NO_STD_STREAMS
#include <iostream> // for 'operator << char' support

#ifndef CONFIG_HERALD_NO_STD_STRING
#include <ostream>
#include <sstream>
#endif

#endif

#ifndef CONFIG_HERALD_NO_STD_STRING
#include <string>
#else
#include "herald/datatype/data.h"
Expand Down Expand Up @@ -49,12 +57,15 @@ class DataString {
DataString substr(std::size_t fromIdx) const noexcept;
DataString substr(std::size_t fromIdx, std::size_t length) const noexcept;

char operator[](std::size_t idx) const noexcept;
bool operator==(const DataString& other) const noexcept;
bool operator!=(const DataString& other) const noexcept;
DataString& operator+=(const DataString& toAppend) noexcept;
DataString& operator+=(const char toAppend) noexcept;
/// \brief Creates a new String from this + next string
/// \note Avoid using this as it's RAM wasteful. Use DataStringStream instead
DataString operator+(const DataString& toAppend) const noexcept;
DataString operator+(const char) const noexcept;
DataString& operator=(const DataString& toCopyAssign) noexcept;
DataString& operator=(const DataString&& toMoveAssign) noexcept;

Expand All @@ -71,10 +82,14 @@ class DataString {
class DataStringIterator {
public:
DataStringIterator(const DataString& stringOver) noexcept;
DataStringIterator(DataString&& stringOver) noexcept;
~DataStringIterator() noexcept;

char operator*() noexcept;

DataStringIterator& operator=(const DataStringIterator& consume) noexcept;
DataStringIterator& operator=(DataStringIterator&& consume) noexcept;

bool operator==(const DataStringIterator& other) const noexcept;
bool operator!=(const DataStringIterator& other) const noexcept;
void operator++() noexcept;
Expand All @@ -95,7 +110,7 @@ class DataStringStream {
~DataStringStream() noexcept;

DataStringStream& operator<<(DataString consume) noexcept;
DataStringStream& operator<<(std::size_t consume) noexcept;
// DataStringStream& operator<<(std::size_t consume) noexcept;
DataStringStream& operator<<(std::uint8_t consume) noexcept;
DataStringStream& operator<<(std::uint16_t consume) noexcept;
DataStringStream& operator<<(std::uint32_t consume) noexcept;
Expand Down Expand Up @@ -129,6 +144,11 @@ inline String to_string(std::size_t toConvert) noexcept {
return String();
}

inline String to_string(std::uint32_t toConvert) noexcept {
// TODO
return String();
}

inline String to_string(std::uint64_t toConvert) noexcept {
// TODO
return String();
Expand Down Expand Up @@ -162,14 +182,48 @@ inline String to_string(double toConvert) noexcept {
}
}

#ifdef CONFIG_HERALD_NO_STD_STRING

#ifndef CONFIG_HERALD_NO_STD_STREAMS
namespace std {

std::ostream& operator<<(std::ostream& lhs, const herald::data::String& rhs) noexcept;
#ifdef CONFIG_HERALD_NO_STD_STRING
/// \brief Streaming support for herald::data::String
/// \note Not defined if String is a std::string
template <>
inline std::ostream&
operator<<(std::ostream& lhs,
const herald::data::String& rhs) noexcept
{
for (auto c: rhs) {
lhs << (char)c;
}
return lhs;
}
#endif

/// \brief Stream for all Herald types that have an explicit herald::data::String operator
template <typename HeraldT,
typename T = typename std::enable_if<false ==
std::is_same<HeraldT, char>::value>::type
>
inline std::ostream&
operator<<(std::ostream& lhs, const HeraldT& rhs) noexcept {
return lhs << (herald::data::String)rhs;
}
}
#endif

#ifdef CONFIG_HERALD_NO_STD_STRING
namespace std {

template <>
struct hash<herald::data::DataString> {
size_t operator()(const herald::data::DataString& v) const {
return std::hash<herald::datatype::Data>()((herald::datatype::Data)v);
}
};

}
#endif

#endif // HERALD_STRING_UTILS_H
14 changes: 6 additions & 8 deletions herald/include/herald/datatype/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
#ifndef HERALD_DATA_H
#define HERALD_DATA_H

// NOTE Fundamental class - DO NOT make reliant upon any external classes
/// \file Holds the fundamental Data class and related low-level constructs.
/// \note Fundamental class - DO NOT make reliant upon any external classes
/// as it results in a circular dependency, as many Herald classes are
/// based upon or use Data, including herald::data::String and
/// herald::datatype::HexString.

#include "memory_arena.h"

#ifndef CONFIG_HERALD_NO_STD_STRING
#include <string>
#include <string> // for Data(std::string) only (uses RAW chars, not HexString)
#endif

namespace herald {
Expand Down Expand Up @@ -568,12 +572,6 @@ Data DataSections<maxSize>::emptyRef = Data();
} // end namespace

namespace std {
template <typename MemoryArenaT>
inline std::ostream& operator<<(std::ostream &os, const herald::datatype::DataRef<MemoryArenaT>& d)
{
return os << d.hexEncodedString();
}

inline void hash_combine_impl(std::size_t& seed, std::size_t value)
{
seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2);
Expand Down
12 changes: 12 additions & 0 deletions herald/include/herald/datatype/hex_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ class HexString {
} // end namespace
} // end namespace

#ifndef CONFIG_HERALD_NO_STD_STREAMS
namespace std {

template <typename MemoryArenaT>
inline std::ostream&
operator<<(std::ostream& os, const herald::datatype::DataRef<MemoryArenaT>& d) {
return os << herald::datatype::HexString::encode(d).encoded();
}

} // namespace std
#endif

#endif
2 changes: 1 addition & 1 deletion herald/include/herald/datatype/proximity.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Proximity {
double value;

herald::data::String description() const noexcept {
return std::to_string((short)unit) + ":" + herald::data::to_string((int)value);
return herald::data::to_string((short)unit) + ":" + herald::data::to_string((int)value);
}

operator herald::data::String() const noexcept {
Expand Down
15 changes: 15 additions & 0 deletions herald/include/herald/datatype/uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include "randomness.h"
#include "herald/data/string_utils.h"

#ifndef CONFIG_HERALD_NO_STD_STREAMS
#include <ostream>
#endif

#include <array>

namespace herald {
Expand Down Expand Up @@ -102,4 +106,15 @@ class UUID {
} // end namespace
} // end namespace

#ifndef CONFIG_HERALD_NO_STD_STREAMS
namespace std {

inline std::ostream&
operator<<(std::ostream& os, const herald::datatype::UUID& uuid) {
return os << (herald::data::String)uuid;
}

} // namespace std
#endif

#endif
3 changes: 1 addition & 2 deletions herald/include/herald/sensor_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
#include "datatype/payload_data.h"
#include "ble/ble_concrete.h"
#include "engine/coordinator.h"
#include "data/string_utils.h"

#include <memory>
#include <string>
#include <array>
#include <variant>
#include <functional>
Expand Down
Loading

0 comments on commit 2aee243

Please sign in to comment.