iCub-main
dynContactList.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010-2011 RobotCub Consortium
3  * Author: Andrea Del Prete
4  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
5  *
6  */
7 
8 #include <iostream>
9 #include <sstream>
10 #include <iomanip>
11 #include <string>
12 
13 #include <yarp/os/ConnectionReader.h>
14 #include <yarp/os/ConnectionWriter.h>
16 #include <iCub/ctrl/math.h>
17 
18 using namespace std;
19 using namespace yarp::os;
20 using namespace iCub::skinDynLib;
21 
22 
23 dynContactList::dynContactList()
24 :vector<dynContact>(){}
25 
26 dynContactList::dynContactList(const size_type &n, const dynContact& value)
27 :vector<dynContact>(n, value){}
28 
29 
30 //~~~~~~~~~~~~~~~~~~~~~~~~~~
31 // SERIALIZATION methods
32 //~~~~~~~~~~~~~~~~~~~~~~~~~~
33 bool dynContactList::read(ConnectionReader& connection)
34 {
35  // A dynContactList is represented as a list of list
36  // where each list is a skinContact
37  if(connection.expectInt32()!=BOTTLE_TAG_LIST)
38  return false;
39 
40  int listLength = connection.expectInt32();
41  if(listLength<0)
42  return false;
43  if(listLength!=size())
44  resize(listLength);
45 
46  for(iterator it=begin(); it!=end(); it++)
47  if(!it->read(connection))
48  return false;
49 
50  return !connection.isError();
51 }
52 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53 bool dynContactList::write(ConnectionWriter& connection) const
54 {
55  // A dynContactList is represented as a list of list
56  // where each list is a skinContact
57  connection.appendInt32(BOTTLE_TAG_LIST);
58  connection.appendInt32(size());
59 
60  for(auto it=begin(); it!=end(); it++)
61  if(!it->write(connection))
62  return false;
63 
64  return !connection.isError();
65 }
66 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67 string dynContactList::toString(const int &precision) const{
68  stringstream ss;
69  for(const_iterator it=begin();it!=end();it++)
70  ss<<"- "<<it->toString(precision)<<";\n";
71  return ss.str();
72 }
73 
iCub::skinDynLib
Definition: common.h:35
math.h
n
int n
Definition: debugFunctions.cpp:58
iCub::skinDynLib::dynContactList::read
virtual bool read(yarp::os::ConnectionReader &connection)
Definition: dynContactList.cpp:33
iCub::skinDynLib::dynContact
Definition: dynContact.h:51
iCub::skinDynLib::dynContactList::write
virtual bool write(yarp::os::ConnectionWriter &connection) const
Write dynContactList to a connection.
Definition: dynContactList.cpp:53
iCub::skinDynLib::dynContactList::toString
virtual std::string toString(const int &precision=-1) const
Useful to print some information.
Definition: dynContactList.cpp:67
iCub::skinDynLib::dynContactList::dynContactList
dynContactList()
Definition: dynContactList.cpp:23
dynContactList.h