WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
IndexedClassedHandleResource.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2016. 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 <stdint.h>
11 
12 #include <memory>
13 #include <vector>
14 
15 #include "HAL/Errors.h"
16 #include "HAL/Types.h"
17 #include "HAL/cpp/make_unique.h"
18 #include "HAL/cpp/priority_mutex.h"
19 #include "HAL/handles/HandlesInternal.h"
20 
21 namespace hal {
22 
36 template <typename THandle, typename TStruct, int16_t size,
37  HAL_HandleEnum enumValue>
39  friend class IndexedClassedHandleResourceTest;
40 
41  public:
45  delete;
46 
47  THandle Allocate(int16_t index, std::shared_ptr<TStruct> toSet,
48  int32_t* status);
49  std::shared_ptr<TStruct> Get(THandle handle);
50  void Free(THandle handle);
51 
52  private:
53  // Dynamic array to shrink HAL file size.
54  std::unique_ptr<std::shared_ptr<TStruct>[]> m_structures;
55  std::unique_ptr<priority_mutex[]> m_handleMutexes;
56 };
57 
58 template <typename THandle, typename TStruct, int16_t size,
59  HAL_HandleEnum enumValue>
60 IndexedClassedHandleResource<THandle, TStruct, size,
61  enumValue>::IndexedClassedHandleResource() {
62  m_structures = std::make_unique<std::shared_ptr<TStruct>[]>(size);
63  m_handleMutexes = std::make_unique<priority_mutex[]>(size);
64 }
65 
66 template <typename THandle, typename TStruct, int16_t size,
67  HAL_HandleEnum enumValue>
68 THandle
69 IndexedClassedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
70  int16_t index, std::shared_ptr<TStruct> toSet, int32_t* status) {
71  // don't aquire the lock if we can fail early.
72  if (index < 0 || index >= size) {
73  *status = RESOURCE_OUT_OF_RANGE;
74  return HAL_kInvalidHandle;
75  }
76  std::lock_guard<priority_mutex> sync(m_handleMutexes[index]);
77  // check for allocation, otherwise allocate and return a valid handle
78  if (m_structures[index] != nullptr) {
79  *status = RESOURCE_IS_ALLOCATED;
80  return HAL_kInvalidHandle;
81  }
82  m_structures[index] = toSet;
83  return static_cast<THandle>(hal::createHandle(index, enumValue));
84 }
85 
86 template <typename THandle, typename TStruct, int16_t size,
87  HAL_HandleEnum enumValue>
88 std::shared_ptr<TStruct> IndexedClassedHandleResource<
89  THandle, TStruct, size, enumValue>::Get(THandle handle) {
90  // get handle index, and fail early if index out of range or wrong handle
91  int16_t index = getHandleTypedIndex(handle, enumValue);
92  if (index < 0 || index >= size) {
93  return nullptr;
94  }
95  std::lock_guard<priority_mutex> sync(m_handleMutexes[index]);
96  // return structure. Null will propogate correctly, so no need to manually
97  // check.
98  return m_structures[index];
99 }
100 
101 template <typename THandle, typename TStruct, int16_t size,
102  HAL_HandleEnum enumValue>
103 void IndexedClassedHandleResource<THandle, TStruct, size, enumValue>::Free(
104  THandle handle) {
105  // get handle index, and fail early if index out of range or wrong handle
106  int16_t index = getHandleTypedIndex(handle, enumValue);
107  if (index < 0 || index >= size) return;
108  // lock and deallocated handle
109  std::lock_guard<priority_mutex> sync(m_handleMutexes[index]);
110  m_structures[index].reset();
111 }
112 } // namespace hal
The IndexedClassedHandleResource class is a way to track handles.
Definition: IndexedClassedHandleResource.h:38