RSC  0.17.1
Properties.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project
4  *
5  * Copyright (C) 2010-2016 Jan Moringen
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "Properties.h"
28 
29 namespace rsc {
30 namespace runtime {
31 
32 // Properties implementation
33 
34 template <typename T>
35 bool anyEqual(boost::any lhs, boost::any rhs) {
36  return boost::any_cast<T>(lhs) == boost::any_cast<T>(rhs);
37 }
38 
39 bool Properties::operator==(const Properties& other) const {
40  if (this->size() != other.size()) {
41  return false;
42  }
43 
44  for (Properties::const_iterator it = other.begin();
45  it != other.end(); ++it) {
46  Properties::const_iterator it_ = this->find(it->first);
47  if (it_ == this->end()) {
48  return false;
49  }
50 
51  boost::any myValue = it_->second;
52  boost::any otherValue = it->second;
53 
54  if (myValue.type() != otherValue.type()) {
55  return false;
56  }
57 
58  if (myValue.type() == typeid(std::string)) {
59  if (!anyEqual<std::string>(myValue, otherValue))
60  return false;
61  } else if (myValue.type() == typeid(bool)) {
62  if (!anyEqual<bool>(myValue, otherValue))
63  return false;
64  } else if (myValue.type() == typeid(int)) {
65  if (!anyEqual<int>(myValue, otherValue))
66  return false;
67  } else if (myValue.type() == typeid(unsigned int)) {
68  if (!anyEqual<unsigned int>(myValue, otherValue))
69  return false;
70  } else if (myValue.type() == typeid(double)) {
71  if (!anyEqual<double>(myValue, otherValue))
72  return false;
73  } else {
74  throw std::runtime_error(boost::str(boost::format("Cannot compare values of type %1%")
75  % typeName(myValue.type())));
76  }
77  }
78  return true;
79 }
80 
83  for (Properties::const_iterator it = other.begin();
84  it != other.end(); ++it) {
85  (*this)[it->first] = it->second;
86  }
87 
88  return *this;
89 }
90 
91 bool
92 Properties::has(const std::string& name) const throw () {
93  return find(name) != end();
94 }
95 
96 // Free functions.
97 
98 Properties operator<<(const Properties& Properties1,
99  const Properties& Properties2) {
100  Properties result;
101  for (Properties::const_iterator it = Properties1.begin();
102  it != Properties1.end(); ++it) {
103  result[it->first] = it->second;
104  }
105  for (Properties::const_iterator it = Properties2.begin();
106  it != Properties2.end(); ++it) {
107  result[it->first] = it->second;
108  }
109  return result;
110 }
111 
112 } }
Properties & operator<<=(const Properties &other)
Merge with other.
Definition: Properties.cpp:82
std::string typeName(const std::type_info &type)
Returns a (demangled) string representation of type.
bool anyEqual(boost::any lhs, boost::any rhs)
Definition: Properties.cpp:35
friend RSC_EXPORT Properties operator<<(const Properties &properties1, const Properties &properties2)
Merge left and right.
Definition: Properties.cpp:98
Properties objects are basically glorified map<string, boost::any> objects.
Definition: Properties.h:57
bool operator==(const Properties &other) const
Compare to other.
Definition: Properties.cpp:39
bool has(const std::string &name) const
Definition: Properties.cpp:92