literal.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. template<typename U>
  48. literal(U &u)
  49. : base_type(terminal_type::make(u))
  50. {}
  51. template<typename U>
  52. literal(U const &u)
  53. : base_type(terminal_type::make(u))
  54. {}
  55. template<typename U>
  56. literal(literal<U, Domain> const &u)
  57. : base_type(terminal_type::make(u.get()))
  58. {}
  59. BOOST_PROTO_EXTENDS_USING_ASSIGN(literal_t)
  60. reference get()
  61. {
  62. return proto::value(*this);
  63. }
  64. const_reference get() const
  65. {
  66. return proto::value(*this);
  67. }
  68. };
  69. }
  70. /// \brief A helper function for creating a \c literal\<\> wrapper.
  71. /// \param t The object to wrap.
  72. /// \return literal\<T &\>(t)
  73. /// \attention The returned value holds the argument by reference.
  74. /// \throw nothrow
  75. template<typename T>
  76. inline literal<T &> const lit(T &t)
  77. {
  78. return literal<T &>(t);
  79. }
  80. /// \overload
  81. ///
  82. template<typename T>
  83. inline literal<T const &> const lit(T const &t)
  84. {
  85. #ifdef BOOST_MSVC
  86. #pragma warning(push)
  87. #pragma warning(disable: 4180) // warning C4180: qualifier applied to function type has no meaning; ignored
  88. #endif
  89. return literal<T const &>(t);
  90. #ifdef BOOST_MSVC
  91. #pragma warning(pop)
  92. #endif
  93. }
  94. }}
  95. #endif