blob: 3ec65112f0754cd465f735bdb76bfe7639454050 [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"
Jeff Thompson016ed642013-07-02 14:39:06 -070012#include "c/Name.h"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070013#include "encoding/BinaryXMLWireFormat.hpp"
14
15namespace ndn {
16
Jeff Thompson016ed642013-07-02 14:39:06 -070017class NameComponent {
18public:
19 NameComponent(unsigned char * value, unsigned int valueLen)
20 : value_(value, value + valueLen)
21 {
22 }
23
24 /**
25 * Set the componentStruct to point to this component, without copying any memory.
26 * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory.
27 * @param componentStruct the C ndn_NameComponent struct to receive the pointer.
28 */
29 void get(struct ndn_NameComponent &componentStruct)
30 {
31 componentStruct.value = &value_[0];
32 componentStruct.valueLength = value_.size();
33 }
34
35 const std::vector<unsigned char> &getValue() const { return value_; }
36
37private:
38 std::vector<unsigned char> value_;
39};
40
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070041class Name {
42public:
Jeff Thompson016ed642013-07-02 14:39:06 -070043 Name() {
44 }
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070045 Name(const char *uri);
46
47 void encode(std::vector<unsigned char> &output, WireFormat &wireFormat) {
48 wireFormat.encodeName(*this, output);
49 }
50 void encode(std::vector<unsigned char> &output) {
51 encode(output, BinaryXMLWireFormat::instance());
52 }
Jeff Thompson42380712013-06-28 10:59:33 -070053 void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat) {
54 wireFormat.decodeName(*this, input, inputLength);
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070055 }
Jeff Thompson42380712013-06-28 10:59:33 -070056 void decode(const unsigned char *input, unsigned int inputLength) {
57 decode(input, inputLength, BinaryXMLWireFormat::instance());
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070058 }
59
Jeff Thompsone5f839b2013-06-28 12:50:38 -070060 /**
Jeff Thompson016ed642013-07-02 14:39:06 -070061 * Set the nameStruct to point to the components in this name, without copying any memory.
62 * WARNING: The resulting pointers in nameStruct are invalid after a further use of this object which could reallocate memory.
63 * @param nameStruct a C ndn_Name struct where the components array is already allocated.
64 */
65 void get(struct ndn_Name &nameStruct);
66
67 /**
Jeff Thompsonb468c312013-07-01 17:50:14 -070068 * Clear the name, and set the components by copying from the name struct.
69 * @param name a C ndn_Name struct
70 */
71 void set(struct ndn_Name &nameStruct);
72
73 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -070074 * Add a new component, copying from value of length valueLength.
75 */
76 void addComponent(unsigned char *value, unsigned int valueLength) {
Jeff Thompson016ed642013-07-02 14:39:06 -070077 components_.push_back(NameComponent(value, valueLength));
Jeff Thompsone5f839b2013-06-28 12:50:38 -070078 }
79
80 /**
81 * Clear all the components.
82 */
83 void clear() {
84 components_.clear();
85 }
86
87 /**
88 * Get the number of components.
89 * @return the number of components
90 */
91 unsigned int getComponentCount() {
92 return components_.size();
93 }
94
Jeff Thompsone6063512013-07-01 15:11:28 -070095 /**
96 * Encode this name as a URI.
97 * @return the encoded URI.
98 */
99 std::string to_uri();
100
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700101private:
Jeff Thompson016ed642013-07-02 14:39:06 -0700102 std::vector<NameComponent> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700103};
104
105}
106
107#endif
108