g2o
Public Types | Public Member Functions | Protected Attributes | List of all members
g2o::ParameterContainer Class Reference

map id to parameters More...

#include <parameter_container.h>

Inheritance diagram for g2o::ParameterContainer:
Inheritance graph
[legend]
Collaboration diagram for g2o::ParameterContainer:
Collaboration graph
[legend]

Public Types

typedef std::map< int, Parameter * > BaseClass
 

Public Member Functions

 ParameterContainer (bool isMainStorage_=true)
 
virtual ~ParameterContainer ()
 
bool addParameter (Parameter *p)
 add parameter to the container More...
 
ParametergetParameter (int id)
 return a parameter based on its ID More...
 
const ParametergetParameter (int id) const
 return a parameter based on its ID More...
 
ParameterdetachParameter (int id)
 remove a parameter from the container, i.e., the user now owns the pointer More...
 
virtual bool read (std::istream &is, const std::map< std::string, std::string > *renamedMap=0)
 read parameters from a stream More...
 
virtual bool write (std::ostream &os) const
 write the data to a stream More...
 
bool isMainStorage () const
 
void clear ()
 

Protected Attributes

bool _isMainStorage
 

Detailed Description

map id to parameters

Definition at line 41 of file parameter_container.h.

Member Typedef Documentation

Definition at line 44 of file parameter_container.h.

Constructor & Destructor Documentation

g2o::ParameterContainer::ParameterContainer ( bool  isMainStorage_ = true)

create a container for the parameters.

Parameters
isMainStorage_pointers to the parameters are owned by this container, i.e., freed in its constructor

Definition at line 42 of file parameter_container.cpp.

42  :
43  _isMainStorage(isMainStorage_)
44  {
45  }
g2o::ParameterContainer::~ParameterContainer ( )
virtual

Definition at line 56 of file parameter_container.cpp.

References clear().

56  {
57  clear();
58  }

Member Function Documentation

bool g2o::ParameterContainer::addParameter ( Parameter p)

add parameter to the container

Definition at line 60 of file parameter_container.cpp.

References g2o::Parameter::id().

Referenced by read().

60  {
61  if (p->id()<0)
62  return false;
63  iterator it=find(p->id());
64  if (it!=end())
65  return false;
66  insert(make_pair(p->id(), p));
67  return true;
68  }
void g2o::ParameterContainer::clear ( )

Definition at line 47 of file parameter_container.cpp.

References _isMainStorage.

Referenced by isMainStorage(), and ~ParameterContainer().

47  {
48  if (!_isMainStorage)
49  return;
50  for (iterator it = begin(); it!=end(); it++){
51  delete it->second;
52  }
53  BaseClass::clear();
54  }
Parameter * g2o::ParameterContainer::detachParameter ( int  id)

remove a parameter from the container, i.e., the user now owns the pointer

Definition at line 84 of file parameter_container.cpp.

84  {
85  iterator it=find(id);
86  if (it==end())
87  return 0;
88  Parameter* p=it->second;
89  erase(it);
90  return p;
91  }
Parameter * g2o::ParameterContainer::getParameter ( int  id)

return a parameter based on its ID

Definition at line 70 of file parameter_container.cpp.

70  {
71  iterator it=find(id);
72  if (it==end())
73  return 0;
74  return it->second;
75  }
const Parameter * g2o::ParameterContainer::getParameter ( int  id) const

return a parameter based on its ID

Definition at line 77 of file parameter_container.cpp.

77  {
78  const_iterator it=find(id);
79  if (it==end())
80  return 0;
81  return it->second;
82  }
bool g2o::ParameterContainer::isMainStorage ( ) const
inline

Definition at line 64 of file parameter_container.h.

References _isMainStorage, and clear().

bool g2o::ParameterContainer::read ( std::istream &  is,
const std::map< std::string, std::string > *  renamedMap = 0 
)
virtual

read parameters from a stream

Definition at line 104 of file parameter_container.cpp.

References __PRETTY_FUNCTION__, addParameter(), g2o::Factory::construct(), g2o::HyperGraph::HyperGraphElement::elementType(), HGET_PARAMETER, g2o::Factory::instance(), g2o::readLine(), and g2o::Parameter::setId().

104  {
105  stringstream currentLine;
106  string token;
107 
108  Factory* factory = Factory::instance();
109  HyperGraph::GraphElemBitset elemBitset;
110  elemBitset[HyperGraph::HGET_PARAMETER] = 1;
111 
112  while (1) {
113  int bytesRead = readLine(is, currentLine);
114  if (bytesRead == -1)
115  break;
116  currentLine >> token;
117  if (bytesRead == 0 || token.size() == 0 || token[0] == '#')
118  continue;
119  if (_renamedTypesLookup && _renamedTypesLookup->size()>0){
120  map<string, string>::const_iterator foundIt = _renamedTypesLookup->find(token);
121  if (foundIt != _renamedTypesLookup->end()) {
122  token = foundIt->second;
123  }
124  }
125 
126  HyperGraph::HyperGraphElement* element = factory->construct(token, elemBitset);
127  if (! element) // not a parameter or otherwise unknown tag
128  continue;
129  assert(element->elementType() == HyperGraph::HGET_PARAMETER && "Should be a param");
130 
131  Parameter* p = static_cast<Parameter*>(element);
132  int pid;
133  currentLine >> pid;
134  p->setId(pid);
135  bool r = p->read(currentLine);
136  if (! r) {
137  cerr << __PRETTY_FUNCTION__ << ": Error reading data " << token << " for parameter " << pid << endl;
138  delete p;
139  } else {
140  if (! addParameter(p) ){
141  cerr << __PRETTY_FUNCTION__ << ": Parameter of type:" << token << " id:" << pid << " already defined" << endl;
142  }
143  }
144  } // while read line
145 
146  return true;
147  }
#define __PRETTY_FUNCTION__
Definition: macros.h:89
static Factory * instance()
return the instance
Definition: factory.cpp:61
std::bitset< HyperGraph::HGET_NUM_ELEMS > GraphElemBitset
Definition: hyper_graph.h:74
SlamParser::Parser::token token
int readLine(std::istream &is, std::stringstream &currentLine)
bool addParameter(Parameter *p)
add parameter to the container
HGET_PARAMETER
Definition: hyper_graph.h:63
bool g2o::ParameterContainer::write ( std::ostream &  os) const
virtual

write the data to a stream

Definition at line 93 of file parameter_container.cpp.

References g2o::Factory::instance(), and g2o::Factory::tag().

93  {
94  Factory* factory = Factory::instance();
95  for (const_iterator it=begin(); it!=end(); it++){
96  os << factory->tag(it->second) << " ";
97  os << it->second->id() << " ";
98  it->second->write(os);
99  os << endl;
100  }
101  return true;
102  }
static Factory * instance()
return the instance
Definition: factory.cpp:61

Member Data Documentation

bool g2o::ParameterContainer::_isMainStorage
protected

Definition at line 71 of file parameter_container.h.

Referenced by clear(), and isMainStorage().


The documentation for this class was generated from the following files: