-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add parseType to select a type from a list and pass it to a generic f…
…unction
- Loading branch information
Showing
5 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef JUTCHSON_PARSE_TYPE_HPP_ | ||
#define JUTCHSON_PARSE_TYPE_HPP_ | ||
|
||
#include "../type.hpp" | ||
|
||
#include "../ParseResult.hpp" | ||
#include "../StringView.hpp" | ||
|
||
namespace JutchsON { | ||
template <typename GenericF, typename Typenames> | ||
using InvokeOnTagsResult = std::invoke_result_t<GenericF, typename std::tuple_element_t<0, Typenames>::TagType>; | ||
|
||
template <size_t i = 0, typename Typenames, typename GenericF> | ||
ParseResult<InvokeOnTagsResult<GenericF, Typenames>> parseType( | ||
JutchsON::StringView s, const Typenames& typenames, GenericF&& genericF | ||
) { | ||
if constexpr (i >= std::tuple_size_v<Typenames>) { | ||
return ParseResult<InvokeOnTagsResult<GenericF, Typenames>> | ||
::makeError(s.location(), std::format("Unknown type {}", s.asStd())); | ||
} else { | ||
if (s == std::get<i>(typenames).value) { | ||
return genericF(std::get<i>(typenames).tag()); | ||
} | ||
return parseType<i + 1>(s, typenames, genericF); | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef JUTCHSON_TYPE_HPP_ | ||
#define JUTCHSON_TYPE_HPP_ | ||
|
||
#include <type_traits> | ||
|
||
namespace JutchsON { | ||
template <typename T, typename Value = void> | ||
struct TaggedValue { | ||
using type = T; | ||
using TagType = TaggedValue<T>; | ||
|
||
Value value; | ||
|
||
TagType tag() const { | ||
return {}; | ||
} | ||
}; | ||
|
||
template <typename T> | ||
struct TaggedValue<T, void> { | ||
using type = T; | ||
}; | ||
|
||
template <typename T> | ||
using PayloadType = typename std::remove_cvref_t<T>::type; | ||
} | ||
|
||
#define JUTCHSON_TAGGED_TYPE_NAME(T) (JutchsON::TaggedValue<T, std::string>{#T}) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "JutchsON.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace { | ||
struct TestType1 {}; | ||
struct TestType2 {}; | ||
} | ||
|
||
TEST(Type, parseType) { | ||
std::tuple typenames{ | ||
JUTCHSON_TAGGED_TYPE_NAME(TestType1), | ||
JUTCHSON_TAGGED_TYPE_NAME(TestType2) | ||
}; | ||
|
||
EXPECT_TRUE(*parseType("TestType1", typenames, [](auto tag) { | ||
return std::same_as<JutchsON::PayloadType<decltype(tag)>, TestType1>; | ||
})); | ||
} | ||
|
||
TEST(Type, parseTypeSecond) { | ||
std::tuple typenames{ | ||
JUTCHSON_TAGGED_TYPE_NAME(TestType1), | ||
JUTCHSON_TAGGED_TYPE_NAME(TestType2) | ||
}; | ||
|
||
EXPECT_TRUE(*parseType("TestType2", typenames, [](auto tag) { | ||
return std::same_as<JutchsON::PayloadType<decltype(tag)>, TestType2>; | ||
})); | ||
} | ||
|
||
TEST(Type, parseTypeUnknown) { | ||
std::tuple typenames{ | ||
JUTCHSON_TAGGED_TYPE_NAME(TestType1), | ||
JUTCHSON_TAGGED_TYPE_NAME(TestType2) | ||
}; | ||
|
||
EXPECT_EQ(parseType("garbage", typenames, [](auto tag) { | ||
return std::same_as<JutchsON::PayloadType<decltype(tag)>, TestType1>; | ||
}), JutchsON::ParseResult<bool>::makeError({0, 0}, "Unknown type garbage")); | ||
} |