Skip to content

Commit

Permalink
Clang has limited constexpr support
Browse files Browse the repository at this point in the history
e.g. Won't allow static_cast. Fake it.
  • Loading branch information
mikee47 committed Apr 19, 2024
1 parent e4765f9 commit 658d95b
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/include/FlashString/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
*/
#define DEFINE_FSTR_ARRAY_LOCAL(name, ElementType, ...) \
static DEFINE_FSTR_ARRAY_DATA(FSTR_DATA_NAME(name), ElementType, __VA_ARGS__); \
static constexpr DEFINE_FSTR_REF_NAMED(name, FSTR::Array<ElementType>);
static FSTR_CONSTEXPR DEFINE_FSTR_REF_NAMED(name, FSTR::Array<ElementType>);

/**
* @brief Define an Array data structure
Expand All @@ -63,9 +63,9 @@
* @param ... List of ElementType items
*/
#define DEFINE_FSTR_ARRAY_DATA(name, ElementType, ...) \
constexpr const struct { \
FSTR_CONSTEXPR const struct { \
FSTR::ObjectBase object; \
ElementType data[sizeof((const ElementType[]){__VA_ARGS__}) / sizeof(ElementType)]; \
ElementType data[FSTR_VA_NARGS(ElementType, __VA_ARGS__)]; \
} FSTR_PACKED FSTR_ALIGNED name PROGMEM = {{sizeof(name.data)}, {__VA_ARGS__}}; \
FSTR_CHECK_STRUCT(name);

Expand Down
10 changes: 4 additions & 6 deletions src/include/FlashString/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
*/
#define DEFINE_FSTR_MAP_LOCAL(name, KeyType, ContentType, ...) \
static DEFINE_FSTR_MAP_DATA(FSTR_DATA_NAME(name), KeyType, ContentType, __VA_ARGS__); \
static constexpr DEFINE_FSTR_REF_NAMED(name, DECL((FSTR::Map<KeyType, ContentType>)));
static FSTR_CONSTEXPR DEFINE_FSTR_REF_NAMED(name, DECL((FSTR::Map<KeyType, ContentType>)));

/**
* @brief Define a Map Object with global reference, specifying the number of elements
Expand All @@ -77,7 +77,7 @@
*/
#define DEFINE_FSTR_MAP_SIZED_LOCAL(name, KeyType, ContentType, size, ...) \
static DEFINE_FSTR_MAP_DATA_SIZED(FSTR_DATA_NAME(name), KeyType, ContentType, size, __VA_ARGS__); \
static constexpr DEFINE_FSTR_REF_NAMED(name, DECL((FSTR::Map<KeyType, ContentType>)));
static FSTR_CONSTEXPR DEFINE_FSTR_REF_NAMED(name, DECL((FSTR::Map<KeyType, ContentType>)));

/**
* @brief Define a Map data structure
Expand All @@ -89,9 +89,7 @@
*/
#define DEFINE_FSTR_MAP_DATA(name, KeyType, ContentType, ...) \
DEFINE_FSTR_MAP_DATA_SIZED(name, KeyType, ContentType, \
(sizeof((const FSTR::MapPair<KeyType, ContentType>[]){__VA_ARGS__}) / \
sizeof(FSTR::MapPair<KeyType, ContentType>)), \
__VA_ARGS__)
FSTR_VA_NARGS(DECL((FSTR::MapPair<KeyType, ContentType>)), __VA_ARGS__), __VA_ARGS__)

