WPILibC++  2020.3.2
CallbackStore.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <functional>
11 
12 #include <wpi/StringRef.h>
13 
14 #include "hal/Value.h"
15 
16 namespace frc {
17 namespace sim {
18 
19 using NotifyCallback = std::function<void(wpi::StringRef, const HAL_Value*)>;
20 typedef void (*CancelCallbackFunc)(int32_t index, int32_t uid);
21 typedef void (*CancelCallbackNoIndexFunc)(int32_t uid);
22 typedef void (*CancelCallbackChannelFunc)(int32_t index, int32_t channel,
23  int32_t uid);
24 
25 void CallbackStoreThunk(const char* name, void* param, const HAL_Value* value);
26 
28  public:
29  CallbackStore(int32_t i, NotifyCallback cb, CancelCallbackNoIndexFunc ccf) {
30  index = i;
31  callback = cb;
32  this->ccnif = ccf;
33  cancelType = NoIndex;
34  }
35 
36  CallbackStore(int32_t i, int32_t u, NotifyCallback cb,
37  CancelCallbackFunc ccf) {
38  index = i;
39  uid = u;
40  callback = cb;
41  this->ccf = ccf;
42  cancelType = Normal;
43  }
44 
45  CallbackStore(int32_t i, int32_t c, int32_t u, NotifyCallback cb,
46  CancelCallbackChannelFunc ccf) {
47  index = i;
48  channel = c;
49  uid = u;
50  callback = cb;
51  this->cccf = ccf;
52  cancelType = Channel;
53  }
54 
55  ~CallbackStore() {
56  switch (cancelType) {
57  case Normal:
58  ccf(index, uid);
59  break;
60  case Channel:
61  cccf(index, channel, uid);
62  break;
63  case NoIndex:
64  ccnif(uid);
65  break;
66  }
67  }
68 
69  void SetUid(int32_t uid) { this->uid = uid; }
70 
71  friend void CallbackStoreThunk(const char* name, void* param,
72  const HAL_Value* value);
73 
74  private:
75  int32_t index;
76  int32_t channel;
77  int32_t uid;
78 
79  NotifyCallback callback;
80  union {
81  CancelCallbackFunc ccf;
82  CancelCallbackChannelFunc cccf;
83  CancelCallbackNoIndexFunc ccnif;
84  };
85  enum CancelType { Normal, Channel, NoIndex };
86  CancelType cancelType;
87 };
88 } // namespace sim
89 } // namespace frc
frc::sim::CallbackStore
Definition: CallbackStore.h:27
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
HAL_Value
HAL Entry Value.
Definition: Value.h:23