Loading [MathJax]/extensions/tex2jax.js
WPILibC++ 2025.3.2
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 * @deprecated Use the HandleResource classes instead
27 */
28class [[deprecated("Use the HandleResource classes instead")]] Resource {
29 public:
30 virtual ~Resource() = default;
31
32 /**
33 * Factory method to create a Resource allocation-tracker *if* needed.
34 *
35 * @param r address of the caller's Resource pointer. If *r == nullptr,
36 * this will construct a Resource and make *r point to it. If
37 * *r != nullptr, i.e. the caller already has a Resource
38 * instance, this won't do anything.
39 * @param elements the number of elements for this Resource allocator to
40 * track, that is, it will allocate resource numbers in the
41 * range [0 .. elements - 1].
42 */
43 static void CreateResourceObject(std::unique_ptr<Resource>& r,
44 uint32_t elements);
45
46 /**
47 * Allocate storage for a new instance of Resource.
48 *
49 * Allocate a bool array of values that will get initialized to indicate that
50 * no resources have been allocated yet. The indices of the resources are
51 * [0 .. elements - 1].
52 */
53 explicit Resource(uint32_t size);
54
55 /**
56 * Allocate a resource.
57 *
58 * When a resource is requested, mark it allocated. In this case, a free
59 * resource value within the range is located and returned after it is marked
60 * allocated.
61 */
62 uint32_t Allocate(const std::string& resourceDesc);
63
64 /**
65 * Allocate a specific resource value.
66 *
67 * The user requests a specific resource value, i.e. channel number and it is
68 * verified unallocated, then returned.
69 */
70 uint32_t Allocate(uint32_t index, const std::string& resourceDesc);
71
72 /**
73 * Free an allocated resource.
74 *
75 * After a resource is no longer needed, for example a destructor is called
76 * for a channel assignment class, Free will release the resource value so it
77 * can be reused somewhere else in the program.
78 */
79 void Free(uint32_t index);
80
81 private:
82 std::vector<bool> m_isAllocated;
83 wpi::mutex m_allocateMutex;
84
85 static wpi::mutex m_createMutex;
86};
87
88} // namespace frc
The Resource class is a convenient way to track allocated resources.
Definition Resource.h:28
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 CAN.h:11
::std::mutex mutex
Definition mutex.h:17