safe_bool.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // This header intentionally has no include guards.
  2. //
  3. // Copyright (c) 2010 Neil Groves
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // This code utilises the experience gained during the evolution of
  9. // <boost/smart_ptr/operator_bool.hpp>
  10. #ifndef BOOST_RANGE_SAFE_BOOL_INCLUDED_HPP
  11. #define BOOST_RANGE_SAFE_BOOL_INCLUDED_HPP
  12. #include <boost/config.hpp>
  13. #include <boost/range/config.hpp>
  14. namespace boost
  15. {
  16. namespace range_detail
  17. {
  18. template<class DataMemberPtr>
  19. class safe_bool
  20. {
  21. public:
  22. typedef safe_bool this_type;
  23. #if (defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, < 0x570)) || defined(__CINT_)
  24. typedef bool unspecified_bool_type;
  25. static unspecified_bool_type to_unspecified_bool(const bool x, DataMemberPtr)
  26. {
  27. return x;
  28. }
  29. #elif defined(_MANAGED)
  30. static void unspecified_bool(this_type***)
  31. {
  32. }
  33. typedef void(*unspecified_bool_type)(this_type***);
  34. static unspecified_bool_type to_unspecified_bool(const bool x, DataMemberPtr)
  35. {
  36. return x ? unspecified_bool : 0;
  37. }
  38. #elif \
  39. ( defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, < 0x3200) ) || \
  40. ( defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ < 304) ) || \
  41. ( defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x590) )
  42. typedef bool (this_type::*unspecified_bool_type)() const;
  43. static unspecified_bool_type to_unspecified_bool(const bool x, DataMemberPtr)
  44. {
  45. return x ? &this_type::detail_safe_bool_member_fn : 0;
  46. }
  47. private:
  48. bool detail_safe_bool_member_fn() const { return false; }
  49. #else
  50. typedef DataMemberPtr unspecified_bool_type;
  51. static unspecified_bool_type to_unspecified_bool(const bool x, DataMemberPtr p)
  52. {
  53. return x ? p : 0;
  54. }
  55. #endif
  56. private:
  57. safe_bool();
  58. safe_bool(const safe_bool&);
  59. void operator=(const safe_bool&);
  60. ~safe_bool();
  61. };
  62. } // namespace range_detail
  63. } // namespace boost
  64. #endif // include guard