WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
LimitedClassedHandleResource.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <array>
10#include <memory>
11
12#include "wpi/hal/Types.h"
14#include "wpi/util/mutex.hpp"
15
16namespace wpi::hal {
17
18/**
19 * The LimitedClassedHandleResource class is a way to track handles. This
20 * version
21 * allows a limited number of handles that are allocated sequentially.
22 *
23 * @tparam THandle The Handle Type (Must be typedefed from HAL_Handle)
24 * @tparam TStruct The struct type held by this resource
25 * @tparam size The number of resources allowed to be allocated
26 * @tparam enumValue The type value stored in the handle
27 *
28 */
29template <typename THandle, typename TStruct, int16_t size,
30 HAL_HandleEnum enumValue>
33
34 public:
38 delete;
39
40 THandle Allocate(std::shared_ptr<TStruct> toSet);
41 int16_t GetIndex(THandle handle) {
42 return getHandleTypedIndex(handle, enumValue, m_version);
43 }
44 std::shared_ptr<TStruct> Get(THandle handle);
45 void Free(THandle handle);
46 void ResetHandles() override;
47
48 private:
49 std::array<std::shared_ptr<TStruct>, size> m_structures;
50 std::array<wpi::util::mutex, size> m_handleMutexes;
51 wpi::util::mutex m_allocateMutex;
52};
53
54template <typename THandle, typename TStruct, int16_t size,
55 HAL_HandleEnum enumValue>
56THandle
58 std::shared_ptr<TStruct> toSet) {
59 // globally lock to loop through indices
60 std::scoped_lock lock(m_allocateMutex);
61 for (int16_t i = 0; i < size; i++) {
62 if (m_structures[i] == nullptr) {
63 // if a false index is found, grab its specific mutex
64 // and allocate it.
65 std::scoped_lock lock(m_handleMutexes[i]);
66 m_structures[i] = toSet;
67 return static_cast<THandle>(createHandle(i, enumValue, m_version));
68 }
69 }
70 return HAL_kInvalidHandle;
71}
72
73template <typename THandle, typename TStruct, int16_t size,
74 HAL_HandleEnum enumValue>
75std::shared_ptr<TStruct>
77 THandle handle) {
78 // get handle index, and fail early if index out of range or wrong handle
79 int16_t index = GetIndex(handle);
80 if (index < 0 || index >= size) {
81 return nullptr;
82 }
83 std::scoped_lock lock(m_handleMutexes[index]);
84 // return structure. Null will propagate correctly, so no need to manually
85 // check.
86 return m_structures[index];
87}
88
89template <typename THandle, typename TStruct, int16_t size,
90 HAL_HandleEnum enumValue>
92 THandle handle) {
93 // get handle index, and fail early if index out of range or wrong handle
94 int16_t index = GetIndex(handle);
95 if (index < 0 || index >= size) {
96 return;
97 }
98 // lock and deallocated handle
99 std::scoped_lock allocateLock(m_allocateMutex);
100 std::scoped_lock handleLock(m_handleMutexes[index]);
101 m_structures[index].reset();
102}
103
104template <typename THandle, typename TStruct, int16_t size,
105 HAL_HandleEnum enumValue>
106void LimitedClassedHandleResource<THandle, TStruct, size,
107 enumValue>::ResetHandles() {
108 {
109 std::scoped_lock allocateLock(m_allocateMutex);
110 for (int i = 0; i < size; i++) {
111 std::scoped_lock handleLock(m_handleMutexes[i]);
112 m_structures[i].reset();
113 }
114 }
116}
117} // namespace wpi::hal
@ index
Definition base.h:690
virtual void ResetHandles()
int16_t m_version
Definition HandlesInternal.hpp:37
The LimitedClassedHandleResource class is a way to track handles.
Definition LimitedClassedHandleResource.hpp:31
void Free(THandle handle)
Definition LimitedClassedHandleResource.hpp:91
LimitedClassedHandleResource & operator=(const LimitedClassedHandleResource &)=delete
friend class LimitedClassedHandleResourceTest
Definition LimitedClassedHandleResource.hpp:32
LimitedClassedHandleResource(const LimitedClassedHandleResource &)=delete
int16_t GetIndex(THandle handle)
Definition LimitedClassedHandleResource.hpp:41
THandle Allocate(std::shared_ptr< TStruct > toSet)
Definition LimitedClassedHandleResource.hpp:57
void ResetHandles() override
Definition LimitedClassedHandleResource.hpp:107
std::shared_ptr< TStruct > Get(THandle handle)
Definition LimitedClassedHandleResource.hpp:76
#define HAL_kInvalidHandle
Definition Types.h:15
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition Types.hpp:9
HAL_HandleEnum
Enum of HAL handle types.
Definition HandlesInternal.hpp:45
int16_t getHandleTypedIndex(HAL_Handle handle, HAL_HandleEnum enumType, int16_t version)
Get if the handle is a correct type and version.
Definition HandlesInternal.hpp:135
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType, int16_t version)
Create a handle for a specific index, type and version.
::std::mutex mutex
Definition mutex.hpp:17