fipa_acl  1.4
byte_sequence.h
Go to the documentation of this file.
1 #ifndef __FIPAACL_MESSAGEPARSER_BYTESEQUENCE_H_
2 #define __FIPAACL_MESSAGEPARSER_BYTESEQUENCE_H_
3 
4 #include <stdio.h>
5 #include <string>
6 #include <vector>
7 
8 #include <boost/cstdint.hpp>
9 
10 namespace fipa { namespace acl {
11 
15 typedef boost::variant<boost::uint_least8_t,boost::uint_least16_t, boost::uint_least32_t> LengthValue;
16 
20 typedef boost::variant<std::string, std::vector<unsigned char> > ByteString;
21 
26 class ByteStringPrinter : public boost::static_visitor<std::string>
27 {
28 
30  std::string encoding;
31 public:
36  ByteStringPrinter(std::string enc) : encoding(enc) {}
37 
42  std::string operator()(const std::string& s) const
43  {
44  return s;
45  }
46 
53  std::string operator()(const std::vector<unsigned char>& vector) const
54  {
55  size_t length = vector.size();
56  std::string tmp("HEX(");
57  tmp += encoding;
58  tmp +=")[";
59 
60  for(size_t i = 0; i < length; i++)
61  {
62  char currentChar[6];
63  sprintf(currentChar, "%02x", vector[i]);
64  tmp += std::string(currentChar);
65  tmp += " ";
66  }
67 
68  tmp += "]";
69 
70  return tmp;
71  }
72 };
73 
74 
79 class ByteStringRawPrinter : public boost::static_visitor<std::string>
80 {
81  std::string* output;
82 public:
86  ByteStringRawPrinter(std::string* _output) : output(_output) {}
87 
91  ByteStringRawPrinter() : output(0) {}
92 
97  std::string operator()(const std::string& s) const
98  {
99  if(output)
100  *output = s;
101 
102  return s;
103  }
104 
111  std::string operator()(const std::vector<unsigned char>& vector) const
112  {
113  if(output)
114  {
115  output->assign(vector.begin(), vector.end());
116  return *output;
117  } else {
118  std::string rawData(vector.begin(), vector.end());
119  return rawData;
120  }
121  }
122 };
123 
131 {
132  std::string encoding;
135 
141  std::string toPrettyString() const
142  {
143  std::string tmp;
144  // Since bytes is a variant we apply the visitor pattern here
145  tmp += boost::apply_visitor( ByteStringPrinter(encoding), bytes);
146  return tmp;
147  }
148 
149  std::string toRawDataString() const
150  {
151  std::string tmp;
152  // Since bytes is a variant we apply the visitor pattern here
153  tmp += boost::apply_visitor( ByteStringRawPrinter(), bytes);
154  return tmp;
155  }
156 
157 
163  void toRawDataString(std::string* output) const
164  {
165  // Since bytes is a variant we apply the visitor pattern here
166  boost::apply_visitor( ByteStringRawPrinter(output), bytes);
167  }
168 };
169 
170 
171 
172 }} // namespace fipa::acl
173 
174 #endif
std::string operator()(const std::vector< unsigned char > &vector) const
fipa::acl::ByteString bytes
std::string toPrettyString() const
Representation of a byte sequence. We also embed information about the encoding.
boost::variant< std::string, std::vector< unsigned char > > ByteString
Definition of a ByteString.
Definition: byte_sequence.h:20
Printer using the visitor pattern for the ByteString type.
Definition: byte_sequence.h:26
std::string operator()(const std::string &s) const
Definition: byte_sequence.h:97
ByteStringRawPrinter(std::string *_output)
Definition: byte_sequence.h:86
std::string toRawDataString() const
std::string operator()(const std::string &s) const
Definition: byte_sequence.h:42
ByteStringPrinter(std::string enc)
Definition: byte_sequence.h:36
void toRawDataString(std::string *output) const
boost::variant< boost::uint_least8_t, boost::uint_least16_t, boost::uint_least32_t > LengthValue
Definition of a length - internally used by the parser.
Definition: byte_sequence.h:15
fipa::acl::LengthValue length
Printer using the visitor pattern for the ByteString type.
Definition: byte_sequence.h:79
Foundation of Physical Intelligent Agents.
Definition: conversation.cpp:6
std::string operator()(const std::vector< unsigned char > &vector) const
Definition: byte_sequence.h:53