Last active
August 29, 2015 14:26
-
-
Save ricejasonf/0d63d5368e965a5b77f5 to your computer and use it in GitHub Desktop.
Nifty type matching for a lightweight variant type I am working on.
This file contains hidden or 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
#include<nbdl> | |
#include "catch.hpp" | |
struct Type1 {}; | |
struct Type2 {}; | |
struct Type3 {}; | |
struct Type4 {}; | |
template<typename T> | |
int getInt(T tag) | |
{ | |
return nbdl::VariantCallback::call(tag, | |
[](Type2) { | |
return 2; | |
}, | |
[](Type1) { | |
return 1; | |
}, | |
[](Type4) { | |
return 4; | |
}); | |
} | |
template<typename T> | |
void modifyInt(int &i, T tag) | |
{ | |
return nbdl::VariantCallback::call(tag, | |
[&](Type2) { | |
i = 2; | |
}, | |
[&](Type1) { | |
i = 1; | |
}, | |
[&](Type4) { | |
i = 4; | |
}); | |
} | |
TEST_CASE("VariantCallback should match lambda based on argument type and then return a value.") | |
{ | |
REQUIRE(getInt(Type1{}) == 1); | |
REQUIRE(getInt(Type2{}) == 2); | |
REQUIRE(getInt(Type4{}) == 4); | |
//Type3 doesn't compile because there is no match | |
//this prevents returning an undefined value (for not void return types) | |
//REQUIRE(getInt(Type3{}) == 3); | |
}; | |
TEST_CASE("VariantCallback should match lambda based on argument type and then not return a value.") | |
{ | |
int value = 0; | |
modifyInt(value, Type1{}); | |
REQUIRE(value == 1); | |
modifyInt(value, Type2{}); | |
REQUIRE(value == 2); | |
modifyInt(value, Type4{}); | |
REQUIRE(value == 4); | |
}; | |
TEST_CASE("VariantCallback returning void should allow unspecified types and do nothing.") | |
{ | |
int value = 56; | |
modifyInt(value, Type3{}); | |
REQUIRE(value == 56); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment