WPILibC++  unspecified
Handle.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2016-2017. 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 #ifndef NT_HANDLE_H_
9 #define NT_HANDLE_H_
10 
11 #include "ntcore_c.h"
12 
13 namespace nt {
14 
15 // Handle data layout:
16 // Bits 30-28: Type
17 // Bits 27-20: Instance index
18 // Bits 19-0: Handle index (0/unused for instance handles)
19 
20 class Handle {
21  public:
22  enum Type {
23  kConnectionListener = 1,
24  kConnectionListenerPoller,
25  kEntry,
26  kEntryListener,
27  kEntryListenerPoller,
28  kInstance,
29  kLogger,
30  kLoggerPoller,
31  kRpcCall,
32  kRpcCallPoller
33  };
34  enum { kIndexMax = 0xfffff };
35 
36  Handle(NT_Handle handle) : m_handle(handle) {}
37  operator NT_Handle() const { return m_handle; }
38 
39  NT_Handle handle() const { return m_handle; }
40 
41  Handle(int inst, int index, Type type) {
42  if (inst < 0 || index < 0) {
43  m_handle = 0;
44  return;
45  }
46  m_handle = ((static_cast<int>(type) & 0xf) << 27) |
47  ((inst & 0x7f) << 20) | (index & 0xfffff);
48  }
49 
50  int GetIndex() const { return static_cast<int>(m_handle) & 0xfffff; }
51  Type GetType() const {
52  return static_cast<Type>((static_cast<int>(m_handle) >> 27) & 0xf);
53  }
54  int GetInst() const { return (static_cast<int>(m_handle) >> 20) & 0x7f; }
55  bool IsType(Type type) const { return type == GetType(); }
56  int GetTypedIndex(Type type) const { return IsType(type) ? GetIndex() : -1; }
57  int GetTypedInst(Type type) const { return IsType(type) ? GetInst() : -1; }
58 
59  private:
60  NT_Handle m_handle;
61 };
62 
63 } // namespace nt
64 
65 #endif // NT_HANDLE_H_
Definition: Handle.h:20
Definition: IEntryNotifier.h:15