WPILibC++  2019.1.1-beta-2-1-g9bc998f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
IndexedHandleResource.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2016-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 #include <stdint.h>
11 
12 #include <array>
13 #include <memory>
14 
15 #include <wpi/mutex.h>
16 
17 #include "hal/Errors.h"
18 #include "hal/Types.h"
19 #include "hal/handles/HandlesInternal.h"
20 
21 namespace hal {
22 
35 template <typename THandle, typename TStruct, int16_t size,
36  HAL_HandleEnum enumValue>
38  friend class IndexedHandleResourceTest;
39 
40  public:
41  IndexedHandleResource() = default;
43  IndexedHandleResource& operator=(const IndexedHandleResource&) = delete;
44 
45  THandle Allocate(int16_t index, int32_t* status);
46  std::shared_ptr<TStruct> Get(THandle handle);
47  void Free(THandle handle);
48  void ResetHandles() override;
49 
50  private:
51  std::array<std::shared_ptr<TStruct>, size> m_structures;
52  std::array<wpi::mutex, size> m_handleMutexes;
53 };
54 
55 template <typename THandle, typename TStruct, int16_t size,
56  HAL_HandleEnum enumValue>
58  int16_t index, int32_t* status) {
59  // don't aquire the lock if we can fail early.
60  if (index < 0 || index >= size) {
61  *status = RESOURCE_OUT_OF_RANGE;
62  return HAL_kInvalidHandle;
63  }
64  std::lock_guard<wpi::mutex> lock(m_handleMutexes[index]);
65  // check for allocation, otherwise allocate and return a valid handle
66  if (m_structures[index] != nullptr) {
67  *status = RESOURCE_IS_ALLOCATED;
68  return HAL_kInvalidHandle;
69  }
70  m_structures[index] = std::make_shared<TStruct>();
71  return static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
72 }
73 
74 template <typename THandle, typename TStruct, int16_t size,
75  HAL_HandleEnum enumValue>
76 std::shared_ptr<TStruct>
77 IndexedHandleResource<THandle, TStruct, size, enumValue>::Get(THandle handle) {
78  // get handle index, and fail early if index out of range or wrong handle
79  int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
80  if (index < 0 || index >= size) {
81  return nullptr;
82  }
83  std::lock_guard<wpi::mutex> lock(m_handleMutexes[index]);
84  // return structure. Null will propogate correctly, so no need to manually
85  // check.
86  return m_structures[index];
87 }
88 
89 template <typename THandle, typename TStruct, int16_t size,
90  HAL_HandleEnum enumValue>
91 void IndexedHandleResource<THandle, TStruct, size, enumValue>::Free(
92  THandle handle) {
93  // get handle index, and fail early if index out of range or wrong handle
94  int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
95  if (index < 0 || index >= size) return;
96  // lock and deallocated handle
97  std::lock_guard<wpi::mutex> lock(m_handleMutexes[index]);
98  m_structures[index].reset();
99 }
100 
101 template <typename THandle, typename TStruct, int16_t size,
102  HAL_HandleEnum enumValue>
103 void IndexedHandleResource<THandle, TStruct, size, enumValue>::ResetHandles() {
104  for (int i = 0; i < size; i++) {
105  std::lock_guard<wpi::mutex> lock(m_handleMutexes[i]);
106  m_structures[i].reset();
107  }
108  HandleBase::ResetHandles();
109 }
110 } // namespace hal
HAL_HandleEnum
Enum of HAL handle types.
Definition: HandlesInternal.h:47
Base for all HAL Handles.
Definition: HandlesInternal.h:29
The IndexedHandleResource class is a way to track handles.
Definition: IndexedHandleResource.h:37
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition: SimDataValue.h:19
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:999
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType, int16_t version)
Create a handle for a specific index, type and version.