WPILibC++  2018.4.1-20180729124724-1140-gcbb62fb
 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  CallbackStore(int32_t i, int32_t u, NotifyCallback cb,
38  CancelCallbackFunc ccf) {
39  index = i;
40  uid = u;
41  callback = cb;
42  this->ccf = ccf;
43  cancelType = Normal;
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  ~CallbackStore() {
55  switch (cancelType) {
56  case Normal:
57  ccf(index, uid);
58  break;
59  case Channel:
60  cccf(index, channel, uid);
61  break;
62  case NoIndex:
63  ccnif(uid);
64  break;
65  }
66  }
67 
68  void SetUid(int32_t uid) { this->uid = uid; }
69 
70  friend void CallbackStoreThunk(const char* name, void* param,
71  const HAL_Value* value);
72 
73  private:
74  int32_t index;
75  int32_t channel;
76  int32_t uid;
77 
78  NotifyCallback callback;
79  union {
80  CancelCallbackFunc ccf;
81  CancelCallbackChannelFunc cccf;
82  CancelCallbackNoIndexFunc ccnif;
83  };
84  enum CancelType { Normal, Channel, NoIndex };
85  CancelType cancelType;
86 };
87 } // namespace sim
88 } // namespace frc
89 
90 #endif
Definition: SPIAccelerometerSim.h:18
Definition: CallbackStore.h:29
HAL Entry Value.
Definition: HAL_Value.h:25