Choreonoid  1.8
GeneralId.h
Go to the documentation of this file.
1 #ifndef CNOID_UTIL_GENERAL_ID_H
2 #define CNOID_UTIL_GENERAL_ID_H
3 
4 #include <string>
5 #include "exportdecl.h"
6 
7 namespace cnoid {
8 
9 class Mapping;
10 
11 class CNOID_EXPORT GeneralId
12 {
13 public:
14  static GeneralId defaultId() { return GeneralId(0); }
15 
17  : valueType(Int), intId(-1) { }
18  GeneralId(int id)
19  : valueType(Int), intId(id) { }
20  GeneralId(const std::string& id)
21  : valueType(String), intId(0), stringId(id) { }
22  GeneralId(const GeneralId& org)
23  : valueType(org.valueType), intId(org.intId), stringId(org.stringId) { }
24 
26  valueType = rhs.valueType;
27  intId = rhs.intId;
28  stringId = rhs.stringId;
29  return *this;
30  }
31  GeneralId& operator=(int rhs){
32  valueType = Int;
33  intId = rhs;
34  stringId.clear();
35  return *this;
36  }
37  GeneralId& operator=(const std::string& rhs){
38  valueType = String;
39  intId = 0;
40  stringId = rhs;
41  return *this;
42  }
43  bool operator==(const GeneralId& rhs) const {
44  if(valueType == Int){
45  return rhs.valueType == Int && intId == rhs.intId;
46  } else {
47  return rhs.valueType == String && stringId == rhs.stringId;
48  }
49  }
50  bool operator!=(const GeneralId& rhs) const {
51  return !(this->operator==(rhs));
52  }
53  bool operator==(int rhs) const {
54  return (valueType == Int && intId == rhs);
55  }
56  bool operator!=(int rhs) const {
57  return !(this->operator==(rhs));
58  }
59  bool operator==(const std::string& rhs) const {
60  return (valueType == String && stringId == rhs);
61  }
62  bool operator!=(const std::string& rhs) const {
63  return !(this->operator==(rhs));
64  }
65 
66  bool isValid() const { return intId >= 0; }
67  bool isInt() const { return valueType == Int; }
68  bool isString() const { return valueType == String; }
69  int toInt() const { return intId; }
70  const std::string& toString() const { return stringId; }
71  std::string label() const;
72 
73  bool read(const Mapping& archive, const char* key);
74  bool write(Mapping& archive, const char* key) const;
75 
76  struct Hash {
77  typedef size_t result_type;
78  result_type operator()(const GeneralId& key) const{
79  if(key.isInt()){
80  return std::hash<int>()(key.toInt());
81  } else {
82  return std::hash<std::string>()(key.toString());
83  }
84  }
85  };
86 
87 private:
88  enum IdValueType { Int, String } valueType;
89  int intId;
90  std::string stringId;
91 };
92 
93 }
94 
95 #endif
cnoid::Mapping
Definition: ValueTree.h:253
cnoid::GeneralId
Definition: GeneralId.h:11
cnoid::write
Listing * write(Mapping *mapping, const std::string &key, const Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:145
cnoid::GeneralId::GeneralId
GeneralId(const std::string &id)
Definition: GeneralId.h:20
cnoid::GeneralId::toInt
int toInt() const
Definition: GeneralId.h:69
cnoid::GeneralId::operator!=
bool operator!=(const std::string &rhs) const
Definition: GeneralId.h:62
cnoid::GeneralId::Hash
Definition: GeneralId.h:76
cnoid::GeneralId::isInt
bool isInt() const
Definition: GeneralId.h:67
cnoid::read
bool read(const Mapping *mapping, const std::string &key, Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:43
cnoid::GeneralId::GeneralId
GeneralId()
Definition: GeneralId.h:16
cnoid::GeneralId::operator!=
bool operator!=(int rhs) const
Definition: GeneralId.h:56
cnoid::GeneralId::isValid
bool isValid() const
Definition: GeneralId.h:66
cnoid::GeneralId::operator==
bool operator==(const std::string &rhs) const
Definition: GeneralId.h:59
cnoid::GeneralId::defaultId
static GeneralId defaultId()
Definition: GeneralId.h:14
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::operator==
bool operator==(ref_ptr< T > const &a, ref_ptr< U > const &b)
Definition: Referenced.h:211
cnoid::GeneralId::operator==
bool operator==(int rhs) const
Definition: GeneralId.h:53
cnoid::GeneralId::GeneralId
GeneralId(int id)
Definition: GeneralId.h:18
cnoid::GeneralId::operator!=
bool operator!=(const GeneralId &rhs) const
Definition: GeneralId.h:50
cnoid::GeneralId::Hash::result_type
size_t result_type
Definition: GeneralId.h:77
cnoid::GeneralId::operator==
bool operator==(const GeneralId &rhs) const
Definition: GeneralId.h:43
cnoid::GeneralId::toString
const std::string & toString() const
Definition: GeneralId.h:70
cnoid::GeneralId::Hash::operator()
result_type operator()(const GeneralId &key) const
Definition: GeneralId.h:78
cnoid::GeneralId::isString
bool isString() const
Definition: GeneralId.h:68
cnoid::GeneralId::operator=
GeneralId & operator=(int rhs)
Definition: GeneralId.h:31
cnoid::GeneralId::operator=
GeneralId & operator=(const GeneralId &rhs)
Definition: GeneralId.h:25
cnoid::GeneralId::GeneralId
GeneralId(const GeneralId &org)
Definition: GeneralId.h:22
cnoid::GeneralId::operator=
GeneralId & operator=(const std::string &rhs)
Definition: GeneralId.h:37