/**
* @brief Define a Map data structure, specifying the number of elements
Expand All @@ -102,7 +100,7 @@
* @param ... List of MapPair definitions { key, &content }
*/
#define DEFINE_FSTR_MAP_DATA_SIZED(name, KeyType, ContentType, size, ...) \
constexpr const struct { \
FSTR_CONSTEXPR const struct { \
FSTR::ObjectBase object; \
FSTR::MapPair<KeyType, ContentType> data[size]; \
} FSTR_PACKED FSTR_ALIGNED name PROGMEM = {{sizeof(name.data)}, {__VA_ARGS__}}; \
Expand Down
6 changes: 5 additions & 1 deletion src/include/FlashString/Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@
/**
* @brief Check structure is POD-compliant and correctly aligned
*/
#ifdef __clang__
#define FSTR_CHECK_STRUCT(name)
#else
#define FSTR_CHECK_STRUCT(name) \
static_assert(std::is_pod<decltype(name)>::value, "FSTR structure not POD"); \
static_assert(offsetof(decltype(name), data) == sizeof(uint32_t), "FSTR structure alignment error");
#endif

/**
* @brief Import an object from an external file with reference
Expand All @@ -107,7 +111,7 @@
#define IMPORT_FSTR_OBJECT_LOCAL(name, ObjectType, file) \
IMPORT_FSTR_DATA(FSTR_DATA_NAME(name), file) \
extern "C" __attribute__((visibility("hidden"))) const FSTR::ObjectBase FSTR_DATA_NAME(name); \
static constexpr DEFINE_FSTR_REF(name, ObjectType, FSTR_DATA_NAME(name));
static FSTR_CONSTEXPR DEFINE_FSTR_REF(name, ObjectType, FSTR_DATA_NAME(name));

namespace FSTR
{
Expand Down
2 changes: 1 addition & 1 deletion src/include/FlashString/Stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Stream : public IDataSourceStream
*/
int available() override
{
return object.length() - readPos;
return int(object.length() - readPos);
}

uint16_t readMemoryBlock(char* data, int bufSize) override;
Expand Down
2 changes: 1 addition & 1 deletion src/include/FlashString/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ typedef const __FlashStringHelper* flash_string_t;
*/
#define DEFINE_FSTR_LOCAL(name, str) \
static DEFINE_FSTR_DATA(FSTR_DATA_NAME(name), str); \
static constexpr DEFINE_FSTR_REF_NAMED(name, FSTR::String);
static FSTR_CONSTEXPR DEFINE_FSTR_REF_NAMED(name, FSTR::String);

/**
* @brief Define a FSTR::String data structure
Expand Down
10 changes: 5 additions & 5 deletions src/include/FlashString/Vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
*/
#define DEFINE_FSTR_VECTOR_LOCAL(name, ObjectType, ...) \
static DEFINE_FSTR_VECTOR_DATA(FSTR_DATA_NAME(name), ObjectType, __VA_ARGS__); \
static constexpr DEFINE_FSTR_REF_NAMED(name, FSTR::Vector<ObjectType>);
static FSTR_CONSTEXPR DEFINE_FSTR_REF_NAMED(name, FSTR::Vector<ObjectType>);

/**
* @brief Define a Vector Object with global reference, specifying the number of elements
Expand All @@ -73,7 +73,7 @@
*/
#define DEFINE_FSTR_VECTOR_SIZED_LOCAL(name, ObjectType, size, ...) \
static DEFINE_FSTR_VECTOR_DATA_SIZED(FSTR_DATA_NAME(name), ObjectType, size, __VA_ARGS__); \
static constexpr DEFINE_FSTR_REF_NAMED(name, FSTR::Vector<ObjectType>);
static FSTR_CONSTEXPR DEFINE_FSTR_REF_NAMED(name, FSTR::Vector<ObjectType>);

/**
* @brief Define a Vector data structure
Expand All @@ -83,7 +83,7 @@
* @note Size will be calculated
*/
#define DEFINE_FSTR_VECTOR_DATA(name, ObjectType, ...) \
DEFINE_FSTR_VECTOR_DATA_SIZED(name, ObjectType, sizeof((const void*[]){__VA_ARGS__}) / sizeof(void*), __VA_ARGS__)
DEFINE_FSTR_VECTOR_DATA_SIZED(name, ObjectType, FSTR_VA_NARGS(ObjectType*, __VA_ARGS__), __VA_ARGS__)

/**
* @brief Define a Vector data structure and specify the number of elements
Expand All @@ -94,10 +94,10 @@
* @note Use in situations where the array size cannot be automatically calculated
*/
#define DEFINE_FSTR_VECTOR_DATA_SIZED(name, ObjectType, size, ...) \
constexpr const struct { \
FSTR_CONSTEXPR const struct { \
FSTR::ObjectBase object; \
const ObjectType* data[size]; \
} name PROGMEM = {{sizeof(name.data)}, __VA_ARGS__}; \
} name PROGMEM = {{sizeof(name.data)}, {__VA_ARGS__}}; \
FSTR_CHECK_STRUCT(name);

namespace FSTR
Expand Down

0 comments on commit 658d95b

Please sign in to comment.