safe_bool.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*=============================================================================
  2. Copyright (c) 2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #if !defined(BOOST_SPIRIT_SAFE_BOOL_HPP)
  8. #define BOOST_SPIRIT_SAFE_BOOL_HPP
  9. #include <boost/config.hpp>
  10. #include <boost/detail/workaround.hpp>
  11. #include <boost/spirit/home/classic/namespace.hpp>
  12. namespace boost { namespace spirit {
  13. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  14. namespace impl
  15. {
  16. template <typename T>
  17. struct no_base {};
  18. template <typename T>
  19. struct safe_bool_impl
  20. {
  21. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
  22. void stub(T*) {};
  23. typedef void (safe_bool_impl::*type)(T*);
  24. #else
  25. typedef T* TP; // workaround to make parsing easier
  26. TP stub;
  27. typedef TP safe_bool_impl::*type;
  28. #endif
  29. };
  30. }
  31. template <typename DerivedT, typename BaseT = impl::no_base<DerivedT> >
  32. struct safe_bool : BaseT
  33. {
  34. private:
  35. typedef impl::safe_bool_impl<DerivedT> impl_t;
  36. typedef typename impl_t::type bool_type;
  37. public:
  38. operator bool_type() const
  39. {
  40. return static_cast<const DerivedT*>(this)->operator_bool() ?
  41. &impl_t::stub : 0;
  42. }
  43. operator bool_type()
  44. {
  45. return static_cast<DerivedT*>(this)->operator_bool() ?
  46. &impl_t::stub : 0;
  47. }
  48. };
  49. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  50. }}
  51. #endif