Added setVector in common.hpp
diff --git a/ndn-cpp/Interest.cpp b/ndn-cpp/Interest.cpp
index 5ddbbab..2b39b49 100644
--- a/ndn-cpp/Interest.cpp
+++ b/ndn-cpp/Interest.cpp
@@ -4,6 +4,7 @@
*/
#include <stdexcept>
+#include "common.hpp"
#include "Interest.hpp"
using namespace std;
@@ -48,10 +49,7 @@
answerOriginKind_ = interestStruct.answerOriginKind;
scope_ = interestStruct.scope;
interestLifetime_ = interestStruct.interestLifetime;
- nonce_.clear();
- if (interestStruct.nonce)
- nonce_.insert
- (nonce_.begin(), interestStruct.nonce, interestStruct.nonce + interestStruct.nonceLength);
+ setVector(nonce_, interestStruct.nonce, interestStruct.nonceLength);
}
void Interest::get(struct ndn_Interest &interestStruct) const
diff --git a/ndn-cpp/PublisherPublicKeyDigest.hpp b/ndn-cpp/PublisherPublicKeyDigest.hpp
index 95ca25d..62c22d6 100644
--- a/ndn-cpp/PublisherPublicKeyDigest.hpp
+++ b/ndn-cpp/PublisherPublicKeyDigest.hpp
@@ -7,6 +7,7 @@
#define NDN_PUBLISHERPUBLICKEYDIGEST_HPP
#include <vector>
+#include "common.hpp"
#include "c/PublisherPublicKeyDigest.h"
namespace ndn {
@@ -40,11 +41,8 @@
*/
void set(const struct ndn_PublisherPublicKeyDigest &publisherPublicKeyDigestStruct)
{
- publisherPublicKeyDigest_.clear();
- if (publisherPublicKeyDigestStruct.publisherPublicKeyDigest)
- publisherPublicKeyDigest_.insert
- (publisherPublicKeyDigest_.begin(), publisherPublicKeyDigestStruct.publisherPublicKeyDigest,
- publisherPublicKeyDigestStruct.publisherPublicKeyDigest + publisherPublicKeyDigestStruct.publisherPublicKeyDigestLength);
+ setVector(publisherPublicKeyDigest_, publisherPublicKeyDigestStruct.publisherPublicKeyDigest,
+ publisherPublicKeyDigestStruct.publisherPublicKeyDigestLength);
}
const std::vector<unsigned char> &getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
diff --git a/ndn-cpp/common.hpp b/ndn-cpp/common.hpp
index 4b146db..f763d55 100644
--- a/ndn-cpp/common.hpp
+++ b/ndn-cpp/common.hpp
@@ -6,6 +6,7 @@
#ifndef NDN_COMMON_HPP
#define NDN_COMMON_HPP
+#include <vector>
#include "../config.h"
// Depending on where ./configure found shared_ptr, define the ptr_lib namespace.
@@ -21,4 +22,21 @@
#error "Can't find shared_ptr in std or boost"
#endif
+namespace ndn {
+
+/**
+ * Clear the vector and copy valueLength bytes from value.
+ * @param v the vector to copy to
+ * @param value the array of bytes, or 0 to not copy
+ * @param valueLength the length of value
+ */
+static inline void setVector(std::vector<unsigned char> &vec, const unsigned char *value, unsigned int valueLength)
+{
+ vec.clear();
+ if (value)
+ vec.insert(vec.begin(), value, value + valueLength);
+}
+
+}
+
#endif