Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 12 | #include "c/Name.h" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 13 | #include "encoding/BinaryXMLWireFormat.hpp" |
| 14 | |
| 15 | namespace ndn { |
| 16 | |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 17 | class NameComponent { |
| 18 | public: |
| 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 | |
| 37 | private: |
| 38 | std::vector<unsigned char> value_; |
| 39 | }; |
| 40 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 41 | class Name { |
| 42 | public: |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 43 | Name() { |
| 44 | } |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 45 | Name(const char *uri); |
| 46 | |
Jeff Thompson | 2a749d1 | 2013-07-02 15:03:08 -0700 | [diff] [blame] | 47 | void encode(std::vector<unsigned char> &output, WireFormat &wireFormat) |
| 48 | { |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 49 | wireFormat.encodeName(*this, output); |
| 50 | } |
Jeff Thompson | 2a749d1 | 2013-07-02 15:03:08 -0700 | [diff] [blame] | 51 | void encode(std::vector<unsigned char> &output) |
| 52 | { |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 53 | encode(output, BinaryXMLWireFormat::instance()); |
| 54 | } |
Jeff Thompson | 2a749d1 | 2013-07-02 15:03:08 -0700 | [diff] [blame] | 55 | void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat) |
| 56 | { |
Jeff Thompson | 4238071 | 2013-06-28 10:59:33 -0700 | [diff] [blame] | 57 | wireFormat.decodeName(*this, input, inputLength); |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 58 | } |
Jeff Thompson | 2a749d1 | 2013-07-02 15:03:08 -0700 | [diff] [blame] | 59 | void decode(const unsigned char *input, unsigned int inputLength) |
| 60 | { |
Jeff Thompson | 4238071 | 2013-06-28 10:59:33 -0700 | [diff] [blame] | 61 | decode(input, inputLength, BinaryXMLWireFormat::instance()); |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 62 | } |
Jeff Thompson | 2a749d1 | 2013-07-02 15:03:08 -0700 | [diff] [blame] | 63 | 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 Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 71 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 72 | /** |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 73 | * 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 Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 80 | * 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 Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 86 | * Add a new component, copying from value of length valueLength. |
| 87 | */ |
| 88 | void addComponent(unsigned char *value, unsigned int valueLength) { |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 89 | components_.push_back(NameComponent(value, valueLength)); |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 90 | } |
| 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 Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Encode this name as a URI. |
| 109 | * @return the encoded URI. |
| 110 | */ |
| 111 | std::string to_uri(); |
| 112 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 113 | private: |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 114 | std::vector<NameComponent> components_; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | #endif |
| 120 | |