literal.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file literal.hpp
  3. /// The literal\<\> terminal wrapper, and the proto::lit() function for
  4. /// creating literal\<\> wrappers.
  5. //
  6. // Copyright 2008 Eric Niebler. Distributed under the Boost
  7. // Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_PROTO_LITERAL_HPP_EAN_01_03_2007
  10. #define BOOST_PROTO_LITERAL_HPP_EAN_01_03_2007
  11. #include <boost/config.hpp>
  12. #include <boost/proto/proto_fwd.hpp>
  13. #include <boost/proto/expr.hpp>
  14. #include <boost/proto/traits.hpp>
  15. #include <boost/proto/extends.hpp>
  16. namespace boost { namespace proto
  17. {
  18. namespace utility
  19. {
  20. /// \brief A simple wrapper for a terminal, provided for
  21. /// ease of use.
  22. ///
  23. /// A simple wrapper for a terminal, provided for
  24. /// ease of use. In all cases, <tt>literal\<X\> l(x);</tt>
  25. /// is equivalent to <tt>terminal\<X\>::type l = {x};</tt>.
  26. ///
  27. /// The \c Domain template parameter defaults to
  28. /// \c proto::default_domain.
  29. template<
  30. typename T
  31. , typename Domain // = default_domain
  32. >
  33. struct literal
  34. : extends<basic_expr<tag::terminal, term<T>, 0>, literal<T, Domain>, Domain>
  35. {
  36. private:
  37. typedef basic_expr<tag::terminal, term<T>, 0> terminal_type;
  38. typedef extends<terminal_type, literal<T, Domain>, Domain> base_type;
  39. typedef literal<T, Domain> literal_t;
  40. public:
  41. typedef typename detail::term_traits<T>::value_type value_type;
  42. typedef typename detail::term_traits<T>::reference reference;
  43. typedef typename detail::term_traits<T>::const_reference const_reference;
  44. literal()
  45. : base_type(terminal_type::make(T()))
  46. {}
  47. #ifndef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
  48. literal(literal const &) = default;
  49. #endif
  50. template<typename U>
  51. literal(U &u)
  52. : base_type(terminal_type::make(u))
  53. {}
  54. template<typename U>
  55. literal(U const &u)
  56. : base_type(terminal_type::make(u))
  57. {}
  58. template<typename U>
  59. literal(literal<U, Domain> const &u)
  60. : base_type(terminal_type::make(u.get()))
  61. {}
  62. BOOST_PROTO_EXTENDS_USING_ASSIGN(literal_t)
  63. reference get()
  64. {
  65. return proto::value(*this);
  66. }
  67. const_reference get() const
  68. {
  69. return proto::value(*this);
  70. }
  71. };
  72. }
  73. /// \brief A helper function for creating a \c literal\<\> wrapper.
  74. /// \param t The object to wrap.
  75. /// \return literal\<T &\>(t)
  76. /// \attention The returned value holds the argument by reference.
  77. /// \throw nothrow
  78. template<typename T>
  79. inline literal<T &> const lit(T &t)
  80. {
  81. return literal<T &>(t);
  82. }
  83. /// \overload
  84. ///
  85. template<typename T>
  86. inline literal<T const &> const lit(T const &t)
  87. {
  88. #ifdef BOOST_MSVC
  89. #pragma warning(push)
  90. #pragma warning(disable: 4180) // warning C4180: qualifier applied to function type has no meaning; ignored
  91. #endif
  92. return literal<T const &>(t);
  93. #ifdef BOOST_MSVC
  94. #pragma warning(pop)
  95. #endif
  96. }
  97. }}
  98. #endif