WPILibC++  unspecified
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 <support/mutex.h>
17 
18 #include "ErrorBase.h"
19 
20 namespace frc {
21 
32 class Resource : public ErrorBase {
33  public:
34  virtual ~Resource() = default;
35 
36  Resource(const Resource&) = delete;
37  Resource& operator=(const Resource&) = delete;
38 
39  static void CreateResourceObject(std::unique_ptr<Resource>& r,
40  uint32_t elements);
41  explicit Resource(uint32_t size);
42  uint32_t Allocate(const std::string& resourceDesc);
43  uint32_t Allocate(uint32_t index, const std::string& resourceDesc);
44  void Free(uint32_t index);
45 
46  private:
47  std::vector<bool> m_isAllocated;
48  wpi::mutex m_allocateMutex;
49 
50  static wpi::mutex m_createMutex;
51 };
52 
53 } // namespace frc
Definition: RobotController.cpp:14
The Resource class is a convenient way to track allocated resources.
Definition: Resource.h:32
uint32_t Allocate(const std::string &resourceDesc)
Allocate a resource.
Definition: Resource.cpp:54
Base class for most objects.
Definition: ErrorBase.h:74
void Free(uint32_t index)
Free an allocated resource.
Definition: Resource.cpp:93
static void CreateResourceObject(std::unique_ptr< Resource > &r, uint32_t elements)
Factory method to create a Resource allocation-tracker if needed.
Definition: Resource.cpp:39