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 <string>
8#include <type_traits>
9#include <unordered_map>
10
11#include <boost/optional/optional.hpp>
12#include <grpcpp/impl/service_type.h>
13
14#include <viam/api/component/generic/v1/generic.grpc.pb.h>
15
16#include <viam/sdk/resource/resource.hpp>
17#include <viam/sdk/resource/resource_api.hpp>
18
19namespace viam {
20namespace sdk {
21
25 public:
29 std::shared_ptr<Resource> resource(const std::string& name);
30
34 template <typename T>
35 std::shared_ptr<T> resource(const std::string& name) {
36 static_assert(std::is_base_of<Resource, T>::value, "T is not derived from Resource");
37 return std::dynamic_pointer_cast<T>(resource(name));
38 }
39
42 void replace_all(const std::unordered_map<Name, std::shared_ptr<Resource>>& resources);
43
47 void add(const Name& name, std::shared_ptr<Resource> resource);
48
52 void add(std::string name, std::shared_ptr<Resource> resource);
53
56 void remove(const Name& name);
57
61 void replace_one(const Name& name, std::shared_ptr<Resource> resource);
62
64 const std::unordered_map<std::string, std::shared_ptr<Resource>>& resources() const;
65
67
68 private:
69 std::mutex lock_;
70 std::unordered_map<std::string, std::shared_ptr<Resource>> resources_;
72 std::unordered_map<std::string, std::string> short_names_;
73 void do_add(const Name& name, std::shared_ptr<Resource> resource);
74 void do_add(std::string name, std::shared_ptr<Resource> resource);
75 void do_remove(const Name& name);
76};
77
78} // namespace sdk
79} // namespace viam
A name for specific instances of resources.
Definition resource_api.hpp:63
Defines a resource manager for use by anything that tracks resources.
Definition resource_manager.hpp:24
void add(std::string name, std::shared_ptr< Resource > resource)
Adds a single resource to the manager.
void replace_one(const Name &name, std::shared_ptr< Resource > resource)
Replaces an existing resource. No-op if the named resource does not exist.
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:35
void add(const Name &name, std::shared_ptr< Resource > resource)
Adds a single resource to the manager.
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.