Added NameComponent.
diff --git a/ndn-cpp/Name.cpp b/ndn-cpp/Name.cpp
index c777ebe..3b98fa7 100644
--- a/ndn-cpp/Name.cpp
+++ b/ndn-cpp/Name.cpp
@@ -5,15 +5,20 @@
*/
#include <sstream>
-#include "c/Name.h"
#include "Name.hpp"
using namespace std;
namespace ndn {
-Name::Name()
+void Name::get(struct ndn_Name &nameStruct)
{
+ if (nameStruct.maxComponents < components_.size())
+ throw runtime_error("nameStruct.maxComponents must be >= this name getNComponents()");
+
+ nameStruct.nComponents = components_.size();
+ for (unsigned int i = 0; i < nameStruct.nComponents; ++i)
+ components_[i].get(nameStruct.components[i]);
}
void Name::set(struct ndn_Name &nameStruct)
@@ -23,26 +28,14 @@
addComponent(nameStruct.components[i].value, nameStruct.components[i].valueLength);
}
-void Name::get(struct ndn_Name &nameStruct)
-{
- if (nameStruct.maxComponents < components_.size())
- throw runtime_error("nameStruct.maxComponents must be >= this name getNComponents()");
-
- nameStruct.nComponents = components_.size();
- for (unsigned int i = 0; i < nameStruct.nComponents; ++i) {
- nameStruct.components[i].value = &components_[i][0];
- nameStruct.components[i].valueLength = components_[i].size();
- }
-}
-
std::string Name::to_uri()
{
// TODO: implement fully.
ostringstream output;
for (unsigned int i = 0; i < components_.size(); ++i) {
output << "/";
- for (unsigned int j = 0; j < components_[i].size(); ++j)
- output << components_[i][j];
+ for (unsigned int j = 0; j < components_[i].getValue().size(); ++j)
+ output << components_[i].getValue()[j];
}
return output.str();
diff --git a/ndn-cpp/Name.hpp b/ndn-cpp/Name.hpp
index 802aaae..3ec6511 100644
--- a/ndn-cpp/Name.hpp
+++ b/ndn-cpp/Name.hpp
@@ -9,15 +9,39 @@
#include <vector>
#include "common.h"
+#include "c/Name.h"
#include "encoding/BinaryXMLWireFormat.hpp"
-extern "C" { struct ndn_Name; }
-
namespace ndn {
+class NameComponent {
+public:
+ NameComponent(unsigned char * value, unsigned int valueLen)
+ : value_(value, value + valueLen)
+ {
+ }
+
+ /**
+ * Set the componentStruct to point to this component, without copying any memory.
+ * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory.
+ * @param componentStruct the C ndn_NameComponent struct to receive the pointer.
+ */
+ void get(struct ndn_NameComponent &componentStruct)
+ {
+ componentStruct.value = &value_[0];
+ componentStruct.valueLength = value_.size();
+ }
+
+ const std::vector<unsigned char> &getValue() const { return value_; }
+
+private:
+ std::vector<unsigned char> value_;
+};
+
class Name {
public:
- Name();
+ Name() {
+ }
Name(const char *uri);
void encode(std::vector<unsigned char> &output, WireFormat &wireFormat) {
@@ -34,23 +58,23 @@
}
/**
+ * Set the nameStruct to point to the components in this name, without copying any memory.
+ * WARNING: The resulting pointers in nameStruct are invalid after a further use of this object which could reallocate memory.
+ * @param nameStruct a C ndn_Name struct where the components array is already allocated.
+ */
+ void get(struct ndn_Name &nameStruct);
+
+ /**
* Clear the name, and set the components by copying from the name struct.
* @param name a C ndn_Name struct
*/
void set(struct ndn_Name &nameStruct);
/**
- * Set the nameStruct to point to the components in this name, without copying any memory.
- * WARNING: The resulting pointers in nameStruct are invalid after a further use of this name which could reallocate the components memory.
- * @param nameStruct a C ndn_Name struct where the components array is already allocated.
- */
- void get(struct ndn_Name &nameStruct);
-
- /**
* Add a new component, copying from value of length valueLength.
*/
void addComponent(unsigned char *value, unsigned int valueLength) {
- components_.push_back(std::vector<unsigned char>(value, value + valueLength));
+ components_.push_back(NameComponent(value, valueLength));
}
/**
@@ -75,7 +99,7 @@
std::string to_uri();
private:
- std::vector<std::vector<unsigned char> > components_;
+ std::vector<NameComponent> components_;
};
}