SerializationHelper.cpp
Go to the documentation of this file.
1 
19 
21 
23  {
24  }
25 
27  {
28  }
29 
30  void SerializationHelper::convertVectorToString(std::vector<double>& vec, std::string& str)
31  {
32  // Concatenate the substrings containing the vector elements.
33  BOOST_FOREACH(double value, vec)
34  {
35  str += boost::lexical_cast<std::string>(value) + std::string(" ");
36  }
37 
38  // Trim trailing whitespace.
39  boost::trim(str);
40  }
41 
42  void SerializationHelper::convertStringToVector(std::string& str, std::vector<double>& vec)
43  {
44  // Erase trailing whitespaces (or a phantom value consisting of a whitespace will appear).
45  str.erase(str.find_last_not_of(" \n\r\t") + 1);
46 
47  // Split string into the substrings.
48  std::vector<std::string> valuesAsSubstrings;
49  boost::split(valuesAsSubstrings, str, boost::is_any_of(" "));
50 
51  // Copy the substrings into the vector.
52  for(unsigned int i = 0; i < valuesAsSubstrings.size(); i++)
53  {
54  vec.push_back(boost::lexical_cast<double>(valuesAsSubstrings[i]));
55  }
56  }
57 
59  {
60  // Clear string.
61  str = "";
62 
63  // Serialize the vector to string.
64  for(int i = 0; i < vec->size(); i++)
65  str += boost::lexical_cast<std::string>((*vec)(i)) + std::string(" ");
66  }
67 
69  {
70  // Erase trailing whitespaces (or a phantom value consisting of a whitespace will appear).
71  str.erase(str.find_last_not_of(" \n\r\t") + 1);
72 
73  // Split string into the substrings.
74  std::vector<std::string> valuesAsSubstrings;
75  boost::split(valuesAsSubstrings, str, boost::is_any_of(" "));
76 
77  // Check, if the number of elements is valid.
78  if(valuesAsSubstrings.size() != size)
79  throw std::invalid_argument("Error in serialization helper. Bad number of substrings.");
80 
81  // Reset boost pointer to vector.
82  vec.reset(new Eigen::VectorXd(size));
83 
84  // Unserialize the matrix from string to matrix.
85  for(unsigned int i = 0; i < size; i++)
86  (*vec)(i) = boost::lexical_cast<double>(valuesAsSubstrings[i]);
87  }
88 
90  {
91  // Clear string.
92  str = "";
93 
94  // Serialize the matrix to string.
95  for(int y = 0; y < mat->rows(); y++)
96  for(int x = 0; x < mat->cols(); x++)
97  str += boost::lexical_cast<std::string>((*mat)(y, x)) + std::string(" ");
98  }
99 
101  {
102  // Erase trailing whitespaces (or a phantom value consisting of a whitespace will appear).
103  str.erase(str.find_last_not_of(" \n\r\t") + 1);
104 
105  // Split string into the substrings.
106  std::vector<std::string> valuesAsSubstrings;
107  boost::split(valuesAsSubstrings, str, boost::is_any_of(" "));
108 
109  // Check, if the number of elements is valid.
110  if(valuesAsSubstrings.size() != pow(size, 2))
111  throw std::invalid_argument("Error in serialization helper. Bad number of substrings.");
112 
113  // Reset boost pointer to vector.
114  mat.reset(new Eigen::MatrixXd(size, size));
115 
116  // UNserialize the matrix from string to matrix.
117  for(unsigned int y = 0; y < size; y++)
118  for(unsigned int x = 0; x < size; x++)
119  (*mat)(y, x) = boost::lexical_cast<double>(valuesAsSubstrings[y * size + x]);
120  }
121 
122 }
static void convertMatrixToString(boost::shared_ptr< Eigen::MatrixXd > &mat, std::string &str)
static void convertStringToVector(std::string &str, std::vector< double > &vec)
static void convertVectorToString(std::vector< double > &vec, std::string &str)
TFSIMD_FORCE_INLINE const tfScalar & y() const
TFSIMD_FORCE_INLINE const tfScalar & x() const
static void convertStringToMatrix(unsigned int size, std::string &str, boost::shared_ptr< Eigen::MatrixXd > &mat)


asr_psm
Author(s): Braun Kai, Gehrung Joachim, Heizmann Heinrich, Meißner Pascal
autogenerated on Fri Nov 15 2019 03:57:54