WPILibC++ 2024.3.2
Resource.h
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 <memory>
10#include <string>
11#include <vector>
12
13#include <wpi/mutex.h>
14
15namespace frc {
16
17/**
18 * The Resource class is a convenient way to track allocated resources.
19 *
20 * It tracks them as indices in the range [0 .. elements - 1]. E.g. the library
21 * uses this to track hardware channel allocation.
22 *
23 * The Resource class does not allocate the hardware channels or other
24 * resources; it just tracks which indices were marked in use by Allocate and
25 * not yet freed by Free.
26 */
27class Resource {
28 public:
29 virtual ~Resource() = default;
30
31 /**
32 * Factory method to create a Resource allocation-tracker *if* needed.
33 *
34 * @param r address of the caller's Resource pointer. If *r == nullptr,
35 * this will construct a Resource and make *r point to it. If
36 * *r != nullptr, i.e. the caller already has a Resource
37 * instance, this won't do anything.
38 * @param elements the number of elements for this Resource allocator to
39 * track, that is, it will allocate resource numbers in the
40 * range [0 .. elements - 1].
41 */
42 static void CreateResourceObject(std::unique_ptr<Resource>& r,
43 uint32_t elements);
44
45 /**
46 * Allocate storage for a new instance of Resource.
47 *
48 * Allocate a bool array of values that will get initialized to indicate that
49 * no resources have been allocated yet. The indices of the resources are
50 * [0 .. elements - 1].
51 */
52 explicit Resource(uint32_t size);
53
54 /**
55 * Allocate a resource.
56 *
57 * When a resource is requested, mark it allocated. In this case, a free
58 * resource value within the range is located and returned after it is marked
59 * allocated.
60 */
61 uint32_t Allocate(const std::string& resourceDesc);
62
63 /**
64 * Allocate a specific resource value.
65 *
66 * The user requests a specific resource value, i.e. channel number and it is
67 * verified unallocated, then returned.
68 */
69 uint32_t Allocate(uint32_t index, const std::string& resourceDesc);
70
71 /**
72 * Free an allocated resource.
73 *
74 * After a resource is no longer needed, for example a destructor is called
75 * for a channel assignment class, Free will release the resource value so it
76 * can be reused somewhere else in the program.
77 */
78 void Free(uint32_t index);
79
80 private:
81 std::vector<bool> m_isAllocated;
82 wpi::mutex m_allocateMutex;
83
84 static wpi::mutex m_createMutex;
85};
86
87} // namespace frc
The Resource class is a convenient way to track allocated resources.
Definition: Resource.h:27
void Free(uint32_t index)
Free an allocated resource.
uint32_t Allocate(uint32_t index, const std::string &resourceDesc)
Allocate a specific resource value.
virtual ~Resource()=default
Resource(uint32_t size)
Allocate storage for a new instance of Resource.
uint32_t Allocate(const std::string &resourceDesc)
Allocate a resource.
static void CreateResourceObject(std::unique_ptr< Resource > &r, uint32_t elements)
Factory method to create a Resource allocation-tracker if needed.
Definition: AprilTagPoseEstimator.h:15
::std::mutex mutex
Definition: mutex.h:17