Implement static default WireFormat, and initialize to BinaryXmlWireFormat
diff --git a/ndn-cpp/encoding/BinaryXMLWireFormat.cpp b/ndn-cpp/encoding/BinaryXMLWireFormat.cpp
index 1c6b7b1..ffda086 100644
--- a/ndn-cpp/encoding/BinaryXMLWireFormat.cpp
+++ b/ndn-cpp/encoding/BinaryXMLWireFormat.cpp
@@ -16,8 +16,12 @@
namespace ndn {
-BinaryXmlWireFormat BinaryXmlWireFormat::instance_;
-
+// This is declared in the WireFormat class.
+WireFormat *WireFormat::newInitialDefaultWireFormat()
+{
+ return new BinaryXmlWireFormat();
+}
+
ptr_lib::shared_ptr<vector<unsigned char> > BinaryXmlWireFormat::encodeInterest(const Interest &interest)
{
struct ndn_NameComponent nameComponents[100];
diff --git a/ndn-cpp/encoding/BinaryXMLWireFormat.hpp b/ndn-cpp/encoding/BinaryXMLWireFormat.hpp
index ffc19b0..eeba0dc 100644
--- a/ndn-cpp/encoding/BinaryXMLWireFormat.hpp
+++ b/ndn-cpp/encoding/BinaryXMLWireFormat.hpp
@@ -17,11 +17,6 @@
virtual ptr_lib::shared_ptr<std::vector<unsigned char> > encodeData(const Data &data);
virtual void decodeData(Data &data, const unsigned char *input, unsigned int inputLength);
-
- static BinaryXmlWireFormat &getInstance() { return instance_; }
-
-private:
- static BinaryXmlWireFormat instance_;
};
}
diff --git a/ndn-cpp/encoding/WireFormat.cpp b/ndn-cpp/encoding/WireFormat.cpp
index 39d2e11..056b789 100644
--- a/ndn-cpp/encoding/WireFormat.cpp
+++ b/ndn-cpp/encoding/WireFormat.cpp
@@ -5,10 +5,16 @@
#include <stdexcept>
#include "WireFormat.hpp"
+#include "BinaryXMLWireFormat.hpp"
using namespace std;
namespace ndn {
+
+ptr_lib::shared_ptr<WireFormat> WireFormat::initialDefaultWireFormat_(newInitialDefaultWireFormat());
+
+WireFormat *WireFormat::defaultWireFormat_ = initialDefaultWireFormat_.get();
+
ptr_lib::shared_ptr<vector<unsigned char> > WireFormat::encodeInterest(const Interest &interest)
{
throw logic_error("unimplemented");
diff --git a/ndn-cpp/encoding/WireFormat.hpp b/ndn-cpp/encoding/WireFormat.hpp
index a7d62bc..7eb9b31 100644
--- a/ndn-cpp/encoding/WireFormat.hpp
+++ b/ndn-cpp/encoding/WireFormat.hpp
@@ -21,6 +21,42 @@
virtual ptr_lib::shared_ptr<std::vector<unsigned char> > encodeData(const Data &data);
virtual void decodeData(Data &data, const unsigned char *input, unsigned int inputLength);
+
+ /**
+ * Set the static default WireFormat used by default encoding and decoding methods.
+ * @param wireFormat A Pointer to a object of a subclass of WireFormat. This does not make a copy and
+ * the caller must ensure that the object remains allocated.
+ */
+ static void setDefaultWireFormat(WireFormat *wireFormat)
+ {
+ defaultWireFormat_ = wireFormat;
+ }
+
+ /**
+ * Return the default WireFormat used by default encoding and decoding methods which was set with
+ * setDefaultWireFormat.
+ * @return A pointer to the WireFormat object.
+ */
+ static WireFormat *getDefaultWireFormat()
+ {
+ return defaultWireFormat_;
+ }
+
+private:
+ /**
+ * This is implemented by only one of the subclasses of WireFormat to return a new object used
+ * as the initial value for the default WireFormat. If the application doesn't include that class, then the application
+ * needs to include another subclass which defines WireFormat::newInitialDefaultWireFormat.
+ * @return a new object, which is held by a shared_ptr and freed when the application exits.
+ */
+ static WireFormat *newInitialDefaultWireFormat();
+
+ /**
+ * Hold the result of newInitialDefaultWireFormat in this shared_ptr which is freed when the application exits.
+ */
+ static ptr_lib::shared_ptr<WireFormat> initialDefaultWireFormat_;
+
+ static WireFormat *defaultWireFormat_;
};
}