Utility.hpp
Go to the documentation of this file.
1 /****
2  * Utility.hpp - Definitions, common macros and utility functions
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the FlashString Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: Nov 2019 - Mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "config.hpp"
25 
58 #define DECL(t) argument_type<void(t)>::type
59 template <typename T> struct argument_type;
60 template <typename T, typename U> struct argument_type<T(U)> {
61  typedef U type;
62 };
63 
86 // clang-format off
87 #define STR(x) XSTR(x)
88 #define XSTR(x) #x
89 #ifdef __WIN32
90 #define IMPORT_FSTR_DATA(name, file) \
91  __asm__(".section .rodata\n" \
92  ".def _" STR(name) "; .scl 2; .type 32; .endef\n" \
93  ".align 4\n" \
94  "_" STR(name) ":\n" \
95  ".long _" STR(name) "_end - _" STR(name) " - 4\n" \
96  ".incbin \"" file "\"\n" \
97  "_" STR(name) "_end:\n");
98 #elif defined(__APPLE__)
99 #define IMPORT_FSTR_DATA(name, file) \
100  __asm__(".const_data\n" \
101  ".globl _" STR(name) "\n" \
102  ".align 4\n" "_" STR(name) ":\n" \
103  ".long _" STR(name) "_end - _" STR(name) " - 4\n" \
104  ".incbin \"" file "\"\n" \
105  "_" STR(name) "_end:\n");
106 #elif defined(__arm__)
107 #define IMPORT_FSTR_DATA(name, file) \
108  __asm__(".section " ICACHE_RODATA_SECTION "." STR(name) "\n" \
109  ".type " STR(name) ", %object\n" \
110  ".align 4\n" STR(name) ":\n" \
111  ".long _" STR(name) "_end - " STR(name) " - 4\n" \
112  ".incbin \"" file "\"\n" \
113  "_" STR(name) "_end:\n");
114 #else
115 #define IMPORT_FSTR_DATA(name, file) \
116  __asm__(".section " ICACHE_RODATA_SECTION "." STR(name) "\n" \
117  ".type " STR(name) ", @object\n" \
118  ".align 4\n" STR(name) ":\n" \
119  ".long _" STR(name) "_end - " STR(name) " - 4\n" \
120  ".incbin \"" file "\"\n" \
121  "_" STR(name) "_end:\n");
122 #endif
123 // clang-format on
124 
125 namespace FSTR
126 {
134 template <typename T> FSTR_INLINE typename std::enable_if<sizeof(T) == 1, T>::type readValue(const T* ptr)
135 {
136  return static_cast<T>(pgm_read_byte(ptr));
137 }
138 
139 template <typename T> FSTR_INLINE typename std::enable_if<sizeof(T) == 2, T>::type readValue(const T* ptr)
140 {
141  return static_cast<T>(pgm_read_word(ptr));
142 }
143 
144 template <typename T> FSTR_ALIGN32 FSTR_INLINE typename std::enable_if<sizeof(T) == 4, T>::type readValue(const T* ptr)
145 {
146  union {
147  uint32_t u32;
148  T value;
149  } tmp;
150  tmp.u32 = pgm_read_dword(ptr);
151  return tmp.value;
152 }
153 
154 template <typename T>
155 FSTR_INLINE typename std::enable_if<(sizeof(T) > 4) && IS_ALIGNED(sizeof(T)), T>::type readValue(const T* ptr)
156 {
157  T value;
158  memcpy_aligned(&value, ptr, sizeof(T));
159  return value;
160 }
161 
164 } // namespace FSTR
165 
#define pgm_read_byte(addr)
Definition: Arch/Esp32/Components/libc/src/include/sys/pgmspace.h:38
#define pgm_read_dword(addr)
Definition: Arch/Esp32/Components/libc/src/include/sys/pgmspace.h:44
#define pgm_read_word(addr)
Definition: Arch/Esp32/Components/libc/src/include/sys/pgmspace.h:39
uint32_t u32
Definition: c_types.h:19
#define FSTR_INLINE
Definition: config.hpp:28
#define FSTR_ALIGN32
Definition: config.hpp:32
#define IS_ALIGNED(_x)
determines if the given value is aligned to a word (4-byte) boundary
Definition: FakePgmSpace.h:34
void * memcpy_aligned(void *dst, const void *src, unsigned len)
copy memory aligned to word boundaries
Definition: Array.hpp:118
std::enable_if< sizeof(T)==1, T >::type readValue(const T *ptr)
Read a typed value from flash memory ensuring correct alignment of access.
Definition: Utility.hpp:134
U type
Definition: Utility.hpp:61
Definition: Utility.hpp:59