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 FlashString.
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 #else
99 #ifdef ARCH_HOST
100 #define IROM_SECTION ".rodata"
101 #else
102 #define IROM_SECTION ".irom0.text"
103 #endif
104 #define IMPORT_FSTR_DATA(name, file) \
105  __asm__(".section " IROM_SECTION "\n" \
106  ".type " STR(name) ", @object\n" \
107  ".align 4\n" STR(name) ":\n" \
108  ".long _" STR(name) "_end - " STR(name) " - 4\n" \
109  ".incbin \"" file "\"\n" \
110  "_" STR(name) "_end:\n");
111 #endif
112 // clang-format on
113 
114 namespace FSTR
115 {
123 template <typename T> FSTR_INLINE typename std::enable_if<sizeof(T) == 1, T>::type readValue(const T* ptr)
124 {
125  return static_cast<T>(pgm_read_byte(ptr));
126 }
127 
128 template <typename T> FSTR_INLINE typename std::enable_if<sizeof(T) == 2, T>::type readValue(const T* ptr)
129 {
130  return static_cast<T>(pgm_read_word(ptr));
131 }
132 
133 template <typename T> FSTR_INLINE typename std::enable_if<IS_ALIGNED(sizeof(T)), T>::type readValue(const T* ptr)
134 {
135  return *static_cast<const T*>(ptr);
136 }
137 
140 } // namespace FSTR
141 
#define pgm_read_byte(addr)
Definition: Arch/Esp8266/Components/libc/include/sys/pgmspace.h:137
#define pgm_read_word(addr)
Definition: Arch/Esp8266/Components/libc/include/sys/pgmspace.h:138
Definition: Utility.hpp:59
std::enable_if< IS_ALIGNED(sizeof(T)), T >::type readValue(const T *ptr)
Definition: Utility.hpp:133
#define FSTR_INLINE
Definition: config.hpp:26
U type
Definition: Utility.hpp:61
Definition: Array.hpp:107