Skip to content

Commit

Permalink
Add toString support
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Oct 29, 2020
1 parent 2b95c4b commit 09c7470
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
28 changes: 28 additions & 0 deletions Sming/Core/Data/BitSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,34 @@ template <typename E> inline constexpr BitSet<uint32_t, E> operator+(E a, E b)
return a | b;
}

inline String toString(uint8_t value)
{
return String(value);
}

/**
* @brief Class template to print the contents of a BitSet to a String
* @note Requires an implementation of `toString(E)`
*/
template <typename S, typename E, size_t size_>
String toString(const BitSet<S, E, size_>& bitset, const String& separator = ", ")
{
String s;

for(unsigned i = 0; i < bitset.size(); ++i) {
if(!bitset[E(i)]) {
continue;
}

if(s) {
s += separator;
}
s += toString(E(i));
}

return s;
}

/**
* @brief A set of 32 bits
*/
Expand Down
8 changes: 8 additions & 0 deletions docs/source/framework/core/data/bitset.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ Bit manipulation operators are provided so you can do logical stuff like this::

And so on.

To display the contents of a BitSet, do this::

Serial.print(_F("My basket contains: "));
Serial.println(basket1);

You will also need to provide an implementation of ``toString(Fruit)``
or whatever type you are using for the set elements.


API
---
Expand Down
42 changes: 31 additions & 11 deletions tests/HostTests/app/test-bitset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,32 @@

namespace
{
#define FRUIT_ELEMENT_MAP(XX) \
XX(apple) \
XX(banana) \
XX(kiwi) \
XX(orange) \
XX(passion) \
XX(pear) \
XX(tomato)

enum class Fruit {
apple,
banana,
kiwi,
orange,
passion,
pear,
tomato,
#define XX(n) n,
FRUIT_ELEMENT_MAP(XX)
#undef XX
MAX
};

using FruitBasket = BitSet<uint8_t, Fruit, unsigned(Fruit::tomato) + 1>;
using NumberSet = BitSet<uint32_t, uint8_t>;
#define XX(n) #n "\0"
DEFINE_FSTR_LOCAL(fruitStrings, FRUIT_ELEMENT_MAP(XX))
#undef XX

String toString(Fruit f)
{
return CStringArray(fruitStrings)[unsigned(f)];
}

using FruitBasket = BitSet<uint8_t, Fruit, size_t(Fruit::MAX)>;

static constexpr FruitBasket fixedBasket = Fruit::orange | Fruit::banana | Fruit::tomato;

Expand Down Expand Up @@ -61,6 +75,9 @@ class BitSetTest : public TestGroup

TEST_CASE("Operations")
{
Serial.print(_F("fixedBasket contains: "));
Serial.println(toString(fixedBasket));

FruitBasket basket;
REQUIRE(basket.value() == 0);
REQUIRE(!basket);
Expand Down Expand Up @@ -103,8 +120,11 @@ class BitSetTest : public TestGroup

TEST_CASE("Number set")
{
NumberSet numbers = 12U;
REQUIRE(numbers.value() == 12);
using NumberSet = BitSet<uint32_t, uint8_t>;
NumberSet numbers = 0x12345678U;
Serial.print(_F("numbers = "));
Serial.println(toString(numbers));
REQUIRE(numbers.value() == 0x12345678U);

numbers = NumberSet{};
REQUIRE(numbers.value() == 0);
Expand Down

0 comments on commit 09c7470

Please sign in to comment.