blob: 48dd70641e236992d8029ba84c2a22af087296f4 [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
Jeff Thompson2a749d12013-07-02 15:03:08 -070047 void encode(std::vector<unsigned char> &output, WireFormat &wireFormat)
48 {
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070049 wireFormat.encodeName(*this, output);
50 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070051 void encode(std::vector<unsigned char> &output)
52 {
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070053 encode(output, BinaryXMLWireFormat::instance());
54 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070055 void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat)
56 {
Jeff Thompson42380712013-06-28 10:59:33 -070057 wireFormat.decodeName(*this, input, inputLength);
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070058 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070059 void decode(const unsigned char *input, unsigned int inputLength)
60 {
Jeff Thompson42380712013-06-28 10:59:33 -070061 decode(input, inputLength, BinaryXMLWireFormat::instance());
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070062 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070063 void decode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
64 {
65 decode(&input[0], input.size(), wireFormat);
66 }
67 void decode(const std::vector<unsigned char> &input)
68 {
69 decode(&input[0], input.size());
70 }
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070071
Jeff Thompsone5f839b2013-06-28 12:50:38 -070072 /**
Jeff Thompson016ed642013-07-02 14:39:06 -070073 * Set the nameStruct to point to the components in this name, without copying any memory.
74 * WARNING: The resulting pointers in nameStruct are invalid after a further use of this object which could reallocate memory.
75 * @param nameStruct a C ndn_Name struct where the components array is already allocated.
76 */
77 void get(struct ndn_Name &nameStruct);
78
79 /**
Jeff Thompsonb468c312013-07-01 17:50:14 -070080 * Clear the name, and set the components by copying from the name struct.
81 * @param name a C ndn_Name struct
82 */
83 void set(struct ndn_Name &nameStruct);
84
85 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -070086 * Add a new component, copying from value of length valueLength.
87 */
88 void addComponent(unsigned char *value, unsigned int valueLength) {
Jeff Thompson016ed642013-07-02 14:39:06 -070089 components_.push_back(NameComponent(value, valueLength));
Jeff Thompsone5f839b2013-06-28 12:50:38 -070090 }
91
92 /**
93 * Clear all the components.
94 */
95 void clear() {
96 components_.clear();
97 }
98
99 /**
100 * Get the number of components.
101 * @return the number of components
102 */
103 unsigned int getComponentCount() {
104 return components_.size();
105 }
106
Jeff Thompsone6063512013-07-01 15:11:28 -0700107 /**
108 * Encode this name as a URI.
109 * @return the encoded URI.
110 */
111 std::string to_uri();
112
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700113private:
Jeff Thompson016ed642013-07-02 14:39:06 -0700114 std::vector<NameComponent> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700115};
116
117}
118
119#endif
120