external libraries: yaml-cpp version bump
[supercollider.git] / external_libraries / yaml-cpp-0.3.0 / include / yaml-cpp / binary.h
blob8504ebebe8d2c8e79d9012f04f2eb9be693e8651
1 #ifndef BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66
2 #define BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66
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
8 #include <string>
9 #include <vector>
11 namespace YAML
13 class Node;
15 std::string EncodeBase64(const unsigned char *data, std::size_t size);
16 std::vector<unsigned char> DecodeBase64(const std::string& input);
18 class Binary {
19 public:
20 Binary(): m_unownedData(0), m_unownedSize(0) {}
21 Binary(const unsigned char *data, std::size_t size): m_unownedData(data), m_unownedSize(size) {}
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; }
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);
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;
50 return true;
53 bool operator != (const Binary& rhs) const {
54 return !(*this == rhs);
57 private:
58 std::vector<unsigned char> m_data;
59 const unsigned char *m_unownedData;
60 std::size_t m_unownedSize;
63 void operator >> (const Node& node, Binary& binary);
66 #endif // BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66