Skip to content

Commit

Permalink
Merge pull request #7 from TartanLlama/cmake_love
Browse files Browse the repository at this point in the history
Do CMake properly
  • Loading branch information
TartanLlama authored Jun 25, 2019
2 parents b87a35a + 1d4e6f4 commit 556c2c3
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 52 deletions.
1 change: 1 addition & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ os:
- Visual Studio 2017

build_script:
- git submodule update --init --recursive
- mkdir build
- cd build
- cmake ..
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "cmake/tl-cmake"]
path = cmake/tl-cmake
url = https://github.com/TartanLlama/tl-cmake.git
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: cpp

dist: trusty
dist: xenial
sudo: false

matrix:
Expand Down Expand Up @@ -33,7 +33,7 @@ matrix:
addons:
apt:
sources:
- llvm-toolchain-precise-3.5
- llvm-toolchain-xenial-3.5
- ubuntu-toolchain-r-test
packages:
- clang++-3.5
Expand All @@ -43,7 +43,7 @@ matrix:
addons:
apt:
sources:
- llvm-toolchain-precise-3.6
- llvm-toolchain-xenial-3.6
- ubuntu-toolchain-r-test
packages:
- clang++-3.6
Expand All @@ -53,7 +53,7 @@ matrix:
addons:
apt:
sources:
- llvm-toolchain-precise-3.7
- llvm-toolchain-xenial-3.7
- ubuntu-toolchain-r-test
packages:
- clang++-3.7
Expand All @@ -63,7 +63,7 @@ matrix:
addons:
apt:
sources:
- llvm-toolchain-precise-3.8
- llvm-toolchain-xenial-3.8
- ubuntu-toolchain-r-test
packages:
- clang++-3.8
Expand All @@ -73,7 +73,7 @@ matrix:
addons:
apt:
sources:
- sourceline: "deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-3.9 main"
- sourceline: "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-3.9 main"
key_url: "http://apt.llvm.org/llvm-snapshot.gpg.key"
- ubuntu-toolchain-r-test
packages:
Expand Down
63 changes: 34 additions & 29 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.11)

project(function_ref)
project(tl-function_ref VERSION 1.0.0 LANGUAGES CXX)

# Prepare "Catch" library for other executables
set(CATCH_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/test)
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})

# Make test executable
set(TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/tests/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/constructors.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/call.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/issues.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/assignment.cpp)

add_executable(tests ${TEST_SOURCES})
option(FUNCTION_REF_ENABLE_TESTS "Enable tests." ON)

add_library(function_ref INTERFACE)
target_sources(function_ref INTERFACE ${CMAKE_SOURCE_DIR}/function_ref.hpp)
target_include_directories(function_ref INTERFACE ${CMAKE_SOURCE_DIR})
include(FetchContent)
FetchContent_Declare(
tl_cmake
GIT_REPOSITORY https://github.com/TartanLlama/tl-cmake.git
)
FetchContent_GetProperties(tl_cmake)
if(NOT tl_cmake_POPULATED)
FetchContent_Populate(tl_cmake)
set(CMAKE_MODULE_PATH ${tl_cmake_SOURCE_DIR} ${CMAKE_MODULE_PATH})
endif()
include(add-tl)

target_link_libraries(tests Catch function_ref)
set_property(TARGET tests PROPERTY CXX_STANDARD 14)
tl_add_library(function-ref SOURCES
include/tl/function_ref.hpp)

# Prepare "Catch" library for other executables
set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test)
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})

find_package(standardese) # find standardese after installation

# generates a custom target that will run standardese to generate the documentation
if (standardese_FOUND)
standardese_generate(function_ref
INCLUDE_DIRECTORY .
CONFIG ${CMAKE_SOURCE_DIR}/standardese.config
INPUT function_ref.hpp)
endif ()
if(FUNCTION_REF_ENABLE_TESTS)
# Make test executable
set(TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/tests/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/constructors.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/call.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/issues.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/assignment.cpp)

add_executable(tests ${TEST_SOURCES})

target_link_libraries(tests Catch function-ref)

set_property(TARGET tests PROPERTY CXX_STANDARD 14)
endif()
1 change: 1 addition & 0 deletions cmake/tl-cmake
Submodule tl-cmake added at 284c6a
3 changes: 3 additions & 0 deletions cmake/tl-function-ref-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/tl-function-ref-targets.cmake")
26 changes: 13 additions & 13 deletions function_ref.hpp → include/tl/function_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
#ifndef TL_FUNCTION_REF_HPP
#define TL_FUNCTION_REF_HPP

#define TL_FUNCTION_REF_VERSION_MAJOR 0
#define TL_FUNCTION_REF_VERSION_MINOR 3
#define TL_FUNCTION_REF_VERSION_MAJOR 1
#define TL_FUNCTION_REF_VERSION_MINOR 0
#define TL_FUNCTION_REF_VERSION_PATCH 0

