|
| 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_FUNCTIONALJOINTVISITOR_HPP |
| 22 | +#define PLAYRHO_DYNAMICS_JOINTS_FUNCTIONALJOINTVISITOR_HPP |
| 23 | + |
| 24 | +#include <PlayRho/Dynamics/Joints/JointVisitor.hpp> |
| 25 | +#include <functional> |
| 26 | +#include <tuple> |
| 27 | +#include <utility> |
| 28 | + |
| 29 | +namespace playrho { |
| 30 | + |
| 31 | + /// @brief Functional joint visitor class. |
| 32 | + /// @note This class is intended to provide an alternate interface for visiting joints |
| 33 | + /// via the use of lamdas instead of having to subclass <code>JointVisitor</code>. |
| 34 | + class FunctionalJointVisitor: public JointVisitor |
| 35 | + { |
| 36 | + public: |
| 37 | + /// @brief Procedure alias. |
| 38 | + template <class T> |
| 39 | + using Proc = std::function<void(T)>; |
| 40 | + |
| 41 | + //using Tuple = std::tuple<Proc<Types>...>; |
| 42 | + //FunctionalJointVisitor(const Tuple& v): procs{v} {} |
| 43 | + |
| 44 | + /// @brief Tuple alias. |
| 45 | + using Tuple = std::tuple< |
| 46 | + Proc<const RevoluteJoint&>, |
| 47 | + Proc< RevoluteJoint&>, |
| 48 | + Proc<const PrismaticJoint&>, |
| 49 | + Proc< PrismaticJoint&>, |
| 50 | + Proc<const DistanceJoint&>, |
| 51 | + Proc< DistanceJoint&>, |
| 52 | + Proc<const PulleyJoint&>, |
| 53 | + Proc< PulleyJoint&>, |
| 54 | + Proc<const MouseJoint&>, |
| 55 | + Proc< MouseJoint&>, |
| 56 | + Proc<const GearJoint&>, |
| 57 | + Proc< GearJoint&>, |
| 58 | + Proc<const WheelJoint&>, |
| 59 | + Proc< WheelJoint&>, |
| 60 | + Proc<const WeldJoint&>, |
| 61 | + Proc< WeldJoint&>, |
| 62 | + Proc<const FrictionJoint&>, |
| 63 | + Proc< FrictionJoint&>, |
| 64 | + Proc<const RopeJoint&>, |
| 65 | + Proc< RopeJoint&>, |
| 66 | + Proc<const MotorJoint&>, |
| 67 | + Proc< MotorJoint&> |
| 68 | + >; |
| 69 | + |
| 70 | + Tuple procs; ///< Procedures. |
| 71 | + |
| 72 | + /// @brief Uses given procedure. |
| 73 | + /// @note Provide a builder pattern mutator method. |
| 74 | + template <class T> |
| 75 | + FunctionalJointVisitor& Use(const Proc<T>& proc) noexcept |
| 76 | + { |
| 77 | + std::get<Proc<T>>(procs) = proc; |
| 78 | + return *this; |
| 79 | + } |
| 80 | + |
| 81 | + // Overrides of all the base class's Visit methods... |
| 82 | + // Uses decltype to ensure the correctly typed invocation of the Handle method. |
| 83 | + void Visit(const RevoluteJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 84 | + void Visit(RevoluteJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 85 | + void Visit(const PrismaticJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 86 | + void Visit(PrismaticJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 87 | + void Visit(const DistanceJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 88 | + void Visit(DistanceJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 89 | + void Visit(const PulleyJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 90 | + void Visit(PulleyJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 91 | + void Visit(const MouseJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 92 | + void Visit(MouseJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 93 | + void Visit(const GearJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 94 | + void Visit(GearJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 95 | + void Visit(const WheelJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 96 | + void Visit(WheelJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 97 | + void Visit(const WeldJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 98 | + void Visit(WeldJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 99 | + void Visit(const FrictionJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 100 | + void Visit(FrictionJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 101 | + void Visit(const RopeJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 102 | + void Visit(RopeJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 103 | + void Visit(const MotorJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 104 | + void Visit(MotorJoint& arg) override { Handle<decltype(arg)>(arg); } |
| 105 | + |
| 106 | + private: |
| 107 | + |
| 108 | + /// @brief Handles the joint through the established function. |
| 109 | + template <class T> |
| 110 | + inline void Handle(T arg) const |
| 111 | + { |
| 112 | + const auto& proc = std::get<Proc<T>>(procs); |
| 113 | + if (proc) |
| 114 | + { |
| 115 | + proc(arg); |
| 116 | + } |
| 117 | + } |
| 118 | + }; |
| 119 | + |
| 120 | +} // namespace playrho |
| 121 | + |
| 122 | +#endif // PLAYRHO_DYNAMICS_JOINTS_FUNCTIONALJOINTVISITOR_HPP |
| 123 | + |
0 commit comments