field.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 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_FUNCTIONAL_FIELD_HPP
  11. #define BOOST_COMPUTE_FUNCTIONAL_FIELD_HPP
  12. #include <string>
  13. namespace boost {
  14. namespace compute {
  15. namespace detail {
  16. template<class T, class Arg>
  17. struct invoked_field
  18. {
  19. typedef T result_type;
  20. invoked_field(const Arg &arg, const std::string &field)
  21. : m_arg(arg),
  22. m_field(field)
  23. {
  24. }
  25. Arg m_arg;
  26. std::string m_field;
  27. };
  28. } // end detail namespace
  29. /// Returns the named field from a value.
  30. ///
  31. /// The template-type \c T specifies the field's value type. Note
  32. /// that the value type must match the actual type of the field
  33. /// otherwise runtime compilation or logic errors may occur.
  34. ///
  35. /// For example, to access the \c second field in a
  36. /// \c std::pair<int, float> object:
  37. /// \code
  38. /// field<float>("second");
  39. /// \endcode
  40. ///
  41. /// This can also be used with vector types to access individual
  42. /// components as well as perform swizzle operations.
  43. ///
  44. /// For example, to access the first and third components of an
  45. /// \c int vector type (e.g. \c int4):
  46. /// \code
  47. /// field<int2_>("xz");
  48. /// \endcode
  49. ///
  50. /// \see \ref get "get<N>"
  51. template<class T>
  52. class field
  53. {
  54. public:
  55. /// Result type.
  56. typedef T result_type;
  57. /// Creates a new field functor with \p field.
  58. field(const std::string &field)
  59. : m_field(field)
  60. {
  61. }
  62. /// \internal_
  63. template<class Arg>
  64. detail::invoked_field<T, Arg> operator()(const Arg &arg) const
  65. {
  66. return detail::invoked_field<T, Arg>(arg, m_field);
  67. }
  68. private:
  69. std::string m_field;
  70. };
  71. } // end compute namespace
  72. } // end boost namespace
  73. #endif // BOOST_COMPUTE_FUNCTIONAL_FIELD_HPP