From 2341fa0d0a060efa6b24c6d1b16b4378035d1a9f Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Wed, 10 Apr 2019 16:39:31 +0200 Subject: [PATCH] v0.5 (#9) * update travis * use CPM * add any tests and bugfixes * v0.5 * update CPM --- CMakeLists.txt | 4 ++-- cmake/CPM.cmake | 9 ++++++--- cmake/CPMProject.CMakeLists.cmake.in | 2 +- include/lars/any.h | 20 ++++++++++++++++++-- tests/any.cpp | 22 ++++++++++++++++++++++ 5 files changed, 49 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d66f84e..f035810 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ cmake_minimum_required (VERSION 3.5) # ---- create project ---- project(LarsVisitor - VERSION 0.3 + VERSION 0.5 LANGUAGES CXX ) @@ -29,7 +29,7 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPM.cmake) CPMAddPackage( NAME LHC GIT_REPOSITORY https://github.com/TheLartians/LHC.git - VERSION 0.3 + VERSION 0.4 ) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/dependencies/ctti) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 38e2342..62cd2ef 100755 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -2,10 +2,12 @@ set(_CPM_Dir "${CMAKE_CURRENT_LIST_DIR}") include(CMakeParseArguments) include(${_CPM_Dir}/DownloadProject.cmake) - -function(CPMHasPackage) -endfunction() +option(CPM_OFFLINE "CPM offline mode" OFF) + +if(NOT ${CPM_OFFLINE}) + set(CPM_PACKAGES "" CACHE INTERNAL "CPM Packages") +endif() function(CPMAddPackage) set(options QUIET) @@ -16,6 +18,7 @@ function(CPMAddPackage) VERSION GIT_TAG BINARY_DIR + UPDATE_DISCONNECTED ) set(multiValueArgs "") diff --git a/cmake/CPMProject.CMakeLists.cmake.in b/cmake/CPMProject.CMakeLists.cmake.in index 3bf475f..477e7dd 100755 --- a/cmake/CPMProject.CMakeLists.cmake.in +++ b/cmake/CPMProject.CMakeLists.cmake.in @@ -17,7 +17,7 @@ else() PROJ @CPM_ARGS_NAME@ GIT_REPOSITORY @CPM_ARGS_GIT_REPOSITORY@ GIT_TAG @CPM_ARGS_GIT_TAG@ - UPDATE_DISCONNECTED 1 + UPDATE_DISCONNECTED @CPM_OFFLINE@ GIT_SHALLOW 1 PREFIX @CPM_ARGS_BINARY_DIR@/dl QUIET diff --git a/include/lars/any.h b/include/lars/any.h index 92aea10..ec53156 100644 --- a/include/lars/any.h +++ b/include/lars/any.h @@ -62,7 +62,7 @@ namespace lars{ TypeIndex type()const{ return _type; } - template T &get_reference(){ + template typename std::enable_if::value,T &>::type get_reference(){ if(!data) throw BadAnyCast("cannot extract value: data not set."); struct GetVisitor:public RecursiveVisitor,VisitableScalar>,T>{ T * result = nullptr; @@ -75,7 +75,7 @@ namespace lars{ else throw BadAnyCast("cannot convert " + std::string(type().name().begin(),type().name().end()) + " to " + get_type_name()); } - template const T &get_reference()const{ + template typename std::enable_if::value,const T &>::type get_reference()const{ if(!data) throw BadAnyCast("cannot extract value: data not set."); // if(type() == get_type_index()) return static_cast>(*data.get()).data; struct GetVisitor:public RecursiveConstVisitor,VisitableScalar>,T>{ @@ -88,6 +88,14 @@ namespace lars{ if(visitor.result) return *visitor.result; else throw BadAnyCast("cannot convert " + std::string(type().name().begin(),type().name().end()) + " to " + get_type_name()); } + + template typename std::enable_if::value,T &>::type get_reference(){ + return *this; + } + + template typename std::enable_if::value,const T &>::type get_reference()const{ + return *this; + } template T get_numeric()const{ struct GetVisitor:public ConstVisitor,VisitableType,VisitableType,VisitableType,VisitableType,VisitableType>{ @@ -143,6 +151,14 @@ namespace lars{ _type = lars::get_type_index(); } + void set(Any && other){ + *this = std::forward(other); + } + + void set(const Any &other){ + *this = other; + } + template void accept_visitor(Visitor &visitor){ assert(data); data->accept(visitor); } template void accept_visitor(ConstVisitor &visitor)const{ assert(data); data->accept(visitor); } diff --git a/tests/any.cpp b/tests/any.cpp index 441f8bf..94f8a0a 100644 --- a/tests/any.cpp +++ b/tests/any.cpp @@ -20,3 +20,25 @@ TEST_CASE("Any") { REQUIRE_THROWS_AS(any.get(), Any::BadAnyCast); } + +TEST_CASE("AnyFunction") { + using namespace lars; + + AnyFunction get = [](){ return 42; }; + REQUIRE(get().get() == 42); + + AnyFunction getAny = [](){ return make_any(42); }; + REQUIRE(getAny().get() == 42); + + AnyFunction take = [](float x, float y){ return x+y; }; + REQUIRE_THROWS(take()); + REQUIRE_THROWS(take(2)); + REQUIRE(take(2,3).get_numeric() == Approx(5)); + REQUIRE(take(2,3.5).get_numeric() == Approx(5.5)); + REQUIRE_THROWS(take(2,3,5)); + + AnyFunction takeAny = [](Any x, Any y){ return x.get_numeric()+y.get_numeric(); }; + REQUIRE(takeAny(2,3).get_numeric() == Approx(5)); + REQUIRE(takeAny(take(2,3.5),3).get_numeric() == Approx(8.5)); + +}