Viam C++ SDK current
Loading...
Searching...
No Matches
resource_api.hpp
1#pragma once
2
3#include <string>
4
5#include <google/protobuf/descriptor.h>
6
7#include <viam/api/common/v1/common.pb.h>
8
9namespace viam {
10namespace sdk {
11
14class APIType {
15 public:
16 APIType(std::string namespace_, std::string resource_type);
17 APIType(){};
18 virtual std::string to_string() const;
19
20 const std::string& type_namespace() const;
21 const std::string& resource_type() const;
22
23 void set_namespace(const std::string& type_namespace);
24 void set_resource_type(const std::string& resource_type);
25
26 private:
27 std::string namespace_;
28 std::string resource_type_;
29};
30
33class API : public APIType {
34 public:
35 virtual std::string to_string() const override;
36 API(){};
37 API(std::string namespace_, std::string resource_type, std::string resource_subtype);
38 API(APIType type, std::string resource_subtype);
39 static API from_string(std::string api);
40
41 const std::string& resource_subtype() const;
42 void set_resource_subtype(const std::string& subtype);
43 bool is_component_type();
44 bool is_service_type();
45 friend bool operator==(API const& lhs, API const& rhs);
46 friend bool operator<(const API& lhs, const API& rhs);
47 friend std::ostream& operator<<(std::ostream& os, const API& v);
48
49 template <typename T>
50 struct traits;
51
52 template <typename T>
53 static API get() {
54 return traits<T>::api();
55 }
56
57 private:
58 std::string resource_subtype_;
59};
60
63class Name {
64 public:
65 std::string short_name() const;
66 std::string to_string() const;
67 viam::common::v1::ResourceName to_proto() const;
68 static Name from_proto(const viam::common::v1::ResourceName& proto);
69 static Name from_string(std::string name);
70 Name(API api, std::string remote_name, std::string name);
71 Name(){};
72 const API& api() const;
73 const std::string& name() const;
74 const std::string& remote_name() const;
75 friend bool operator==(const Name& lhs, const Name& rhs);
76 friend std::ostream& operator<<(std::ostream& os, const Name& v);
77
78 private:
79 API api_;
80 std::string remote_name_;
81 std::string name_;
82};
83
85 public:
86 bool operator<(const RPCSubtype& rhs) const {
87 return (api_.to_string() + proto_service_name_ + descriptor_.DebugString()) <
88 (rhs.api_.to_string() + rhs.proto_service_name_ + rhs.descriptor_.DebugString());
89 };
90 const std::string& proto_service_name() const;
91 const API& api() const;
92
93 RPCSubtype(API api, const google::protobuf::ServiceDescriptor& descriptor);
94 RPCSubtype(API api,
95 std::string proto_service_name,
96 const google::protobuf::ServiceDescriptor& descriptor);
97 friend bool operator==(const RPCSubtype& lhs, const RPCSubtype& rhs);
98
99 private:
100 const google::protobuf::ServiceDescriptor& descriptor_;
101 std::string proto_service_name_;
102 API api_;
103};
104
106 public:
107 ModelFamily(std::string namespace_, std::string family);
108 std::string to_string() const;
109
110 private:
111 std::string namespace_;
112 std::string family_;
113};
114
117class Model {
118 public:
119 std::string to_string() const;
120
121 Model(std::string namespace_, std::string family, std::string model_name);
122 Model(ModelFamily model, std::string model_name);
123 Model();
124
129 static Model from_str(std::string model);
130 friend bool operator==(const Model& lhs, const Model& rhs);
131
132 private:
133 ModelFamily model_family_;
134 std::string model_name_;
135};
136
137} // namespace sdk
138} // namespace viam
139
140template <>
141struct std::hash<::viam::sdk::Name> {
142 size_t operator()(::viam::sdk::Name const& key) const noexcept {
143 return std::hash<std::string>()(key.to_string());
144 }
145};
146
147template <>
148struct std::hash<::viam::sdk::RPCSubtype> {
149 size_t operator()(::viam::sdk::RPCSubtype const& key) const noexcept {
150 const ::viam::sdk::API& api = key.api();
151 std::string hash = api.to_string() + key.proto_service_name();
152 return std::hash<std::string>()(hash);
153 };
154};
155
156template <>
157struct std::hash<::viam::sdk::API> {
158 size_t operator()(const ::viam::sdk::API& key) const noexcept {
159 return std::hash<std::string>()(key.to_string());
160 };
161};
Defines a resource's namespace (e.g., RDK) and type (e.g., component or service).
Definition resource_api.hpp:14
Extends APIType to additionally define a resource's subtype (e.g., camera).
Definition resource_api.hpp:33
Definition resource_api.hpp:105
Defines the namespace_, family, and name for a particular resource model.
Definition resource_api.hpp:117
static Model from_str(std::string model)
Parses a single model string into a Model, using default values for namespace and family if not provi...
A name for specific instances of resources.
Definition resource_api.hpp:63
Definition resource_api.hpp:84
Definition resource_api.hpp:50