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" |
| 12 | #include "encoding/BinaryXMLWireFormat.hpp" |
| 13 | |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 14 | extern "C" { struct ndn_Name; } |
| 15 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 16 | namespace ndn { |
| 17 | |
| 18 | class Name { |
| 19 | public: |
| 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 Thompson | 4238071 | 2013-06-28 10:59:33 -0700 | [diff] [blame] | 29 | void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat) { |
| 30 | wireFormat.decodeName(*this, input, inputLength); |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 31 | } |
Jeff Thompson | 4238071 | 2013-06-28 10:59:33 -0700 | [diff] [blame] | 32 | void decode(const unsigned char *input, unsigned int inputLength) { |
| 33 | decode(input, inputLength, BinaryXMLWireFormat::instance()); |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 36 | /** |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 37 | * 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 Thompson | ccb13c1 | 2013-07-01 18:16:00 -0700 | [diff] [blame] | 43 | * 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 Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 50 | * 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 Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Encode this name as a URI. |
| 73 | * @return the encoded URI. |
| 74 | */ |
| 75 | std::string to_uri(); |
| 76 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 77 | private: |
| 78 | std::vector<std::vector<unsigned char> > components_; |
| 79 | }; |
| 80 | |
| 81 | } |
| 82 | |
| 83 | #endif |
| 84 | |