ObjectID.cpp
Go to the documentation of this file.
1#include "ObjectID.h"
2
3#include <SimoxUtility/algorithm/string/string_tools.h>
4
6
7namespace armarx
8{
10 {
11 }
12
13 ObjectID::ObjectID(const std::string& dataset,
14 const std::string& className,
15 const std::string& instancName) :
16 _dataset(dataset), _className(className), _instanceName(instancName)
17 {
18 }
19
20 ObjectID::ObjectID(const std::string& nameOrID)
21 {
22 if (nameOrID.find("/") != nameOrID.npos)
23 {
24 setFromString(nameOrID);
25 }
26 else
27 {
28 // Dataset, instanceName are left empty.
29 _className = nameOrID;
30 }
31 }
32
34 ObjectID::FromString(const std::string& idString)
35 {
36 ObjectID id;
37 id.setFromString(idString);
38 return id;
39 }
40
41 void
42 ObjectID::setFromString(const std::string& idString)
43 {
44 const std::vector<std::string> split = simox::alg::split(idString, "/", true);
45 ARMARX_CHECK(split.size() == 2 || split.size() == 3)
46 << "Expected ID of format 'Dataset/ClassName' or 'Dataset/ClassName/InstanceName'"
47 << ", but got: '" << idString << "' "
48 << "(expected 2 or 3 '/'s, but found " << split.size() << ").";
49
50 _dataset = split[0];
51 _className = split[1];
52
53 if (split.size() == 3)
54 {
55 _instanceName = split[2];
56 }
57 }
58
59 std::string
61 {
62 std::string _str = _dataset + "/" + _className;
63 if (!_instanceName.empty())
64 {
65 _str += "/" + _instanceName;
66 }
67 return _str;
68 }
69
72 {
73 return ObjectID(_dataset, _className);
74 }
75
76 bool
78 {
79 return _className == rhs._className && _dataset == rhs._dataset;
80 }
81
83 ObjectID::withInstanceName(const std::string& instanceName) const
84 {
85 return ObjectID(_dataset, _className, instanceName);
86 }
87
88 bool
90 {
91 return _className == rhs._className && _dataset == rhs._dataset &&
92 _instanceName == rhs._instanceName;
93 }
94
95 bool
96 ObjectID::operator<(const ObjectID& rhs) const
97 {
98 int c = _dataset.compare(rhs._dataset);
99 if (c != 0)
100 {
101 return c < 0;
102 }
103 // equal dataset
104 c = _className.compare(rhs._className);
105 if (c != 0)
106 {
107 return c < 0;
108 }
109 // equal class name
110 return _instanceName < rhs._instanceName;
111 }
112
113} // namespace armarx
114
115std::ostream&
116armarx::operator<<(std::ostream& os, const ObjectID& id)
117{
118 return os << "'" << id.str() << "'";
119}
constexpr T c
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition ObjectID.h:11
void setFromString(const std::string &idString)
Definition ObjectID.cpp:42
std::string className() const
Definition ObjectID.h:30
bool operator<(const ObjectID &rhs) const
Definition ObjectID.cpp:96
static ObjectID FromString(const std::string &idString)
Construct from a string produced by str(), e.g. ("mydataset/myobject", "mydataset/myclass/myinstance"...
Definition ObjectID.cpp:34
ObjectID withInstanceName(const std::string &instanceName) const
Definition ObjectID.cpp:83
ObjectID getClassID() const
Return just the class ID without an intance name.
Definition ObjectID.cpp:71
bool equalClass(const ObjectID &rhs) const
Indicates whether dataset and class name are equal.
Definition ObjectID.cpp:77
bool operator==(const ObjectID &rhs) const
Indicates whether dataset, class name and instance name are equal.
Definition ObjectID.cpp:89
std::string instanceName() const
Definition ObjectID.h:36
std::string dataset() const
Definition ObjectID.h:24
std::string str() const
Return "dataset/className" or "dataset/className/instanceName".
Definition ObjectID.cpp:60
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::ostream & operator<<(std::ostream &os, const PythonApplicationManager::Paths &paths)
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)