WPILibC++  2020.3.2-60-g3011ebe
Resource.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2019 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 <memory>
13 #include <string>
14 #include <vector>
15 
16 #include <wpi/mutex.h>
17 
18 #include "frc/ErrorBase.h"
19 
20 namespace frc {
21 
32 class Resource : public ErrorBase {
33  public:
34  virtual ~Resource() = default;
35 
47  static void CreateResourceObject(std::unique_ptr<Resource>& r,
48  uint32_t elements);
49 
57  explicit Resource(uint32_t size);
58 
66  uint32_t Allocate(const std::string& resourceDesc);
67 
74  uint32_t Allocate(uint32_t index, const std::string& resourceDesc);
75 
83  void Free(uint32_t index);
84 
85  private:
86  std::vector<bool> m_isAllocated;
87  wpi::mutex m_allocateMutex;
88 
89  static wpi::mutex m_createMutex;
90 };
91 
92 } // namespace frc
frc::Resource
The Resource class is a convenient way to track allocated resources.
Definition: Resource.h:32
frc::Resource::Resource
Resource(uint32_t size)
Allocate storage for a new instance of Resource.
frc::Resource::CreateResourceObject
static void CreateResourceObject(std::unique_ptr< Resource > &r, uint32_t elements)
Factory method to create a Resource allocation-tracker if needed.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc
A class that enforces constraints on the differential drive kinematics.
Definition: PDPSim.h:16
frc::Resource::Allocate
uint32_t Allocate(const std::string &resourceDesc)
Allocate a resource.
frc::Resource::Free
void Free(uint32_t index)
Free an allocated resource.