Dangerous SNMP
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
encoder.hpp
1 #pragma once
2 
3 #include "berstream.hpp"
4 #include "bytestream.hpp"
5 
6 #include <string>
7 #include <vector>
8 
9 #include <cstring>
10 
11 namespace dangerous { namespace snmp {
12 
20 class Encoder {
21 public:
28  template <typename EncodingClass>
29  static std::string encodeToString( typename EncodingClass::value_type& value ) {
32  ByteStream byteStream;
33  BerStream berStream( &byteStream );
34 
35  // Encode the value.
36  bool success = berStream.write<EncodingClass>( value );
37  if( ! success ) {
38  throw Exception( std::string("Could not encode ") + EncodingClass::NAME );
39  }
40 
43  std::string returnValue;
44  // Copy the encoded data from the byte stream to the string.
45  byteStream.copyTo( returnValue );
46 
47  return returnValue;
48  }
49 
56  template <typename EncodingClass>
57  static std::vector<char> encodeToVector( typename EncodingClass::value_type& value ) {
60  ByteStream byteStream;
61  BerStream berStream( &byteStream );
62 
63  // Encode the value.
64  bool success = berStream.write<EncodingClass>( value );
65  if( ! success ) {
66  throw Exception( std::string("Could not encode ") + EncodingClass::NAME );
67  }
68 
71  std::vector<char> returnValue;
72  // Copy the encoded data from the byte stream to the vector.
73  byteStream.copyTo( returnValue );
74 
75  return returnValue;
76  }
77 };
78 
79 } }
bool write(const typename EncodingClass::value_type &value)
This performs a full, logical write of a kind of encoded data.
Definition: berstream.hpp:207
A ByteStream is an object that is basically a big wrapper around a character buffer.
Definition: bytestream.hpp:30
This class is a "helper" class that takes some kind of thing and returns a standard string that repre...
Definition: encoder.hpp:20
static std::string encodeToString(typename EncodingClass::value_type &value)
This BER-encodes a value and returns a standard string of the result.
Definition: encoder.hpp:29
A BerStream represents a BER-encoded stream of data.
Definition: berstream.hpp:16
static std::vector< char > encodeToVector(typename EncodingClass::value_type &value)
This BER-encodes a value and returns a standard vector of the result.
Definition: encoder.hpp:57
This defines the base exception class for Dangerous SNMP.
Definition: exception.hpp:10
void copyTo(std::vector< char > &buffer) const
This copies the portion of the ByteStream that's currently available for reading to the specified tar...