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 | /** |
| 23 | * @brief Class representing a general-use binary blob |
| 24 | */ |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 25 | class Blob |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 26 | { |
| 27 | public: |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 28 | typedef std::vector<char> base; |
| 29 | |
| 30 | typedef base::value_type value_type; |
| 31 | typedef base::pointer pointer; |
| 32 | typedef base::const_pointer const_pointer; |
| 33 | typedef base::reference reference; |
| 34 | typedef base::const_reference const_reference; |
| 35 | typedef base::iterator iterator; |
| 36 | typedef base::const_iterator const_iterator; |
| 37 | typedef base::const_reverse_iterator const_reverse_iterator; |
| 38 | typedef base::reverse_iterator reverse_iterator; |
| 39 | typedef base::size_type size_type; |
| 40 | typedef base::difference_type difference_type; |
| 41 | typedef base::allocator_type allocator_type; |
| 42 | |
| 43 | public: |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 44 | /** |
| 45 | * @brief Creates an empty blob |
| 46 | */ |
| 47 | Blob () |
| 48 | { |
| 49 | } |
| 50 | |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 51 | Blob (const std::string &data) |
| 52 | : m_data (data.begin (), data.end ()) |
| 53 | { |
| 54 | } |
| 55 | |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 56 | Blob (const void *buf, size_t length) |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 57 | : 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] | 58 | { |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @brief Get pointer to the first byte of the binary blob |
| 63 | */ |
| 64 | inline char* |
| 65 | buf () |
| 66 | { |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 67 | return &m_data.front (); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @brief Get const pointer to the first byte of the binary blob |
| 72 | */ |
| 73 | inline const char* |
| 74 | buf () const |
| 75 | { |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 76 | return &m_data.front (); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 77 | } |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 78 | |
| 79 | iterator begin () { return m_data.begin (); } |
| 80 | const_iterator begin () const { return m_data.begin (); } |
| 81 | iterator end () { return m_data.end (); } |
| 82 | const_iterator end () const { return m_data.end (); } |
| 83 | size_t size () const { return m_data.size (); } |
| 84 | |
| 85 | void swap (Blob &x) { m_data.swap (x.m_data); } |
| 86 | void push_back (value_type val) { m_data.push_back (val); } |
| 87 | |
| 88 | bool empty () const { return m_data.empty (); } |
| 89 | |
| 90 | Blob & |
| 91 | operator = (const Blob &other) { m_data = other.m_data; return *this; } |
| 92 | |
| 93 | reference operator [] (size_type pos) { return m_data [pos]; } |
| 94 | const_reference operator [] (size_type pos) const { return m_data [pos]; } |
| 95 | |
| 96 | char getItem (size_type pos) const { return m_data [pos]; } |
| 97 | |
| 98 | void clear () { m_data.clear (); } |
| 99 | |
| 100 | private: |
| 101 | friend bool operator == (const Blob &a, const Blob &b); |
| 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 | |
| 107 | private: |
| 108 | std::vector< char > m_data; |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 111 | inline bool operator == (const Blob &a, const Blob &b) { return a.m_data == b.m_data; } |
| 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 | |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 117 | NDN_NAMESPACE_END |
| 118 | |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 119 | #include <boost/functional/hash.hpp> |
| 120 | namespace boost |
| 121 | { |
| 122 | inline std::size_t |
| 123 | hash_value (const ns3::ndn::Blob v) |
| 124 | { |
| 125 | return boost::hash_range (v.begin(), v.end()); |
| 126 | } |
| 127 | } |
| 128 | |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 129 | #endif // NDN_BLOB_H |