SharedPtrCompare.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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::shared_ptr objects. The type T must implement
  10. // comparison operators. You must be careful when managing containers whose
  11. // ordering is based on std::shared_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. //
  15. // NOTE: std::shared_ptr<T> already has comparison operators, but these
  16. // compare pointer values instead of comparing the objects referenced by the
  17. // pointers. If a container sorted using std::shared_ptr<T> is created for
  18. // two different executions of a program, the object ordering implied by the
  19. // pointer ordering can differ. This might be undesirable for reproducibility
  20. // of results between executions.
  21. namespace WwiseGTE
  22. {
  23. // sp0 == sp1
  24. template <typename T>
  25. struct SharedPtrEQ
  26. {
  27. bool operator()(std::shared_ptr<T> const& sp0, std::shared_ptr<T> const& sp1) const
  28. {
  29. return (sp0 ? (sp1 ? *sp0 == *sp1 : false) : !sp1);
  30. }
  31. };
  32. // sp0 != sp1
  33. template <typename T>
  34. struct SharedPtrNEQ
  35. {
  36. bool operator()(std::shared_ptr<T> const& sp0, std::shared_ptr<T> const& sp1) const
  37. {
  38. return !SharedPtrEQ<T>()(sp0, sp1);
  39. }
  40. };
  41. // sp0 < sp1
  42. template <typename T>
  43. struct SharedPtrLT
  44. {
  45. bool operator()(std::shared_ptr<T> const& sp0, std::shared_ptr<T> const& sp1) const
  46. {
  47. return (sp1 ? (!sp0 || *sp0 < *sp1) : false);
  48. }
  49. };
  50. // sp0 <= sp1
  51. template <typename T>
  52. struct SharedPtrLTE
  53. {
  54. bool operator()(std::shared_ptr<T> const& sp0, std::shared_ptr<T> const& sp1) const
  55. {
  56. return !SharedPtrLT<T>()(sp1, sp0);
  57. }
  58. };
  59. // sp0 > sp1
  60. template <typename T>
  61. struct SharedPtrGT
  62. {
  63. bool operator()(std::shared_ptr<T> const& sp0, std::shared_ptr<T> const& sp1) const
  64. {
  65. return SharedPtrLT<T>()(sp1, sp0);
  66. }
  67. };
  68. // sp0 >= sp1
  69. template <typename T>
  70. struct SharedPtrGTE
  71. {
  72. bool operator()(std::shared_ptr<T> const& sp0, std::shared_ptr<T> const& sp1) const
  73. {
  74. return !SharedPtrLT<T>()(sp0, sp1);
  75. }
  76. };
  77. }