forked from bblanchon/ArduinoJson
Make string support generic (issue #1807)
This commit is contained in:
@ -6,6 +6,7 @@ HEAD
|
|||||||
|
|
||||||
* Drop support for C++98/C++03. Minimum required is C++11.
|
* Drop support for C++98/C++03. Minimum required is C++11.
|
||||||
* Remove `ARDUINOJSON_NAMESPACE`; use `ArduinoJson` instead.
|
* Remove `ARDUINOJSON_NAMESPACE`; use `ArduinoJson` instead.
|
||||||
|
* Make string support generic (issue #1807)
|
||||||
|
|
||||||
v6.20.1 (2023-02-08)
|
v6.20.1 (2023-02-08)
|
||||||
-------
|
-------
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
// MIT License
|
// MIT License
|
||||||
|
|
||||||
#define ARDUINOJSON_ENABLE_PROGMEM 1
|
#define ARDUINOJSON_ENABLE_PROGMEM 1
|
||||||
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
|
|
||||||
|
|
||||||
#include "custom_string.hpp"
|
#include "custom_string.hpp"
|
||||||
#include "progmem_emulation.hpp"
|
#include "progmem_emulation.hpp"
|
||||||
|
@ -9,6 +9,6 @@
|
|||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T declval();
|
T&& declval();
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -7,15 +7,17 @@
|
|||||||
#include <ArduinoJson/Namespace.hpp>
|
#include <ArduinoJson/Namespace.hpp>
|
||||||
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
template <class T>
|
template <class...>
|
||||||
|
using void_t = void;
|
||||||
|
|
||||||
|
template <class T, typename = void>
|
||||||
struct is_std_string : false_type {};
|
struct is_std_string : false_type {};
|
||||||
|
|
||||||
template <class TCharTraits, class TAllocator>
|
template <class T>
|
||||||
struct is_std_string<std::basic_string<char, TCharTraits, TAllocator>>
|
struct is_std_string<
|
||||||
|
T, void_t<decltype(T().push_back('a')), decltype(T().append(""))>>
|
||||||
: true_type {};
|
: true_type {};
|
||||||
|
|
||||||
template <typename TDestination>
|
template <typename TDestination>
|
||||||
@ -25,7 +27,7 @@ class Writer<TDestination,
|
|||||||
Writer(TDestination& str) : _str(&str) {}
|
Writer(TDestination& str) : _str(&str) {}
|
||||||
|
|
||||||
size_t write(uint8_t c) {
|
size_t write(uint8_t c) {
|
||||||
_str->operator+=(static_cast<char>(c));
|
_str->push_back(static_cast<char>(c));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
// ArduinoJson - https://arduinojson.org
|
|
||||||
// Copyright © 2014-2023, Benoit BLANCHON
|
|
||||||
// MIT License
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
#include <ArduinoJson/Strings/Adapters/RamString.hpp>
|
|
||||||
#include <ArduinoJson/Strings/StringAdapter.hpp>
|
|
||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
struct StringAdapter<
|
|
||||||
T, typename enable_if<is_same<T, ::String>::value ||
|
|
||||||
is_same<T, ::StringSumHelper>::value>::type> {
|
|
||||||
typedef SizedRamString AdaptedString;
|
|
||||||
|
|
||||||
static AdaptedString adapt(const ::String& s) {
|
|
||||||
return AdaptedString(s.c_str(), s.length());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
|
@ -1,23 +0,0 @@
|
|||||||
// ArduinoJson - https://arduinojson.org
|
|
||||||
// Copyright © 2014-2023, Benoit BLANCHON
|
|
||||||
// MIT License
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <ArduinoJson/Strings/Adapters/RamString.hpp>
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|
||||||
|
|
||||||
template <typename TCharTraits, typename TAllocator>
|
|
||||||
struct StringAdapter<std::basic_string<char, TCharTraits, TAllocator>, void> {
|
|
||||||
typedef SizedRamString AdaptedString;
|
|
||||||
|
|
||||||
static AdaptedString adapt(
|
|
||||||
const std::basic_string<char, TCharTraits, TAllocator>& s) {
|
|
||||||
return AdaptedString(s.c_str(), s.size());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
|
51
src/ArduinoJson/Strings/Adapters/StringObject.hpp
Normal file
51
src/ArduinoJson/Strings/Adapters/StringObject.hpp
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
// ArduinoJson - https://arduinojson.org
|
||||||
|
// Copyright © 2014-2023, Benoit BLANCHON
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <ArduinoJson/Strings/Adapters/RamString.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StringAdapter.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StringTraits.hpp>
|
||||||
|
|
||||||
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct StringAdapter<
|
||||||
|
T,
|
||||||
|
typename enable_if<
|
||||||
|
(string_traits<T>::has_cstr || string_traits<T>::has_data) &&
|
||||||
|
(string_traits<T>::has_length || string_traits<T>::has_size)>::type> {
|
||||||
|
typedef SizedRamString AdaptedString;
|
||||||
|
|
||||||
|
static AdaptedString adapt(const T& s) {
|
||||||
|
return AdaptedString(get_data(s), get_size(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
template <typename U>
|
||||||
|
static typename enable_if<string_traits<U>::has_size, size_t>::type get_size(
|
||||||
|
const U& s) {
|
||||||
|
return s.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
static typename enable_if<!string_traits<U>::has_size, size_t>::type get_size(
|
||||||
|
const U& s) {
|
||||||
|
return s.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
static typename enable_if<string_traits<U>::has_data, const char*>::type
|
||||||
|
get_data(const U& s) {
|
||||||
|
return s.data();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
static typename enable_if<!string_traits<U>::has_data, const char*>::type
|
||||||
|
get_data(const U& s) {
|
||||||
|
return s.c_str();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
@ -1,22 +0,0 @@
|
|||||||
// ArduinoJson - https://arduinojson.org
|
|
||||||
// Copyright © 2014-2023, Benoit BLANCHON
|
|
||||||
// MIT License
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <ArduinoJson/Strings/Adapters/RamString.hpp>
|
|
||||||
|
|
||||||
#include <string_view>
|
|
||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct StringAdapter<std::string_view, void> {
|
|
||||||
typedef SizedRamString AdaptedString;
|
|
||||||
|
|
||||||
static AdaptedString adapt(const std::string_view& s) {
|
|
||||||
return AdaptedString(s.data(), s.size());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
|
@ -7,18 +7,7 @@
|
|||||||
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
||||||
#include <ArduinoJson/Strings/Adapters/JsonString.hpp>
|
#include <ArduinoJson/Strings/Adapters/JsonString.hpp>
|
||||||
#include <ArduinoJson/Strings/Adapters/RamString.hpp>
|
#include <ArduinoJson/Strings/Adapters/RamString.hpp>
|
||||||
|
#include <ArduinoJson/Strings/Adapters/StringObject.hpp>
|
||||||
#if ARDUINOJSON_ENABLE_STD_STRING
|
|
||||||
# include <ArduinoJson/Strings/Adapters/StdString.hpp>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if ARDUINOJSON_ENABLE_STRING_VIEW
|
|
||||||
# include <ArduinoJson/Strings/Adapters/StringView.hpp>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if ARDUINOJSON_ENABLE_ARDUINO_STRING
|
|
||||||
# include <ArduinoJson/Strings/Adapters/ArduinoString.hpp>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if ARDUINOJSON_ENABLE_PROGMEM
|
#if ARDUINOJSON_ENABLE_PROGMEM
|
||||||
# include <ArduinoJson/Strings/Adapters/FlashString.hpp>
|
# include <ArduinoJson/Strings/Adapters/FlashString.hpp>
|
||||||
|
79
src/ArduinoJson/Strings/StringTraits.hpp
Normal file
79
src/ArduinoJson/Strings/StringTraits.hpp
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
// ArduinoJson - https://arduinojson.org
|
||||||
|
// Copyright © 2014-2023, Benoit BLANCHON
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <ArduinoJson/Strings/Adapters/RamString.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StringAdapter.hpp>
|
||||||
|
|
||||||
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
|
namespace string_traits_impl {
|
||||||
|
|
||||||
|
// const char* c_str() const
|
||||||
|
// - String
|
||||||
|
// - std::string
|
||||||
|
|
||||||
|
template <class T, class = void>
|
||||||
|
struct has_cstr : false_type {};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct has_cstr<T,
|
||||||
|
typename enable_if<is_same<decltype(declval<const T>().c_str()),
|
||||||
|
const char*>::value>::type>
|
||||||
|
: true_type {};
|
||||||
|
|
||||||
|
// const char* data() const
|
||||||
|
// - std::string
|
||||||
|
// - std::string_view
|
||||||
|
// - etl::string
|
||||||
|
|
||||||
|
template <class T, class = void>
|
||||||
|
struct has_data : false_type {};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct has_data<T,
|
||||||
|
typename enable_if<is_same<decltype(declval<const T>().data()),
|
||||||
|
const char*>::value>::type>
|
||||||
|
: true_type {};
|
||||||
|
|
||||||
|
// size_t length() const
|
||||||
|
// - String
|
||||||
|
|
||||||
|
template <class T, class = void>
|
||||||
|
struct has_length : false_type {};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct has_length<
|
||||||
|
T, typename enable_if<
|
||||||
|
is_same<decltype(declval<const T>().length()), size_t>::value>::type>
|
||||||
|
: true_type {};
|
||||||
|
|
||||||
|
// size_t size() const
|
||||||
|
// - std::string
|
||||||
|
// - std::string_view
|
||||||
|
// - etl::string
|
||||||
|
|
||||||
|
template <class T, class = void>
|
||||||
|
struct has_size : false_type {};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct has_size<
|
||||||
|
T, typename enable_if<
|
||||||
|
is_same<decltype(declval<const T>().size()), size_t>::value>::type>
|
||||||
|
: true_type {};
|
||||||
|
|
||||||
|
} // namespace string_traits_impl
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct string_traits {
|
||||||
|
enum {
|
||||||
|
has_cstr = string_traits_impl::has_cstr<T>::value,
|
||||||
|
has_length = string_traits_impl::has_length<T>::value,
|
||||||
|
has_data = string_traits_impl::has_data<T>::value,
|
||||||
|
has_size = string_traits_impl::has_size<T>::value
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
Reference in New Issue
Block a user