Skip to content

Commit

Permalink
v0.5 (#9)
Browse files Browse the repository at this point in the history
* update travis

* use CPM

* add any tests and bugfixes

* v0.5

* update CPM
  • Loading branch information
TheLartians authored Apr 10, 2019
1 parent fa1d510 commit 2341fa0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cmake_minimum_required (VERSION 3.5)
# ---- create project ----

project(LarsVisitor
VERSION 0.3
VERSION 0.5
LANGUAGES CXX
)

Expand All @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -16,6 +18,7 @@ function(CPMAddPackage)
VERSION
GIT_TAG
BINARY_DIR
UPDATE_DISCONNECTED
)

set(multiValueArgs "")
Expand Down
2 changes: 1 addition & 1 deletion cmake/CPMProject.CMakeLists.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions include/lars/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace lars{

TypeIndex type()const{ return _type; }

template <class T> T &get_reference(){
template <class T> typename std::enable_if<!std::is_same<T,Any>::value,T &>::type get_reference(){
if(!data) throw BadAnyCast("cannot extract value: data not set.");
struct GetVisitor:public RecursiveVisitor<VisitableScalar<T>,VisitableScalar<std::shared_ptr<T>>,T>{
T * result = nullptr;
Expand All @@ -75,7 +75,7 @@ namespace lars{
else throw BadAnyCast("cannot convert " + std::string(type().name().begin(),type().name().end()) + " to " + get_type_name<T>());
}

template <class T> const T &get_reference()const{
template <class T> typename std::enable_if<!std::is_same<T,Any>::value,const T &>::type get_reference()const{
if(!data) throw BadAnyCast("cannot extract value: data not set.");
// if(type() == get_type_index<T>()) return static_cast<VisitableType<T>>(*data.get()).data;
struct GetVisitor:public RecursiveConstVisitor<VisitableScalar<T>,VisitableScalar<std::shared_ptr<T>>,T>{
Expand All @@ -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<T>());
}

template <class T> typename std::enable_if<std::is_same<T,Any>::value,T &>::type get_reference(){
return *this;
}

template <class T> typename std::enable_if<std::is_same<T,Any>::value,const T &>::type get_reference()const{
return *this;
}

template <class T = double> T get_numeric()const{
struct GetVisitor:public ConstVisitor<VisitableType<float>,VisitableType<double>,VisitableType<unsigned>,VisitableType<int>,VisitableType<bool>,VisitableType<char>>{
Expand Down Expand Up @@ -143,6 +151,14 @@ namespace lars{
_type = lars::get_type_index<typename T::element_type>();
}

void set(Any && other){
*this = std::forward<Any>(other);
}

void set(const Any &other){
*this = other;
}

template <class Visitor> void accept_visitor(Visitor &visitor){ assert(data); data->accept(visitor); }
template <class ConstVisitor> void accept_visitor(ConstVisitor &visitor)const{ assert(data); data->accept(visitor); }

Expand Down
22 changes: 22 additions & 0 deletions tests/any.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,25 @@ TEST_CASE("Any") {
REQUIRE_THROWS_AS(any.get<std::string>(), Any::BadAnyCast);

}

TEST_CASE("AnyFunction") {
using namespace lars;

AnyFunction get = [](){ return 42; };
REQUIRE(get().get<int>() == 42);

AnyFunction getAny = [](){ return make_any<int>(42); };
REQUIRE(getAny().get<int>() == 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));

}

0 comments on commit 2341fa0

Please sign in to comment.