binary.h
Go to the documentation of this file.
1 #ifndef BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
2 #define BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
3 
4 #if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
5 #pragma once
6 #endif
7 
8 #include <string>
9 #include <vector>
10 
11 namespace YAML_PM
12 {
13  class Node;
14 
15  std::string EncodeBase64(const unsigned char *data, std::size_t size);
16  std::vector<unsigned char> DecodeBase64(const std::string& input);
17 
18  class Binary {
19  public:
21  Binary(const unsigned char *data, std::size_t size): m_unownedData(data), m_unownedSize(size) {}
22 
23  bool owned() const { return !m_unownedData; }
24  std::size_t size() const { return owned() ? m_data.size() : m_unownedSize; }
25  const unsigned char *data() const { return owned() ? &m_data[0] : m_unownedData; }
26 
27  void swap(std::vector<unsigned char>& rhs) {
28  if(m_unownedData) {
29  m_data.swap(rhs);
30  rhs.clear();
31  rhs.resize(m_unownedSize);
32  std::copy(m_unownedData, m_unownedData + m_unownedSize, &rhs[0]);
33  m_unownedData = 0;
34  m_unownedSize = 0;
35  } else {
36  m_data.swap(rhs);
37  }
38  }
39 
40  bool operator == (const Binary& rhs) const {
41  const std::size_t s = size();
42  if(s != rhs.size())
43  return false;
44  const unsigned char *d1 = data();
45  const unsigned char *d2 = rhs.data();
46  for(std::size_t i=0;i<s;i++) {
47  if(*d1++ != *d2++)
48  return false;
49  }
50  return true;
51  }
52 
53  bool operator != (const Binary& rhs) const {
54  return !(*this == rhs);
55  }
56 
57  private:
58  std::vector<unsigned char> m_data;
59  const unsigned char *m_unownedData;
60  std::size_t m_unownedSize;
61  };
62 
63  void operator >> (const Node& node, Binary& binary);
64 }
65 
66 #endif // BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
YAML_PM::Binary::owned
bool owned() const
Definition: binary.h:23
YAML_PM::Binary::data
const unsigned char * data() const
Definition: binary.h:25
YAML_PM::Binary::Binary
Binary(const unsigned char *data, std::size_t size)
Definition: binary.h:21
YAML_PM::DecodeBase64
std::vector< unsigned char > DecodeBase64(const std::string &input)
Definition: binary.cpp:66
YAML_PM
Definition: aliasmanager.h:11
s
XmlRpcServer s
YAML_PM::Binary
Definition: binary.h:18
YAML_PM::Binary::m_unownedSize
std::size_t m_unownedSize
Definition: binary.h:60
YAML_PM::Binary::operator!=
bool operator!=(const Binary &rhs) const
Definition: binary.h:53
testing::internal::string
::std::string string
Definition: gtest.h:1979
YAML_PM::Binary::Binary
Binary()
Definition: binary.h:20
YAML_PM::operator>>
void operator>>(const Node &node, Binary &binary)
Definition: binary.cpp:95
YAML_PM::Node
Definition: node.h:33
YAML_PM::Binary::size
std::size_t size() const
Definition: binary.h:24
YAML_PM::Binary::m_data
std::vector< unsigned char > m_data
Definition: binary.h:58
YAML_PM::Binary::m_unownedData
const unsigned char * m_unownedData
Definition: binary.h:59
YAML_PM::EncodeBase64
std::string EncodeBase64(const unsigned char *data, std::size_t size)
Definition: binary.cpp:8
YAML_PM::Binary::operator==
bool operator==(const Binary &rhs) const
Definition: binary.h:40
YAML_PM::Binary::swap
void swap(std::vector< unsigned char > &rhs)
Definition: binary.h:27


mp2p_icp
Author(s):
autogenerated on Wed Oct 23 2024 02:45:38