WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Resource.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2017. 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 "ErrorBase.h"
17 #include "HAL/cpp/priority_mutex.h"
18 
19 namespace frc {
20 
30 class Resource : public ErrorBase {
31  public:
32  virtual ~Resource() = default;
33 
34  Resource(const Resource&) = delete;
35  Resource& operator=(const Resource&) = delete;
36 
37  static void CreateResourceObject(std::unique_ptr<Resource>& r,
38  uint32_t elements);
39  explicit Resource(uint32_t size);
40  uint32_t Allocate(const std::string& resourceDesc);
41  uint32_t Allocate(uint32_t index, const std::string& resourceDesc);
42  void Free(uint32_t index);
43 
44  private:
45  std::vector<bool> m_isAllocated;
46  hal::priority_recursive_mutex m_allocateLock;
47 
48  static hal::priority_recursive_mutex m_createLock;
49 };
50 
51 } // namespace frc
The Resource class is a convenient way to track allocated resources.
Definition: Resource.h:30
uint32_t Allocate(const std::string &resourceDesc)
Allocate a resource.
Definition: Resource.cpp:55
Base class for most objects.
Definition: ErrorBase.h:72
Definition: priority_mutex.h:26
void Free(uint32_t index)
Free an allocated resource.
Definition: Resource.cpp:94
static void CreateResourceObject(std::unique_ptr< Resource > &r, uint32_t elements)
Factory method to create a Resource allocation-tracker if needed.
Definition: Resource.cpp:40