testable_classes.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (C) 2014 Andrzej Krzemienski.
  2. //
  3. // Use, modification, and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/optional for documentation.
  8. //
  9. // You are welcome to contact the author at:
  10. // akrzemi1@gmail.com
  11. #ifndef BOOST_OPTIONAL_TEST_TESTABKE_CLASSES_AK_07JAN2015_HPP
  12. #define BOOST_OPTIONAL_TEST_TESTABKE_CLASSES_AK_07JAN2015_HPP
  13. #include "boost/optional/optional.hpp"
  14. struct ScopeGuard // no copy/move ctor/assign
  15. {
  16. int val_;
  17. explicit ScopeGuard(int v) : val_(v) {}
  18. int& val() { return val_; }
  19. const int& val() const { return val_; }
  20. private:
  21. ScopeGuard(ScopeGuard const&);
  22. void operator=(ScopeGuard const&);
  23. };
  24. struct Abstract
  25. {
  26. virtual int& val() = 0;
  27. virtual const int& val() const = 0;
  28. virtual ~Abstract() {}
  29. Abstract(){}
  30. private:
  31. Abstract(Abstract const&);
  32. void operator=(Abstract const&);
  33. };
  34. struct Impl : Abstract
  35. {
  36. int val_;
  37. Impl(int v) : val_(v) {}
  38. int& val() { return val_; }
  39. const int& val() const { return val_; }
  40. };
  41. template <typename T>
  42. struct concrete_type_of
  43. {
  44. typedef T type;
  45. };
  46. template <>
  47. struct concrete_type_of<Abstract>
  48. {
  49. typedef Impl type;
  50. };
  51. template <>
  52. struct concrete_type_of<const Abstract>
  53. {
  54. typedef const Impl type;
  55. };
  56. template <typename T>
  57. struct has_arrow
  58. {
  59. static const bool value = true;
  60. };
  61. template <>
  62. struct has_arrow<int>
  63. {
  64. static const bool value = false;
  65. };
  66. template <>
  67. struct has_arrow< boost::optional<int> >
  68. {
  69. static const bool value = false;
  70. };
  71. int& val(int& i) { return i; }
  72. int& val(Abstract& a) { return a.val(); }
  73. int& val(Impl& a) { return a.val(); }
  74. int& val(ScopeGuard& g) { return g.val(); }
  75. template <typename T> int& val(T& o) { return *o; }
  76. const int& val(const int& i) { return i; }
  77. const int& val(const Abstract& a) { return a.val(); }
  78. const int& val(const Impl& a) { return a.val(); }
  79. const int& val(const ScopeGuard& g) { return g.val(); }
  80. template <typename T> const int& val(const T& o) { return *o; }
  81. bool operator==(const Abstract& l, const Abstract& r) { return l.val() == r.val(); }
  82. bool operator==(const ScopeGuard& l, const ScopeGuard& r) { return l.val() == r.val(); }
  83. bool operator<(const Abstract& l, const Abstract& r) { return l.val() < r.val(); }
  84. bool operator<(const ScopeGuard& l, const ScopeGuard& r) { return l.val() < r.val(); }
  85. #endif //BOOST_OPTIONAL_TEST_TESTABKE_CLASSES_AK_07JAN2015_HPP