Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support 17[xml] #207

Merged
merged 3 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ add_executable(json_benchmark ${JSONBENCHMARK})
add_executable(test_some ${TEST_SOME})
add_executable(test_ut ${TEST_UT})
add_executable(test_json_files ${TEST_JSON_FILES})
add_executable(xml_example ${XML_EXAMPLE})
add_executable(test_xml ${TEST_XML})
add_executable(xml_benchmark ${XMLBENCH})
add_executable(test_xml_nothrow ${TEST_XMLNOTHROW})
if(CMAKE_CXX_STANDARD GREATER_EQUAL 20)
add_executable(xml_example ${XML_EXAMPLE})
add_executable(yaml_example ${YAML_EXAMPLE})
add_executable(test_xml ${TEST_XML})
add_executable(xml_benchmark ${XMLBENCH})
add_executable(test_yaml ${TEST_YAML})
add_executable(yaml_benchmark ${YAMLBENCH})
add_executable(test_nothrow ${TEST_NOTHROW})
add_executable(test_util ${TEST_UTIL})
add_executable(test_xml_nothrow ${TEST_XMLNOTHROW})

endif()

# unit test
Expand Down
159 changes: 1 addition & 158 deletions iguana/json_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,137 +3,11 @@

#pragma once

#include "define.h"
#include "detail/charconv.h"
#include "enum_reflection.hpp"
#include "reflection.hpp"
#include "util.hpp"
#include "value.hpp"
#include <filesystem>
#include <forward_list>
#include <fstream>
#include <math.h>
#include <optional>
#include <stdexcept>
#include <string_view>
#include <type_traits>

