WPILibC++  2019.2.1-19-g8a9822a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
CallbackStore.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018 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 #ifndef __FRC_ROBORIO__
11 
12 #include <functional>
13 
14 #include <wpi/StringRef.h>
15 
16 #include "mockdata/HAL_Value.h"
17 
18 namespace frc {
19 namespace sim {
20 
21 using NotifyCallback = std::function<void(wpi::StringRef, const HAL_Value*)>;
22 typedef void (*CancelCallbackFunc)(int32_t index, int32_t uid);
23 typedef void (*CancelCallbackNoIndexFunc)(int32_t uid);
24 typedef void (*CancelCallbackChannelFunc)(int32_t index, int32_t channel,
25  int32_t uid);
26 
27 void CallbackStoreThunk(const char* name, void* param, const HAL_Value* value);
28 
30  public:
31  CallbackStore(int32_t i, NotifyCallback cb, CancelCallbackNoIndexFunc ccf) {
32  index = i;
33  callback = cb;
34  this->ccnif = ccf;
35  cancelType = NoIndex;
36  }
37 
38  CallbackStore(int32_t i, int32_t u, NotifyCallback cb,
39  CancelCallbackFunc ccf) {
40  index = i;
41  uid = u;
42  callback = cb;
43  this->ccf = ccf;
44  cancelType = Normal;
45  }
46 
47  CallbackStore(int32_t i, int32_t c, int32_t u, NotifyCallback cb,
48  CancelCallbackChannelFunc ccf) {
49  index = i;
50  channel = c;
51  uid = u;
52  callback = cb;
53  this->cccf = ccf;
54  cancelType = Channel;
55  }
56 
57  ~CallbackStore() {
58  switch (cancelType) {
59  case Normal:
60  ccf(index, uid);
61  break;
62  case Channel:
63  cccf(index, channel, uid);
64  break;
65  case NoIndex:
66  ccnif(uid);
67  break;
68  }
69  }
70 
71  void SetUid(int32_t uid) { this->uid = uid; }
72 
73  friend void CallbackStoreThunk(const char* name, void* param,
74  const HAL_Value* value);
75 
76  private:
77  int32_t index;
78  int32_t channel;
79  int32_t uid;
80 
81  NotifyCallback callback;
82  union {
83  CancelCallbackFunc ccf;
84  CancelCallbackChannelFunc cccf;
85  CancelCallbackNoIndexFunc ccnif;
86  };
87  enum CancelType { Normal, Channel, NoIndex };
88  CancelType cancelType;
89 };
90 } // namespace sim
91 } // namespace frc
92 
93 #endif
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
Definition: CallbackStore.h:29
HAL Entry Value.
Definition: HAL_Value.h:25