[29] | 1 | // Copyright Daniel Wallin, David Abrahams 2005. Use, modification and |
---|
| 2 | // distribution is subject to the Boost Software License, Version 1.0. (See |
---|
| 3 | // accompanying file LICENSE_1_0.txt or copy at |
---|
| 4 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
| 5 | |
---|
| 6 | #ifndef KEYWORD_050328_HPP |
---|
| 7 | #define KEYWORD_050328_HPP |
---|
| 8 | |
---|
| 9 | #include <boost/parameter/aux_/unwrap_cv_reference.hpp> |
---|
| 10 | #include <boost/parameter/aux_/tag.hpp> |
---|
| 11 | #include <boost/parameter/aux_/default.hpp> |
---|
| 12 | |
---|
| 13 | namespace boost { namespace parameter { |
---|
| 14 | |
---|
| 15 | // Instances of unique specializations of keyword<...> serve to |
---|
| 16 | // associate arguments with parameter names. For example: |
---|
| 17 | // |
---|
| 18 | // struct rate_; // parameter names |
---|
| 19 | // struct skew_; |
---|
| 20 | // namespace |
---|
| 21 | // { |
---|
| 22 | // keyword<rate_> rate; // keywords |
---|
| 23 | // keyword<skew_> skew; |
---|
| 24 | // } |
---|
| 25 | // |
---|
| 26 | // ... |
---|
| 27 | // |
---|
| 28 | // f(rate = 1, skew = 2.4); |
---|
| 29 | // |
---|
| 30 | template <class Tag> |
---|
| 31 | struct keyword |
---|
| 32 | { |
---|
| 33 | template <class T> |
---|
| 34 | typename aux::tag<Tag, T>::type const |
---|
| 35 | operator=(T& x) const |
---|
| 36 | { |
---|
| 37 | typedef typename aux::tag<Tag, T>::type result; |
---|
| 38 | return result(x); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | template <class Default> |
---|
| 42 | aux::default_<Tag, Default> |
---|
| 43 | operator|(Default& default_) const |
---|
| 44 | { |
---|
| 45 | return aux::default_<Tag, Default>(default_); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | template <class Default> |
---|
| 49 | aux::lazy_default<Tag, Default> |
---|
| 50 | operator||(Default& default_) const |
---|
| 51 | { |
---|
| 52 | return aux::lazy_default<Tag, Default>(default_); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) // avoid partial ordering bugs |
---|
| 56 | template <class T> |
---|
| 57 | typename aux::tag<Tag, T const>::type const |
---|
| 58 | operator=(T const& x) const |
---|
| 59 | { |
---|
| 60 | typedef typename aux::tag<Tag, T const>::type result; |
---|
| 61 | return result(x); |
---|
| 62 | } |
---|
| 63 | #endif |
---|
| 64 | |
---|
| 65 | #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) // avoid partial ordering bugs |
---|
| 66 | template <class Default> |
---|
| 67 | aux::default_<Tag, const Default> |
---|
| 68 | operator|(const Default& default_) const |
---|
| 69 | #if BOOST_WORKAROUND(BOOST_MSVC, == 1300) |
---|
| 70 | volatile |
---|
| 71 | #endif |
---|
| 72 | { |
---|
| 73 | return aux::default_<Tag, const Default>(default_); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | template <class Default> |
---|
| 77 | aux::lazy_default<Tag, Default> |
---|
| 78 | operator||(Default const& default_) const |
---|
| 79 | #if BOOST_WORKAROUND(BOOST_MSVC, == 1300) |
---|
| 80 | volatile |
---|
| 81 | #endif |
---|
| 82 | { |
---|
| 83 | return aux::lazy_default<Tag, Default>(default_); |
---|
| 84 | } |
---|
| 85 | #endif |
---|
| 86 | |
---|
| 87 | public: // Insurance against ODR violations |
---|
| 88 | |
---|
| 89 | // People will need to define these keywords in header files. To |
---|
| 90 | // prevent ODR violations, it's important that the keyword used in |
---|
| 91 | // every instantiation of a function template is the same object. |
---|
| 92 | // We provide a reference to a common instance of each keyword |
---|
| 93 | // object and prevent construction by users. |
---|
| 94 | |
---|
| 95 | static keyword<Tag>& get() |
---|
| 96 | { |
---|
| 97 | static keyword<Tag> result; |
---|
| 98 | return result; |
---|
| 99 | } |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | // Reduces boilerplate required to declare and initialize keywords |
---|
| 103 | // without violating ODR. Declares a keyword tag type with the given |
---|
| 104 | // name in namespace tag_namespace, and declares and initializes a |
---|
| 105 | // reference in an anonymous namespace to a singleton instance of that |
---|
| 106 | // type. |
---|
| 107 | |
---|
| 108 | #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) |
---|
| 109 | |
---|
| 110 | # define BOOST_PARAMETER_KEYWORD(tag_namespace,name) \ |
---|
| 111 | namespace tag_namespace \ |
---|
| 112 | { \ |
---|
| 113 | struct name \ |
---|
| 114 | { \ |
---|
| 115 | static char const* keyword_name() \ |
---|
| 116 | { \ |
---|
| 117 | return #name; \ |
---|
| 118 | } \ |
---|
| 119 | }; \ |
---|
| 120 | } \ |
---|
| 121 | static ::boost::parameter::keyword<tag_namespace::name>& name \ |
---|
| 122 | = ::boost::parameter::keyword<tag_namespace::name>::get(); |
---|
| 123 | |
---|
| 124 | #else |
---|
| 125 | |
---|
| 126 | #define BOOST_PARAMETER_KEYWORD(tag_namespace,name) \ |
---|
| 127 | namespace tag_namespace \ |
---|
| 128 | { \ |
---|
| 129 | struct name \ |
---|
| 130 | { \ |
---|
| 131 | static char const* keyword_name() \ |
---|
| 132 | { \ |
---|
| 133 | return #name; \ |
---|
| 134 | } \ |
---|
| 135 | }; \ |
---|
| 136 | } \ |
---|
| 137 | namespace \ |
---|
| 138 | { \ |
---|
| 139 | ::boost::parameter::keyword<tag_namespace::name>& name \ |
---|
| 140 | = ::boost::parameter::keyword<tag_namespace::name>::get(); \ |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | #endif |
---|
| 144 | |
---|
| 145 | }} // namespace boost::parameter |
---|
| 146 | |
---|
| 147 | #endif // KEYWORD_050328_HPP |
---|
| 148 | |
---|