WPILibC++  2019.1.1-beta-2-25-g73de336
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Resource.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 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 
36  Resource(Resource&&) = default;
37  Resource& operator=(Resource&&) = default;
38 
50  static void CreateResourceObject(std::unique_ptr<Resource>& r,
51  uint32_t elements);
52 
60  explicit Resource(uint32_t size);
61 
69  uint32_t Allocate(const std::string& resourceDesc);
70 
77  uint32_t Allocate(uint32_t index, const std::string& resourceDesc);
78 
86  void Free(uint32_t index);
87 
88  private:
89  std::vector<bool> m_isAllocated;
90  wpi::mutex m_allocateMutex;
91 
92  static wpi::mutex m_createMutex;
93 };
94 
95 } // namespace frc
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
The Resource class is a convenient way to track allocated resources.
Definition: Resource.h:32
void Free(uint32_t index)
Free an allocated 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.
Base class for most objects.
Definition: ErrorBase.h:74