Skip to content
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
178 changes: 19 additions & 159 deletions src/mpp/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

namespace mpp {

namespace compact {
enum Family : uint8_t {
MP_NIL /* = 0x00 */,
MP_IGNR /* = 0x01 */,
Expand All @@ -50,43 +49,6 @@ enum Family : uint8_t {
MP_EXT /* = 0x09 */,
MP_END
};
} // namespace compact {

using FamilyUnder_t = uint32_t;
enum Family : FamilyUnder_t {
MP_NIL = 1u << compact::MP_NIL,
MP_IGNR = 1u << compact::MP_IGNR,
MP_BOOL = 1u << compact::MP_BOOL,
MP_INT = 1u << compact::MP_INT,
MP_FLT = 1u << compact::MP_FLT,
MP_STR = 1u << compact::MP_STR,
MP_BIN = 1u << compact::MP_BIN,
MP_ARR = 1u << compact::MP_ARR,
MP_MAP = 1u << compact::MP_MAP,
MP_EXT = 1u << compact::MP_EXT,
MP_NUM = (1u << compact::MP_INT) | (1u << compact::MP_FLT),
MP_NONE = 0,
MP_ANY = std::numeric_limits<FamilyUnder_t>::max(),
};

enum ReadError_t {
READ_ERROR_NEED_MORE,
READ_ERROR_BAD_MSGPACK,
READ_ERROR_WRONG_TYPE,
READ_ERROR_MAX_DEPTH_REACHED,
READ_ERROR_ABORTED_BY_USER,
READ_ERROR_END
};

enum ReadResult_t : FamilyUnder_t {
READ_SUCCESS = 0,
READ_NEED_MORE = 1u << READ_ERROR_NEED_MORE,
READ_BAD_MSGPACK = 1u << READ_ERROR_BAD_MSGPACK,
READ_WRONG_TYPE = 1u << READ_ERROR_WRONG_TYPE,
READ_MAX_DEPTH_REACHED = 1u << READ_ERROR_MAX_DEPTH_REACHED,
READ_ABORTED_BY_USER = 1u << READ_ERROR_ABORTED_BY_USER,
READ_RESULT_END
};

inline const char *FamilyName[] = {
"MP_NIL",
Expand All @@ -102,7 +64,7 @@ inline const char *FamilyName[] = {
"MP_BAD",
"MP_NONE"
};
static_assert(std::size(FamilyName) == compact::MP_END + 2, "Smth is forgotten");
static_assert(std::size(FamilyName) == MP_END + 2, "Smth is forgotten");

inline const char *FamilyHumanName[] = {
"nil",
Expand All @@ -118,98 +80,32 @@ inline const char *FamilyHumanName[] = {
"bad",
"none"
};
static_assert(std::size(FamilyHumanName) == compact::MP_END + 2, "Smth is forgotten");

inline const char *ReadErrorName[] = {
"READ_ERROR_NEED_MORE",
"READ_ERROR_BAD_MSGPACK",
"READ_ERROR_WRONG_TYPE",
"READ_ERROR_MAX_DEPTH_REACHED",
"READ_ERROR_ABORTED_BY_USER",
"READ_ERROR_UNKNOWN",
"READ_SUCCESS",
};
static_assert(std::size(ReadErrorName) == READ_ERROR_END + 2, "Forgotten");

inline constexpr Family
operator|(Family a, Family b)
{
return static_cast<Family>(static_cast<FamilyUnder_t>(a) |
static_cast<FamilyUnder_t>(b));
}

inline constexpr Family
operator&(Family a, Family b)
{
return static_cast<Family>(static_cast<FamilyUnder_t>(a) &
static_cast<FamilyUnder_t>(b));
}

inline constexpr ReadResult_t
operator|(ReadResult_t a, ReadResult_t b)
{
return static_cast<ReadResult_t>(static_cast<FamilyUnder_t>(a) |
static_cast<FamilyUnder_t>(b));
}

inline constexpr ReadResult_t
operator&(ReadResult_t a, ReadResult_t b)
{
return static_cast<ReadResult_t>(static_cast<FamilyUnder_t>(a) &
static_cast<FamilyUnder_t>(b));
}

inline constexpr ReadResult_t
operator~(ReadResult_t a)
{
return static_cast<ReadResult_t>(~static_cast<FamilyUnder_t>(a));
}
static_assert(std::size(FamilyHumanName) == MP_END + 2, "Smth is forgotten");

inline std::ostream&
operator<<(std::ostream& strm, compact::Family t)
operator<<(std::ostream& strm, Family t)
{
if (t >= compact::Family::MP_END)
return strm << FamilyName[compact::Family::MP_END]
if (t >= Family::MP_END)
return strm << FamilyName[Family::MP_END]
<< "(" << static_cast<uint64_t>(t) << ")";
return strm << FamilyName[t];
}

inline std::ostream&
operator<<(std::ostream& strm, Family t)
{
if (t == MP_NONE)
return strm << FamilyName[compact::Family::MP_END + 1];
static_assert(sizeof(FamilyUnder_t) == sizeof(t), "Very wrong");
FamilyUnder_t base = t;
bool first = true;
do {
static_assert(sizeof(unsigned) == sizeof(t), "Wrong ctz");
unsigned part = __builtin_ctz(base);
base ^= 1u << part;
if (first)
first = false;
else
strm << "|";
strm << static_cast<compact::Family>(part);
} while (base != 0);
return strm;
}

template <compact::Family ...FAMILY>
template <Family ...FAMILY>
struct family_sequence {
static constexpr std::size_t size() noexcept
{
return sizeof ...(FAMILY);
}
};

template <compact::Family NEW_FAMILY, compact::Family ...FAMILY>
template <Family NEW_FAMILY, Family... FAMILY>
static constexpr auto family_sequence_populate(struct family_sequence<FAMILY...>)
{
return family_sequence<NEW_FAMILY, FAMILY...>{};
}

template <compact::Family ...FAMILY_A, compact::Family ...FAMILY_B>
template <Family ...FAMILY_A, Family ...FAMILY_B>
inline constexpr auto
operator+(family_sequence<FAMILY_A...>, family_sequence<FAMILY_B...>)
{
Expand All @@ -218,90 +114,54 @@ operator+(family_sequence<FAMILY_A...>, family_sequence<FAMILY_B...>)

namespace details {

template <compact::Family NEEDLE, compact::Family HEAD, compact::Family ...TAIL>
template <Family NEEDLE, Family HEAD, Family ...TAIL>
struct family_sequence_contains_impl_h {
static constexpr bool value =
family_sequence_contains_impl_h<NEEDLE, TAIL...>::value;
};

template <compact::Family NEEDLE, compact::Family LAST>
template <Family NEEDLE, Family LAST>
struct family_sequence_contains_impl_h<NEEDLE, LAST> {
static constexpr bool value = false;
};

template <compact::Family NEEDLE, compact::Family ...TAIL>
template <Family NEEDLE, Family ...TAIL>
struct family_sequence_contains_impl_h<NEEDLE, NEEDLE, TAIL...> {
static constexpr bool value = true;
};

template <compact::Family NEEDLE>
template <Family NEEDLE>
struct family_sequence_contains_impl_h<NEEDLE, NEEDLE> {
static constexpr bool value = true;
};

template <compact::Family NEEDLE, compact::Family ...HAYSTACK>
template <Family NEEDLE, Family ...HAYSTACK>
struct family_sequence_contains_h {
static constexpr bool value =
family_sequence_contains_impl_h<NEEDLE, HAYSTACK...>::value;
};

template <compact::Family NEEDLE>
template <Family NEEDLE>
struct family_sequence_contains_h<NEEDLE> {
static constexpr bool value = false;
};

} //namespace details
} // namespace details

template <compact::Family NEEDLE, compact::Family ...HAYSTACK>
template <Family NEEDLE, Family ...HAYSTACK>
static constexpr bool family_sequence_contains(family_sequence<HAYSTACK...>) {
return details::family_sequence_contains_h<NEEDLE, HAYSTACK...>::value;
}

template <compact::Family ...FAMILY>
template <Family ...FAMILY>
std::ostream&
operator<<(std::ostream& strm, family_sequence<FAMILY...>)
{
if (sizeof ...(FAMILY) == 0)
return strm << FamilyName[compact::Family::MP_END + 1];
return strm << FamilyName[Family::MP_END + 1];
size_t count = 0;
((strm << (count++ ? ", " : "") << FamilyName[FAMILY]), ...);
return strm;
}

inline std::ostream&
operator<<(std::ostream& strm, ReadError_t t)
{
if (t >= READ_ERROR_END)
return strm << ReadErrorName[READ_ERROR_END]
<< "(" << static_cast<uint64_t>(t) << ")";
return strm << ReadErrorName[t];
}

inline std::ostream&
operator<<(std::ostream& strm, ReadResult_t t)
{
if (t == READ_SUCCESS)
return strm << ReadErrorName[READ_ERROR_END + 1];
static_assert(sizeof(FamilyUnder_t) == sizeof(t), "Very wrong");
FamilyUnder_t base = t;
bool first = true;
do {
static_assert(sizeof(unsigned) == sizeof(t), "Wrong ctz");
unsigned part = __builtin_ctz(base);
base ^= 1u << part;
if (first)
first = false;
else
strm << "|";
strm << static_cast<ReadError_t>(part);
} while (base != 0);
return strm;
}

struct StrValue { uint32_t offset; uint32_t size; };
struct BinValue { uint32_t offset; uint32_t size; };
struct ArrValue { uint32_t offset; uint32_t size; };
struct MapValue { uint32_t offset; uint32_t size; };
struct ExtValue { int8_t type; uint8_t offset; uint32_t size; };

} // namespace mpp {
} // namespace mpp
Loading
Loading