namespace iguana {

template <typename T>
inline constexpr bool char_v = std::is_same_v<std::decay_t<T>, char> ||
std::is_same_v<std::decay_t<T>, char16_t> ||
std::is_same_v<std::decay_t<T>, char32_t> ||
std::is_same_v<std::decay_t<T>, wchar_t>;

template <typename T>
inline constexpr bool bool_v =
std::is_same_v<std::decay_t<T>, bool> ||
std::is_same_v<std::decay_t<T>, std::vector<bool>::reference>;

template <typename T>
inline constexpr bool int_v = std::is_integral_v<std::decay_t<T>> &&
!char_v<std::decay_t<T>> && !bool_v<T>;

template <typename T>
inline constexpr bool float_v = std::is_floating_point_v<std::decay_t<T>>;

template <typename T> inline constexpr bool num_v = float_v<T> || int_v<T>;

template <typename T>
inline constexpr bool enum_v = std::is_enum_v<std::decay_t<T>>;

template <typename T>
constexpr inline bool optional_v =
is_template_instant_of<std::optional, std::remove_cvref_t<T>>::value;

template <class, class = void> struct is_container : std::false_type {};

template <class T>
struct is_container<
T, std::void_t<decltype(std::declval<T>().size(), std::declval<T>().begin(),
std::declval<T>().end())>> : std::true_type {};

template <class, class = void> struct is_map_container : std::false_type {};

template <class T>
struct is_map_container<
T, std::void_t<decltype(std::declval<typename T::mapped_type>())>>
: is_container<T> {};

template <typename T>
constexpr inline bool container_v = is_container<std::remove_cvref_t<T>>::value;

template <typename T>
constexpr inline bool map_container_v =
is_map_container<std::remove_cvref_t<T>>::value;

template <class T>
constexpr inline bool c_array_v = std::is_array_v<std::remove_cvref_t<T>> &&
std::extent_v<std::remove_cvref_t<T>> > 0;

template <typename Type, typename = void> struct is_array : std::false_type {};

template <typename T>
struct is_array<
T, std::void_t<decltype(std::declval<T>().size()),
typename std::enable_if_t<(std::tuple_size<T>::value != 0)>>>
: std::true_type {};

template <typename T>
constexpr inline bool array_v = is_array<std::remove_cvref_t<T>>::value;

template <typename Type>
constexpr inline bool fixed_array_v = c_array_v<Type> || array_v<Type>;

template <typename T>
constexpr inline bool string_view_v =
is_template_instant_of<std::basic_string_view,
std::remove_cvref_t<T>>::value;

template <typename T>
constexpr inline bool string_v =
is_template_instant_of<std::basic_string, std::remove_cvref_t<T>>::value;

// TODO: type must be char
template <typename T> constexpr inline bool json_view_v = container_v<T>;

template <typename T>
constexpr inline bool json_byte_v =
std::is_same_v<char, std::remove_cvref_t<T>> ||
std::is_same_v<unsigned char, std::remove_cvref_t<T>> ||
std::is_same_v<std::byte, std::remove_cvref_t<T>>;

template <typename T>
constexpr inline bool sequence_container_v =
is_sequence_container<std::remove_cvref_t<T>>::value;

template <typename T>
constexpr inline bool tuple_v = is_tuple<std::remove_cvref_t<T>>::value;

template <typename T>
constexpr inline bool string_container_v = string_v<T> || string_view_v<T>;

template <typename T>
constexpr inline bool unique_ptr_v =
is_template_instant_of<std::unique_ptr, std::remove_cvref_t<T>>::value;

template <typename T> struct is_variant : std::false_type {};

template <typename... T>
struct is_variant<std::variant<T...>> : std::true_type {};

template <typename T>
constexpr inline bool variant_v = is_variant<std::remove_cvref_t<T>>::value;

template <class T>
constexpr inline bool non_refletable_v =
container_v<T> || c_array_v<T> || tuple_v<T> || optional_v<T> ||
unique_ptr_v<T> || std::is_fundamental_v<std::remove_cvref_t<T>> ||
variant_v<T>;

template <typename T>
constexpr inline bool refletable_v = is_reflection_v<std::remove_cvref_t<T>>;

class numeric_str {
public:
std::string_view &value() { return val_; }
Expand All @@ -160,37 +34,6 @@ template <typename T>
constexpr inline bool numeric_str_v =
std::is_same_v<numeric_str, std::remove_cvref_t<T>>;

template <size_t N> struct string_literal {
static constexpr size_t size = (N > 0) ? (N - 1) : 0;

constexpr string_literal() = default;

constexpr string_literal(const char (&str)[N]) { std::copy_n(str, N, value); }

char value[N];
constexpr const char *end() const noexcept { return value + size; }

constexpr const std::string_view sv() const noexcept { return {value, size}; }
};

template <char... C, typename It> IGUANA_INLINE void match(It &&it, It &&end) {
const auto n = static_cast<size_t>(std::distance(it, end));
if (n < sizeof...(C))
IGUANA_UNLIKELY {
// TODO: compile time generate this message, currently borken with
// MSVC
static constexpr auto error = "Unexpected end of buffer. Expected:";
throw std::runtime_error(error);
}
if (((... || (*it++ != C))))
IGUANA_UNLIKELY {
// TODO: compile time generate this message, currently borken with
// MSVC
static constexpr char b[] = {C..., '\0'};
throw std::runtime_error(std::string("Expected these: ").append(b));
}
}

template <typename It> IGUANA_INLINE void skip_comment(It &&it, It &&end) {
++it;
if (it == end)
Expand Down
157 changes: 157 additions & 0 deletions iguana/util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#pragma once

#include "define.h"
#include "detail/charconv.h"
#include "enum_reflection.hpp"
#include "reflection.hpp"

#include <filesystem>
#include <forward_list>
#include <fstream>
#include <math.h>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string_view>
#include <type_traits>

namespace iguana {

template <typename T>
inline constexpr bool char_v = std::is_same_v<std::decay_t<T>, char> ||
std::is_same_v<std::decay_t<T>, char16_t> ||
std::is_same_v<std::decay_t<T>, char32_t> ||
std::is_same_v<std::decay_t<T>, wchar_t>;

template <typename T>
inline constexpr bool bool_v =
std::is_same_v<std::decay_t<T>, bool> ||
std::is_same_v<std::decay_t<T>, std::vector<bool>::reference>;

template <typename T>
inline constexpr bool int_v = std::is_integral_v<std::decay_t<T>> &&
!char_v<std::decay_t<T>> && !bool_v<T>;

template <typename T>
inline constexpr bool float_v = std::is_floating_point_v<std::decay_t<T>>;

template <typename T> inline constexpr bool num_v = float_v<T> || int_v<T>;

template <typename T>
inline constexpr bool enum_v = std::is_enum_v<std::decay_t<T>>;

template <typename T>
constexpr inline bool optional_v =
is_template_instant_of<std::optional, std::remove_cvref_t<T>>::value;

template <class, class = void> struct is_container : std::false_type {};

template <class T>
struct is_container<
T, std::void_t<decltype(std::declval<T>().size(), std::declval<T>().begin(),
std::declval<T>().end())>> : std::true_type {};

template <class, class = void> struct is_map_container : std::false_type {};

template <class T>
struct is_map_container<
T, std::void_t<decltype(std::declval<typename T::mapped_type>())>>
: is_container<T> {};

template <typename T>
constexpr inline bool container_v = is_container<std::remove_cvref_t<T>>::value;

template <typename T>
constexpr inline bool map_container_v =
is_map_container<std::remove_cvref_t<T>>::value;

template <class T>
constexpr inline bool c_array_v = std::is_array_v<std::remove_cvref_t<T>> &&
std::extent_v<std::remove_cvref_t<T>> > 0;

template <typename Type, typename = void> struct is_array : std::false_type {};

template <typename T>
struct is_array<
T, std::void_t<decltype(std::declval<T>().size()),
typename std::enable_if_t<(std::tuple_size<T>::value != 0)>>>
: std::true_type {};

template <typename T>
constexpr inline bool array_v = is_array<std::remove_cvref_t<T>>::value;

template <typename Type>
constexpr inline bool fixed_array_v = c_array_v<Type> || array_v<Type>;

template <typename T>
constexpr inline bool string_view_v =
is_template_instant_of<std::basic_string_view,
std::remove_cvref_t<T>>::value;

template <typename T>
constexpr inline bool string_v =
is_template_instant_of<std::basic_string, std::remove_cvref_t<T>>::value;

// TODO: type must be char
template <typename T> constexpr inline bool json_view_v = container_v<T>;

template <typename T>
constexpr inline bool json_byte_v =
std::is_same_v<char, std::remove_cvref_t<T>> ||
std::is_same_v<unsigned char, std::remove_cvref_t<T>> ||
std::is_same_v<std::byte, std::remove_cvref_t<T>>;

template <typename T>
constexpr inline bool sequence_container_v =
is_sequence_container<std::remove_cvref_t<T>>::value;

template <typename T>
constexpr inline bool tuple_v = is_tuple<std::remove_cvref_t<T>>::value;

template <typename T>
constexpr inline bool string_container_v = string_v<T> || string_view_v<T>;

template <typename T>
constexpr inline bool unique_ptr_v =
is_template_instant_of<std::unique_ptr, std::remove_cvref_t<T>>::value;

template <typename T> struct is_variant : std::false_type {};

template <typename... T>
struct is_variant<std::variant<T...>> : std::true_type {};

template <typename T>
constexpr inline bool variant_v = is_variant<std::remove_cvref_t<T>>::value;

template <class T>
constexpr inline bool non_refletable_v =
container_v<T> || c_array_v<T> || tuple_v<T> || optional_v<T> ||
unique_ptr_v<T> || std::is_fundamental_v<std::remove_cvref_t<T>> ||
variant_v<T>;

template <typename T>
constexpr inline bool refletable_v = is_reflection_v<std::remove_cvref_t<T>>;

template <typename T>
constexpr inline bool plain_v =
string_container_v<T> || num_v<T> || char_v<T> || bool_v<T> || enum_v<T>;

template <char... C, typename It> IGUANA_INLINE void match(It &&it, It &&end) {
const auto n = static_cast<size_t>(std::distance(it, end));
if (n < sizeof...(C))
IGUANA_UNLIKELY {
// TODO: compile time generate this message, currently borken with
// MSVC
static constexpr auto error = "Unexpected end of buffer. Expected:";
throw std::runtime_error(error);
}
if (((... || (*it++ != C))))
IGUANA_UNLIKELY {
// TODO: compile time generate this message, currently borken with
// MSVC
static constexpr char b[] = {C..., '\0'};
throw std::runtime_error(std::string("Expected these: ").append(b));
}
}

} // namespace iguana
Loading
Loading