Overte C++ Documentation
Endpoint.h
1 //
2 // Created by Bradley Austin Davis 2015/10/09
3 // Copyright 2015 High Fidelity, Inc.
4 // Copyright 2023 Overte e.V.
5 //
6 // Distributed under the Apache License, Version 2.0.
7 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
8 // SPDX-License-Identifier: Apache-2.0
9 //
10 
11 #pragma once
12 #ifndef hifi_Controllers_Endpoint_h
13 #define hifi_Controllers_Endpoint_h
14 
15 #include <list>
16 #include <memory>
17 #include <functional>
18 
19 #include <QtCore/QObject>
20 
21 #include "../AxisValue.h"
22 #include "../Input.h"
23 #include "../Pose.h"
24 
25 namespace controller {
26  /*
27  * Encapsulates a particular input / output,
28  * i.e. Hydra.Button0, Standard.X, Action.Yaw
29  */
30 
31 // All the derived classes need to have private constructors
32 class Endpoint : public QObject, public std::enable_shared_from_this<Endpoint> {
33  Q_OBJECT;
34  public:
35  using Pointer = std::shared_ptr<Endpoint>;
36  using List = std::list<Pointer>;
37  using Pair = std::pair<Pointer, Pointer>;
38  using ReadLambda = std::function<float()>;
39  using WriteLambda = std::function<void(float)>;
40 
41  virtual AxisValue value() { return peek(); }
42  virtual AxisValue peek() const = 0;
43  virtual void apply(AxisValue value, const Pointer& source) = 0;
44  virtual Pose peekPose() const { return Pose(); };
45  virtual Pose pose() { return peekPose(); }
46  virtual void apply(const Pose& value, const Pointer& source) {}
47  virtual bool isPose() const { return _input.isPose(); }
48  virtual bool writeable() const { return true; }
49  virtual bool readable() const { return true; }
50  virtual void reset() { }
51 
52  const Input& getInput() { return _input; }
53 
54  protected:
55  Endpoint(const Input& input) : _input(input) {}
56  Input _input;
57  };
58 
59  class LambdaEndpoint : public Endpoint {
60  public:
61  using Endpoint::apply;
62 
63  virtual AxisValue peek() const override { return AxisValue(_readLambda(), 0); }
64  virtual void apply(AxisValue value, const Pointer& source) override { _writeLambda(value.value); }
65 
66  private:
67  LambdaEndpoint(ReadLambda readLambda, WriteLambda writeLambda = [](float) {})
68  : Endpoint(Input::INVALID_INPUT), _readLambda(readLambda), _writeLambda(writeLambda) { }
69 
70  ReadLambda _readLambda;
71  WriteLambda _writeLambda;
72  };
73 
74  extern Endpoint::WriteLambda DEFAULT_WRITE_LAMBDA;
75 
76  class LambdaRefEndpoint : public Endpoint {
77  public:
78  static std::shared_ptr<Endpoint> newEndpoint(const ReadLambda& readLambda, const WriteLambda& writeLambda = DEFAULT_WRITE_LAMBDA) {
79  return std::shared_ptr<Endpoint>(new LambdaRefEndpoint(readLambda, writeLambda));
80  };
81 
82  using Endpoint::apply;
83 
84  virtual AxisValue peek() const override { return AxisValue(_readLambda(), 0); }
85  virtual void apply(AxisValue value, const Pointer& source) override { _writeLambda(value.value); }
86 
87  private:
88  LambdaRefEndpoint(const ReadLambda& readLambda, const WriteLambda& writeLambda = DEFAULT_WRITE_LAMBDA)
89  : Endpoint(Input::INVALID_INPUT), _readLambda(readLambda), _writeLambda(writeLambda) {
90  }
91 
92  const ReadLambda& _readLambda;
93  const WriteLambda& _writeLambda;
94  };
95 
96 
97  class VirtualEndpoint : public Endpoint {
98  public:
99  virtual AxisValue peek() const override { return _currentValue; }
100  virtual void apply(AxisValue value, const Pointer& source) override { _currentValue = value; }
101 
102  virtual Pose peekPose() const override { return _currentPose; }
103  virtual void apply(const Pose& value, const Pointer& source) override {
104  _currentPose = value;
105  }
106  protected:
107  VirtualEndpoint(const Input& id = Input::INVALID_INPUT)
108  : Endpoint(id) {
109  }
110 
111  AxisValue _currentValue { 0.0f, 0, false };
112  Pose _currentPose {};
113  };
114 
115 }
116 
117 #endif