keyword.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef KEYWORD_050328_HPP
  6. #define KEYWORD_050328_HPP
  7. #include <boost/parameter/aux_/unwrap_cv_reference.hpp>
  8. #include <boost/parameter/aux_/tag.hpp>
  9. #include <boost/parameter/aux_/default.hpp>
  10. namespace boost { namespace parameter {
  11. // Instances of unique specializations of keyword<...> serve to
  12. // associate arguments with parameter names. For example:
  13. //
  14. // struct rate_; // parameter names
  15. // struct skew_;
  16. // namespace
  17. // {
  18. // keyword<rate_> rate; // keywords
  19. // keyword<skew_> skew;
  20. // }
  21. //
  22. // ...
  23. //
  24. // f(rate = 1, skew = 2.4);
  25. //
  26. template <class Tag>
  27. struct keyword
  28. {
  29. template <class T>
  30. typename aux::tag<Tag, T>::type const
  31. operator=(T& x) const
  32. {
  33. typedef typename aux::tag<Tag, T>::type result;
  34. return result(x);
  35. }
  36. template <class Default>
  37. aux::default_<Tag, Default>
  38. operator|(Default& default_) const
  39. {
  40. return aux::default_<Tag, Default>(default_);
  41. }
  42. template <class Default>
  43. aux::lazy_default<Tag, Default>
  44. operator||(Default& default_) const
  45. {
  46. return aux::lazy_default<Tag, Default>(default_);
  47. }
  48. template <class T>
  49. typename aux::tag<Tag, T const>::type const
  50. operator=(T const& x) const
  51. {
  52. typedef typename aux::tag<Tag, T const>::type result;
  53. return result(x);
  54. }
  55. template <class Default>
  56. aux::default_<Tag, const Default>
  57. operator|(const Default& default_) const
  58. {
  59. return aux::default_<Tag, const Default>(default_);
  60. }
  61. template <class Default>
  62. aux::lazy_default<Tag, Default>
  63. operator||(Default const& default_) const
  64. {
  65. return aux::lazy_default<Tag, Default>(default_);
  66. }
  67. public: // Insurance against ODR violations
  68. // People will need to define these keywords in header files. To
  69. // prevent ODR violations, it's important that the keyword used in
  70. // every instantiation of a function template is the same object.
  71. // We provide a reference to a common instance of each keyword
  72. // object and prevent construction by users.
  73. static keyword<Tag> const instance;
  74. // This interface is deprecated
  75. static keyword<Tag>& get()
  76. {
  77. return const_cast<keyword<Tag>&>(instance);
  78. }
  79. };
  80. template <class Tag>
  81. keyword<Tag> const keyword<Tag>::instance = {};
  82. // Reduces boilerplate required to declare and initialize keywords
  83. // without violating ODR. Declares a keyword tag type with the given
  84. // name in namespace tag_namespace, and declares and initializes a
  85. // reference in an anonymous namespace to a singleton instance of that
  86. // type.
  87. #define BOOST_PARAMETER_KEYWORD(tag_namespace,name) \
  88. namespace tag_namespace \
  89. { \
  90. struct name \
  91. { \
  92. static char const* keyword_name() \
  93. { \
  94. return #name; \
  95. } \
  96. }; \
  97. } \
  98. namespace \
  99. { \
  100. ::boost::parameter::keyword<tag_namespace::name> const& name \
  101. = ::boost::parameter::keyword<tag_namespace::name>::instance;\
  102. }
  103. }} // namespace boost::parameter
  104. #endif // KEYWORD_050328_HPP