YARP
Yet Another Robot Platform
Triple.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_SERVERSQL_IMPL_TRIPLE_H
8 #define YARP_SERVERSQL_IMPL_TRIPLE_H
9 
10 #include <string>
11 #include <yarp/conf/compiler.h>
12 
13 
14 namespace yarp {
15 namespace serversql {
16 namespace impl {
17 
24 class Triple
25 {
26 public:
27  std::string ns;
28  std::string name;
29  std::string value;
30  bool hasNs;
31  bool hasName;
32  bool hasValue;
33 
34 
36  {
37  reset();
38  }
39 
40  Triple(const Triple& alt)
41  {
42  hasNs = alt.hasNs;
43  hasName = alt.hasName;
44  hasValue = alt.hasValue;
45  ns = alt.ns;
46  name = alt.name;
47  value = alt.value;
48  }
49 
50  void reset()
51  {
52  hasNs = hasName = hasValue = false;
53  ns = name = value = "";
54  }
55 
56  void split(const std::string& str)
57  {
58  hasNs = hasName = hasValue = false;
59  ns = name = value = "";
60  size_t start = 0;
61  size_t stop = std::string::npos;
62  size_t ins = str.find(':');
63  if (ins!=std::string::npos) {
64  ns = str.substr(0,ins);
65  start = ins+1;
66  hasNs = true;
67  }
68  size_t ine = str.find('=',start);
69  if (ine!=std::string::npos) {
70  value = str.substr(ine+1,std::string::npos);
71  stop = ine;
72  hasValue = true;
73  }
74  name = str.substr(start,stop-start);
75  hasName = true;
76  }
77 
78  const char* getNs()
79  {
80  if (!hasNs) {
81  return nullptr;
82  }
83  return ns.c_str();
84  }
85 
86  const char* getName()
87  {
88  if (!hasName) {
89  return nullptr;
90  }
91  return name.c_str();
92  }
93 
94  const char* getValue()
95  {
96  if (!hasValue) {
97  return nullptr;
98  }
99  return value.c_str();
100  }
101 
102  std::string toString() const
103  {
104  std::string r = "";
105  if (hasName) {
106  r = name;
107  }
108  if (hasValue) {
109  r += "=";
110  r += value;
111  }
112  if (hasNs) {
113  r = std::string(ns) + ":" + r;
114  }
115  return r;
116  }
117 
118  void setNsNameValue(const char* ns, const char* name, const char* value)
119  {
121  hasNs = true;
122  this->ns = ns;
123  }
124 
125  void setNameValue(const char* name, const char* value)
126  {
127  reset();
128  hasName = true;
129  this->name = name;
130  hasValue = true;
131  this->value = value;
132  }
133 };
134 
135 } // namespace impl
136 } // namespace serversql
137 } // namespace yarp
138 
139 
140 #endif // YARP_SERVERSQL_IMPL_TRIPLE_H
The basic unit of data the name server works with.
Definition: Triple.h:25
const char * getValue()
Definition: Triple.h:94
void split(const std::string &str)
Definition: Triple.h:56
void setNsNameValue(const char *ns, const char *name, const char *value)
Definition: Triple.h:118
const char * getNs()
Definition: Triple.h:78
Triple(const Triple &alt)
Definition: Triple.h:40
std::string toString() const
Definition: Triple.h:102
void setNameValue(const char *name, const char *value)
Definition: Triple.h:125
const char * getName()
Definition: Triple.h:86
The main, catch-all namespace for YARP.
Definition: dirs.h:16