fallback_policy.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file fallback_policy.hpp
  9. * \author Andrey Semashev
  10. * \date 18.08.2012
  11. *
  12. * The header contains definition of fallback policies when attribute value visitation or extraction fails.
  13. */
  14. #ifndef BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_
  15. #define BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_
  16. #include <boost/type_index.hpp>
  17. #include <boost/type_traits/remove_cv.hpp>
  18. #include <boost/type_traits/remove_reference.hpp>
  19. #include <boost/log/detail/config.hpp>
  20. #include <boost/log/exceptions.hpp>
  21. #include <boost/log/attributes/fallback_policy_fwd.hpp>
  22. #include <boost/log/detail/header.hpp>
  23. #ifdef BOOST_HAS_PRAGMA_ONCE
  24. #pragma once
  25. #endif
  26. namespace boost {
  27. BOOST_LOG_OPEN_NAMESPACE
  28. /*!
  29. * The \c fallback_to_none policy results in returning an empty value reference if the attribute value cannot be extracted.
  30. */
  31. struct fallback_to_none
  32. {
  33. enum { guaranteed_result = false };
  34. /*!
  35. * The method is called in order to apply a function object to the default value.
  36. */
  37. template< typename FunT >
  38. static bool apply_default(FunT&)
  39. {
  40. return false;
  41. }
  42. /*!
  43. * The method is called in order to apply a function object to the default value.
  44. */
  45. template< typename FunT >
  46. static bool apply_default(FunT const&)
  47. {
  48. return false;
  49. }
  50. /*!
  51. * The method is called when value extraction failed because the attribute value has different type than requested.
  52. */
  53. static void on_invalid_type(typeindex::type_index const&)
  54. {
  55. }
  56. /*!
  57. * The method is called when value extraction failed because the attribute value was not found.
  58. */
  59. static void on_missing_value()
  60. {
  61. }
  62. };
  63. /*!
  64. * The \c fallback_to_throw policy results in throwing an exception if the attribute value cannot be extracted.
  65. */
  66. struct fallback_to_throw
  67. {
  68. enum { guaranteed_result = true };
  69. /*!
  70. * The method is called in order to apply a function object to the default value.
  71. */
  72. template< typename FunT >
  73. static bool apply_default(FunT&)
  74. {
  75. return false;
  76. }
  77. /*!
  78. * The method is called in order to apply a function object to the default value.
  79. */
  80. template< typename FunT >
  81. static bool apply_default(FunT const&)
  82. {
  83. return false;
  84. }
  85. /*!
  86. * The method is called when value extraction failed because the attribute value has different type than requested.
  87. */
  88. static void on_invalid_type(typeindex::type_index const& t)
  89. {
  90. BOOST_LOG_THROW_DESCR_PARAMS(invalid_type, "Attribute value has incompatible type", (t));
  91. }
  92. /*!
  93. * The method is called when value extraction failed because the attribute value was not found.
  94. */
  95. static void on_missing_value()
  96. {
  97. BOOST_LOG_THROW_DESCR(missing_value, "Attribute value not found");
  98. }
  99. };
  100. /*!
  101. * The \c fallback_to_default policy results in a default value if the attribute value cannot be extracted.
  102. */
  103. template< typename DefaultT >
  104. struct fallback_to_default
  105. {
  106. enum { guaranteed_result = true };
  107. //! Default value type
  108. typedef typename remove_cv< typename remove_reference< DefaultT >::type >::type default_type;
  109. /*!
  110. * Default constructor.
  111. */
  112. fallback_to_default() : m_default()
  113. {
  114. }
  115. /*!
  116. * Initializing constructor.
  117. */
  118. explicit fallback_to_default(default_type const& def_val) : m_default(def_val)
  119. {
  120. }
  121. /*!
  122. * The method is called in order to apply a function object to the default value.
  123. */
  124. template< typename FunT >
  125. bool apply_default(FunT& fun) const
  126. {
  127. fun(m_default);
  128. return true;
  129. }
  130. /*!
  131. * The method is called in order to apply a function object to the default value.
  132. */
  133. template< typename FunT >
  134. bool apply_default(FunT const& fun) const
  135. {
  136. fun(m_default);
  137. return true;
  138. }
  139. /*!
  140. * The method is called when value extraction failed because the attribute value has different type than requested.
  141. */
  142. static void on_invalid_type(typeindex::type_index const&)
  143. {
  144. }
  145. /*!
  146. * The method is called when value extraction failed because the attribute value was not found.
  147. */
  148. static void on_missing_value()
  149. {
  150. }
  151. private:
  152. //! Default value
  153. DefaultT m_default;
  154. };
  155. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  156. } // namespace boost
  157. #include <boost/log/detail/footer.hpp>
  158. #endif // BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_