Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * Zhenkai Zhu |
| 6 | * |
| 7 | * BSD license, See the LICENSE file for more information |
| 8 | * |
| 9 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 10 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 11 | */ |
| 12 | |
| 13 | #ifndef NDN_BLOB_H |
| 14 | #define NDN_BLOB_H |
| 15 | |
| 16 | #include "ns3/ndn-common.h" |
| 17 | |
| 18 | #include <vector> |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 19 | |
| 20 | NDN_NAMESPACE_BEGIN |
| 21 | |
| 22 | /** |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 23 | * @ingroup ndn-cxx |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 24 | * @brief Class representing a general-use binary blob |
| 25 | */ |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 26 | class Blob |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 27 | { |
| 28 | public: |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 29 | typedef std::vector<char> base; |
| 30 | |
| 31 | typedef base::value_type value_type; |
| 32 | typedef base::pointer pointer; |
| 33 | typedef base::const_pointer const_pointer; |
| 34 | typedef base::reference reference; |
| 35 | typedef base::const_reference const_reference; |
| 36 | typedef base::iterator iterator; |
| 37 | typedef base::const_iterator const_iterator; |
| 38 | typedef base::const_reverse_iterator const_reverse_iterator; |
| 39 | typedef base::reverse_iterator reverse_iterator; |
| 40 | typedef base::size_type size_type; |
| 41 | typedef base::difference_type difference_type; |
| 42 | typedef base::allocator_type allocator_type; |
| 43 | |
| 44 | public: |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 45 | /** |
| 46 | * @brief Creates an empty blob |
| 47 | */ |
| 48 | Blob () |
| 49 | { |
| 50 | } |
| 51 | |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 52 | Blob (const std::string &data) |
| 53 | : m_data (data.begin (), data.end ()) |
| 54 | { |
| 55 | } |
| 56 | |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 57 | Blob (const void *buf, size_t length) |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 58 | : m_data (reinterpret_cast<const char*> (buf), reinterpret_cast<const char*> (buf) + length) |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @brief Get pointer to the first byte of the binary blob |
| 64 | */ |
| 65 | inline char* |
| 66 | buf () |
| 67 | { |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 68 | return &m_data.front (); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @brief Get const pointer to the first byte of the binary blob |
| 73 | */ |
| 74 | inline const char* |
| 75 | buf () const |
| 76 | { |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 77 | return &m_data.front (); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 78 | } |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 79 | |
| 80 | iterator begin () { return m_data.begin (); } |
| 81 | const_iterator begin () const { return m_data.begin (); } |
| 82 | iterator end () { return m_data.end (); } |
| 83 | const_iterator end () const { return m_data.end (); } |
| 84 | size_t size () const { return m_data.size (); } |
| 85 | |
| 86 | void swap (Blob &x) { m_data.swap (x.m_data); } |
| 87 | void push_back (value_type val) { m_data.push_back (val); } |
| 88 | |
| 89 | bool empty () const { return m_data.empty (); } |
| 90 | |
| 91 | Blob & |
| 92 | operator = (const Blob &other) { m_data = other.m_data; return *this; } |
| 93 | |
| 94 | reference operator [] (size_type pos) { return m_data [pos]; } |
| 95 | const_reference operator [] (size_type pos) const { return m_data [pos]; } |
| 96 | |
| 97 | char getItem (size_type pos) const { return m_data [pos]; } |
| 98 | |
| 99 | void clear () { m_data.clear (); } |
| 100 | |
| 101 | private: |
| 102 | friend bool operator == (const Blob &a, const Blob &b); |
| 103 | friend bool operator < (const Blob &a, const Blob &b); |
| 104 | friend bool operator <= (const Blob &a, const Blob &b); |
| 105 | friend bool operator > (const Blob &a, const Blob &b); |
| 106 | friend bool operator >= (const Blob &a, const Blob &b); |
| 107 | |
| 108 | private: |
| 109 | std::vector< char > m_data; |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 112 | inline bool operator == (const Blob &a, const Blob &b) { return a.m_data == b.m_data; } |
| 113 | inline bool operator < (const Blob &a, const Blob &b) { return a.m_data < b.m_data; } |
| 114 | inline bool operator <= (const Blob &a, const Blob &b) { return a.m_data <= b.m_data; } |
| 115 | inline bool operator > (const Blob &a, const Blob &b) { return a.m_data > b.m_data; } |
| 116 | inline bool operator >= (const Blob &a, const Blob &b) { return a.m_data >= b.m_data; } |
| 117 | |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 118 | NDN_NAMESPACE_END |
| 119 | |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 120 | #include <boost/functional/hash.hpp> |
| 121 | namespace boost |
| 122 | { |
| 123 | inline std::size_t |
| 124 | hash_value (const ns3::ndn::Blob v) |
| 125 | { |
| 126 | return boost::hash_range (v.begin(), v.end()); |
| 127 | } |
| 128 | } |
| 129 | |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 130 | #endif // NDN_BLOB_H |