blob: e1b729604ede4a8c060e2b423bef7a151ee6c5f7 [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 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
9 */
10
11#ifndef NDN_NAME_COMPONENT_H
12#define NDN_NAME_COMPONENT_H
13
14#include <string>
15#include <vector>
16
17#include "blob.h"
18#include <stdint.h>
19
20NDN_NAMESPACE_BEGIN
21
22namespace name {
23
24/**
25 * @brief Class to representing binary blob of NDN name component
26 *
27 * This class is based on std::vector<char> and just provides several helpers
28 * to work with name components, as well as operator to apply canonical
29 * ordering on name components
30 */
31class Component : public Blob
32{
33public:
34 /**
35 * @brief Default constructor an empty exclude
36 */
37 Component ();
38
39 /**
40 * @brief Create component from URI encoded string
41 * @param uri URI encoded name component (convert escaped with % characters)
42 */
43 Component (const std::string &uri);
44
45 /**
46 * @brief Create component from URI encoded string, with string specified by a pair of iterators
47 * @param begin begin iterator pointing to the URI encoded name
48 * @param end end iterator
49 */
50 Component (std::string::const_iterator begin, std::string::const_iterator end);
51
52 /**
53 * @brief Create component using a binary blob
54 * @param buf pointer to first byte of binary blob to store as a name component
55 * @param length length of the binary blob
56 */
57 Component (const void *buf, size_t length);
58
59 /**
60 * @brief Apply canonical ordering on component comparison
61 * @return 0 They compare equal
62 * <0 If *this comes before other in the canonical ordering
63 * >0 If *this comes after in the canonical ordering
64 *
65 * @see http://www.ccnx.org/releases/latest/doc/technical/CanonicalOrder.html
66 */
67 int
68 compare (const Component &other) const;
69
70 /**
71 * @brief Apply canonical ordering on component comparison (less or equal)
72 *
73 * @see http://www.ccnx.org/releases/latest/doc/technical/CanonicalOrder.html
74 */
75 inline bool
76 operator <= (const Component &other) const;
77
78 /**
79 * @brief Apply canonical ordering on component comparison (less)
80 *
81 * @see http://www.ccnx.org/releases/latest/doc/technical/CanonicalOrder.html
82 */
83 inline bool
84 operator < (const Component &other) const;
85
86 /**
87 * @brief Apply canonical ordering on component comparison (greater or equal)
88 *
89 * @see http://www.ccnx.org/releases/latest/doc/technical/CanonicalOrder.html
90 */
91 inline bool
92 operator >= (const Component &other) const;
93
94 /**
95 * @brief Apply canonical ordering on component comparison (greater)
96 *
97 * @see http://www.ccnx.org/releases/latest/doc/technical/CanonicalOrder.html
98 */
99 inline bool
100 operator > (const Component &other) const;
101
102 ////////////////////////////////////
103 // Component construction helpers //
104 ////////////////////////////////////
105
106 /**
Alexander Afanasyev157c9e62013-07-16 20:58:04 -0700107 * @brief Create component from URI encoded string
108 * @param uri URI encoded name component (convert escaped with % characters)
109 * @return *this
110 */
111 Component &
112 fromUri (const std::string &uri);
113
114 /**
115 * @brief Create component from URI encoded string, with string specified by a pair of iterators
116 * @param begin begin iterator pointing to the URI encoded name
117 * @param end end iterator
118 * @return *this
119 */
120 Component &
121 fromUri (std::string::const_iterator begin, std::string::const_iterator end);
122
123 /**
Alexander Afanasyev92136012013-07-16 20:36:30 -0700124 * @brief Create network-ordered numeric component
125 *
126 * @param number number to be encoded and added as a component
Alexander Afanasyev157c9e62013-07-16 20:58:04 -0700127 * @return *this
Alexander Afanasyev92136012013-07-16 20:36:30 -0700128 *
129 * Number is encoded and added in network order. Tail zero-bytes are not included.
130 * For example, if the number is 1, then 1-byte binary blob will be added 0x01.
131 * If the number is 256, then 2 binary blob will be added: 0x01 0x01
132 *
133 * If the number is zero, an empty component will be created
134 */
Alexander Afanasyev157c9e62013-07-16 20:58:04 -0700135 Component &
Alexander Afanasyev92136012013-07-16 20:36:30 -0700136 fromNumber (uint64_t number);
137
138 /**
139 * @brief Create network-ordered numeric component to the name with marker
140 *
141 * @param number number to be encoded and added as a component
142 * @param marker byte marker, specified by the desired naming convention
143 *
144 * Currently defined naming conventions of the marker:
145 * - 0x00 sequence number
146 * - 0xC1 control number
147 * - 0xFB block id
148 * - 0xFD version number
149 *
150 * This version is almost exactly as appendNumber, with exception that it adds initial marker.
151 * The number is formatted in the exactly the same way.
152 *
153 * @see fromNumber
154 */
Alexander Afanasyev157c9e62013-07-16 20:58:04 -0700155 Component &
Alexander Afanasyev92136012013-07-16 20:36:30 -0700156 fromNumberWithMarker (uint64_t number, unsigned char marker);
157
158 //////////////////////////////////////////////////////////////////////////////////
159 // helpers
160 //////////////////////////////////////////////////////////////////////////////////
161
162 /**
163 * @brief Convert binary blob name component to std::string (no conversion is made)
164 * @param comp name component to be converted
165 * @see asUriString
166 */
167 std::string
168 toBlob () const;
169
170 /**
171 * @brief Write blob of the name component to the specified output stream
172 * @param os output stream
173 */
174 void
175 toBlob (std::ostream &os) const;
176
177 /**
178 * @brief Convert binary blob name component to std::string, escaping all non-printable characters in URI format
179 * @param comp name component to be converted
180 * @see asString
181 */
182 std::string
183 toUri () const;
184
185 /**
186 * @brief Write name as URI to the specified output stream
187 * @param os output stream
188 */
189 void
190 toUri (std::ostream &os) const;
191
192 /**
193 * @brief Convert binary blob name component (network-ordered number) to number
194 * @param comp name component to be converted
195 */
196 uint64_t
197 toNumber () const;
198
199 /**
200 * @brief Convert binary blob name component (network-ordered number) to number, using appropriate marker from the naming convention
201 * @param comp name component to be converted
202 * @param marker required marker from the naming convention
203 *
204 * If the required marker does not exist, an exception will be thrown
205 */
206 uint64_t
207 toNumberWithMarker (unsigned char marker) const;
208
209 /**
210 * @brief Convert binary blob name component, assuming sequence number naming convention (marker = 0x00)
211 * @param comp name component to be converted
212 * @see asNumberWithMarker
213 */
214 inline uint64_t
215 toSeqNum () const;
216
217 /**
218 * @brief Convert binary blob name component, assuming control number naming convention (marker = 0xC1)
219 * @param comp name component to be converted
220 * @see asNumberWithMarker
221 */
222 inline uint64_t
223 toControlNum () const;
224
225 /**
226 * @brief Convert binary blob name component, assuming block ID naming convention (marker = 0xFB)
227 * @param comp name component to be converted
228 * @see asNumberWithMarker
229 */
230 inline uint64_t
231 toBlkId () const;
232
233 /**
234 * @brief Convert binary blob name component, assuming time-stamping version naming convention (marker = 0xFD)
235 * @param comp name component to be converted
236 * @see asNumberWithMarker
237 */
238 inline uint64_t
239 toVersion () const;
240};
241
242bool
243Component::operator <= (const Component &other) const
244{
245 return (compare (other) <= 0);
246}
247
248bool
249Component::operator < (const Component &other) const
250{
251 return (compare (other) < 0);
252}
253
254bool
255Component::operator >= (const Component &other) const
256{
257 return (compare (other) >= 0);
258}
259
260bool
261Component::operator > (const Component &other) const
262{
263 return (compare (other) > 0);
264}
265
266inline uint64_t
267Component::toSeqNum () const
268{
269 return toNumberWithMarker (0x00);
270}
271
272inline uint64_t
273Component::toControlNum () const
274{
275 return toNumberWithMarker (0xC1);
276}
277
278inline uint64_t
279Component::toBlkId () const
280{
281 return toNumberWithMarker (0xFB);
282}
283
284inline uint64_t
285Component::toVersion () const
286{
287 return toNumberWithMarker (0xFD);
288}
289
290/**
291 * @brief Stream output operator (output in escaped URI format)
292 */
293std::ostream&
294operator <<(std::ostream &os, const Component &name);
295
296} // name
297
298NDN_NAMESPACE_END
299
300#endif // NDN_NAME_COMPONENT_H