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 | |
| 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 Thompson | 4238071 | 2013-06-28 10:59:33 -0700 | [diff] [blame] | 53 | void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat) { |
| 54 | wireFormat.decodeName(*this, input, inputLength); |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 55 | } |
Jeff Thompson | 4238071 | 2013-06-28 10:59:33 -0700 | [diff] [blame] | 56 | void decode(const unsigned char *input, unsigned int inputLength) { |
| 57 | decode(input, inputLength, BinaryXMLWireFormat::instance()); |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 60 | /** |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame^] | 61 | * 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 Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 68 | * 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 Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 74 | * Add a new component, copying from value of length valueLength. |
| 75 | */ |
| 76 | void addComponent(unsigned char *value, unsigned int valueLength) { |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame^] | 77 | components_.push_back(NameComponent(value, valueLength)); |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 78 | } |
| 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 Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Encode this name as a URI. |
| 97 | * @return the encoded URI. |
| 98 | */ |
| 99 | std::string to_uri(); |
| 100 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 101 | private: |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame^] | 102 | std::vector<NameComponent> components_; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } |
| 106 | |
| 107 | #endif |
| 108 | |