New examples (#47)

* simplify yaml build

* add simple_match
This commit is contained in:
Lars Melchior
2019-05-17 18:07:31 +02:00
committed by GitHub
parent 8253393c75
commit b1ef60175a
3 changed files with 59 additions and 3 deletions

View File

@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
# ---- Dependencies ----
include(../../cmake/CPM.cmake)
CPMAddPackage(
NAME simple_match
GIT_REPOSITORY https://github.com/jbandela/simple_match.git
GIT_TAG a3ab17f3d98db302de68ad85ed399a42ae41889e
DOWNLOAD_ONLY True
)
add_library(simple_match INTERFACE)
target_include_directories(simple_match INTERFACE "${simple_match_SOURCE_DIR}/include")
# ---- Executable ----
add_executable(CPMSimpleMatchExample "main.cpp")
set_target_properties(CPMSimpleMatchExample PROPERTIES CXX_STANDARD 17)
target_link_libraries(CPMSimpleMatchExample simple_match)

View File

@@ -0,0 +1,34 @@
#include <simple_match/simple_match.hpp>
#include <iostream>
int main(int argc, char ** argv){
using namespace simple_match;
using namespace simple_match::placeholders;
std::string input;
std::cout << "please enter a number or 'quit' to exit" << std::endl;
while (true) {
std::cout << "> ";
std::getline(std::cin, input);
if (input == "quit") { break; }
int x;
try {
x = std::stoi(input);
} catch(std::invalid_argument &) {
std::cout << "invalid input" << std::endl;
continue;
}
match(x,
1, []() {std::cout << "The answer is one\n"; },
2, []() {std::cout << "The answer is two\n"; },
_x < 10, [](auto&& a) {std::cout << "The answer " << a << " is less than 10\n"; },
10 < _x < 20, [](auto&& a) {std::cout << "The answer " << a << " is between 10 and 20 exclusive\n"; },
_, []() {std::cout << "Did not match\n"; }
);
}
return 0;
}

View File

@@ -13,9 +13,9 @@ CPMAddPackage(
GIT_TAG 012269756149ae99745b6dafefd415843d7420bb
OPTIONS
"YAML_CPP_BUILD_TESTS Off"
"YAML_CPP_BUILD_CONTRIB On"
"YAML_CPP_BUILD_TOOLS On"
)
"YAML_CPP_BUILD_CONTRIB Off"
"YAML_CPP_BUILD_TOOLS Off"
)
# ---- Executable ----