JointKey.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  */
20 
21 #ifndef PLAYRHO_DYNAMICS_JOINTS_JOINTKEY_HPP
22 #define PLAYRHO_DYNAMICS_JOINTS_JOINTKEY_HPP
23 
26 
28 #include <utility>
29 #include <functional>
30 
31 namespace playrho {
32 namespace d2 {
33 
34 class Joint;
35 class Body;
36 
38 class JointKey
39 {
40 public:
41 
43  static PLAYRHO_CONSTEXPR inline JointKey Get(const Body* bodyA, const Body* bodyB) noexcept
44  {
45  return (bodyA < bodyB)? JointKey{bodyA, bodyB}: JointKey{bodyB, bodyA};
46  }
47 
49  PLAYRHO_CONSTEXPR const Body* GetBody1() const noexcept
50  {
51  return m_body1;
52  }
53 
56  {
57  return m_body2;
58  }
59 
60 private:
62  PLAYRHO_CONSTEXPR inline JointKey(const Body* body1, const Body* body2):
63  m_body1(body1), m_body2(body2)
64  {
65  // Intentionally empty.
66  }
67 
70  const Body* m_body1;
71 
74  const Body* m_body2;
75 };
76 
78 JointKey GetJointKey(const Joint& joint) noexcept;
79 
81 PLAYRHO_CONSTEXPR inline int Compare(const JointKey& lhs, const JointKey& rhs) noexcept
82 {
83  if (lhs.GetBody1() < rhs.GetBody1())
84  {
85  return -1;
86  }
87  if (lhs.GetBody1() > rhs.GetBody1())
88  {
89  return +1;
90  }
91  if (lhs.GetBody2() < rhs.GetBody2())
92  {
93  return -1;
94  }
95  if (lhs.GetBody2() > rhs.GetBody2())
96  {
97  return +1;
98  }
99  return 0;
100 }
101 
104 PLAYRHO_CONSTEXPR inline bool IsFor(const JointKey key, const Body* body) noexcept
105 {
106  return body == key.GetBody1() || body == key.GetBody2();
107 }
108 
110 inline Joint* GetJointPtr(std::pair<JointKey, Joint*> value)
111 {
112  return std::get<Joint*>(value);
113 }
114 
115 } // namespace d2
116 } // namespace playrho
117 
118 namespace std
119 {
121  template <>
122  struct less<playrho::d2::JointKey>
123  {
125  PLAYRHO_CONSTEXPR inline
126  bool operator()(const playrho::d2::JointKey& lhs, const playrho::d2::JointKey& rhs) const
127  {
128  return playrho::d2::Compare(lhs, rhs) < 0;
129  }
130  };
131 
133  template <>
134  struct equal_to<playrho::d2::JointKey>
135  {
136 
138  PLAYRHO_CONSTEXPR inline
139  bool operator()( const playrho::d2::JointKey& lhs, const playrho::d2::JointKey& rhs ) const
140  {
141  return playrho::d2::Compare(lhs, rhs) == 0;
142  }
143  };
144 
145 } // namespace std
146 
147 #endif // PLAYRHO_DYNAMICS_JOINTS_JOINTKEY_HPP