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