NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
blob.h
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>
19 
20 NDN_NAMESPACE_BEGIN
21 
26 class Blob
27 {
28 public:
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:
48  Blob ()
49  {
50  }
51 
52  Blob (const std::string &data)
53  : m_data (data.begin (), data.end ())
54  {
55  }
56 
57  Blob (const void *buf, size_t length)
58  : m_data (reinterpret_cast<const char*> (buf), reinterpret_cast<const char*> (buf) + length)
59  {
60  }
61 
65  inline char*
66  buf ()
67  {
68  return &m_data.front ();
69  }
70 
74  inline const char*
75  buf () const
76  {
77  return &m_data.front ();
78  }
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;
110 };
111 
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 
118 NDN_NAMESPACE_END
119 
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 
130 #endif // NDN_BLOB_H
Class representing a general-use binary blob.
Definition: blob.h:26
Blob()
Creates an empty blob.
Definition: blob.h:48
char * buf()
Get pointer to the first byte of the binary blob.
Definition: blob.h:66
const char * buf() const
Get const pointer to the first byte of the binary blob.
Definition: blob.h:75
boost::error_info< struct tag_pos, int > pos
Report of the position of the error (error-specific meaning)
Definition: error.h:105