WeakPtrCompare.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <memory>
  9. // Comparison operators for std::weak_ptr objects. The type T must implement
  10. // comparison operators. You must be careful when managing containers whose
  11. // ordering is based on std::weak_ptr comparisons. The underlying objects
  12. // can change, which invalidates the container ordering. If objects do not
  13. // change while the container persists, these are safe to use.
  14. namespace WwiseGTE
  15. {
  16. // wp0 == wp1
  17. template <typename T>
  18. struct WeakPtrEQ
  19. {
  20. bool operator()(std::weak_ptr<T> const& wp0, std::weak_ptr<T> const& wp1) const
  21. {
  22. auto sp0 = wp0.lock(), sp1 = wp1.lock();
  23. return (sp0 ? (sp1 ? *sp0 == *sp1 : false) : !sp1);
  24. }
  25. };
  26. // wp0 != wp1
  27. template <typename T>
  28. struct WeakPtrNEQ
  29. {
  30. bool operator()(std::weak_ptr<T> const& wp0, std::weak_ptr<T> const& wp1) const
  31. {
  32. return !WeakPtrEQ<T>()(wp0, wp1);
  33. }
  34. };
  35. // wp0 < wp1
  36. template <typename T>
  37. struct WeakPtrLT
  38. {
  39. bool operator()(std::weak_ptr<T> const& wp0, std::weak_ptr<T> const& wp1) const
  40. {
  41. auto sp0 = wp0.lock(), sp1 = wp1.lock();
  42. return (sp1 ? (!sp0 || *sp0 < *sp1) : false);
  43. }
  44. };
  45. // wp0 <= wp1
  46. template <typename T>
  47. struct WeakPtrLTE
  48. {
  49. bool operator()(std::weak_ptr<T> const& wp0, std::weak_ptr<T> const& wp1) const
  50. {
  51. return !WeakPtrLT<T>()(wp1, wp0);
  52. }
  53. };
  54. // wp0 > wp1
  55. template <typename T>
  56. struct WeakPtrGT
  57. {
  58. bool operator()(std::weak_ptr<T> const& wp0, std::weak_ptr<T> const& wp1) const
  59. {
  60. return WeakPtrLT<T>()(wp1, wp0);
  61. }
  62. };
  63. // wp0 >= wp1
  64. template <typename T>
  65. struct WeakPtrGTE
  66. {
  67. bool operator()(std::weak_ptr<T> const& wp0, std::weak_ptr<T> const& wp1) const
  68. {
  69. return !WeakPtrLT<T>()(wp0, wp1);
  70. }
  71. };
  72. }