RSC  0.12.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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, 2011 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  for (Properties::const_iterator it = other.begin();
44  it != other.end(); ++it) {
45  Properties::const_iterator it_ = this->find(it->first);
46  if (it_ == this->end())
47  return false;
48 
49  boost::any myValue = it_->second;
50  boost::any otherValue = it->second;
51 
52  if (myValue.type() != otherValue.type())
53  return false;
54 
55  if (myValue.type() == typeid(std::string)) {
56  if (!anyEqual<std::string>(myValue, otherValue))
57  return false;
58  } else if (myValue.type() == typeid(bool)) {
59  if (!anyEqual<bool>(myValue, otherValue))
60  return false;
61  } else if (myValue.type() == typeid(int)) {
62  if (!anyEqual<int>(myValue, otherValue))
63  return false;
64  } else if (myValue.type() == typeid(unsigned int)) {
65  if (!anyEqual<unsigned int>(myValue, otherValue))
66  return false;
67  } else if (myValue.type() == typeid(double)) {
68  if (!anyEqual<double>(myValue, otherValue))
69  return false;
70  } else {
71  throw std::runtime_error(boost::str(boost::format("Cannot compare values of type %1%")
72  % typeName(myValue.type())));
73  }
74  }
75  return true;
76 }
77 
80  for (Properties::const_iterator it = other.begin();
81  it != other.end(); ++it) {
82  (*this)[it->first] = it->second;
83  }
84 
85  return *this;
86 }
87 
88 bool
89 Properties::has(const std::string& name) const throw () {
90  return find(name) != end();
91 }
92 
93 // Free functions.
94 
95 Properties operator<<(const Properties& Properties1,
96  const Properties& Properties2) {
97  Properties result;
98  for (Properties::const_iterator it = Properties1.begin();
99  it != Properties1.end(); ++it) {
100  result[it->first] = it->second;
101  }
102  for (Properties::const_iterator it = Properties2.begin();
103  it != Properties2.end(); ++it) {
104  result[it->first] = it->second;
105  }
106  return result;
107 }
108 
109 } }
Properties & operator<<=(const Properties &other)
Merge with other.
Definition: Properties.cpp:79
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
Properties objects are basically glorified map<string, boost::any> objects.
Definition: Properties.h:57
ostream & operator<<(ostream &stream, const Printable &record)
Output operator on std::ostream for reference Printables.
Definition: Printable.cpp:56
bool operator==(const Properties &other) const
Compare to other.
Definition: Properties.cpp:39
bool has(const std::string &name) const
Definition: Properties.cpp:89