2015-04-18 01:35:36 +00:00
|
|
|
/* Function objects for OctaSTD.
|
|
|
|
*
|
|
|
|
* This file is part of OctaSTD. See COPYING.md for futher information.
|
|
|
|
*/
|
|
|
|
|
2015-07-13 19:08:55 +00:00
|
|
|
#ifndef OSTD_FUNCTIONAL_HH
|
|
|
|
#define OSTD_FUNCTIONAL_HH
|
2015-04-18 01:35:36 +00:00
|
|
|
|
2015-06-30 22:30:48 +00:00
|
|
|
#include <string.h>
|
2017-01-28 17:52:34 +00:00
|
|
|
|
2017-01-14 14:09:27 +00:00
|
|
|
#include <new>
|
2017-01-28 17:52:34 +00:00
|
|
|
#include <functional>
|
2017-01-28 18:39:50 +00:00
|
|
|
#include <tuple>
|
2015-06-30 22:30:48 +00:00
|
|
|
|
2015-07-13 19:08:55 +00:00
|
|
|
#include "ostd/platform.hh"
|
|
|
|
#include "ostd/utility.hh"
|
|
|
|
#include "ostd/type_traits.hh"
|
2015-04-18 18:02:44 +00:00
|
|
|
|
2015-07-13 19:07:14 +00:00
|
|
|
namespace ostd {
|
2015-04-18 01:50:20 +00:00
|
|
|
|
2015-06-03 22:27:09 +00:00
|
|
|
/* basic function objects */
|
|
|
|
|
2015-07-13 19:08:55 +00:00
|
|
|
#define OSTD_DEFINE_BINARY_OP(name, op, RT) \
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T> \
|
|
|
|
struct name { \
|
2016-06-23 18:18:35 +00:00
|
|
|
RT operator()(T const &x, T const &y) const { \
|
2015-06-07 23:55:08 +00:00
|
|
|
return x op y; \
|
2015-06-03 22:27:09 +00:00
|
|
|
} \
|
2015-06-07 23:55:08 +00:00
|
|
|
using FirstArgument = T; \
|
|
|
|
using SecondARgument = T; \
|
|
|
|
using Result = RT; \
|
2015-06-03 22:27:09 +00:00
|
|
|
};
|
|
|
|
|
2015-07-13 19:08:55 +00:00
|
|
|
OSTD_DEFINE_BINARY_OP(Less, <, bool)
|
|
|
|
OSTD_DEFINE_BINARY_OP(LessEqual, <=, bool)
|
|
|
|
OSTD_DEFINE_BINARY_OP(Greater, >, bool)
|
|
|
|
OSTD_DEFINE_BINARY_OP(GreaterEqual, >=, bool)
|
|
|
|
OSTD_DEFINE_BINARY_OP(Equal, ==, bool)
|
|
|
|
OSTD_DEFINE_BINARY_OP(NotEqual, !=, bool)
|
|
|
|
OSTD_DEFINE_BINARY_OP(LogicalAnd, &&, bool)
|
|
|
|
OSTD_DEFINE_BINARY_OP(LogicalOr, ||, bool)
|
|
|
|
OSTD_DEFINE_BINARY_OP(Modulo, %, T)
|
|
|
|
OSTD_DEFINE_BINARY_OP(Multiply, *, T)
|
|
|
|
OSTD_DEFINE_BINARY_OP(Divide, /, T)
|
|
|
|
OSTD_DEFINE_BINARY_OP(Add, +, T)
|
|
|
|
OSTD_DEFINE_BINARY_OP(Subtract, -, T)
|
|
|
|
OSTD_DEFINE_BINARY_OP(BitAnd, &, T)
|
|
|
|
OSTD_DEFINE_BINARY_OP(BitOr, |, T)
|
|
|
|
OSTD_DEFINE_BINARY_OP(BitXor, ^, T)
|
|
|
|
|
|
|
|
#undef OSTD_DEFINE_BINARY_OP
|
2015-06-03 22:27:09 +00:00
|
|
|
|
2015-08-02 13:56:10 +00:00
|
|
|
namespace detail {
|
2016-01-12 22:24:40 +00:00
|
|
|
template<typename T, bool = IsSame<RemoveConst<T>, char>>
|
2015-08-02 13:56:10 +00:00
|
|
|
struct CharEqual {
|
|
|
|
using FirstArgument = T *;
|
|
|
|
using SecondArgument = T *;
|
|
|
|
using Result = bool;
|
|
|
|
bool operator()(T *x, T *y) const {
|
|
|
|
return !strcmp(x, y);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T>
|
|
|
|
struct CharEqual<T, false> {
|
2015-08-02 13:56:10 +00:00
|
|
|
using FirstArgument = T *;
|
|
|
|
using SecondArgument = T *;
|
|
|
|
using Result = bool;
|
|
|
|
bool operator()(T *x, T *y) const {
|
|
|
|
return x == y;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T>
|
|
|
|
struct EqualWithCstr {
|
2015-08-02 13:56:10 +00:00
|
|
|
using FirstArgument = T;
|
|
|
|
using SecondArgument = T;
|
2016-06-23 18:18:35 +00:00
|
|
|
bool operator()(T const &x, T const &y) const {
|
2015-08-02 13:56:10 +00:00
|
|
|
return x == y;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T>
|
|
|
|
struct EqualWithCstr<T *>: detail::CharEqual<T> {};
|
2015-08-02 13:56:10 +00:00
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T>
|
|
|
|
struct LogicalNot {
|
2016-06-23 18:18:35 +00:00
|
|
|
bool operator()(T const &x) const { return !x; }
|
2015-06-07 23:55:08 +00:00
|
|
|
using Argument = T;
|
|
|
|
using Result = bool;
|
2015-06-03 22:27:09 +00:00
|
|
|
};
|
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T>
|
|
|
|
struct Negate {
|
2016-06-23 18:18:35 +00:00
|
|
|
bool operator()(T const &x) const { return -x; }
|
2015-06-07 23:55:08 +00:00
|
|
|
using Argument = T;
|
|
|
|
using Result = T;
|
2015-06-03 22:27:09 +00:00
|
|
|
};
|
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T>
|
|
|
|
struct BinaryNegate {
|
2015-06-07 23:55:08 +00:00
|
|
|
using FirstArgument = typename T::FirstArgument;
|
|
|
|
using SecondArgument = typename T::SecondArgument;
|
|
|
|
using Result = bool;
|
2015-06-03 22:27:09 +00:00
|
|
|
|
2016-06-23 18:18:35 +00:00
|
|
|
explicit BinaryNegate(T const &f): p_fn(f) {}
|
2015-06-03 22:27:09 +00:00
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
bool operator()(FirstArgument const &x, SecondArgument const &y) {
|
2015-06-03 22:27:09 +00:00
|
|
|
return !p_fn(x, y);
|
|
|
|
}
|
|
|
|
private:
|
2015-06-04 21:57:06 +00:00
|
|
|
T p_fn;
|
2015-06-03 22:27:09 +00:00
|
|
|
};
|
2015-04-27 18:38:34 +00:00
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T>
|
|
|
|
struct UnaryNegate {
|
2015-06-07 23:55:08 +00:00
|
|
|
using Argument = typename T::Argument;
|
|
|
|
using Result = bool;
|
2015-04-18 17:36:06 +00:00
|
|
|
|
2016-06-23 18:18:35 +00:00
|
|
|
explicit UnaryNegate(T const &f): p_fn(f) {}
|
|
|
|
bool operator()(Argument const &x) {
|
2015-06-03 22:27:09 +00:00
|
|
|
return !p_fn(x);
|
2015-04-18 17:36:06 +00:00
|
|
|
}
|
2015-06-03 22:27:09 +00:00
|
|
|
private:
|
2015-06-04 21:57:06 +00:00
|
|
|
T p_fn;
|
2015-06-03 22:27:09 +00:00
|
|
|
};
|
2015-04-18 17:36:06 +00:00
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T>
|
|
|
|
UnaryNegate<T> not1(T const &fn) {
|
2015-06-04 21:57:06 +00:00
|
|
|
return UnaryNegate<T>(fn);
|
2015-06-03 22:27:09 +00:00
|
|
|
}
|
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T>
|
|
|
|
BinaryNegate<T> not2(T const &fn) {
|
2015-06-04 21:57:06 +00:00
|
|
|
return BinaryNegate<T>(fn);
|
2015-06-03 22:27:09 +00:00
|
|
|
}
|
2015-04-18 18:02:44 +00:00
|
|
|
|
2015-06-27 01:38:58 +00:00
|
|
|
/* endian swap */
|
|
|
|
|
2017-01-30 18:11:39 +00:00
|
|
|
template<typename T, size_t N = sizeof(T), bool IsNum = IsArithmetic<T>>
|
2016-01-12 21:45:26 +00:00
|
|
|
struct EndianSwap;
|
2015-06-27 01:38:58 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct EndianSwap<T, 2, true> {
|
|
|
|
using Argument = T;
|
|
|
|
using Result = T;
|
2015-06-27 01:59:54 +00:00
|
|
|
T operator()(T v) const {
|
2017-01-30 18:19:09 +00:00
|
|
|
union { T iv; std::uint16_t sv; } u;
|
2015-06-27 01:38:58 +00:00
|
|
|
u.iv = v;
|
2015-07-05 22:59:36 +00:00
|
|
|
u.sv = endian_swap16(u.sv);
|
2015-06-27 01:38:58 +00:00
|
|
|
return u.iv;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct EndianSwap<T, 4, true> {
|
|
|
|
using Argument = T;
|
|
|
|
using Result = T;
|
2015-06-27 01:59:54 +00:00
|
|
|
T operator()(T v) const {
|
2017-01-30 18:19:09 +00:00
|
|
|
union { T iv; std::uint32_t sv; } u;
|
2015-06-27 01:38:58 +00:00
|
|
|
u.iv = v;
|
2015-07-05 22:59:36 +00:00
|
|
|
u.sv = endian_swap32(u.sv);
|
2015-06-27 01:38:58 +00:00
|
|
|
return u.iv;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct EndianSwap<T, 8, true> {
|
|
|
|
using Argument = T;
|
|
|
|
using Result = T;
|
2015-06-27 01:59:54 +00:00
|
|
|
T operator()(T v) const {
|
2017-01-30 18:19:09 +00:00
|
|
|
union { T iv; std::uint64_t sv; } u;
|
2015-06-27 01:38:58 +00:00
|
|
|
u.iv = v;
|
2015-07-05 22:59:36 +00:00
|
|
|
u.sv = endian_swap64(u.sv);
|
2015-06-27 01:38:58 +00:00
|
|
|
return u.iv;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T endian_swap(T x) { return EndianSwap<T>()(x); }
|
|
|
|
|
|
|
|
namespace detail {
|
2017-01-30 18:11:39 +00:00
|
|
|
template<typename T, size_t N = sizeof(T), bool IsNum = IsArithmetic<T>>
|
2016-01-12 21:45:26 +00:00
|
|
|
struct EndianSame;
|
2015-06-27 01:38:58 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct EndianSame<T, 2, true> {
|
|
|
|
using Argument = T;
|
|
|
|
using Result = T;
|
2015-06-27 01:59:54 +00:00
|
|
|
T operator()(T v) const { return v; }
|
2015-06-27 01:38:58 +00:00
|
|
|
};
|
|
|
|
template<typename T>
|
|
|
|
struct EndianSame<T, 4, true> {
|
|
|
|
using Argument = T;
|
|
|
|
using Result = T;
|
2015-06-27 01:59:54 +00:00
|
|
|
T operator()(T v) const { return v; }
|
2015-06-27 01:38:58 +00:00
|
|
|
};
|
|
|
|
template<typename T>
|
|
|
|
struct EndianSame<T, 8, true> {
|
|
|
|
using Argument = T;
|
|
|
|
using Result = T;
|
2015-06-27 01:59:54 +00:00
|
|
|
T operator()(T v) const { return v; }
|
2015-06-27 01:38:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-07-13 19:08:55 +00:00
|
|
|
#if OSTD_BYTE_ORDER == OSTD_ENDIAN_LIL
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T>
|
|
|
|
struct FromLilEndian: detail::EndianSame<T> {};
|
|
|
|
template<typename T>
|
|
|
|
struct FromBigEndian: EndianSwap<T> {};
|
2015-06-27 01:38:58 +00:00
|
|
|
#else
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T>
|
|
|
|
struct FromLilEndian: EndianSwap<T> {};
|
|
|
|
template<typename T>
|
|
|
|
struct FromBigEndian: detail::EndianSame<T> {};
|
2015-06-27 01:38:58 +00:00
|
|
|
#endif
|
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T>
|
|
|
|
T from_lil_endian(T x) { return FromLilEndian<T>()(x); }
|
|
|
|
template<typename T>
|
|
|
|
T from_big_endian(T x) { return FromBigEndian<T>()(x); }
|
2015-06-27 01:38:58 +00:00
|
|
|
|
2015-07-13 19:07:14 +00:00
|
|
|
} /* namespace ostd */
|
2015-04-18 01:35:36 +00:00
|
|
|
|
2016-02-07 21:17:15 +00:00
|
|
|
#endif
|