2020namespace Aws {
2121namespace Client {
2222
23+ class ParameterPath {
24+ public:
25+ /* *
26+ * Legacy constructor for backwards compatibility.
27+ * @hint For ROS1, the node namespace separator is the same as the parameter namespace separator,
28+ * and so the path would simply be the given string (dictated by Ros1NodeParameterReader).
29+ * For ROS2 the separators differ; If only node namespace separators were found, we would treat them as parameter separators.
30+ * @param name Parameter name, which in practice would be the path of the parameter, e.g. config/timeoutMs.
31+ */
32+ ParameterPath (const char * name) : name_(name) {}
33+
34+ /* *
35+ * @example The parameter under node "lidar_node" in namespace "sensor_nodes",
36+ * with parameter namespace "settings" and parameter key "frequency", should be specified as follows:
37+ * node_namespace: ["sensor_nodes", "lidar_node"]
38+ * parameter_path_keys: ["settings", "frequency"]
39+ * @note Node namespaces should be empty if used by a node looking for a parameter of its own (a local parameter).
40+ * @param node_namespaces
41+ * @param parameter_path_keys
42+ */
43+ ParameterPath (const std::vector<std::string> & node_namespaces, const std::vector<std::string> & parameter_path_keys) :
44+ node_namespaces_ (node_namespaces), parameter_path_keys_(parameter_path_keys) {}
45+
46+ /* *
47+ * @example The local parameter "timeout" in parameter namespace "config" should be specified as follows:
48+ * ParameterPath("config", "timeout")
49+ * @tparam Args
50+ * @param parameter_path_keys
51+ */
52+ template <typename ...Args>
53+ ParameterPath (Args... parameter_path_keys) :
54+ parameter_path_keys_ (std::vector<std::string>{(parameter_path_keys)...}) {}
55+
56+ /* *
57+ * @note Only applies if node_namespaces was specified during construction; otherwise, an empty string is returned.
58+ * @param node_namespace_separator
59+ * @return string The parameter's whereabouts in the node namespace hierarchy.
60+ */
61+ std::string get_node_path (char node_namespace_separator) const
62+ {
63+ std::string resolved_path;
64+ /* Construct the node's path by the provided lists of keys */
65+ for (auto key = node_namespaces_.begin (); key != node_namespaces_.end (); key++) {
66+ resolved_path += *key + node_namespace_separator;
67+ }
68+ if (!resolved_path.empty () && resolved_path.back () == node_namespace_separator) {
69+ resolved_path.pop_back ();
70+ }
71+ return resolved_path;
72+ }
73+ /* *
74+ * @note Only applies if parameter_path_keys was specified during construction; otherwise, an empty string is returned.
75+ * @param parameter_namespace_separator
76+ * @return string The parameter path including parameter namespaces but excluding node's namespaces.
77+ */
78+ std::string get_local_path (char parameter_namespace_separator) const
79+ {
80+ std::string resolved_path;
81+ /* Construct the parameter's path by the provided lists of keys */
82+ for (auto key = parameter_path_keys_.begin (); key != parameter_path_keys_.end (); key++) {
83+ resolved_path += *key + parameter_namespace_separator;
84+ }
85+ if (!resolved_path.empty () && resolved_path.back () == parameter_namespace_separator) {
86+ resolved_path.pop_back ();
87+ }
88+ return resolved_path;
89+ }
90+ /* *
91+ * Resolves & returns the parameter's path. Supports two separate use cases:
92+ * 1. A single, 'flat' string was provided upon construction.
93+ * In this case, we return it as is.
94+ * 2. Detailed lists of strings were specified for the parameter & node namespaces.
95+ * In this case, we construct the resolved path using the provided separators.
96+ * @param node_namespace_separator
97+ * @param parameter_namespace_separator
98+ * @return string representing the full, resolved path of the parameter.
99+ * @note If node_namespaces and parameter_path_keys are empty, an empty string would be returned.
100+ */
101+ std::string get_resolved_path (char node_namespace_separator,
102+ char parameter_namespace_separator) const
103+ {
104+ if (!name_.empty ()) {
105+ return name_;
106+ } else {
107+ std::string resolved_path = get_node_path (node_namespace_separator);
108+ if (!resolved_path.empty ()) {
109+ resolved_path += node_namespace_separator;
110+ }
111+ resolved_path += get_local_path (parameter_namespace_separator);
112+ return resolved_path;
113+ }
114+ }
115+ private:
116+ /* *
117+ * Member variables to store the parameter's path in the namespace hierarchy.
118+ * parameter_path_keys_ Parameter namespaces list.
119+ * node_namespaces_ Node namespaces list.
120+ * name_ A single string. Used when simpler initialization is required, instead of the aforementioned namespace lists.
121+ */
122+ const std::vector<std::string> parameter_path_keys_;
123+ const std::vector<std::string> node_namespaces_;
124+ const std::string name_;
125+ };
126+
23127class ParameterReaderInterface
24128{
25129public:
@@ -28,58 +132,65 @@ class ParameterReaderInterface
28132 /* *
29133 * read a list from the provided parameter name
30134 * @param name the name of the parameter to be read
31- * @param out the output of 'double' type
135+ * @param out the output of 'double' type.
32136 * @return AWS_ERR_OK if read was successful, AWS_ERR_NOT_FOUND if the parameter was not found
137+ * @note if the return code is not AWS_ERR_OK, out remains unchanged.
33138 */
34- virtual AwsError ReadList (const char * name , std::vector<std::string> & out) const = 0;
139+ virtual AwsError ReadList (const ParameterPath & parameter_path , std::vector<std::string> & out) const = 0;
35140
36141 /* *
37142 * read a double value from the provided parameter name
38- * @param name the name of the parameter to be read
143+ * @param parameter_path an object representing the path of the parameter to be read
39144 * @param out the output of 'double' type
40145 * @return AWS_ERR_OK if read was successful, AWS_ERR_NOT_FOUND if the parameter was not found
146+ * @note if the return code is not AWS_ERR_OK, out remains unchanged.
41147 */
42- virtual AwsError ReadDouble (const char * name , double & out) const = 0;
148+ virtual AwsError ReadDouble (const ParameterPath & parameter_path , double & out) const = 0;
43149
44150 /* *
45151 * read an integer value from the provided parameter name
46- * @param name the name of the parameter to be read
152+ * @param parameter_path an object representing the path of the parameter to be read
47153 * @param out the output of 'int' type
48154 * @return AWS_ERR_OK if read was successful, AWS_ERR_NOT_FOUND if the parameter was not found
155+ * @note if the return code is not AWS_ERR_OK, out remains unchanged.
49156 */
50- virtual AwsError ReadInt (const char * name , int & out) const = 0;
157+ virtual AwsError ReadInt (const ParameterPath & parameter_path , int & out) const = 0;
51158
52159 /* *
53160 * read a boolean value from the provided parameter name
54- * @param name the name of the parameter to be read
161+ * @param parameter_path an object representing the path of the parameter to be read
55162 * @param out the output of 'bool' type
56163 * @return AWS_ERR_OK if read was successful, AWS_ERR_NOT_FOUND if the parameter was not found
164+ * @note if the return code is not AWS_ERR_OK, out remains unchanged.
57165 */
58- virtual AwsError ReadBool (const char * name , bool & out) const = 0;
166+ virtual AwsError ReadBool (const ParameterPath & parameter_path , bool & out) const = 0;
59167
60168 /* *
61169 * read a string value from the provided parameter name
62- * @param name the name of the parameter to be read
170+ * @param parameter_path an object representing the path of the parameter to be read
63171 * @param out the output of 'Aws::String' type
64172 * @return AWS_ERR_OK if read was successful, AWS_ERR_NOT_FOUND if the parameter was not found
173+ * @note if the return code is not AWS_ERR_OK, out remains unchanged.
65174 */
66- virtual AwsError ReadString (const char * name , Aws::String & out) const = 0;
175+ virtual AwsError ReadString (const ParameterPath & parameter_path , Aws::String & out) const = 0;
67176
68177 /* *
69178 * read a string from the provided parameter name
70- * @param name the name of the parameter to be read
179+ * @param parameter_path an object representing the path of the parameter to be read
71180 * @param out the output of 'std::string' type
72181 * @return AWS_ERR_OK if read was successful, AWS_ERR_NOT_FOUND if the parameter was not found
182+ * @note if the return code is not AWS_ERR_OK, out remains unchanged.
73183 */
74- virtual AwsError ReadStdString (const char * name , std::string & out) const = 0;
184+ virtual AwsError ReadStdString (const ParameterPath & parameter_path , std::string & out) const = 0;
75185
76186 /* *
77187 * read a map from the provided parameter name
78- * @param name the name of the parameter to be read
188+ * @param parameter_path an object representing the path of the parameter to be read
79189 * @param out the output of 'std::map' type
80190 * @return AWS_ERR_OK if read was successful, AWS_ERR_NOT_FOUND if the parameter was not found
191+ * @note if the return code is not AWS_ERR_OK, out remains unchanged.
81192 */
82- virtual AwsError ReadMap (const char * name , std::map<std::string, std::string> & out) const = 0;
193+ virtual AwsError ReadMap (const ParameterPath & parameter_path , std::map<std::string, std::string> & out) const = 0;
83194};
84195
85196} // namespace Client
0 commit comments