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