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