Skip to content

Commit 0cb1ab7

Browse files
committed
Fix FSTR_IMPORT and IMPORT_FSTR_ARRAY so they're compatible with DECLARE_xxx
Thanks to @aemseemann for this suggestion aemseemann/Sming#4 (comment) Global C symbols are named using standard __fstr__ prefix and code uses references.
1 parent 4fc9e9f commit 0cb1ab7

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

src/include/FlashString/Array.hpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@
9595

9696
/**
9797
* @brief Define an Array containing data from an external file
98-
* @param name Name for the object
99-
* @param ElementType
98+
* @param name Name for the Array object
99+
* @param ElementType Array element type
100100
* @param file Absolute path to the file containing the content
101+
* @See See also `IMPORT_FSTR_DATA`
102+
* @{
101103
*/
102-
#define IMPORT_FSTR_ARRAY(name, ElementType, file) \
103-
IMPORT_FSTR_DATA(name, file) \
104-
extern "C" const FSTR::Array<ElementType> name;
104+
#define IMPORT_FSTR_ARRAY(name, ElementType, file) IMPORT_FSTR_OBJECT(name, FSTR::Array<ElementType>, file)
105+
#define IMPORT_FSTR_ARRAY_LOCAL(name, ElementType, file) IMPORT_FSTR_OBJECT_LOCAL(name, FSTR::Array<ElementType>, file)
106+
/** @} */
105107

106108
namespace FSTR
107109
{

src/include/FlashString/Object.hpp

+20
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,26 @@
8181
static_assert(std::is_pod<decltype(name)>::value, "FSTR structure not POD"); \
8282
static_assert(offsetof(decltype(name), data) == sizeof(uint32_t), "FSTR structure alignment error");
8383

84+
/**
85+
* @brief Import an object from an external file with reference
86+
* @param name Name for the object
87+
* @param ObjectType Object type for reference
88+
* @param file Absolute path to the file containing the content
89+
* @See See also `IMPORT_FSTR_DATA`
90+
* @note Can only be used at file scope
91+
* @{
92+
*/
93+
#define IMPORT_FSTR_OBJECT(name, ObjectType, file) \
94+
IMPORT_FSTR_DATA(FSTR_DATA_NAME(name), file) \
95+
extern "C" const FSTR::ObjectBase FSTR_DATA_NAME(name); \
96+
DEFINE_FSTR_REF(name, ObjectType, FSTR_DATA_NAME(name));
97+
98+
#define IMPORT_FSTR_OBJECT_LOCAL(name, ObjectType, file) \
99+
IMPORT_FSTR_DATA(FSTR_DATA_NAME(name), file) \
100+
extern "C" const FSTR::ObjectBase FSTR_DATA_NAME(name); \
101+
static constexpr DEFINE_FSTR_REF(name, ObjectType, FSTR_DATA_NAME(name));
102+
/** @} */
103+
84104
namespace FSTR
85105
{
86106
/**

src/include/FlashString/String.hpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,12 @@ typedef const __FlashStringHelper* flash_string_t;
130130
* @brief Define a FSTR::String containing data from an external file
131131
* @param name Name for the FSTR::String object
132132
* @param file Absolute path to the file containing the content
133+
* @See See also `IMPORT_FSTR_DATA`
134+
* @{
133135
*/
134-
#define IMPORT_FSTR(name, file) \
135-
IMPORT_FSTR_DATA(name, file) \
136-
extern "C" const FSTR::String name;
136+
#define IMPORT_FSTR(name, file) IMPORT_FSTR_OBJECT(name, FSTR::String, file)
137+
#define IMPORT_FSTR_LOCAL(name, file) IMPORT_FSTR_OBJECT_LOCAL(name, FSTR::String, file)
138+
/** @} */
137139

138140
/**
139141
* @brief declare a table of FlashStrings

test/app/data.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ DEFINE_FSTR_MAP(arrayMap, int, FSTR::Array<float>, {1, &row1}, {2, &row2});
5858

5959
DEFINE_FSTR_LOCAL(key1, "key1");
6060
DEFINE_FSTR_LOCAL(key2, "key2");
61-
IMPORT_FSTR(FS_content1, COMPONENT_PATH "/files/content1.txt");
62-
IMPORT_FSTR(FS_content2, COMPONENT_PATH "/files/content2.txt");
61+
IMPORT_FSTR_LOCAL(FS_content1, COMPONENT_PATH "/files/content1.txt");
62+
IMPORT_FSTR_LOCAL(FS_content2, COMPONENT_PATH "/files/content2.txt");
6363
DEFINE_FSTR_MAP(stringMap, FSTR::String, FSTR::String, {&key1, &FS_content1}, {&key2, &FS_content2});
6464

6565
DEFINE_FSTR_MAP(enumMap, MapKey, FSTR::String, {KeyA, &FS_content1}, {KeyB, &FS_content2});

utility.rst

+12-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,24 @@ using :c:func:`IMPORT_FSTR` or :c:func:`IMPORT_FSTR_ARRAY`. For example::
99

1010
IMPORT_FSTR(myData, PROJECT_DIR "/files/myData.bin");
1111

12+
This defines a C++ reference to the data called ``myData`` so it can be referred to using
13+
:c:func:`DECLARE_FSTR` if required.
14+
1215
.. note::
1316

1417
File paths must be absolute or the compiler won't be able to locate it reliably.
1518

1619
Sming provides :envvar:`PROJECT_DIR` and :envvar:`COMPONENT_PATH` to help with this.
1720

18-
A single global C symbol ``myFlashString`` will be defined so take care
19-
with naming to avoid the risk of conflicts, especially if used within a Component.
21+
22+
.. note::
23+
24+
A corresponding global C symbol will also be defined, based on the provided name,
25+
to provide linkage with the imported data.
26+
27+
Such symbols cannot be localised as it requires use of ``extern``, so take care with
28+
naming to avoid the risk of conflicts, especially if used within a Component.
29+
2030

2131
One use for imported files is to serve content via HTTP, like this::
2232

0 commit comments

Comments
 (0)