blob: 238c2be5ad7c1691a0d37b24b69be40ef3d56aa7 [file] [log] [blame]
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
5 * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
6 * @author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
7 * See COPYING for copyright and distribution information.
8 */
9
10#ifndef NDN_NAME_COMPONENT_HPP
11#define NDN_NAME_COMPONENT_HPP
12
13#include "common.hpp"
14#include "encoding/block.hpp"
15#include "encoding/encoding-buffer.hpp"
16
17namespace ndn {
18namespace name {
19
20/**
21 * A Name::Component holds a read-only name component value.
22 */
23class Component : public Block
24{
25public:
26 /// @brief Error that can be thrown from the block
27 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
28
29 /**
30 * Create a new Name::Component with a null value.
31 */
32 Component()
33 : Block(Tlv::NameComponent)
34 {
35 }
36
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080037 /**
38 * @brief Directly create component from wire block
39 *
40 * ATTENTION wire MUST BE of type Tlv::Component. Any other value would cause an exception
41 */
42 Component(const Block& wire)
43 : Block(wire)
44 {
45 if (type() != Tlv::NameComponent)
46 throw Error("Constructing name component from non name component TLV wire block");
47 }
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080048
49 /**
50 * Create a new Name::Component, taking another pointer to the Blob value.
51 * @param value A blob with a pointer to an immutable array. The pointer is copied.
52 */
53 Component(const ConstBufferPtr &buffer)
54 : Block (Tlv::NameComponent, buffer)
55 {
56 }
57
58 /**
59 * Create a new Name::Component, copying the given value.
60 * @param value The value byte array.
61 */
62 Component(const Buffer& value)
63 : Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(value)))
64 {
65 }
66
67 /**
68 * Create a new Name::Component, copying the given value.
69 * @param value Pointer to the value byte array.
70 * @param valueLen Length of value.
71 */
72 Component(const uint8_t *value, size_t valueLen)
73 : Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(value, valueLen)))
74 {
75 }
76
77 template<class InputIterator>
78 Component(InputIterator begin, InputIterator end)
79 : Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(begin, end)))
80 {
81 }
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080082
83 explicit
84 Component(const char *str)
85 : Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(str, ::strlen(str))))
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080086 {
87 }
88
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080089 explicit
90 Component(const std::string& str)
91 : Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(str.begin(), str.end())))
92 {
93 }
94
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080095 /**
96 * @brief Fast encoding or block size estimation
97 */
98 template<bool T>
99 size_t
100 wireEncode(EncodingImpl<T> &block) const;
101
102 /**
103 * Make a Blob value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme.
104 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
105 * which means the component should be skipped in a URI name.
106 * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset.
107 * @param beginOffset The offset in escapedString of the beginning of the portion to decode.
108 * @param endOffset The offset in escapedString of the end of the portion to decode.
109 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
110 */
111 static Component
112 fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset);
113
114 /**
115 * Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
116 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
117 * which means the component should be skipped in a URI name.
118 * @param escapedString The null-terminated escaped string.
119 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
120 */
121 static Component
122 fromEscapedString(const char *escapedString)
123 {
124 return fromEscapedString(escapedString, 0, ::strlen(escapedString));
125 }
126
127 /**
128 * Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
129 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
130 * which means the component should be skipped in a URI name.
131 * @param escapedString The escaped string.
132 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
133 */
134 static Component
135 fromEscapedString(const std::string& escapedString)
136 {
137 return fromEscapedString(escapedString.c_str());
138 }
139
140 /**
141 * Write the value to result, escaping characters according to the NDN URI Scheme.
142 * This also adds "..." to a value with zero or more ".".
143 * @param value the buffer with the value to escape
144 * @param result the string stream to write to.
145 */
146 void
147 toEscapedString(std::ostream& result) const;
148
149 /**
150 * Convert the value by escaping characters according to the NDN URI Scheme.
151 * This also adds "..." to a value with zero or more ".".
152 * @param value the buffer with the value to escape
153 * @return The escaped string.
154 */
155 inline std::string
156 toEscapedString() const
157 {
158 std::ostringstream result;
159 toEscapedString(result);
160 return result.str();
161 }
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800162
163 inline void
164 toUri(std::ostream& result) const
165 {
166 return toEscapedString(result);
167 }
168
169 inline std::string
170 toUri() const
171 {
172 return toEscapedString();
173 }
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800174
175 /**
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700176 * @brief Interpret this name component as nonNegativeInteger
177 *
178 * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding
179 *
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800180 * @return The integer number.
181 */
182 uint64_t
183 toNumber() const;
184
185 /**
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700186 * @brief An alias for toNumber()
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800187 */
188 uint64_t
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700189 toVersion() const;
190
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800191 /**
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700192 * @brief An alias for toNumber()
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800193 */
194 uint64_t
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700195 toSegment() const;
196
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800197 /**
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700198 * @brief Create a component encoded as nonNegativeInteger
199 *
200 * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding
201 *
202 * @param number The non-negative number
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800203 * @return The component value.
204 */
205 static Component
206 fromNumber(uint64_t number);
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800207
208 /**
209 * Check if this is the same component as other.
210 * @param other The other Component to compare with.
211 * @return true if the components are equal, otherwise false.
212 */
213 bool
214 equals(const Component& other) const
215 {
216 if (value_size() != other.value_size())
217 return false;
Alexander Afanasyev60c86812014-02-20 15:19:33 -0800218 if (value_size() == 0 /* == other.value_size()*/)
219 return true;
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800220
Alexander Afanasyev60c86812014-02-20 15:19:33 -0800221 // somehow, behavior is wrong on OSX 10.9 when component is empty
222 // (probably some bug in STL...)
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800223 return std::equal(value_begin(), value_end(), other.value_begin());
224 }
225
226 bool
227 empty() const
228 {
229 return !hasValue();
230 }
231
232 /**
233 * Check if this is the same component as other.
234 * @param other The other Component to compare with.
235 * @return true if the components are equal, otherwise false.
236 */
237 bool
238 operator == (const Component& other) const { return equals(other); }
239
240 /**
241 * Check if this is not the same component as other.
242 * @param other The other Component to compare with.
243 * @return true if the components are not equal, otherwise false.
244 */
245 bool
246 operator != (const Component& other) const { return !equals(other); }
247
248 /**
249 * Compare this to the other Component using NDN canonical ordering.
250 * @param other The other Component to compare with.
251 * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or
252 * 1 if *this comes after other in the canonical ordering.
253 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700254 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800255 */
256 int
257 compare(const Component& other) const;
258
259 /**
260 * Return true if this is less than or equal to the other Component in the NDN canonical ordering.
261 * @param other The other Component to compare with.
262 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700263 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800264 */
265 bool
266 operator <= (const Component& other) const { return compare(other) <= 0; }
267
268 /**
269 * Return true if this is less than the other Component in the NDN canonical ordering.
270 * @param other The other Component to compare with.
271 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700272 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800273 */
274 bool
275 operator < (const Component& other) const { return compare(other) < 0; }
276
277 /**
278 * Return true if this is less than or equal to the other Component in the NDN canonical ordering.
279 * @param other The other Component to compare with.
280 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700281 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800282 */
283 bool
284 operator >= (const Component& other) const { return compare(other) >= 0; }
285
286 /**
287 * Return true if this is greater than the other Component in the NDN canonical ordering.
288 * @param other The other Component to compare with.
289 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700290 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800291 */
292 bool
293 operator > (const Component& other) const { return compare(other) > 0; }
294
295 //
296 // !!! MUST NOT INCLUDE ANY DATA HERE !!!
297 //
298 // This class is just a helper and is directly reinterpret_cast'ed from Block
299};
300
301inline std::ostream &
302operator << (std::ostream &os, const Component &component)
303{
304 component.toEscapedString(os);
305 return os;
306}
307
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700308
309inline Component
310Component::fromNumber(uint64_t number)
311{
312 /// \todo Change to Tlv::NumberComponent
313 return nonNegativeIntegerBlock(Tlv::NameComponent, number);
314}
315
316
317inline uint64_t
318Component::toNumber() const
319{
320 /// \todo Check if Component is of Tlv::NumberComponent type
321 return readNonNegativeInteger(static_cast<const Block&>(*this));
322}
323
324
325inline uint64_t
326Component::toVersion() const
327{
328 return toNumber();
329}
330
331inline uint64_t
332Component::toSegment() const
333{
334 return toNumber();
335}
336
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800337template<bool T>
338inline size_t
339Component::wireEncode(EncodingImpl<T>& block) const
340{
341 size_t total_len = 0;
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800342 if (value_size() > 0)
343 total_len += block.prependByteArray (value(), value_size());
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800344 total_len += block.prependVarNumber (value_size());
345 total_len += block.prependVarNumber (Tlv::NameComponent);
346 return total_len;
347}
348
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800349} // namespace name
350} // namespace ndn
351
352#endif // NDN_NAME_COMPONENT_HPP