From 2aee243de9cb644765c5a1d35536752a40cb4876 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Tue, 26 Apr 2022 19:25:01 +0100 Subject: [PATCH] Part of #113. Compilation partial fixes. Signed off by: Adam Fowler --- herald-tests/data-tests.cpp | 60 +++++++------------ herald-tests/datatypes-tests.cpp | 12 ++-- herald-tests/randomuuid-tests.cpp | 6 +- .../herald/ble/ble_concrete_database.h | 2 +- herald/include/herald/data/string_utils.h | 60 ++++++++++++++++++- herald/include/herald/datatype/data.h | 14 ++--- herald/include/herald/datatype/hex_string.h | 12 ++++ herald/include/herald/datatype/proximity.h | 2 +- herald/include/herald/datatype/uuid.h | 15 +++++ herald/include/herald/sensor_array.h | 3 +- herald/src/data/string_utils.cpp | 38 +++++++++++- herald/src/datatype/uuid.cpp | 1 + 12 files changed, 159 insertions(+), 66 deletions(-) diff --git a/herald-tests/data-tests.cpp b/herald-tests/data-tests.cpp index 7ed35c5..ff70e02 100644 --- a/herald-tests/data-tests.cpp +++ b/herald-tests/data-tests.cpp @@ -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() == ""); } } @@ -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 } } @@ -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 } } @@ -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); @@ -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); @@ -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); @@ -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]") { diff --git a/herald-tests/datatypes-tests.cpp b/herald-tests/datatypes-tests.cpp index ded113b..56073b1 100644 --- a/herald-tests/datatypes-tests.cpp +++ b/herald-tests/datatypes-tests.cpp @@ -149,9 +149,9 @@ 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); } } @@ -159,9 +159,9 @@ TEST_CASE("datatypes-uuid-charctordash", "[datatypes][uuid][charctordash]") { 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); } } @@ -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); } } diff --git a/herald-tests/randomuuid-tests.cpp b/herald-tests/randomuuid-tests.cpp index 4bb7846..c5efdbf 100644 --- a/herald-tests/randomuuid-tests.cpp +++ b/herald-tests/randomuuid-tests.cpp @@ -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 } } @@ -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 } } diff --git a/herald/include/herald/ble/ble_concrete_database.h b/herald/include/herald/ble/ble_concrete_database.h index 80c7675..7095a2d 100644 --- a/herald/include/herald/ble/ble_concrete_database.h +++ b/herald/include/herald/ble/ble_concrete_database.h @@ -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); diff --git a/herald/include/herald/data/string_utils.h b/herald/include/herald/data/string_utils.h index 56ac81d..cc29cec 100644 --- a/herald/include/herald/data/string_utils.h +++ b/herald/include/herald/data/string_utils.h @@ -5,9 +5,17 @@ #ifndef HERALD_STRING_UTILS_H #define HERALD_STRING_UTILS_H +#ifndef CONFIG_HERALD_NO_STD_STREAMS +#include // for 'operator << char' support + #ifndef CONFIG_HERALD_NO_STD_STRING #include #include +#endif + +#endif + +#ifndef CONFIG_HERALD_NO_STD_STRING #include #else #include "herald/datatype/data.h" @@ -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; @@ -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; @@ -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; @@ -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(); @@ -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 ::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 { + size_t operator()(const herald::data::DataString& v) const { + return std::hash()((herald::datatype::Data)v); + } +}; + +} #endif #endif // HERALD_STRING_UTILS_H \ No newline at end of file diff --git a/herald/include/herald/datatype/data.h b/herald/include/herald/datatype/data.h index 49fc1d5..4a06df2 100644 --- a/herald/include/herald/datatype/data.h +++ b/herald/include/herald/datatype/data.h @@ -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 +#include // for Data(std::string) only (uses RAW chars, not HexString) #endif namespace herald { @@ -568,12 +572,6 @@ Data DataSections::emptyRef = Data(); } // end namespace namespace std { - template - inline std::ostream& operator<<(std::ostream &os, const herald::datatype::DataRef& 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); diff --git a/herald/include/herald/datatype/hex_string.h b/herald/include/herald/datatype/hex_string.h index 9f721a5..1172ab2 100644 --- a/herald/include/herald/datatype/hex_string.h +++ b/herald/include/herald/datatype/hex_string.h @@ -61,4 +61,16 @@ class HexString { } // end namespace } // end namespace +#ifndef CONFIG_HERALD_NO_STD_STREAMS +namespace std { + +template +inline std::ostream& +operator<<(std::ostream& os, const herald::datatype::DataRef& d) { + return os << herald::datatype::HexString::encode(d).encoded(); +} + +} // namespace std +#endif + #endif \ No newline at end of file diff --git a/herald/include/herald/datatype/proximity.h b/herald/include/herald/datatype/proximity.h index afd9821..43afeb8 100644 --- a/herald/include/herald/datatype/proximity.h +++ b/herald/include/herald/datatype/proximity.h @@ -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 { diff --git a/herald/include/herald/datatype/uuid.h b/herald/include/herald/datatype/uuid.h index 932847c..55d468c 100644 --- a/herald/include/herald/datatype/uuid.h +++ b/herald/include/herald/datatype/uuid.h @@ -9,6 +9,10 @@ #include "randomness.h" #include "herald/data/string_utils.h" +#ifndef CONFIG_HERALD_NO_STD_STREAMS +#include +#endif + #include namespace herald { @@ -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 \ No newline at end of file diff --git a/herald/include/herald/sensor_array.h b/herald/include/herald/sensor_array.h index d150987..e0b9a77 100644 --- a/herald/include/herald/sensor_array.h +++ b/herald/include/herald/sensor_array.h @@ -16,9 +16,8 @@ #include "datatype/payload_data.h" #include "ble/ble_concrete.h" #include "engine/coordinator.h" +#include "data/string_utils.h" -#include -#include #include #include #include diff --git a/herald/src/data/string_utils.cpp b/herald/src/data/string_utils.cpp index e817d58..86d39c6 100644 --- a/herald/src/data/string_utils.cpp +++ b/herald/src/data/string_utils.cpp @@ -87,6 +87,12 @@ DataString::substr(std::size_t fromIdx, std::size_t length) const noexcept return DataString(value.subdata(fromIdx, length)); } +char +DataString::operator[](std::size_t idx) const noexcept +{ + return (char)value.at(idx); +} + bool DataString::operator==(const DataString& other) const noexcept { @@ -102,7 +108,13 @@ DataString::operator!=(const DataString& other) const noexcept DataString& DataString::operator+=(const DataString& toAppend) noexcept { - value.append(toAppend.value); + value.append((uint8_t)toAppend.value); +} + +DataString& +DataString::operator+=(const char toAppend) noexcept +{ + value.append((uint8_t)toAppend); } DataString @@ -111,10 +123,18 @@ DataString::operator+(const DataString& toAppend) const noexcept return DataString(value + toAppend.value); } +DataString +DataString::operator+(const char toAppend) const noexcept +{ + herald::datatype::Data nv(value); + nv.append(toAppend); + return DataString(std::move(nv)); +} + DataString& DataString::operator=(const DataString& toCopyAssign) noexcept { - value = Data() + value = herald::datatype::Data() } DataString& @@ -157,6 +177,20 @@ DataStringIterator::operator*() noexcept return '\0'; } +DataStringIterator& +DataStringIterator::operator=(const DataStringIterator& other) noexcept +{ + pos = other.pos + over = other.over +} + +DataStringIterator& +DataStringIterator::operator=(DataStringIterator&& other) noexcept +{ + pos = other.pos + over = other.over +} + bool DataStringIterator::operator==(const DataStringIterator& other) const noexcept { diff --git a/herald/src/datatype/uuid.cpp b/herald/src/datatype/uuid.cpp index bab79d7..8df4a9a 100644 --- a/herald/src/datatype/uuid.cpp +++ b/herald/src/datatype/uuid.cpp @@ -16,6 +16,7 @@ // #include // #include // #include +#include #include namespace herald {