#if (defined(_MSC_VER) && _MSC_VER == 1900)
/// \exclude
Expand Down Expand Up @@ -62,8 +63,7 @@

namespace tl {
namespace detail {
#ifndef TL_TRAITS_MUTEX
#define TL_TRAITS_MUTEX
namespace fnref {
// C++14-style aliases for brevity
template <class T> using remove_const_t = typename std::remove_const<T>::type;
template <class T>
Expand Down Expand Up @@ -98,17 +98,16 @@ template <class F, class, class... Us> struct invoke_result_impl;

template <class F, class... Us>
struct invoke_result_impl<
F, decltype(invoke(std::declval<F>(), std::declval<Us>()...), void()),
F, decltype(tl::detail::fnref::invoke(std::declval<F>(), std::declval<Us>()...), void()),
Us...> {
using type = decltype(invoke(std::declval<F>(), std::declval<Us>()...));
using type = decltype(tl::detail::fnref::invoke(std::declval<F>(), std::declval<Us>()...));
};

template <class F, class... Us>
using invoke_result = invoke_result_impl<F, void, Us...>;

template <class F, class... Us>
using invoke_result_t = typename invoke_result<F, Us...>::type;
#endif

template <class, class R, class F, class... Args>
struct is_invocable_r_impl : std::false_type {};
Expand All @@ -122,6 +121,7 @@ template <class R, class F, class... Args>
using is_invocable_r = is_invocable_r_impl<std::true_type, R, F, Args...>;

} // namespace detail
} // namespace fnref

/// A lightweight non-owning reference to a callable.
///
Expand All @@ -147,13 +147,13 @@ template <class R, class... Args> class function_ref<R(Args...)> {
///
/// \synopsis template <typename F> constexpr function_ref(F &&f) noexcept
template <typename F,
detail::enable_if_t<
!std::is_same<detail::decay_t<F>, function_ref>::value &&
detail::is_invocable_r<R, F &&, Args...>::value> * = nullptr>
detail::fnref::enable_if_t<
!std::is_same<detail::fnref::decay_t<F>, function_ref>::value &&
detail::fnref::is_invocable_r<R, F &&, Args...>::value> * = nullptr>
TL_FUNCTION_REF_11_CONSTEXPR function_ref(F &&f) noexcept
: obj_(const_cast<void*>(reinterpret_cast<const void *>(std::addressof(f)))) {
callback_ = [](void *obj, Args... args) -> R {
return detail::invoke(
return detail::fnref::invoke(
*reinterpret_cast<typename std::add_pointer<F>::type>(obj),
std::forward<Args>(args)...);
};
Expand All @@ -167,12 +167,12 @@ template <class R, class... Args> class function_ref<R(Args...)> {
///
/// \synopsis template <typename F> constexpr function_ref &operator=(F &&f) noexcept;
template <typename F,
detail::enable_if_t<detail::is_invocable_r<R, F &&, Args...>::value>
detail::fnref::enable_if_t<detail::fnref::is_invocable_r<R, F &&, Args...>::value>
* = nullptr>
TL_FUNCTION_REF_11_CONSTEXPR function_ref<R(Args...)> &operator=(F &&f) noexcept {
obj_ = reinterpret_cast<void *>(std::addressof(f));
callback_ = [](void *obj, Args... args) {
return detail::invoke(
return detail::fnref::invoke(
*reinterpret_cast<typename std::add_pointer<F>::type>(obj),
std::forward<Args>(args)...);
};
Expand Down
2 changes: 1 addition & 1 deletion tests/assignment.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "catch.hpp"
#include "function_ref.hpp"
#include <tl/function_ref.hpp>

void f(){}
struct b {
Expand Down
2 changes: 1 addition & 1 deletion tests/call.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "catch.hpp"
#include "function_ref.hpp"
#include <tl/function_ref.hpp>

namespace {
bool f_called = false;
Expand Down
2 changes: 1 addition & 1 deletion tests/constructors.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "catch.hpp"
#include "function_ref.hpp"
#include <tl/function_ref.hpp>

void foo(){}
struct bar {
Expand Down
12 changes: 11 additions & 1 deletion tests/issues.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "catch.hpp"
#include "function_ref.hpp"
#include <tl/function_ref.hpp>

TEST_CASE("Issue #2") {
const auto lam = [](int ) {};
Expand All @@ -19,4 +19,14 @@ tl::function_ref<Fruit* ()> bar()

TEST_CASE("Issue #9") {
bar();
}

void foo(const tl::function_ref<int(const std::vector<int>)>& func) {
REQUIRE(func({ 12 }) == 144);
}

TEST_CASE("Issue #10") {
int z = 12;
auto f = [&](const std::vector<int> i) { return i[0] * z; };
foo(f);
}

0 comments on commit 556c2c3

Please sign in to comment.