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