blob: 802aaae63afc52f38268466a5a50f4fcd47006aa [file] [log] [blame]
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07001/*
2 * Author: Jeff Thompson
3 *
4 * BSD license, See the LICENSE file for more information.
5 */
6
7#ifndef NDN_NAME_HPP
8#define NDN_NAME_HPP
9
10#include <vector>
11#include "common.h"
12#include "encoding/BinaryXMLWireFormat.hpp"
13
Jeff Thompsonb468c312013-07-01 17:50:14 -070014extern "C" { struct ndn_Name; }
15
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070016namespace ndn {
17
18class Name {
19public:
20 Name();
21 Name(const char *uri);
22
23 void encode(std::vector<unsigned char> &output, WireFormat &wireFormat) {
24 wireFormat.encodeName(*this, output);
25 }
26 void encode(std::vector<unsigned char> &output) {
27 encode(output, BinaryXMLWireFormat::instance());
28 }
Jeff Thompson42380712013-06-28 10:59:33 -070029 void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat) {
30 wireFormat.decodeName(*this, input, inputLength);
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070031 }
Jeff Thompson42380712013-06-28 10:59:33 -070032 void decode(const unsigned char *input, unsigned int inputLength) {
33 decode(input, inputLength, BinaryXMLWireFormat::instance());
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070034 }
35
Jeff Thompsone5f839b2013-06-28 12:50:38 -070036 /**
Jeff Thompsonb468c312013-07-01 17:50:14 -070037 * Clear the name, and set the components by copying from the name struct.
38 * @param name a C ndn_Name struct
39 */
40 void set(struct ndn_Name &nameStruct);
41
42 /**
Jeff Thompsonccb13c12013-07-01 18:16:00 -070043 * Set the nameStruct to point to the components in this name, without copying any memory.
44 * WARNING: The resulting pointers in nameStruct are invalid after a further use of this name which could reallocate the components memory.
45 * @param nameStruct a C ndn_Name struct where the components array is already allocated.
46 */
47 void get(struct ndn_Name &nameStruct);
48
49 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -070050 * Add a new component, copying from value of length valueLength.
51 */
52 void addComponent(unsigned char *value, unsigned int valueLength) {
53 components_.push_back(std::vector<unsigned char>(value, value + valueLength));
54 }
55
56 /**
57 * Clear all the components.
58 */
59 void clear() {
60 components_.clear();
61 }
62
63 /**
64 * Get the number of components.
65 * @return the number of components
66 */
67 unsigned int getComponentCount() {
68 return components_.size();
69 }
70
Jeff Thompsone6063512013-07-01 15:11:28 -070071 /**
72 * Encode this name as a URI.
73 * @return the encoded URI.
74 */
75 std::string to_uri();
76
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070077private:
78 std::vector<std::vector<unsigned char> > components_;
79};
80
81}
82
83#endif
84