WPILibC++ 2024.3.2
frc::Resource Class Reference

The Resource class is a convenient way to track allocated resources. More...

#include <frc/Resource.h>

Public Member Functions

virtual ~Resource ()=default
 
 Resource (uint32_t size)
 Allocate storage for a new instance of Resource. More...
 
uint32_t Allocate (const std::string &resourceDesc)
 Allocate a resource. More...
 
uint32_t Allocate (uint32_t index, const std::string &resourceDesc)
 Allocate a specific resource value. More...
 
void Free (uint32_t index)
 Free an allocated resource. More...
 

Static Public Member Functions

static void CreateResourceObject (std::unique_ptr< Resource > &r, uint32_t elements)
 Factory method to create a Resource allocation-tracker if needed. More...
 

Detailed Description

The Resource class is a convenient way to track allocated resources.

It tracks them as indices in the range [0 .. elements - 1]. E.g. the library uses this to track hardware channel allocation.

The Resource class does not allocate the hardware channels or other resources; it just tracks which indices were marked in use by Allocate and not yet freed by Free.

Constructor & Destructor Documentation

◆ ~Resource()

virtual frc::Resource::~Resource ( )
virtualdefault

◆ Resource()

frc::Resource::Resource ( uint32_t  size)
explicit

Allocate storage for a new instance of Resource.

Allocate a bool array of values that will get initialized to indicate that no resources have been allocated yet. The indices of the resources are [0 .. elements - 1].

Member Function Documentation

◆ Allocate() [1/2]

uint32_t frc::Resource::Allocate ( const std::string &  resourceDesc)

Allocate a resource.

When a resource is requested, mark it allocated. In this case, a free resource value within the range is located and returned after it is marked allocated.

◆ Allocate() [2/2]

uint32_t frc::Resource::Allocate ( uint32_t  index,
const std::string &  resourceDesc 
)

Allocate a specific resource value.

The user requests a specific resource value, i.e. channel number and it is verified unallocated, then returned.

◆ CreateResourceObject()

static void frc::Resource::CreateResourceObject ( std::unique_ptr< Resource > &  r,
uint32_t  elements 
)
static

Factory method to create a Resource allocation-tracker if needed.

Parameters
raddress of the caller's Resource pointer. If *r == nullptr, this will construct a Resource and make *r point to it. If *r != nullptr, i.e. the caller already has a Resource instance, this won't do anything.
elementsthe number of elements for this Resource allocator to track, that is, it will allocate resource numbers in the range [0 .. elements - 1].

◆ Free()

void frc::Resource::Free ( uint32_t  index)

Free an allocated resource.

After a resource is no longer needed, for example a destructor is called for a channel assignment class, Free will release the resource value so it can be reused somewhere else in the program.


The documentation for this class was generated from the following file: