mirror of
https://github.com/red0124/ssp.git
synced 2025-01-23 04:55:20 +01:00
initial commit, copy everything to new repository
This commit is contained in:
parent
2c0a35acf9
commit
9d45ff7b62
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
compile_commands.json
|
||||
.clang-format
|
||||
experiment/
|
361
include/ss/converter.hpp
Normal file
361
include/ss/converter.hpp
Normal file
@ -0,0 +1,361 @@
|
||||
#pragma once
|
||||
|
||||
#include "extract.hpp"
|
||||
#include "function_traits.hpp"
|
||||
#include "restrictions.hpp"
|
||||
#include "type_traits.hpp"
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
namespace ss {
|
||||
INIT_HAS_METHOD(tied);
|
||||
INIT_HAS_METHOD(ss_valid);
|
||||
INIT_HAS_METHOD(error);
|
||||
|
||||
////////////////
|
||||
// replace validator
|
||||
////////////////
|
||||
|
||||
// replace 'validator' types with elements they operate on
|
||||
// eg. no_validator_tup_t<int, ss::nx<char, 'A', 'B'>> <=> std::tuple<int, char>
|
||||
// where ss::nx<char, 'A', 'B'> is a validator '(n)one e(x)cept' which
|
||||
// checks if the returned character is either 'A' or 'B', returns error if not
|
||||
// additionaly if one element is left in the pack, it will be unwraped from
|
||||
// the tuple eg. no_void_validator_tup_t<int> <=> int instead of std::tuple<int>
|
||||
template <typename T, typename U = void>
|
||||
struct no_validator;
|
||||
|
||||
template <typename T>
|
||||
struct no_validator<T, typename std::enable_if_t<has_m_ss_valid_t<T>>> {
|
||||
using type = typename member_wrapper<decltype(&T::ss_valid)>::arg_type;
|
||||
};
|
||||
|
||||
template <typename T, typename U>
|
||||
struct no_validator {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using no_validator_t = typename no_validator<T>::type;
|
||||
|
||||
template <typename... Ts>
|
||||
struct no_validator_tup {
|
||||
using type =
|
||||
typename apply_trait<no_validator, std::tuple<Ts...>>::type;
|
||||
};
|
||||
|
||||
template <typename... Ts>
|
||||
struct no_validator_tup<std::tuple<Ts...>> {
|
||||
using type = typename no_validator_tup<Ts...>::type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct no_validator_tup<std::tuple<T>> {
|
||||
using type = no_validator_t<T>;
|
||||
};
|
||||
|
||||
template <typename... Ts>
|
||||
using no_validator_tup_t = typename no_validator_tup<Ts...>::type;
|
||||
|
||||
////////////////
|
||||
// no void tuple
|
||||
////////////////
|
||||
|
||||
template <typename... Ts>
|
||||
struct no_void_tup {
|
||||
using type =
|
||||
typename filter_not<std::is_void, no_validator_tup_t<Ts...>>::type;
|
||||
};
|
||||
|
||||
template <typename... Ts>
|
||||
using no_void_tup_t = filter_not_t<std::is_void, Ts...>;
|
||||
|
||||
////////////////
|
||||
// no void or validator
|
||||
////////////////
|
||||
|
||||
// replace 'validators' and remove void from tuple
|
||||
template <typename... Ts>
|
||||
struct no_void_validator_tup {
|
||||
using type = no_validator_tup_t<no_void_tup_t<Ts...>>;
|
||||
};
|
||||
|
||||
template <typename... Ts>
|
||||
using no_void_validator_tup_t = typename no_void_validator_tup<Ts...>::type;
|
||||
|
||||
////////////////
|
||||
// tied class
|
||||
////////////////
|
||||
|
||||
// check if parameter pack is only one element which is a class and has
|
||||
// the 'tied' method which is to be used for type deduction when converting
|
||||
template <typename T, typename... Ts>
|
||||
struct tied_class {
|
||||
constexpr static bool value =
|
||||
(sizeof...(Ts) == 0 && std::is_class_v<T> && has_m_tied<T>::value);
|
||||
};
|
||||
|
||||
template <typename... Ts>
|
||||
constexpr bool tied_class_v = tied_class<Ts...>::value;
|
||||
|
||||
////////////////
|
||||
// converter
|
||||
////////////////
|
||||
|
||||
class converter {
|
||||
using string_range = std::pair<const char*, const char*>;
|
||||
using split_input = std::vector<string_range>;
|
||||
constexpr static auto default_delimiter = ',';
|
||||
|
||||
public:
|
||||
// parses line with given delimiter, returns a 'T' object created with
|
||||
// extracted values of type 'Ts'
|
||||
template <typename T, typename... Ts>
|
||||
T convert_struct(const char* const line,
|
||||
const std::string& delim = "") {
|
||||
return to_struct<T>(convert<Ts...>(line, delim));
|
||||
}
|
||||
|
||||
// parses line with given delimiter, returns tuple of objects with
|
||||
// extracted values of type 'Ts'
|
||||
template <typename... Ts>
|
||||
no_void_validator_tup_t<Ts...> convert(const char* const line,
|
||||
const std::string& delim = "") {
|
||||
input_ = split(line, delim);
|
||||
return convert<Ts...>(input_);
|
||||
}
|
||||
|
||||
// parses already split line, returns 'T' object with extracted values
|
||||
template <typename T, typename... Ts>
|
||||
T convert_struct(const split_input& elems) {
|
||||
return to_struct<T>(convert<Ts...>(elems));
|
||||
}
|
||||
|
||||
// parses already split line, returns either a tuple of objects with
|
||||
// parsed values (returns raw element (no tuple) if Ts is empty), or if
|
||||
// one argument is given which is a class which has a tied
|
||||
// method which returns a tuple, returns that type
|
||||
template <typename T, typename... Ts>
|
||||
no_void_validator_tup_t<T, Ts...> convert(const split_input& elems) {
|
||||
if constexpr (tied_class_v<T, Ts...>) {
|
||||
using arg_ref_tuple =
|
||||
typename std::result_of_t<decltype (&T::tied)(T)>;
|
||||
|
||||
using arg_tuple =
|
||||
typename apply_trait<std::decay,
|
||||
arg_ref_tuple>::type;
|
||||
|
||||
return to_struct<T>(
|
||||
convert_impl(elems, (arg_tuple*){}));
|
||||
} else {
|
||||
return convert_impl<T, Ts...>(elems);
|
||||
}
|
||||
}
|
||||
|
||||
bool valid() const {
|
||||
return error_.empty();
|
||||
}
|
||||
|
||||
const std::string& error() const {
|
||||
return error_;
|
||||
}
|
||||
|
||||
// 'splits' string by given delimiter, returns vector of pairs which
|
||||
// contain the beginings and the ends of each column of the string
|
||||
const split_input& split(const char* const line,
|
||||
const std::string& delim = "") {
|
||||
input_.clear();
|
||||
if (line[0] == '\0') {
|
||||
return input_;
|
||||
}
|
||||
|
||||
switch (delim.size()) {
|
||||
case 0:
|
||||
return split_impl(line, ',');
|
||||
case 1:
|
||||
return split_impl(line, delim[0]);
|
||||
default:
|
||||
return split_impl(line, delim, delim.size());
|
||||
};
|
||||
}
|
||||
|
||||
private:
|
||||
////////////////
|
||||
// error
|
||||
////////////////
|
||||
|
||||
std::string error_sufix(const string_range msg, size_t pos) const {
|
||||
std::string error;
|
||||
error.reserve(32);
|
||||
error.append("at column ")
|
||||
.append(std::to_string(pos + 1))
|
||||
.append(": \'")
|
||||
.append(msg.first, msg.second)
|
||||
.append("\'");
|
||||
return error;
|
||||
}
|
||||
|
||||
void set_error_invalid_conversion(const string_range msg, size_t pos) {
|
||||
error_.clear();
|
||||
error_.append("invalid conversion for parameter ")
|
||||
.append(error_sufix(msg, pos));
|
||||
}
|
||||
|
||||
void set_error_validate(const char* const error, const string_range msg,
|
||||
size_t pos) {
|
||||
error_.clear();
|
||||
error_.append(error).append(" ").append(error_sufix(msg, pos));
|
||||
}
|
||||
|
||||
void set_error_number_of_colums(size_t expected_pos, size_t pos) {
|
||||
error_.append("invalid number of columns, expected: ")
|
||||
.append(std::to_string(expected_pos))
|
||||
.append(", got: ")
|
||||
.append(std::to_string(pos));
|
||||
}
|
||||
|
||||
////////////////
|
||||
// convert implementation
|
||||
////////////////
|
||||
|
||||
template <typename... Ts>
|
||||
no_void_validator_tup_t<Ts...> convert_impl(const split_input& elems) {
|
||||
error_.clear();
|
||||
no_void_validator_tup_t<Ts...> ret{};
|
||||
if (sizeof...(Ts) != elems.size()) {
|
||||
set_error_number_of_colums(sizeof...(Ts), elems.size());
|
||||
return ret;
|
||||
}
|
||||
return extract_tuple<Ts...>(elems);
|
||||
}
|
||||
|
||||
// do not know how to specialize by return type :(
|
||||
template <typename... Ts>
|
||||
no_void_validator_tup_t<std::tuple<Ts...>> convert_impl(
|
||||
const split_input& elems, const std::tuple<Ts...>*) {
|
||||
return convert_impl<Ts...>(elems);
|
||||
}
|
||||
|
||||
////////////////
|
||||
// substring
|
||||
////////////////
|
||||
|
||||
template <typename Delim>
|
||||
const split_input& split_impl(const char* const line, Delim delim,
|
||||
size_t delim_size = 1) {
|
||||
auto range = substring(line, delim);
|
||||
input_.push_back(range);
|
||||
while (range.second[0] != '\0') {
|
||||
range = substring(range.second + delim_size, delim);
|
||||
input_.push_back(range);
|
||||
}
|
||||
return input_;
|
||||
}
|
||||
|
||||
bool no_match(const char* end, char delim) const {
|
||||
return *end != delim;
|
||||
}
|
||||
|
||||
bool no_match(const char* end, const std::string& delim) const {
|
||||
return strncmp(end, delim.c_str(), delim.size()) != 0;
|
||||
}
|
||||
|
||||
template <typename Delim>
|
||||
string_range substring(const char* const begin, Delim delim) const {
|
||||
const char* end;
|
||||
for (end = begin; *end != '\0' && no_match(end, delim); ++end)
|
||||
;
|
||||
|
||||
return string_range{begin, end};
|
||||
}
|
||||
|
||||
////////////////
|
||||
// conversion
|
||||
////////////////
|
||||
|
||||
#ifdef SS_THROW_ON_INVALID
|
||||
#define SS_RETURN_ON_INVALID // nop
|
||||
#else
|
||||
#define SS_RETURN_ON_INVALID \
|
||||
if (!valid()) { \
|
||||
return; \
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
void extract_one(no_validator_t<T>& dst, const string_range msg,
|
||||
size_t pos) {
|
||||
SS_RETURN_ON_INVALID;
|
||||
|
||||
if (!extract(msg.first, msg.second, dst)) {
|
||||
set_error_invalid_conversion(msg, pos);
|
||||
return;
|
||||
}
|
||||
|
||||
if constexpr (has_m_ss_valid_t<T>) {
|
||||
if (T validator; !validator.ss_valid(dst)) {
|
||||
if constexpr (has_m_error_t<T>) {
|
||||
set_error_validate(validator.error(),
|
||||
msg, pos);
|
||||
} else {
|
||||
set_error_validate("validation error",
|
||||
msg, pos);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void extract_one<std::string>(std::string& dst, const string_range msg,
|
||||
size_t) {
|
||||
SS_RETURN_ON_INVALID;
|
||||
extract(msg.first, msg.second, dst);
|
||||
}
|
||||
|
||||
#undef SS_RETURN_ON_INVALID
|
||||
|
||||
template <size_t ArgN, size_t TupN, typename... Ts>
|
||||
void extract_multiple(no_void_validator_tup_t<Ts...>& tup,
|
||||
const split_input& elems) {
|
||||
using elem_t = std::tuple_element_t<ArgN, std::tuple<Ts...>>;
|
||||
|
||||
constexpr bool not_void = !std::is_void_v<elem_t>;
|
||||
constexpr bool not_tuple =
|
||||
count_not<std::is_void, Ts...>::size == 1;
|
||||
|
||||
if constexpr (not_void) {
|
||||
if constexpr (not_tuple) {
|
||||
extract_one<elem_t>(tup, elems[ArgN], ArgN);
|
||||
} else {
|
||||
auto& el = std::get<TupN>(tup);
|
||||
extract_one<elem_t>(el, elems[ArgN], ArgN);
|
||||
}
|
||||
}
|
||||
|
||||
if constexpr (sizeof...(Ts) > ArgN + 1) {
|
||||
constexpr size_t NewTupN = (not_void) ? TupN + 1 : TupN;
|
||||
extract_multiple<ArgN + 1, NewTupN, Ts...>(tup, elems);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
no_void_validator_tup_t<Ts...> extract_tuple(const split_input& elems) {
|
||||
static_assert(!all_of<std::is_void, Ts...>::value,
|
||||
"at least one parameter must be non void");
|
||||
|
||||
no_void_validator_tup_t<Ts...> ret;
|
||||
extract_multiple<0, 0, Ts...>(ret, elems);
|
||||
return ret;
|
||||
};
|
||||
|
||||
////////////////
|
||||
// members
|
||||
////////////////
|
||||
|
||||
std::vector<string_range> input_;
|
||||
std::string error_;
|
||||
};
|
||||
|
||||
} /* ss */
|
391
include/ss/extract.hpp
Normal file
391
include/ss/extract.hpp
Normal file
@ -0,0 +1,391 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#ifdef SS_THROW_ON_INVALID
|
||||
#define SS_THROW_OR_NULL(x) throw std::invalid_argument(x)
|
||||
#define SS_THROW_OR_FALSE(x) throw std::invalid_argument(x)
|
||||
#else
|
||||
#define SS_THROW_OR_NULL(x) return std::nullopt
|
||||
#define SS_THROW_OR_FALSE(x) return false
|
||||
#endif
|
||||
|
||||
namespace ss {
|
||||
|
||||
// todo
|
||||
// taken from
|
||||
// https://gist.github.com/oschonrock/67fc870ba067ebf0f369897a9d52c2dd
|
||||
|
||||
////////////////
|
||||
// number converters
|
||||
////////////////
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_floating_point_v<T>, T> pow10(int n) {
|
||||
T ret = 1.0;
|
||||
T r = 10.0;
|
||||
if (n < 0) {
|
||||
n = -n;
|
||||
r = 0.1;
|
||||
}
|
||||
|
||||
while (n) {
|
||||
if (n & 1) {
|
||||
ret *= r;
|
||||
}
|
||||
r *= r;
|
||||
n >>= 1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
||||
const char* begin, const char* const end) {
|
||||
if (begin == end) {
|
||||
SS_THROW_OR_NULL("floating point");
|
||||
}
|
||||
int sign = 1;
|
||||
T int_part = 0.0;
|
||||
T frac_part = 0.0;
|
||||
bool has_frac = false;
|
||||
bool has_exp = false;
|
||||
|
||||
// +/- sign
|
||||
if (*begin == '-') {
|
||||
++begin;
|
||||
sign = -1;
|
||||
}
|
||||
|
||||
while (begin != end) {
|
||||
if (*begin >= '0' && *begin <= '9') {
|
||||
int_part = int_part * 10 + (*begin - '0');
|
||||
} else if (*begin == '.') {
|
||||
has_frac = true;
|
||||
++begin;
|
||||
break;
|
||||
} else if (*begin == 'e') {
|
||||
has_exp = true;
|
||||
++begin;
|
||||
break;
|
||||
} else {
|
||||
SS_THROW_OR_NULL("floating point");
|
||||
}
|
||||
++begin;
|
||||
}
|
||||
|
||||
if (has_frac) {
|
||||
T frac_exp = 0.1;
|
||||
|
||||
while (begin != end) {
|
||||
if (*begin >= '0' && *begin <= '9') {
|
||||
frac_part += frac_exp * (*begin - '0');
|
||||
frac_exp *= 0.1;
|
||||
} else if (*begin == 'e') {
|
||||
has_exp = true;
|
||||
++begin;
|
||||
break;
|
||||
} else {
|
||||
SS_THROW_OR_NULL("floating point");
|
||||
}
|
||||
++begin;
|
||||
}
|
||||
}
|
||||
|
||||
// parsing exponent part
|
||||
T exp_part = 1.0;
|
||||
if (begin != end && has_exp) {
|
||||
int exp_sign = 1;
|
||||
if (*begin == '-') {
|
||||
exp_sign = -1;
|
||||
++begin;
|
||||
} else if (*begin == '+') {
|
||||
++begin;
|
||||
}
|
||||
|
||||
int e = 0;
|
||||
while (begin != end && *begin >= '0' && *begin <= '9') {
|
||||
e = e * 10 + *begin - '0';
|
||||
++begin;
|
||||
}
|
||||
|
||||
exp_part = pow10<T>(exp_sign * e);
|
||||
}
|
||||
|
||||
if (begin != end) {
|
||||
SS_THROW_OR_NULL("floating point");
|
||||
}
|
||||
|
||||
return sign * (int_part + frac_part) * exp_part;
|
||||
}
|
||||
|
||||
inline std::optional<short> from_char(char c) {
|
||||
if (c >= '0' && c <= '9') {
|
||||
return c - '0';
|
||||
}
|
||||
SS_THROW_OR_NULL("integral");
|
||||
}
|
||||
|
||||
#if defined(__clang__) || defined(__GNUC__) || defined(__GUNG__)
|
||||
////////////////
|
||||
// mul overflow detection
|
||||
////////////////
|
||||
template <typename T>
|
||||
bool mul_overflow(T& result, T operand) {
|
||||
return __builtin_mul_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool mul_overflow(int& result, int operand) {
|
||||
return __builtin_smul_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool mul_overflow(long& result, long operand) {
|
||||
return __builtin_smull_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool mul_overflow(long long& result, long long operand) {
|
||||
return __builtin_smulll_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool mul_overflow(unsigned int& result, unsigned int operand) {
|
||||
return __builtin_umul_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool mul_overflow(unsigned long& result, unsigned long operand) {
|
||||
return __builtin_umull_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool mul_overflow(unsigned long long& result,
|
||||
unsigned long long operand) {
|
||||
return __builtin_umulll_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
////////////////
|
||||
// addition overflow detection
|
||||
////////////////
|
||||
|
||||
template <typename T>
|
||||
inline bool add_overflow(T& result, T operand) {
|
||||
return __builtin_add_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool add_overflow(int& result, int operand) {
|
||||
return __builtin_sadd_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool add_overflow(long& result, long operand) {
|
||||
return __builtin_saddl_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool add_overflow(long long& result, long long operand) {
|
||||
return __builtin_saddll_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool add_overflow(unsigned int& result, unsigned int operand) {
|
||||
return __builtin_uadd_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool add_overflow(unsigned long& result, unsigned long operand) {
|
||||
return __builtin_uaddl_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool add_overflow(unsigned long long& result,
|
||||
unsigned long long operand) {
|
||||
return __builtin_uaddll_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
////////////////
|
||||
// substraction overflow detection
|
||||
////////////////
|
||||
template <typename T>
|
||||
inline bool sub_overflow(T& result, T operand) {
|
||||
return __builtin_sub_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool sub_overflow(int& result, int operand) {
|
||||
return __builtin_ssub_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool sub_overflow(long& result, long operand) {
|
||||
return __builtin_ssubl_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool sub_overflow(long long& result, long long operand) {
|
||||
return __builtin_ssubll_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool sub_overflow(unsigned int& result, unsigned int operand) {
|
||||
return __builtin_usub_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool sub_overflow(unsigned long& result, unsigned long operand) {
|
||||
return __builtin_usubl_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool sub_overflow(unsigned long long& result,
|
||||
unsigned long long operand) {
|
||||
return __builtin_usubll_overflow(result, operand, &result);
|
||||
}
|
||||
|
||||
template <typename T, typename F>
|
||||
bool shift_and_add_overflow(T& value, T digit, F add_last_digit_owerflow) {
|
||||
if (mul_overflow<T>(value, 10) ||
|
||||
add_last_digit_owerflow(value, digit)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
|
||||
#warning "use clang or gcc!!!"
|
||||
template <typename T, typename U>
|
||||
bool shift_and_add_overflow(T& value, T digit, U is_negative) {
|
||||
digit = (is_negative) ? -digit : digit;
|
||||
T old_value = value;
|
||||
value = 10 * value + digit;
|
||||
|
||||
T expected_old_value = (value - digit) / 10;
|
||||
if (old_value != expected_old_value) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_integral_v<T>, std::optional<T>> to_num(
|
||||
const char* begin, const char* end) {
|
||||
if (begin == end) {
|
||||
SS_THROW_OR_NULL("integral");
|
||||
}
|
||||
bool is_negative = false;
|
||||
if constexpr (std::is_signed_v<T>) {
|
||||
is_negative = *begin == '-';
|
||||
if (is_negative) {
|
||||
++begin;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
|
||||
auto add_last_digit_owerflow =
|
||||
(is_negative) ? sub_overflow<T> : add_overflow<T>;
|
||||
#else
|
||||
auto add_last_digit_owerflow = is_negative;
|
||||
#endif
|
||||
|
||||
T value = 0;
|
||||
for (auto i = begin; i != end; ++i) {
|
||||
if (auto digit = from_char(*i);
|
||||
!digit ||
|
||||
shift_and_add_overflow<T>(value, digit.value(),
|
||||
add_last_digit_owerflow)) {
|
||||
SS_THROW_OR_NULL("integral");
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////
|
||||
// extract
|
||||
////////////////
|
||||
|
||||
namespace error {
|
||||
template <typename T>
|
||||
struct unsupported_type {
|
||||
constexpr static bool value = false;
|
||||
};
|
||||
} /* namespace */
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<!std::is_integral_v<T> && !std::is_floating_point_v<T>, bool>
|
||||
extract(const char*, const char*, T&) {
|
||||
|
||||
static_assert(error::unsupported_type<T>::value,
|
||||
"Conversion for given type is not defined, an "
|
||||
"\'extract\' function needs to be defined!");
|
||||
}
|
||||
|
||||
////////////////
|
||||
// extract specialization
|
||||
////////////////
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_integral_v<T> || std::is_floating_point_v<T>, bool>
|
||||
extract(const char* begin, const char* end, T& value) {
|
||||
auto optional_value = to_num<T>(begin, end);
|
||||
#ifndef SS_THROW_ON_INVALID
|
||||
if (!optional_value) {
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
value = optional_value.value();
|
||||
return true;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool extract(const char* begin, const char* end, bool& value) {
|
||||
if (end == begin + 1) {
|
||||
if (*begin == '1') {
|
||||
value = true;
|
||||
return true;
|
||||
} else if (*begin == '0') {
|
||||
value = false;
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
size_t size = end - begin;
|
||||
if (size == 4 && strncmp(begin, "true", size) == 0) {
|
||||
value = true;
|
||||
return true;
|
||||
} else if (size == 5 && strncmp(begin, "false", size) == 0) {
|
||||
value = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
SS_THROW_OR_FALSE("boolean");
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool extract(const char* begin, const char* end, char& value) {
|
||||
value = *begin;
|
||||
if (end != begin + 1) {
|
||||
SS_THROW_OR_FALSE("character");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool extract(const char* begin, const char* end, std::string& value) {
|
||||
value = std::string(begin, end);
|
||||
return true;
|
||||
}
|
||||
|
||||
#undef SS_THROW_OR_NULL
|
||||
#undef SS_THROW_OR_FALSE
|
||||
|
||||
} /* ss */
|
80
include/ss/function_traits.hpp
Normal file
80
include/ss/function_traits.hpp
Normal file
@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
|
||||
namespace ss {
|
||||
|
||||
////////////////
|
||||
// function traits
|
||||
////////////////
|
||||
|
||||
template <size_t N, typename T, typename... Ts>
|
||||
struct decayed_arg_n {
|
||||
static_assert(N - 1 != sizeof...(Ts), "index out of range");
|
||||
using type = typename decayed_arg_n<N - 1, Ts...>::type;
|
||||
};
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
struct decayed_arg_n<0, T, Ts...> {
|
||||
using type = std::decay_t<T>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct function_traits;
|
||||
|
||||
template <typename R, typename C, typename Arg>
|
||||
struct function_traits<std::function<R(C&, const Arg&) const>> {
|
||||
using arg_type = Arg;
|
||||
};
|
||||
|
||||
template <typename R, typename... Ts>
|
||||
struct function_traits<R(Ts...)> {
|
||||
using arg0 = typename decayed_arg_n<0, Ts...>::type;
|
||||
};
|
||||
|
||||
template <typename R, typename... Ts>
|
||||
struct function_traits<R(Ts...) const> : function_traits<R(Ts...)> {};
|
||||
|
||||
template <typename R, typename... Ts>
|
||||
struct function_traits<R(Ts...)&> : function_traits<R(Ts...)> {};
|
||||
|
||||
template <typename R, typename... Ts>
|
||||
struct function_traits<R(Ts...) const&> : function_traits<R(Ts...)> {};
|
||||
|
||||
template <typename R, typename... Ts>
|
||||
struct function_traits<R(Ts...) &&> : function_traits<R(Ts...)> {};
|
||||
|
||||
template <typename R, typename... Ts>
|
||||
struct function_traits<R(Ts...) const&&> : function_traits<R(Ts...)> {};
|
||||
|
||||
template <typename MemberFunction>
|
||||
struct member_wrapper;
|
||||
|
||||
template <typename R, typename T>
|
||||
struct member_wrapper<R T::*> {
|
||||
using arg_type = typename function_traits<R>::arg0;
|
||||
};
|
||||
|
||||
////////////////
|
||||
// has method
|
||||
////////////////
|
||||
|
||||
#define INIT_HAS_METHOD(method) \
|
||||
template <typename T> \
|
||||
class has_m_##method { \
|
||||
template <typename C> \
|
||||
static std::true_type test(decltype(&C::method)); \
|
||||
\
|
||||
template <typename C> \
|
||||
static std::false_type test(...); \
|
||||
\
|
||||
public: \
|
||||
constexpr static bool value = decltype(test<T>(0))::value; \
|
||||
}; \
|
||||
\
|
||||
template <typename T> \
|
||||
constexpr bool has_m_##method##_t = has_m_##method<T>::value;
|
||||
|
||||
} /* trait */
|
140
include/ss/parser.hpp
Normal file
140
include/ss/parser.hpp
Normal file
@ -0,0 +1,140 @@
|
||||
#pragma once
|
||||
|
||||
#include "converter.hpp"
|
||||
#include "extract.hpp"
|
||||
#include "restrictions.hpp"
|
||||
#include <cstring>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace ss {
|
||||
|
||||
class parser {
|
||||
public:
|
||||
parser(const std::string& file_name, const std::string& delimiter)
|
||||
: file_name_{file_name}, delim_{delimiter},
|
||||
file_{fopen(file_name_.c_str(), "r")} {
|
||||
if (file_) {
|
||||
read_line();
|
||||
} else {
|
||||
set_error_file_not_open();
|
||||
eof_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
~parser() {
|
||||
fclose(file_);
|
||||
}
|
||||
|
||||
bool valid() const {
|
||||
return error_.empty();
|
||||
}
|
||||
|
||||
bool eof() const {
|
||||
return eof_;
|
||||
}
|
||||
|
||||
bool ignore_next() {
|
||||
return buff_.read(file_);
|
||||
}
|
||||
|
||||
const std::string& error() const {
|
||||
return error_;
|
||||
}
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
T get_struct() {
|
||||
return to_struct<T>(get_next<Ts...>());
|
||||
}
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
no_void_validator_tup_t<T, Ts...> get_next() {
|
||||
error_.clear();
|
||||
if (eof_) {
|
||||
set_error_eof_reached();
|
||||
return {};
|
||||
}
|
||||
|
||||
auto value = converter_.convert<T, Ts...>(buff_.get(), delim_);
|
||||
|
||||
if (!converter_.valid()) {
|
||||
set_error_invalid_conversion();
|
||||
}
|
||||
|
||||
read_line();
|
||||
return value;
|
||||
}
|
||||
|
||||
private:
|
||||
////////////////
|
||||
// line reading
|
||||
////////////////
|
||||
|
||||
class buffer {
|
||||
char* buffer_{nullptr};
|
||||
size_t size_{0};
|
||||
|
||||
public:
|
||||
~buffer() {
|
||||
free(buffer_);
|
||||
}
|
||||
|
||||
bool read(FILE* file) {
|
||||
ssize_t size = getline(&buffer_, &size_, file);
|
||||
if (size == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer_[size - 1] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* get() const {
|
||||
return buffer_;
|
||||
}
|
||||
};
|
||||
|
||||
void read_line() {
|
||||
eof_ = !buff_.read(file_);
|
||||
++line_number_;
|
||||
}
|
||||
|
||||
////////////////
|
||||
// error
|
||||
////////////////
|
||||
|
||||
void set_error_file_not_open() {
|
||||
error_.append(file_name_).append(" could not be not open.");
|
||||
}
|
||||
|
||||
void set_error_eof_reached() {
|
||||
error_.append(file_name_).append(" reached end of file.");
|
||||
}
|
||||
|
||||
void set_error_invalid_conversion() {
|
||||
error_.append(file_name_)
|
||||
.append(" ")
|
||||
.append(std::to_string(line_number_))
|
||||
.append(": ")
|
||||
.append(converter_.error())
|
||||
.append(": \"")
|
||||
.append(buff_.get());
|
||||
error_.append("\"");
|
||||
}
|
||||
|
||||
////////////////
|
||||
// members
|
||||
////////////////
|
||||
|
||||
const std::string file_name_;
|
||||
const std::string delim_;
|
||||
std::string error_;
|
||||
ss::converter converter_;
|
||||
FILE* file_{nullptr};
|
||||
buffer buff_;
|
||||
size_t line_number_{0};
|
||||
bool eof_{false};
|
||||
};
|
||||
|
||||
} /* ss */
|
100
include/ss/restrictions.hpp
Normal file
100
include/ss/restrictions.hpp
Normal file
@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
|
||||
namespace ss {
|
||||
|
||||
////////////////
|
||||
// all except
|
||||
////////////////
|
||||
|
||||
template <typename T, auto... Values>
|
||||
struct ax {
|
||||
private:
|
||||
template <auto X, auto... Xs>
|
||||
bool ss_valid_impl(const T& x) const {
|
||||
if constexpr (sizeof...(Xs) != 0) {
|
||||
return x != X && ss_valid_impl<Xs...>(x);
|
||||
}
|
||||
return x != X;
|
||||
}
|
||||
|
||||
public:
|
||||
bool ss_valid(const T& value) const {
|
||||
return ss_valid_impl<Values...>(value);
|
||||
}
|
||||
|
||||
const char* error() const {
|
||||
return "value excluded";
|
||||
}
|
||||
};
|
||||
|
||||
////////////////
|
||||
// none except
|
||||
////////////////
|
||||
|
||||
template <typename T, auto... Values>
|
||||
struct nx {
|
||||
private:
|
||||
template <auto X, auto... Xs>
|
||||
bool ss_valid_impl(const T& x) const {
|
||||
if constexpr (sizeof...(Xs) != 0) {
|
||||
return x == X || ss_valid_impl<Xs...>(x);
|
||||
}
|
||||
return x == X;
|
||||
}
|
||||
|
||||
public:
|
||||
bool ss_valid(const T& value) const {
|
||||
return ss_valid_impl<Values...>(value);
|
||||
}
|
||||
|
||||
const char* error() const {
|
||||
return "value excluded";
|
||||
}
|
||||
};
|
||||
|
||||
////////////////
|
||||
// in range
|
||||
////////////////
|
||||
|
||||
template <typename T, auto Min, auto Max>
|
||||
struct ir {
|
||||
bool ss_valid(const T& value) const {
|
||||
return value >= Min && value <= Max;
|
||||
}
|
||||
|
||||
const char* error() const {
|
||||
return "out of range";
|
||||
}
|
||||
};
|
||||
|
||||
////////////////
|
||||
// out of range
|
||||
////////////////
|
||||
|
||||
template <typename T, auto Min, auto Max>
|
||||
struct oor {
|
||||
bool ss_valid(const T& value) const {
|
||||
return value < Min || value > Max;
|
||||
}
|
||||
|
||||
const char* error() const {
|
||||
return "in restricted range";
|
||||
}
|
||||
};
|
||||
|
||||
////////////////
|
||||
// non empty
|
||||
////////////////
|
||||
|
||||
template <typename T>
|
||||
struct ne {
|
||||
bool ss_valid(const T& value) const {
|
||||
return !value.empty();
|
||||
}
|
||||
|
||||
const char* error() const {
|
||||
return "empty field";
|
||||
}
|
||||
};
|
||||
|
||||
} /* ss */
|
329
include/ss/type_traits.hpp
Normal file
329
include/ss/type_traits.hpp
Normal file
@ -0,0 +1,329 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdlib>
|
||||
#include <tuple>
|
||||
|
||||
namespace ss {
|
||||
|
||||
////////////////
|
||||
// tup merge/cat
|
||||
////////////////
|
||||
|
||||
template <typename T, typename Ts>
|
||||
struct tup_cat;
|
||||
|
||||
template <typename... Ts, typename... Us>
|
||||
struct tup_cat<std::tuple<Ts...>, std::tuple<Us...>> {
|
||||
using type = std::tuple<Ts..., Us...>;
|
||||
};
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
struct tup_cat<T, std::tuple<Ts...>> {
|
||||
using type = std::tuple<T, Ts...>;
|
||||
};
|
||||
|
||||
template <typename... Ts>
|
||||
using tup_cat_t = typename tup_cat<Ts...>::type;
|
||||
|
||||
////////////////
|
||||
// tup first/head
|
||||
////////////////
|
||||
|
||||
template <size_t N, typename T, typename... Ts>
|
||||
struct left_of_impl;
|
||||
|
||||
template <size_t N, typename T, typename... Ts>
|
||||
struct left_of_impl {
|
||||
static_assert(N < 128, "recursion limit reached");
|
||||
static_assert(N != 0, "cannot take the whole tuple");
|
||||
using type = tup_cat_t<T, typename left_of_impl<N - 1, Ts...>::type>;
|
||||
};
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
struct left_of_impl<0, T, Ts...> {
|
||||
using type = std::tuple<T>;
|
||||
};
|
||||
|
||||
template <size_t N, typename... Ts>
|
||||
using left_of_t = typename left_of_impl<N, Ts...>::type;
|
||||
|
||||
template <typename... Ts>
|
||||
using first_t = typename left_of_impl<sizeof...(Ts) - 2, Ts...>::type;
|
||||
|
||||
template <typename... Ts>
|
||||
using head_t = typename left_of_impl<0, Ts...>::type;
|
||||
|
||||
////////////////
|
||||
// tup tail/last
|
||||
////////////////
|
||||
|
||||
template <size_t N, typename T, typename... Ts>
|
||||
struct right_of_impl;
|
||||
|
||||
template <size_t N, typename T, typename... Ts>
|
||||
struct right_of_impl {
|
||||
using type = typename right_of_impl<N - 1, Ts...>::type;
|
||||
};
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
struct right_of_impl<0, T, Ts...> {
|
||||
using type = std::tuple<T, Ts...>;
|
||||
};
|
||||
|
||||
template <size_t N, typename... Ts>
|
||||
using right_of_t = typename right_of_impl<N, Ts...>::type;
|
||||
|
||||
template <typename... Ts>
|
||||
using tail_t = typename right_of_impl<1, Ts...>::type;
|
||||
|
||||
template <typename... Ts>
|
||||
using last_t = typename right_of_impl<sizeof...(Ts) - 1, Ts...>::type;
|
||||
|
||||
////////////////
|
||||
// apply trait
|
||||
////////////////
|
||||
|
||||
template <template <typename...> class Trait, typename T>
|
||||
struct apply_trait;
|
||||
|
||||
template <template <typename...> class Trait, typename T, typename... Ts>
|
||||
struct apply_trait<Trait, std::tuple<T, Ts...>> {
|
||||
using type =
|
||||
tup_cat_t<typename Trait<T>::type,
|
||||
typename apply_trait<Trait, std::tuple<Ts...>>::type>;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename T>
|
||||
struct apply_trait {
|
||||
using type = std::tuple<typename Trait<T>::type>;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename T>
|
||||
struct apply_trait<Trait, std::tuple<T>> {
|
||||
using type = std::tuple<typename Trait<T>::type>;
|
||||
};
|
||||
|
||||
////////////////
|
||||
// apply optional trait
|
||||
////////////////
|
||||
|
||||
// type is T if true, and std::false_type othervise
|
||||
template <typename T, typename U>
|
||||
struct optional_trait;
|
||||
|
||||
template <typename U>
|
||||
struct optional_trait<std::true_type, U> {
|
||||
using type = U;
|
||||
};
|
||||
|
||||
template <typename U>
|
||||
struct optional_trait<std::false_type, U> {
|
||||
using type = std::false_type;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename T>
|
||||
struct apply_optional_trait;
|
||||
|
||||
template <template <typename...> class Trait, typename T, typename... Ts>
|
||||
struct apply_optional_trait<Trait, std::tuple<T, Ts...>> {
|
||||
using type = tup_cat_t<
|
||||
typename optional_trait<typename Trait<T>::type, T>::type,
|
||||
typename apply_optional_trait<Trait, std::tuple<Ts...>>::type>;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename T>
|
||||
struct apply_optional_trait {
|
||||
using type = std::tuple<
|
||||
typename optional_trait<typename Trait<T>::type, T>::type>;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename T>
|
||||
struct apply_optional_trait<Trait, std::tuple<T>> {
|
||||
using type = std::tuple<
|
||||
typename optional_trait<typename Trait<T>::type, T>::type>;
|
||||
};
|
||||
|
||||
////////////////
|
||||
// filter false_type
|
||||
////////////////
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
struct remove_false {
|
||||
using type = tup_cat_t<T, typename remove_false<Ts...>::type>;
|
||||
};
|
||||
|
||||
template <typename... Ts>
|
||||
struct remove_false<std::false_type, Ts...> {
|
||||
using type = typename remove_false<Ts...>::type;
|
||||
};
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
struct remove_false<std::tuple<T, Ts...>> {
|
||||
using type = tup_cat_t<T, typename remove_false<Ts...>::type>;
|
||||
};
|
||||
|
||||
template <typename... Ts>
|
||||
struct remove_false<std::tuple<std::false_type, Ts...>> {
|
||||
using type = typename remove_false<Ts...>::type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct remove_false<T> {
|
||||
using type = std::tuple<T>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct remove_false<std::tuple<T>> {
|
||||
using type = std::tuple<T>;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct remove_false<std::false_type> {
|
||||
using type = std::tuple<>;
|
||||
};
|
||||
|
||||
////////////////
|
||||
// negate trait
|
||||
////////////////
|
||||
|
||||
template <template <typename...> class Trait>
|
||||
struct negate_impl {
|
||||
template <typename... Ts>
|
||||
using type = std::integral_constant<bool, !Trait<Ts...>::value>;
|
||||
};
|
||||
|
||||
////////////////
|
||||
// filter by trait
|
||||
////////////////
|
||||
|
||||
template <template <typename...> class Trait, typename... Ts>
|
||||
struct filter_if {
|
||||
using type = typename filter_if<Trait, std::tuple<Ts...>>::type;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename... Ts>
|
||||
struct filter_if<Trait, std::tuple<Ts...>> {
|
||||
|
||||
using type = typename remove_false<typename apply_optional_trait<
|
||||
Trait, std::tuple<Ts...>>::type>::type;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename... Ts>
|
||||
using filter_if_t = typename filter_if<Trait, Ts...>::type;
|
||||
|
||||
template <template <typename...> class Trait, typename... Ts>
|
||||
struct filter_not {
|
||||
using type = typename filter_not<Trait, std::tuple<Ts...>>::type;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename... Ts>
|
||||
struct filter_not<Trait, std::tuple<Ts...>> {
|
||||
|
||||
using type = typename remove_false<typename apply_optional_trait<
|
||||
negate_impl<Trait>::template type, std::tuple<Ts...>>::type>::type;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename... Ts>
|
||||
using filter_not_t = typename filter_not<Trait, Ts...>::type;
|
||||
|
||||
////////////////
|
||||
// count
|
||||
////////////////
|
||||
|
||||
template <template <typename...> class Trait, typename T, typename... Ts>
|
||||
struct count;
|
||||
|
||||
template <template <typename...> class Trait, typename T, typename... Ts>
|
||||
struct count {
|
||||
static constexpr size_t size =
|
||||
std::tuple_size<filter_if_t<Trait, T, Ts...>>::value;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename T>
|
||||
struct count<Trait, T> {
|
||||
static constexpr size_t size = Trait<T>::value;
|
||||
};
|
||||
|
||||
////////////////
|
||||
// count not
|
||||
////////////////
|
||||
|
||||
template <template <typename...> class Trait, typename T, typename... Ts>
|
||||
struct count;
|
||||
|
||||
template <template <typename...> class Trait, typename T, typename... Ts>
|
||||
struct count_not {
|
||||
static constexpr size_t size =
|
||||
std::tuple_size<filter_not_t<Trait, T, Ts...>>::value;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename T>
|
||||
struct count_not<Trait, T> {
|
||||
static constexpr size_t size = !Trait<T>::value;
|
||||
};
|
||||
|
||||
////////////////
|
||||
// all of
|
||||
////////////////
|
||||
|
||||
template <template <typename...> class Trait, typename... Ts>
|
||||
struct all_of {
|
||||
static constexpr bool value =
|
||||
count<Trait, Ts...>::size == sizeof...(Ts);
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename... Ts>
|
||||
struct all_of<Trait, std::tuple<Ts...>> {
|
||||
static constexpr bool value =
|
||||
count<Trait, Ts...>::size == sizeof...(Ts);
|
||||
};
|
||||
|
||||
////////////////
|
||||
// any of
|
||||
////////////////
|
||||
|
||||
template <template <typename...> class Trait, typename... Ts>
|
||||
struct any_of {
|
||||
static_assert(sizeof...(Ts) > 0);
|
||||
static constexpr bool value = count<Trait, Ts...>::size > 0;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename... Ts>
|
||||
struct any_of<Trait, std::tuple<Ts...>> {
|
||||
static_assert(sizeof...(Ts) > 0);
|
||||
static constexpr bool value = count<Trait, Ts...>::size > 0;
|
||||
};
|
||||
|
||||
////////////////
|
||||
// none of
|
||||
////////////////
|
||||
|
||||
template <template <typename...> class Trait, typename... Ts>
|
||||
struct none_of {
|
||||
static constexpr bool value = count<Trait, Ts...>::size == 0;
|
||||
};
|
||||
|
||||
template <template <typename...> class Trait, typename... Ts>
|
||||
struct none_of<Trait, std::tuple<Ts...>> {
|
||||
static constexpr bool value = count<Trait, Ts...>::size == 0;
|
||||
};
|
||||
|
||||
////////////////
|
||||
// tuple to struct
|
||||
////////////////
|
||||
|
||||
template <class S, std::size_t... Is, class Tup>
|
||||
S to_struct(std::index_sequence<Is...>, Tup&& tup) {
|
||||
using std::get;
|
||||
return {get<Is>(std::forward<Tup>(tup))...};
|
||||
}
|
||||
|
||||
template <class S, class Tup>
|
||||
S to_struct(Tup&& tup) {
|
||||
using T = std::remove_reference_t<Tup>;
|
||||
|
||||
return to_struct<S>(std::make_index_sequence<std::tuple_size<T>{}>{},
|
||||
std::forward<Tup>(tup));
|
||||
}
|
||||
|
||||
} /* trait */
|
6205
test/doctest.h
Normal file
6205
test/doctest.h
Normal file
File diff suppressed because it is too large
Load Diff
26
test/makefile
Normal file
26
test/makefile
Normal file
@ -0,0 +1,26 @@
|
||||
CXX=clang++
|
||||
CXXFLAGS=-Wall -Wextra -std=c++17 -lstdc++fs
|
||||
TESTS=test_parser test_converter test_extractions
|
||||
|
||||
all: $(TESTS)
|
||||
|
||||
# pattern rule, replacing built-in implicit .cpp-suffix rule
|
||||
%: %.cpp
|
||||
$(CXX) $(CXXFLAGS) $< -o $@
|
||||
|
||||
debug: CXXFLAGS += -g
|
||||
debug: all
|
||||
|
||||
clean:
|
||||
@$(RM) -fv $(TESTS)
|
||||
@$(RM) *.csv
|
||||
|
||||
test:
|
||||
@for i in $(TESTS); do \
|
||||
./$$i; \
|
||||
done
|
||||
|
||||
# don't use any implicit rules
|
||||
.SUFFIXES:
|
||||
# these rules won't actually build the targets they're named after
|
||||
.PHONY: all clean run debug
|
288
test/test_converter.cpp
Normal file
288
test/test_converter.cpp
Normal file
@ -0,0 +1,288 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "../include/ss/converter.hpp"
|
||||
#include "doctest.h"
|
||||
#include <algorithm>
|
||||
|
||||
TEST_CASE("testing split") {
|
||||
ss::converter c;
|
||||
for (const auto& [s, expected, delim] :
|
||||
// clang-format off
|
||||
{std::tuple{"a,b,c,d", std::vector{"a", "b", "c", "d"}, ","},
|
||||
{"", {}, " "},
|
||||
{"a,b,c", {"a", "b", "c"}, ""},
|
||||
{" x x x x | x ", {" x x x x ", " x "}, "|"},
|
||||
{"a::b::c::d", {"a", "b", "c", "d"}, "::"},
|
||||
{"x\t-\ty", {"x", "y"}, "\t-\t"},
|
||||
{"x", {"x"}, ","}} // clang-format on
|
||||
) {
|
||||
auto split = c.split(s, delim);
|
||||
CHECK(split.size() == expected.size());
|
||||
for (size_t i = 0; i < split.size(); ++i) {
|
||||
auto s = std::string(split[i].first, split[i].second);
|
||||
CHECK(s == expected[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("testing valid conversions") {
|
||||
ss::converter c;
|
||||
|
||||
{
|
||||
auto tup = c.convert<int>("5");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == 5);
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<int, void>("5,junk");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == 5);
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<void, int>("junk,5");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == 5);
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<int, void, void>("5\njunk\njunk", "\n");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == 5);
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<void, int, void>("junk 5 junk", " ");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == 5);
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<void, void, int>("junk\tjunk\t5", "\t");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == 5);
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<int, double, void>("5,6.6,junk");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == std::tuple{5, 6.6});
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<int, void, double>("5,junk,6.6");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == std::tuple{5, 6.6});
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<void, int, double>("junk;5;6.6", ";");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == std::tuple{5, 6.6});
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("testing invalid conversions") {
|
||||
ss::converter c;
|
||||
|
||||
c.convert<int>("");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<int, void>("");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<int, void>(",junk");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<void, int>("junk,");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<int>("x");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<int, void>("x");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<int, void>("x,junk");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<void, int>("junk,x");
|
||||
REQUIRE(!c.valid());
|
||||
}
|
||||
|
||||
TEST_CASE("testing ss:ax restriction (all except)") {
|
||||
ss::converter c;
|
||||
|
||||
c.convert<ss::ax<int, 0>>("0");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<ss::ax<int, 0, 1, 2>>("1");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<void, char, ss::ax<int, 0, 1, 2>>("junk,c,1");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<ss::ax<int, 1>, char>("1,c");
|
||||
REQUIRE(!c.valid());
|
||||
{
|
||||
int tup = c.convert<ss::ax<int, 1>>("3");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == 3);
|
||||
}
|
||||
{
|
||||
std::tuple<char, int> tup =
|
||||
c.convert<char, ss::ax<int, 1>>("c,3");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == std::tuple{'c', 3});
|
||||
}
|
||||
{
|
||||
std::tuple<int, char> tup =
|
||||
c.convert<ss::ax<int, 1>, char>("3,c");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == std::tuple{3, 'c'});
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("testing ss:nx restriction (none except)") {
|
||||
ss::converter c;
|
||||
|
||||
c.convert<ss::nx<int, 1>>("3");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<char, ss::nx<int, 1, 2, 69>>("c,3");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<ss::nx<int, 1>, char>("3,c");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
{
|
||||
auto tup = c.convert<ss::nx<int, 3>>("3");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == 3);
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<ss::nx<int, 0, 1, 2>>("2");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == 2);
|
||||
}
|
||||
{
|
||||
auto tup =
|
||||
c.convert<char, void, ss::nx<int, 0, 1, 2>>("c,junk,1");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == std::tuple{'c', 1});
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<ss::nx<int, 1>, char>("1,c");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == std::tuple{1, 'c'});
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("testing ss:ir restriction (in range)") {
|
||||
ss::converter c;
|
||||
|
||||
c.convert<ss::ir<int, 0, 2>>("3");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<char, ss::ir<int, 4, 69>>("c,3");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<ss::ir<int, 1, 2>, char>("3,c");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
{
|
||||
auto tup = c.convert<ss::ir<int, 1, 5>>("3");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == 3);
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<ss::ir<int, 0, 2>>("2");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == 2);
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<char, void, ss::ir<int, 0, 1>>("c,junk,1");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == std::tuple{'c', 1});
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<ss::ir<int, 1, 20>, char>("1,c");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == std::tuple{1, 'c'});
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("testing ss:oor restriction (out of range)") {
|
||||
ss::converter c;
|
||||
|
||||
c.convert<ss::oor<int, 1, 5>>("3");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<ss::oor<int, 0, 2>>("2");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<char, ss::oor<int, 0, 1>, void>("c,1,junk");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<ss::oor<int, 1, 20>, char>("1,c");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
{
|
||||
auto tup = c.convert<ss::oor<int, 0, 2>>("3");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == 3);
|
||||
}
|
||||
|
||||
{
|
||||
auto tup =
|
||||
c.convert<char, void, ss::oor<int, 4, 69>>("c,junk,3");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == std::tuple{'c', 3});
|
||||
}
|
||||
|
||||
{
|
||||
auto tup = c.convert<ss::oor<int, 1, 2>, char>("3,c");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == std::tuple{3, 'c'});
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<int> extracted_vector = {1, 2, 3};
|
||||
|
||||
// custom extract
|
||||
template <>
|
||||
inline bool ss::extract(const char* begin, const char* end,
|
||||
std::vector<int>& value) {
|
||||
if (begin == end) {
|
||||
return false;
|
||||
}
|
||||
value = extracted_vector;
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST_CASE("testing ss:ne restriction (not empty)") {
|
||||
ss::converter c;
|
||||
|
||||
c.convert<ss::ne<std::string>>("");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<int, ss::ne<std::string>>("3,");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<ss::ne<std::string>, int>(",3");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<void, ss::ne<std::string>, int>("junk,,3");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<ss::ne<std::vector<int>>>("");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
{
|
||||
auto tup = c.convert<ss::ne<std::string>>("s");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == "s");
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<int, ss::ne<std::string>>("1,s");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == std::tuple{1, "s"});
|
||||
}
|
||||
{
|
||||
auto tup = c.convert<ss::ne<std::vector<int>>>("{1 2 3}");
|
||||
REQUIRE(c.valid());
|
||||
CHECK(tup == extracted_vector);
|
||||
}
|
||||
}
|
177
test/test_extractions.cpp
Normal file
177
test/test_extractions.cpp
Normal file
@ -0,0 +1,177 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "../include/ss/extract.hpp"
|
||||
#include "doctest.h"
|
||||
#include <algorithm>
|
||||
|
||||
constexpr auto eps = 0.000001;
|
||||
using ld = long double;
|
||||
|
||||
#define CHECK_FLOATING_CONVERSION(input, type) \
|
||||
{ \
|
||||
std::string s = #input; \
|
||||
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
|
||||
REQUIRE(t.has_value()); \
|
||||
CHECK(std::abs(t.value() - type(input)) < eps); \
|
||||
} \
|
||||
{ \
|
||||
/* check negative too */ \
|
||||
auto s = std::string("-") + #input; \
|
||||
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
|
||||
REQUIRE(t.has_value()); \
|
||||
CHECK(std::abs(t.value() - type(-input)) < eps); \
|
||||
}
|
||||
|
||||
TEST_CASE("testing extract functions for floating point values") {
|
||||
CHECK_FLOATING_CONVERSION(123.456, float);
|
||||
CHECK_FLOATING_CONVERSION(123.456, double);
|
||||
CHECK_FLOATING_CONVERSION(123.456, ld);
|
||||
|
||||
CHECK_FLOATING_CONVERSION(69, float);
|
||||
CHECK_FLOATING_CONVERSION(69, double);
|
||||
CHECK_FLOATING_CONVERSION(69, ld);
|
||||
|
||||
CHECK_FLOATING_CONVERSION(420., float);
|
||||
CHECK_FLOATING_CONVERSION(420., double);
|
||||
CHECK_FLOATING_CONVERSION(420., ld);
|
||||
|
||||
CHECK_FLOATING_CONVERSION(0.123, float);
|
||||
CHECK_FLOATING_CONVERSION(0.123, double);
|
||||
CHECK_FLOATING_CONVERSION(0.123, ld);
|
||||
|
||||
CHECK_FLOATING_CONVERSION(123e4, float);
|
||||
CHECK_FLOATING_CONVERSION(123e4, double);
|
||||
CHECK_FLOATING_CONVERSION(123e4, ld);
|
||||
}
|
||||
|
||||
#define CHECK_DECIMAL_CONVERSION(input, type) \
|
||||
{ \
|
||||
std::string s = #input; \
|
||||
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
|
||||
REQUIRE(t.has_value()); \
|
||||
CHECK(t.value() == type(input)); \
|
||||
} \
|
||||
{ \
|
||||
/* check negative too */ \
|
||||
if (std::is_signed_v<type>) { \
|
||||
auto s = std::string("-") + #input; \
|
||||
auto t = \
|
||||
ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
|
||||
REQUIRE(t.has_value()); \
|
||||
CHECK(t.value() == type(-input)); \
|
||||
} \
|
||||
}
|
||||
|
||||
using us = unsigned short;
|
||||
using ui = unsigned int;
|
||||
using ul = unsigned long;
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
|
||||
TEST_CASE("testing extract functions for decimal values") {
|
||||
CHECK_DECIMAL_CONVERSION(1234, short);
|
||||
CHECK_DECIMAL_CONVERSION(1234, us);
|
||||
CHECK_DECIMAL_CONVERSION(1234, int);
|
||||
CHECK_DECIMAL_CONVERSION(1234, ui);
|
||||
CHECK_DECIMAL_CONVERSION(1234, long);
|
||||
CHECK_DECIMAL_CONVERSION(1234, ul);
|
||||
CHECK_DECIMAL_CONVERSION(1234, ll);
|
||||
CHECK_DECIMAL_CONVERSION(1234567891011, ull);
|
||||
}
|
||||
|
||||
#define CHECK_INVALID_CONVERSION(input, type) \
|
||||
{ \
|
||||
std::string s = input; \
|
||||
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
|
||||
CHECK(!t.has_value()); \
|
||||
}
|
||||
|
||||
TEST_CASE("testing extract functions for numbers with invalid inputs") {
|
||||
// negative unsigned value
|
||||
CHECK_INVALID_CONVERSION("-1234", ul);
|
||||
|
||||
// floating pint for int
|
||||
CHECK_INVALID_CONVERSION("123.4", int);
|
||||
|
||||
// random input for float
|
||||
CHECK_INVALID_CONVERSION("xxx1", float);
|
||||
|
||||
// random input for int
|
||||
CHECK_INVALID_CONVERSION("xxx1", int);
|
||||
|
||||
// empty field for int
|
||||
CHECK_INVALID_CONVERSION("", int);
|
||||
}
|
||||
|
||||
#define CHECK_OUT_OF_RANGE_CONVERSION(type) \
|
||||
{ \
|
||||
std::string s = \
|
||||
std::to_string(std::numeric_limits<type>::max()); \
|
||||
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
|
||||
CHECK(t.has_value()); \
|
||||
for (auto& i : s) { \
|
||||
if (i != '9' && i != '.') { \
|
||||
i = '9'; \
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
|
||||
CHECK(!t.has_value()); \
|
||||
} \
|
||||
{ \
|
||||
std::string s = \
|
||||
std::to_string(std::numeric_limits<type>::min()); \
|
||||
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
|
||||
CHECK(t.has_value()); \
|
||||
for (auto& i : s) { \
|
||||
if (std::is_signed_v<type> && i != '9' && i != '.') { \
|
||||
i = '9'; \
|
||||
break; \
|
||||
} else if (std::is_unsigned_v<type>) { \
|
||||
s = "-1"; \
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
|
||||
CHECK(!t.has_value()); \
|
||||
}
|
||||
|
||||
TEST_CASE("testing extract functions for numbers with out of range inputs") {
|
||||
CHECK_OUT_OF_RANGE_CONVERSION(short);
|
||||
CHECK_OUT_OF_RANGE_CONVERSION(us);
|
||||
CHECK_OUT_OF_RANGE_CONVERSION(int);
|
||||
CHECK_OUT_OF_RANGE_CONVERSION(ui);
|
||||
CHECK_OUT_OF_RANGE_CONVERSION(long);
|
||||
CHECK_OUT_OF_RANGE_CONVERSION(ul);
|
||||
CHECK_OUT_OF_RANGE_CONVERSION(ll);
|
||||
CHECK_OUT_OF_RANGE_CONVERSION(ull);
|
||||
}
|
||||
|
||||
TEST_CASE("testing extract functions for boolean values") {
|
||||
for (const auto& [b, s] : {std::pair<bool, std::string>{true, "1"},
|
||||
{false, "0"},
|
||||
{true, "true"},
|
||||
{false, "false"}}) {
|
||||
bool v;
|
||||
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
|
||||
CHECK(v == b);
|
||||
}
|
||||
|
||||
for (const std::string& s : {"2", "tru", "truee", "xxx", ""}) {
|
||||
bool v;
|
||||
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("testing extract functions for char values") {
|
||||
for (const auto& [c, s] :
|
||||
{std::pair<char, std::string>{'a', "a"}, {'x', "x"}, {' ', " "}}) {
|
||||
char v;
|
||||
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
|
||||
CHECK(v == c);
|
||||
}
|
||||
|
||||
for (const std::string& s : {"aa", "xxx", ""}) {
|
||||
char v;
|
||||
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v));
|
||||
}
|
||||
}
|
234
test/test_parser.cpp
Normal file
234
test/test_parser.cpp
Normal file
@ -0,0 +1,234 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "../include/ss/parser.hpp"
|
||||
#include "doctest.h"
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
|
||||
struct unique_file_name {
|
||||
const std::string name;
|
||||
|
||||
unique_file_name() : name{std::tmpnam(nullptr)} {
|
||||
}
|
||||
|
||||
~unique_file_name() {
|
||||
std::filesystem::remove(name);
|
||||
}
|
||||
};
|
||||
|
||||
struct X {
|
||||
constexpr static auto delim = ",";
|
||||
int i;
|
||||
double d;
|
||||
std::string s;
|
||||
|
||||
std::string to_string() const {
|
||||
return std::to_string(i)
|
||||
.append(delim)
|
||||
.append(std::to_string(d))
|
||||
.append(delim)
|
||||
.append(s);
|
||||
}
|
||||
auto tied() const {
|
||||
return std::tie(i, d, s);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<ss::has_m_tied_t<T>, bool> operator==(const T& lhs,
|
||||
const T& rhs) {
|
||||
return lhs.tied() == rhs.tied();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void make_and_write(const std::string& file_name,
|
||||
const std::vector<T>& data) {
|
||||
std::ofstream out{file_name};
|
||||
for (const auto& i : data) {
|
||||
out << i.to_string() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("testing parser") {
|
||||
unique_file_name f;
|
||||
std::vector<X> data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"}};
|
||||
make_and_write(f.name, data);
|
||||
{
|
||||
ss::parser p{f.name, ","};
|
||||
std::vector<X> i;
|
||||
|
||||
while (!p.eof()) {
|
||||
auto a = p.get_next<int, double, std::string>();
|
||||
i.emplace_back(ss::to_struct<X>(a));
|
||||
}
|
||||
|
||||
CHECK(std::equal(i.begin(), i.end(), data.begin()));
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser p{f.name, ","};
|
||||
std::vector<X> i;
|
||||
|
||||
p.ignore_next();
|
||||
while (!p.eof()) {
|
||||
auto a = p.get_next<int, double, std::string>();
|
||||
i.emplace_back(ss::to_struct<X>(a));
|
||||
}
|
||||
|
||||
CHECK(std::equal(i.begin(), i.end(), data.begin() + 1));
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser p{f.name, ","};
|
||||
std::vector<X> i;
|
||||
|
||||
while (!p.eof()) {
|
||||
i.push_back(
|
||||
p.get_struct<X, int, double, std::string>());
|
||||
}
|
||||
|
||||
CHECK(std::equal(i.begin(), i.end(), data.begin()));
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser p{f.name, ","};
|
||||
std::vector<X> i;
|
||||
|
||||
while (!p.eof()) {
|
||||
i.push_back(p.get_next<X>());
|
||||
}
|
||||
|
||||
CHECK(std::equal(i.begin(), i.end(), data.begin()));
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser p{f.name, ","};
|
||||
std::vector<X> i;
|
||||
|
||||
while (!p.eof()) {
|
||||
auto a = p.get_struct<X, ss::ax<int, 3>, double,
|
||||
std::string>();
|
||||
if (p.valid()) {
|
||||
i.push_back(a);
|
||||
}
|
||||
}
|
||||
std::vector<X> expected = {{1, 2, "x"}, {5, 6, "z"}};
|
||||
CHECK(std::equal(i.begin(), i.end(), expected.begin()));
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser p{f.name, ","};
|
||||
std::vector<X> i;
|
||||
|
||||
while (!p.eof()) {
|
||||
auto a = p.get_struct<X, ss::nx<int, 3>, double,
|
||||
std::string>();
|
||||
if (p.valid()) {
|
||||
i.push_back(a);
|
||||
}
|
||||
}
|
||||
std::vector<X> expected = {{3, 4, "y"}};
|
||||
CHECK(std::equal(i.begin(), i.end(), expected.begin()));
|
||||
}
|
||||
|
||||
{
|
||||
unique_file_name empty_f;
|
||||
std::vector<X> empty_data = {};
|
||||
make_and_write(empty_f.name, empty_data);
|
||||
|
||||
ss::parser p{empty_f.name, ","};
|
||||
std::vector<X> i;
|
||||
|
||||
while (!p.eof()) {
|
||||
i.push_back(p.get_next<X>());
|
||||
}
|
||||
CHECK(i.empty());
|
||||
}
|
||||
}
|
||||
|
||||
size_t move_called = 0;
|
||||
size_t copy_called = 0;
|
||||
|
||||
struct my_string {
|
||||
char* data{nullptr};
|
||||
|
||||
my_string() = default;
|
||||
|
||||
~my_string() {
|
||||
delete[] data;
|
||||
}
|
||||
|
||||
my_string(const my_string&) {
|
||||
copy_called++;
|
||||
}
|
||||
|
||||
my_string(my_string&& other) : data{other.data} {
|
||||
move_called++;
|
||||
other.data = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
inline bool ss::extract(const char* begin, const char* end, my_string& s) {
|
||||
size_t size = end - begin;
|
||||
s.data = new char[size + 1];
|
||||
strncpy(s.data, begin, size);
|
||||
s.data[size] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
struct Y {
|
||||
my_string x;
|
||||
my_string y;
|
||||
my_string z;
|
||||
auto tied() {
|
||||
return std::tie(x, y, z);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("testing the moving of parsed values") {
|
||||
size_t move_called_one_col;
|
||||
|
||||
{
|
||||
unique_file_name f;
|
||||
std::ofstream out{f.name};
|
||||
out << "x" << std::endl;
|
||||
|
||||
ss::parser p{f.name, ","};
|
||||
auto x = p.get_next<my_string>();
|
||||
CHECK(copy_called == 0);
|
||||
CHECK(move_called < 3);
|
||||
move_called_one_col = move_called;
|
||||
move_called = copy_called = 0;
|
||||
}
|
||||
|
||||
unique_file_name f;
|
||||
{
|
||||
std::ofstream out{f.name};
|
||||
out << "a,b,c" << std::endl;
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
ss::parser p{f.name, ","};
|
||||
auto x = p.get_next<my_string, my_string, my_string>();
|
||||
CHECK(copy_called == 0);
|
||||
CHECK(move_called == 3 * move_called_one_col);
|
||||
move_called = copy_called = 0;
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser p{f.name, ","};
|
||||
auto x = p.get_struct<Y, my_string, my_string, my_string>();
|
||||
CHECK(copy_called == 0);
|
||||
CHECK(move_called == 6 * move_called_one_col);
|
||||
move_called = copy_called = 0;
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser p{f.name, ","};
|
||||
auto x = p.get_next<Y>();
|
||||
CHECK(copy_called == 0);
|
||||
CHECK(move_called == 6 * move_called_one_col);
|
||||
move_called = copy_called = 0;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user