blob: 4b488b3a2e171b09bc1d75d1d3d1800bc590f8b0 [file] [log] [blame]
Alexander Afanasyev92136012013-07-16 20:36:30 -07001/* -*- 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 Afanasyev92136012013-07-16 20:36:30 -070019
20NDN_NAMESPACE_BEGIN
21
22/**
23 * @brief Class representing a general-use binary blob
24 */
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -070025class Blob
Alexander Afanasyev92136012013-07-16 20:36:30 -070026{
27public:
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -070028 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
43public:
Alexander Afanasyev92136012013-07-16 20:36:30 -070044 /**
45 * @brief Creates an empty blob
46 */
47 Blob ()
48 {
49 }
50
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -070051 Blob (const std::string &data)
52 : m_data (data.begin (), data.end ())
53 {
54 }
55
Alexander Afanasyev92136012013-07-16 20:36:30 -070056 Blob (const void *buf, size_t length)
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -070057 : m_data (reinterpret_cast<const char*> (buf), reinterpret_cast<const char*> (buf) + length)
Alexander Afanasyev92136012013-07-16 20:36:30 -070058 {
59 }
60
61 /**
62 * @brief Get pointer to the first byte of the binary blob
63 */
64 inline char*
65 buf ()
66 {
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -070067 return &m_data.front ();
Alexander Afanasyev92136012013-07-16 20:36:30 -070068 }
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 Afanasyevf23a7b62013-07-17 13:22:25 -070076 return &m_data.front ();
Alexander Afanasyev92136012013-07-16 20:36:30 -070077 }
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -070078
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
100private:
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
107private:
108 std::vector< char > m_data;
Alexander Afanasyev92136012013-07-16 20:36:30 -0700109};
110
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -0700111inline bool operator == (const Blob &a, const Blob &b) { return a.m_data == b.m_data; }
112inline bool operator < (const Blob &a, const Blob &b) { return a.m_data < b.m_data; }
113inline bool operator <= (const Blob &a, const Blob &b) { return a.m_data <= b.m_data; }
114inline bool operator > (const Blob &a, const Blob &b) { return a.m_data > b.m_data; }
115inline bool operator >= (const Blob &a, const Blob &b) { return a.m_data >= b.m_data; }
116
Alexander Afanasyev92136012013-07-16 20:36:30 -0700117NDN_NAMESPACE_END
118
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -0700119#include <boost/functional/hash.hpp>
120namespace boost
121{
122inline std::size_t
123hash_value (const ns3::ndn::Blob v)
124{
125 return boost::hash_range (v.begin(), v.end());
126}
127}
128
Alexander Afanasyev92136012013-07-16 20:36:30 -0700129#endif // NDN_BLOB_H