Viam C++ SDK current
Loading...
Searching...
No Matches
resource_manager.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <memory>
7#include <mutex>
8#include <string>
9#include <type_traits>
10#include <unordered_map>
11
12#include <viam/sdk/resource/resource.hpp>
13#include <viam/sdk/resource/resource_api.hpp>
14
15namespace viam {
16namespace sdk {
17
21 public:
22 ResourceManager() = default;
23
27 std::shared_ptr<Resource> resource(const std::string& name);
28
32 template <typename T>
33 std::shared_ptr<T> resource(const std::string& name) {
34 static_assert(std::is_base_of<Resource, T>::value, "T is not derived from Resource");
35 return std::dynamic_pointer_cast<T>(resource(name));
36 }
37
40 void replace_all(const std::unordered_map<Name, std::shared_ptr<Resource>>& resources);
41
45 void add(const Name& name, std::shared_ptr<Resource> resource);
46
50 void add(std::string name, std::shared_ptr<Resource> resource);
51
54 void remove(const Name& name);
55
60 void replace_one(const Name& name,
61 const std::function<std::shared_ptr<Resource>()>& create_resource);
62
64 const std::unordered_map<std::string, std::shared_ptr<Resource>>& resources() const;
65
66 private:
67 std::mutex lock_;
68 std::unordered_map<std::string, std::shared_ptr<Resource>> resources_;
70 std::unordered_map<std::string, std::string> short_names_;
71 void do_add(const Name& name, std::shared_ptr<Resource> resource);
72 void do_add(std::string name, std::shared_ptr<Resource> resource);
73 void do_remove(const Name& name);
74};
75
76} // namespace sdk
77} // namespace viam
A name for specific instances of resources.
Definition resource_api.hpp:61
Defines a resource manager for use by anything that tracks resources.
Definition resource_manager.hpp:20
void add(std::string name, std::shared_ptr< Resource > resource)
Adds a single resource to the manager.
const std::unordered_map< std::string, std::shared_ptr< Resource > > & resources() const
Returns a reference to the existing resources within the manager.
std::shared_ptr< T > resource(const std::string &name)
Returns a resource after dynamically downcasting to T.
Definition resource_manager.hpp:33
void add(const Name &name, std::shared_ptr< Resource > resource)
Adds a single resource to the manager.
void replace_one(const Name &name, const std::function< std::shared_ptr< Resource >()> &create_resource)
Replaces an existing resource. No-op if the named resource does not exist.
std::shared_ptr< Resource > resource(const std::string &name)
Returns a resource.
void replace_all(const std::unordered_map< Name, std::shared_ptr< Resource > > &resources)
Replaces all resources in the manager.
void remove(const Name &name)
Remodes a single resource from the manager.