blob: 1b1aa2d8aeb2e1d0ca64281c021cfa718a4b3f70 [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/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070023 * @ingroup ndn-cxx
Alexander Afanasyev92136012013-07-16 20:36:30 -070024 * @brief Class representing a general-use binary blob
25 */
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -070026class Blob
Alexander Afanasyev92136012013-07-16 20:36:30 -070027{
28public:
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -070029 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
44public:
Alexander Afanasyev92136012013-07-16 20:36:30 -070045 /**
46 * @brief Creates an empty blob
47 */
48 Blob ()
49 {
50 }
51
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -070052 Blob (const std::string &data)
53 : m_data (data.begin (), data.end ())
54 {
55 }
56
Alexander Afanasyev92136012013-07-16 20:36:30 -070057 Blob (const void *buf, size_t length)
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -070058 : m_data (reinterpret_cast<const char*> (buf), reinterpret_cast<const char*> (buf) + length)
Alexander Afanasyev92136012013-07-16 20:36:30 -070059 {
60 }
61
62 /**
63 * @brief Get pointer to the first byte of the binary blob
64 */
65 inline char*
66 buf ()
67 {
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -070068 return &m_data.front ();
Alexander Afanasyev92136012013-07-16 20:36:30 -070069 }
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 Afanasyevf23a7b62013-07-17 13:22:25 -070077 return &m_data.front ();
Alexander Afanasyev92136012013-07-16 20:36:30 -070078 }
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -070079
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
101private:
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
108private:
109 std::vector< char > m_data;
Alexander Afanasyev92136012013-07-16 20:36:30 -0700110};
111
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -0700112inline 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; }
116inline bool operator >= (const Blob &a, const Blob &b) { return a.m_data >= b.m_data; }
117
Alexander Afanasyev92136012013-07-16 20:36:30 -0700118NDN_NAMESPACE_END
119
Alexander Afanasyevf23a7b62013-07-17 13:22:25 -0700120#include <boost/functional/hash.hpp>
121namespace boost
122{
123inline std::size_t
124hash_value (const ns3::ndn::Blob v)
125{
126 return boost::hash_range (v.begin(), v.end());
127}
128}
129
Alexander Afanasyev92136012013-07-16 20:36:30 -0700130#endif // NDN_BLOB_H