Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simplify library related macros

  • Loading branch information...
commit a3104fd5608aa2457875c0b3a9e3b776a297f6fb 1 parent 6e9e938
@zhouyan authored
View
69 CMakeLists.txt
@@ -120,7 +120,9 @@ IF (Eigen_FOUND AND Random123_FOUND)
ENDIF (LINUX_RT)
ENDIF (UNIX AND (NOT APPLE) AND (NOT WIN32))
FILE (READ ${PROJECT_SOURCE_DIR}/cmake/try_chrono.cpp TRY_CHRONO_CPP)
+ SET (CMAKE_REQUIRED_FLAGS "-DVSMC_HAS_CXX11LIB_CHRONO=1")
CHECK_CXX_SOURCE_RUNS ("${TRY_CHRONO_CPP}" VSMC_USE_STD_CHRONO)
+ SET (CMAKE_REQUIRED_FLAGS "-DVSMC_HAS_CXX11LIB_CHRONO=0")
CHECK_CXX_SOURCE_RUNS ("${TRY_CHRONO_CPP}" VSMC_USE_BOOST_CHRONO)
SET (CMAKE_REQUIRED_LIBRARIES)
@@ -141,42 +143,51 @@ IF (Eigen_FOUND AND Random123_FOUND)
SET (VSMC_USE_CHRONO FALSE)
IF (VSMC_STD_CHRONO AND VSMC_USE_STD_CHRONO)
SET (VSMC_USE_CHRONO TRUE)
- SET (VSMC_TEST_FLAGS "${VSMC_TEST_FLAGS} -DVSMC_USE_STD_CHRONO")
+ SET (VSMC_TEST_FLAGS
+ "${VSMC_TEST_FLAGS} -DVSMC_HAS_CXX11LIB_CHRONO=1")
ELSEIF (VSMC_BOOST_CHRONO AND VSMC_USE_BOOST_CHRONO)
SET (VSMC_USE_CHRONO TRUE)
- SET (VSMC_TEST_FLAGS "${VSMC_TEST_FLAGS} -DVSMC_USE_BOOST_CHRONO")
+ SET (VSMC_TEST_FLAGS
+ "${VSMC_TEST_FLAGS} -DVSMC_HAS_CXX11LIB_CHRONO=0")
ENDIF (VSMC_STD_CHRONO AND VSMC_USE_STD_CHRONO)
# Test <functional>
- FILE (READ ${PROJECT_SOURCE_DIR}/cmake/try_function.cpp TRY_FUNCTION_CPP)
- CHECK_CXX_SOURCE_RUNS ("${TRY_FUNCTION_CPP}" VSMC_USE_STD_FUNCTION)
- CHECK_CXX_SOURCE_RUNS ("${TRY_FUNCTION_CPP}" VSMC_USE_BOOST_FUNCTION)
-
- IF (VSMC_FUNCTION)
- STRING (REGEX MATCH "std" VSMC_STD_FUNCTION ${VSMC_FUNCTION})
- STRING (REGEX MATCH "boost" VSMC_BOOST_FUNCTION ${VSMC_FUNCTION})
- ENDIF (VSMC_FUNCTION)
+ FILE (READ ${PROJECT_SOURCE_DIR}/cmake/try_functional.cpp
+ TRY_FUNCTIONAL_CPP)
+ SET (CMAKE_REQUIRED_FLAGS "-DVSMC_HAS_CXX11LIB_FUNCTIONAL=1")
+ CHECK_CXX_SOURCE_RUNS ("${TRY_FUNCTIONAL_CPP}" VSMC_USE_STD_FUNCTIONAL)
+ SET (CMAKE_REQUIRED_FLAGS "-DVSMC_HAS_CXX11LIB_FUNCTIONAL=0")
+ CHECK_CXX_SOURCE_RUNS ("${TRY_FUNCTIONAL_CPP}" VSMC_USE_BOOST_FUNCTIONAL)
+
+ IF (VSMC_FUNCTIONAL)
+ STRING (REGEX MATCH "std" VSMC_STD_FUNCTIONAL ${VSMC_FUNCTIONAL})
+ STRING (REGEX MATCH "boost" VSMC_BOOST_FUNCTIONAL ${VSMC_FUNCTIONAL})
+ ENDIF (VSMC_FUNCTIONAL)
IF (NOT ( # No match between prefered and available
- (VSMC_STD_FUNCTION AND VSMC_USE_STD_FUNCTION) OR
- (VSMC_BOOST_FUNCTION AND VSMC_USE_BOOST_FUNCTION)))
- SET (VSMC_STD_FUNCTION TRUE)
- SET (VSMC_BOOST_FUNCTION TRUE)
+ (VSMC_STD_FUNCTIONAL AND VSMC_USE_STD_FUNCTIONAL) OR
+ (VSMC_BOOST_FUNCTIONAL AND VSMC_USE_BOOST_FUNCTIONAL)))
+ SET (VSMC_STD_FUNCTIONAL TRUE)
+ SET (VSMC_BOOST_FUNCTIONAL TRUE)
ENDIF (NOT ( # No match between prefered and available
- (VSMC_STD_FUNCTION AND VSMC_USE_STD_FUNCTION) OR
- (VSMC_BOOST_FUNCTION AND VSMC_USE_BOOST_FUNCTION)))
-
- IF (VSMC_STD_FUNCTION AND VSMC_USE_STD_FUNCTION)
- SET (VSMC_TEST_FLAGS "${VSMC_TEST_FLAGS} -DVSMC_USE_STD_FUNCTION")
- ELSEIF (VSMC_BOOST_FUNCTION AND VSMC_USE_BOOST_FUNCTION)
- SET (VSMC_TEST_FLAGS "${VSMC_TEST_FLAGS} -DVSMC_USE_BOOST_FUNCTION")
- ELSE (VSMC_STD_FUNCTION AND VSMC_USE_STD_FUNCTION)
+ (VSMC_STD_FUNCTIONAL AND VSMC_USE_STD_FUNCTIONAL) OR
+ (VSMC_BOOST_FUNCTIONAL AND VSMC_USE_BOOST_FUNCTIONAL)))
+
+ IF (VSMC_STD_FUNCTIONAL AND VSMC_USE_STD_FUNCTIONAL)
+ SET (VSMC_TEST_FLAGS
+ "${VSMC_TEST_FLAGS} -DVSMC_HAS_CXX11LIB_FUNCTIONAL=1")
+ ELSEIF (VSMC_BOOST_FUNCTIONAL AND VSMC_USE_BOOST_FUNCTIONAL)
+ SET (VSMC_TEST_FLAGS
+ "${VSMC_TEST_FLAGS} -DVSMC_HAS_CXX11LIB_FUNCTIONAL=0")
+ ELSE (VSMC_STD_FUNCTIONAL AND VSMC_USE_STD_FUNCTIONAL)
SET (VSMC_TEST FALSE)
- ENDIF (VSMC_STD_FUNCTION AND VSMC_USE_STD_FUNCTION)
+ ENDIF (VSMC_STD_FUNCTIONAL AND VSMC_USE_STD_FUNCTIONAL)
# Test <random>
FILE (READ ${PROJECT_SOURCE_DIR}/cmake/try_random.cpp TRY_RANDOM_CPP)
+ SET (CMAKE_REQUIRED_FLAGS "-DVSMC_HAS_CXX11LIB_RANDOM=1")
CHECK_CXX_SOURCE_RUNS ("${TRY_RANDOM_CPP}" VSMC_USE_STD_RANDOM)
+ SET (CMAKE_REQUIRED_FLAGS "-DVSMC_HAS_CXX11LIB_RANDOM=0")
CHECK_CXX_SOURCE_RUNS ("${TRY_RANDOM_CPP}" VSMC_USE_BOOST_RANDOM)
IF (VSMC_RANDOM)
@@ -194,9 +205,11 @@ IF (Eigen_FOUND AND Random123_FOUND)
(VSMC_BOOST_RANDOM AND VSMC_USE_BOOST_RANDOM)))
IF (VSMC_STD_RANDOM AND VSMC_USE_STD_RANDOM)
- SET (VSMC_TEST_FLAGS "${VSMC_TEST_FLAGS} -DVSMC_USE_STD_RANDOM")
+ SET (VSMC_TEST_FLAGS
+ "${VSMC_TEST_FLAGS} -DVSMC_HAS_CXX11LIB_RANDOM=1")
ELSEIF (VSMC_BOOST_RANDOM AND VSMC_USE_BOOST_RANDOM)
- SET (VSMC_TEST_FLAGS "${VSMC_TEST_FLAGS} -DVSMC_USE_BOOST_RANDOM")
+ SET (VSMC_TEST_FLAGS
+ "${VSMC_TEST_FLAGS} -DVSMC_HAS_CXX11LIB_RANDOM=0")
ELSE (VSMC_STD_RANDOM AND VSMC_USE_STD_RANDOM)
SET (VSMC_TEST FALSE)
ENDIF (VSMC_STD_RANDOM AND VSMC_USE_STD_RANDOM)
@@ -204,8 +217,10 @@ IF (Eigen_FOUND AND Random123_FOUND)
# Test <type_traits>
FILE (READ ${PROJECT_SOURCE_DIR}/cmake/try_type_traits.cpp
TRY_TYPE_TRAITS_CPP)
+ SET (CMAKE_REQUIRED_FLAGS "-DVSMC_HAS_CXX11LIB_TYPE_TRAITS=1")
CHECK_CXX_SOURCE_RUNS ("${TRY_TYPE_TRAITS_CPP}"
VSMC_USE_STD_TYPE_TRAITS)
+ SET (CMAKE_REQUIRED_FLAGS "-DVSMC_HAS_CXX11LIB_TYPE_TRAITS=0")
CHECK_CXX_SOURCE_RUNS ("${TRY_TYPE_TRAITS_CPP}"
VSMC_USE_BOOST_TYPE_TRAITS)
@@ -227,10 +242,10 @@ IF (Eigen_FOUND AND Random123_FOUND)
IF (VSMC_STD_TYPE_TRAITS AND VSMC_USE_STD_TYPE_TRAITS)
SET (VSMC_TEST_FLAGS
- "${VSMC_TEST_FLAGS} -DVSMC_USE_STD_TYPE_TRAITS")
+ "${VSMC_TEST_FLAGS} -DVSMC_HAS_CXX11LIB_TYPE_TRAITS=1")
ELSEIF (VSMC_BOOST_TYPE_TRAITS AND VSMC_USE_BOOST_TYPE_TRAITS)
SET (VSMC_TEST_FLAGS
- "${VSMC_TEST_FLAGS} -DVSMC_USE_BOOST_TYPE_TRAITS")
+ "${VSMC_TEST_FLAGS} -DVSMC_HAS_CXX11LIB_TYPE_TRAITS=0")
ELSE (VSMC_STD_TYPE_TRAITS AND VSMC_USE_STD_TYPE_TRAITS)
SET (VSMC_TEST FALSE)
ENDIF (VSMC_STD_TYPE_TRAITS AND VSMC_USE_STD_TYPE_TRAITS)
View
45 cmake/try_functional.cpp
@@ -0,0 +1,45 @@
+#include <cassert>
+#include <cstddef>
+#include <vsmc/internal/functional.hpp>
+
+int fn (int a)
+{
+ return 2 * a;
+}
+
+class cl
+{
+ public :
+
+ typedef vsmc::internal::function<int (int)> f_type;
+
+ cl (f_type f = NULL) : f_(f) {}
+
+ int operator() (int a)
+ {
+ return f_(a);
+ }
+
+ private :
+
+ f_type f_;
+};
+
+int main ()
+{
+ vsmc::internal::function<int (int)> f;
+ assert(!bool(f));
+
+ f = fn;
+ assert(bool(f));
+ assert(f(2) == 4);
+
+ f = NULL;
+ assert(!bool(f));
+
+ cl c;
+ c = cl(fn);
+ assert(c(2) == 4);
+
+ return 0;
+}
View
10 doc/macros.markdown
@@ -76,7 +76,7 @@ followed. For example, in namespace `vsmc::rng`, `bernoulli_distribution` is a
`typedef` of `boost::random::bernoulli_distribution<double>` if this macro is
not defined.
-* \b `VSMC_HAS_CXX11LIB_FUNCTION`
+* \b `VSMC_HAS_CXX11LIB_FUNCTIONAL`
If defined to non-zero value, vSMC use C++11 `<funtional>` instead of
`<boost/function.hpp>`. User can use either library regardless of what vSMC
use.
@@ -86,14 +86,6 @@ If defined to non-zero value, vSMC use C++11 `<type_traits>` instead of
`<boost/type_traits.hpp>`. User can use either library regardless of what vSMC
use.
-There are also three other macros, `VSMC_USE_STD_RANDOM`,
-`VSMC_USE_STD_FUNCTION` and `VSMC_USE_STD_TYPE_TRAITS`, which when defined,
-will enable the corresponding C++11 libraries as well. They exists only for
-backward compatibility. The use of the three listed above are recommended
-since they also allow users to explicitly disable the use of a certain library
-even when vSMC may try to detect the existence of these libraries in future
-(for example, when the implementation is incorrect).
-
For the time being, vSMC defaults to use [Boost][Boost] instead of C++11
features. This is mainly due to the that, unlike language features, there is
no easy way to detect which standard C++ library is available, without
View
2  include/vsmc/helper/parallel_cl/cl.hpp
@@ -190,7 +190,7 @@
// #else
// #include <tr1/functional>
// #endif
-#include <vsmc/internal/function.hpp>
+#include <vsmc/internal/functional.hpp>
/*! \namespace cl
*
View
2  include/vsmc/internal/common.hpp
@@ -24,7 +24,7 @@
#include <vsmc/internal/config.hpp>
#include <vsmc/internal/version.hpp>
-#include <vsmc/internal/function.hpp>
+#include <vsmc/internal/functional.hpp>
#include <vsmc/internal/type_traits.hpp>
#include <vsmc/internal/types.hpp>
#include <vsmc/internal/forward.hpp>
View
20 include/vsmc/internal/config.hpp
@@ -1,22 +1,6 @@
#ifndef VSMC_INTERNAL_CONFIG_HPP
#define VSMC_INTERNAL_CONFIG_HPP
-#ifdef VSMC_USE_STD_CHRONO
-#define VSMC_HAS_CXX11LIB_CHRONO 1
-#endif
-
-#ifdef VSMC_USE_STD_FUNCTION
-#define VSMC_HAS_CXX11LIB_FUNCTION 1
-#endif
-
-#ifdef VSMC_USE_STD_RANDOM
-#define VSMC_HAS_CXX11LIB_RANDOM 1
-#endif
-
-#ifdef VSMC_USE_STD_TYPE_TRAITS
-#define VSMC_HAS_CXX11LIB_TYPE_TRAITS 1
-#endif
-
#include <vsmc/internal/compiler.hpp>
#ifndef VSMC_HAS_CXX11_AUTO_TYPE
@@ -43,8 +27,8 @@
#define VSMC_HAS_CXX11LIB_CHRONO 0
#endif
-#ifndef VSMC_HAS_CXX11LIB_FUNCTION
-#define VSMC_HAS_CXX11LIB_FUNCTION 0
+#ifndef VSMC_HAS_CXX11LIB_FUNCTIONAL
+#define VSMC_HAS_CXX11LIB_FUNCTIONAL 0
#endif
#ifndef VSMC_HAS_CXX11LIB_RANDOM
View
12 include/vsmc/internal/function.hpp → include/vsmc/internal/functional.hpp
@@ -1,22 +1,22 @@
-#ifndef VSMC_INTERNAL_FUNCTION_HPP
-#define VSMC_INTERNAL_FUNCTION_HPP
+#ifndef VSMC_INTERNAL_FUNCTIONAL_HPP
+#define VSMC_INTERNAL_FUNCTIONAL_HPP
#include <vsmc/internal/config.hpp>
-#if VSMC_HAS_CXX11LIB_FUNCTION
+#if VSMC_HAS_CXX11LIB_FUNCTIONAL
#include <functional>
namespace vsmc { namespace internal {
using std::function;
} }
-#else // VSMC_HAS_CXX11LIB_FUNCTION
+#else // VSMC_HAS_CXX11LIB_FUNCTIONAL
#include <boost/function.hpp>
namespace vsmc { namespace internal {
using boost::function;
} }
-#endif // VSMC_HAS_CXX11LIB_FUNCTION
+#endif // VSMC_HAS_CXX11LIB_FUNCTIONAL
-#endif // VSMC_INTERNAL_FUNCTION_HPP
+#endif // VSMC_INTERNAL_FUNCTIONAL_HPP
Please sign in to comment.
Something went wrong with that request. Please try again.