dim.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_UTILITY_DIM_HPP
  11. #define BOOST_COMPUTE_UTILITY_DIM_HPP
  12. #include <boost/compute/config.hpp>
  13. #include <boost/compute/utility/extents.hpp>
  14. namespace boost {
  15. namespace compute {
  16. #ifndef BOOST_COMPUTE_NO_VARIADIC_TEMPLATES
  17. /// The variadic \c dim() function provides a concise syntax for creating
  18. /// \ref extents objects.
  19. ///
  20. /// For example,
  21. /// \code
  22. /// extents<2> region = dim(640, 480); // region == (640, 480)
  23. /// \endcode
  24. ///
  25. /// \see \ref extents "extents<N>"
  26. template<class... Args>
  27. inline extents<sizeof...(Args)> dim(Args... args)
  28. {
  29. return extents<sizeof...(Args)>({ static_cast<size_t>(args)... });
  30. }
  31. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1800)
  32. // for some inexplicable reason passing one parameter to 'dim' variadic template
  33. // generates compile error on msvc 2013 update 4
  34. template<class T>
  35. inline extents<1> dim(T arg)
  36. {
  37. return extents<1>(static_cast<size_t>(arg));
  38. }
  39. #endif // BOOST_WORKAROUND(BOOST_MSVC, <= 1800)
  40. #else
  41. // dim() function definitions for non-c++11 compilers
  42. #define BOOST_COMPUTE_DETAIL_ASSIGN_DIM(z, n, var) \
  43. var[n] = BOOST_PP_CAT(e, n);
  44. #define BOOST_COMPUTE_DETAIL_DEFINE_DIM(z, n, var) \
  45. inline extents<n> dim(BOOST_PP_ENUM_PARAMS(n, size_t e)) \
  46. { \
  47. extents<n> exts; \
  48. BOOST_PP_REPEAT(n, BOOST_COMPUTE_DETAIL_ASSIGN_DIM, exts) \
  49. return exts; \
  50. }
  51. BOOST_PP_REPEAT(BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_DETAIL_DEFINE_DIM, ~)
  52. #undef BOOST_COMPUTE_DETAIL_ASSIGN_DIM
  53. #undef BOOST_COMPUTE_DETAIL_DEFINE_DIM
  54. #endif // BOOST_COMPUTE_NO_VARIADIC_TEMPLATES
  55. /// \internal_
  56. template<size_t N>
  57. inline extents<N> dim()
  58. {
  59. return extents<N>();
  60. }
  61. } // end compute namespace
  62. } // end boost namespace
  63. #endif // BOOST_COMPUTE_UTILITY_DIM_HPP