g2o
Loading...
Searching...
No Matches
property.cpp
Go to the documentation of this file.
1// g2o - General Graph Optimization
2// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright notice,
10// this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#include "property.h"
28
29#include <iostream>
30#include <vector>
31
32#include "logger.h"
33#include "macros.h"
34#include "string_tools.h"
35using namespace std;
36
37namespace g2o {
38
39BaseProperty::BaseProperty(const std::string& name_) : _name(name_) {}
40
42
44 std::pair<PropertyMapIterator, bool> result = insert(make_pair(p->name(), p));
45 return result.second;
46}
47
48bool PropertyMap::eraseProperty(const std::string& name) {
49 PropertyMapIterator it = find(name);
50 if (it == end()) return false;
51 delete it->second;
52 erase(it);
53 return true;
54}
55
57 for (PropertyMapIterator it = begin(); it != end(); ++it) {
58 if (it->second) delete it->second;
59 }
60}
61
62bool PropertyMap::updatePropertyFromString(const std::string& name,
63 const std::string& value) {
64 PropertyMapIterator it = find(name);
65 if (it == end()) return false;
66 it->second->fromString(value);
67 return true;
68}
69
70void PropertyMap::writeToCSV(std::ostream& os) const {
71 for (auto it = cbegin(); it != cend(); ++it) {
72 if (it != cbegin()) os << ",";
73 os << it->second->name();
74 }
75 os << std::endl;
76 for (auto it = cbegin(); it != cend(); ++it) {
77 if (it != cbegin()) os << ",";
78 os << it->second->toString();
79 }
80 os << std::endl;
81}
82
83bool PropertyMap::updateMapFromString(const std::string& values) {
84 bool status = true;
85 vector<string> valuesMap = strSplit(values, ",");
86 for (size_t i = 0; i < valuesMap.size(); ++i) {
87 vector<string> m = strSplit(valuesMap[i], "=");
88 if (m.size() != 2) {
89 G2O_DEBUG("{}: unable to extract name=value pair from {}",
90 __PRETTY_FUNCTION__, valuesMap[i]);
91 continue;
92 }
93 string name = trim(m[0]);
94 string value = trim(m[1]);
95 status = status && updatePropertyFromString(name, value);
96 }
97 return status;
98}
99
100} // namespace g2o
BaseProperty(const std::string &name_)
Definition property.cpp:39
const std::string & name()
Definition property.h:43
virtual ~BaseProperty()
Definition property.cpp:41
bool updateMapFromString(const std::string &values)
Definition property.cpp:83
bool updatePropertyFromString(const std::string &name, const std::string &value)
Definition property.cpp:62
bool addProperty(BaseProperty *p)
Definition property.cpp:43
bool eraseProperty(const std::string &name_)
Definition property.cpp:48
void writeToCSV(std::ostream &os) const
Definition property.cpp:70
BaseClass::iterator PropertyMapIterator
Definition property.h:81
#define G2O_DEBUG(...)
Definition logger.h:90
#define __PRETTY_FUNCTION__
Definition macros.h:90
std::string trim(const std::string &s)
std::vector< std::string > strSplit(const std::string &str, const std::string &delimiters)
Definition jet.h:876