From 8cbb036bddf59db82a3b0035dd872901fe7ade3e Mon Sep 17 00:00:00 2001 From: Junhao Li Date: Wed, 2 May 2018 23:30:26 -0400 Subject: [PATCH] default to use serialize and parse from custom class and override << and >> (#31) --- .gitmodules | 3 + Makefile | 4 +- README.md | 61 +++++++---------- benchmark.mk | 4 +- ci.sh | 8 --- example/basic.cc | 4 +- example/custom_type.cc | 30 ++++----- example/file_stream.cc | 4 +- googletest | 1 + src/basic_type/float_serializer.h | 4 +- src/basic_type/float_serializer_test.cc | 37 ++++++----- src/basic_type/int_serializer.h | 4 +- src/basic_type/int_serializer_test.cc | 65 ++++++++++--------- src/basic_type/string_serializer.h | 4 +- src/basic_type/string_serializer_test.cc | 17 ++--- src/basic_type/uint_serializer.h | 4 +- src/basic_type/uint_serializer_test.cc | 41 ++++++------ src/benchmark/double_array_test.cc | 4 +- src/benchmark/map_test.cc | 4 +- src/benchmark/sparse_matrix_test.cc | 4 +- src/buffer/char_array_input_buffer.h | 13 ++-- src/buffer/input_buffer.h | 11 ---- src/buffer/output_buffer.h | 11 ---- src/buffer/stream.h | 7 -- src/buffer/stream_input_buffer.h | 14 ++-- src/buffer/stream_output_buffer.h | 14 ++-- src/buffer/string_input_buffer.h | 13 ++-- src/buffer/string_output_buffer.h | 13 ++-- src/container/array_serializer.h | 12 ++-- src/container/array_serializer_test.cc | 25 +++---- src/container/deque_serializer.h | 4 +- src/container/deque_serializer_test.cc | 17 ++--- src/container/list_serializer.h | 4 +- src/container/list_serializer_test.cc | 17 ++--- src/container/map_serializer.h | 4 +- src/container/map_serializer_test.cc | 17 ++--- src/container/pair_serializer.h | 4 +- src/container/pair_serializer_test.cc | 9 +-- src/container/set_serializer.h | 4 +- src/container/set_serializer_test.cc | 17 ++--- src/container/unique_ptr_serializer.h | 4 +- src/container/unique_ptr_serializer_test.cc | 17 ++--- src/container/unordered_map_serializer.h | 4 +- .../unordered_map_serializer_test.cc | 17 ++--- src/container/unordered_set_serializer.h | 4 +- .../unordered_set_serializer_test.cc | 17 ++--- src/container/vector_serializer.h | 16 ++--- src/container/vector_serializer_test.cc | 65 ++++++++++--------- src/hps.h | 47 +++++++------- src/hps_test.cc | 40 ++++++------ src/serializer.h | 11 +--- 51 files changed, 380 insertions(+), 399 deletions(-) create mode 100644 .gitmodules create mode 160000 googletest delete mode 100644 src/buffer/input_buffer.h delete mode 100644 src/buffer/output_buffer.h delete mode 100644 src/buffer/stream.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8cf8b5e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "googletest"] + path = googletest + url = https://github.com/google/googletest.git diff --git a/Makefile b/Makefile index f2e9f5b..ea2643b 100644 --- a/Makefile +++ b/Makefile @@ -20,8 +20,8 @@ OBJS := $(SRCS:$(SRC_DIR)/%.cc=$(BUILD_DIR)/%.o) TEST_OBJS := $(TESTS:$(SRC_DIR)/%.cc=$(BUILD_DIR)/%.o) # Test related. -GTEST_DIR := gtest/googletest -GMOCK_DIR := gtest/googlemock +GTEST_DIR := googletest/googletest +GMOCK_DIR := googletest/googlemock GTEST_ALL_SRC := ${GTEST_DIR}/src/gtest-all.cc GMOCK_ALL_SRC := ${GMOCK_DIR}/src/gmock-all.cc TEST_MAIN_SRC := ${GMOCK_DIR}/src/gmock_main.cc diff --git a/README.md b/README.md index 4380138..a2af4e7 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,8 @@ For primitive types and most STL containers, serialization requires only one lin int main() { std::vector data({22, 333, -4444}); - std::string serialized = hps::serialize_to_string(data); - auto parsed = hps::parse_from_string>(serialized); + std::string serialized = hps::to_string(data); + auto parsed = hps::from_string>(serialized); assert(parsed == data); @@ -52,19 +52,21 @@ Then we can send the string over the network in MPI for example ```c++ MPI_Send(serialized.c_str(), serialized.size(), MPI_CHAR, 0, 0, MPI_COMM_WORLD); ``` -There are also the `serialize_to_stream` and `parse_from_stream` functions for writing the data to or reading it from file streams. +There are also the `to_stream` and `from_stream` functions for writing the data to or reading it from file streams. For example ```c++ std::ofstream out_file("data.log", std::ofstream::binary); -hps::serialize_to_stream(data, out_file); +hps::to_stream(data, out_file); std::ifstream in_file("data.log", std::ifstream::binary); -auto parsed = hps::parse_from_stream>(in_file); +auto parsed = hps::from_stream>(in_file); ``` The bottom of this document contains all the APIs that HPS provides. We can also extend HPS to support custom types. HPS internally uses static polymorphism on the class `Serializer` to support different types. -All we need to do is to specialize the `Serializer` for the new type, and HPS will support it, together with any combination of this type with STL containers and other specialized types. +`Serializer` will call the `serialize` and `parse` method of the corresponding type by default. +All we need to do is provide the `serialize` and `parse` method or specialize the `Serializer` for the new type, and HPS will support it, together with any combination of this type with STL containers and other specialized types. +We recommend providing the `serialize` and `parse` method whenever you have control over the structure of the new type. The following example shows the serialization of a typical quantum system object consisting of several electrons, some of which are excited from orbitals `orbs_from` to `orbs_to`. @@ -75,34 +77,21 @@ The following example shows the serialization of a typical quantum system object class QuantumState { public: - uint16_t n_elecs; - std::unordered_set orbs_from; - std::unordered_set orbs_to; + unsigned n_elecs; + std::unordered_set orbs_from; + std::unordered_set orbs_to; template - void serialize(hps::OutputBuffer& buf) const { - hps::Serializer::serialize(n_elecs, buf); - hps::Serializer, B>::serialize(orbs_from, buf); - hps::Serializer, B>::serialize(orbs_to, buf); + void serialize(B& buf) const { + buf << n_elecs << orbs_from << orbs_to; } template - void parse(hps::InputBuffer& buf) { - hps::Serializer::parse(n_elecs, buf); - hps::Serializer, B>::parse(orbs_from, buf); - hps::Serializer, B>::parse(orbs_to, buf); + void parse(B& buf) { + buf >> n_elecs >> orbs_from >> orbs_to; } }; -namespace hps { -template -class Serializer { - public: - static void serialize(const QuantumState& qs, OutputBuffer& buf) { qs.serialize(buf); } - static void parse(QuantumState& qs, InputBuffer& buf) { qs.parse(buf); } -}; -} // namespace hps - int main() { QuantumState qs; @@ -110,7 +99,7 @@ int main() { qs.orbs_from.insert({11, 22}); qs.orbs_to.insert({44, 66}); - std::string serialized = hps::serialize_to_string(qs); + std::string serialized = hps::to_string(qs); std::cout << "size (B): " << serialized.size() << std::endl; // size (B): 7 @@ -165,43 +154,43 @@ Note: fixed cost includes the estimated amount of lines of commands needed for a ```c++ // Serialize data t to an STL ostream. -void serialize_to_stream(const T& t, std::ostream& stream); +void to_stream(const T& t, std::ostream& stream); ``` ```c++ // Parse from an STL istream and save to the data t passed in. // Recommended for repeated use inside a loop. -void parse_from_stream(T& t, std::istream& stream); +void from_stream(T& t, std::istream& stream); ``` ```c++ // Parse from an STL istream and return the data. -T parse_from_stream(std::istream& stream); +T from_stream(std::istream& stream); ``` ```c++ // Serialize data t to the STL string passed in. // Recommended for repeated use inside a loop. -void serialize_to_string(const T& t, std::string& str); +void to_string(const T& t, std::string& str); ``` ```c++ // Serialize data t to an STL string and return it. -std::string serialize_to_string(const T& t); +std::string to_string(const T& t); ``` ```c++ // Parse from an STL string and save to the data t passed in. // Recommended for repeated use inside a loop. -void parse_from_string(T& t, const std::string& str); +void from_string(T& t, const std::string& str); ``` ```c++ // Parse from an STL string and return the data. -T parse_from_string(const std::string& str); +T from_string(const std::string& str); ``` ```c++ // Parse from a char array and save to the data t passed in. // Recommended for repeated use inside a loop. -void parse_from_char_array(T& t, const char* arr); +void from_char_array(T& t, const char* arr); ``` ```c++ // Parse from a char array and return the data. -T parse_from_char_array(const char* arr); +T from_char_array(const char* arr); ``` HPS supports the following types and any combinations of them out of the box: diff --git a/benchmark.mk b/benchmark.mk index 4c69c2b..a6a1f91 100644 --- a/benchmark.mk +++ b/benchmark.mk @@ -36,8 +36,8 @@ OBJS := $(SRCS:$(SRC_DIR)/%.cc=$(BUILD_DIR)/%.o) TEST_OBJS := $(TESTS:$(SRC_DIR)/%.cc=$(BUILD_DIR)/%.o) # Test related. -GTEST_DIR := gtest/googletest -GMOCK_DIR := gtest/googlemock +GTEST_DIR := googletest/googletest +GMOCK_DIR := googletest/googlemock GTEST_ALL_SRC := ${GTEST_DIR}/src/gtest-all.cc GMOCK_ALL_SRC := ${GMOCK_DIR}/src/gmock-all.cc TEST_MAIN_SRC := ${GMOCK_DIR}/src/gmock_main.cc diff --git a/ci.sh b/ci.sh index 94b44f0..46314be 100755 --- a/ci.sh +++ b/ci.sh @@ -4,14 +4,6 @@ set -x export TOOLS_DIR=$TRAVIS_BUILD_DIR/tools echo $TOOLS_DIR -# Install Google Test. -echo "Downloading Google Test" -wget -O release-1.8.0.tar.gz https://github.com/google/googletest/archive/release-1.8.0.tar.gz -tar xzf release-1.8.0.tar.gz -rm release-1.8.0.tar.gz -mv googletest-release-1.8.0 gtest -echo "Completed" - # Install or Load Protocol Buffers. if [ -f "$TOOLS_DIR/protobuf/bin/protoc" ]; then echo "Found cached Protocol Buffers" diff --git a/example/basic.cc b/example/basic.cc index ef7dd88..0b27cd4 100644 --- a/example/basic.cc +++ b/example/basic.cc @@ -5,8 +5,8 @@ int main() { std::vector data({22, 333, -4444}); - std::string serialized = hps::serialize_to_string(data); - auto parsed = hps::parse_from_string>(serialized); + std::string serialized = hps::to_string(data); + auto parsed = hps::from_string>(serialized); assert(parsed == data); diff --git a/example/custom_type.cc b/example/custom_type.cc index fbb8be0..80b9af9 100644 --- a/example/custom_type.cc +++ b/example/custom_type.cc @@ -1,30 +1,24 @@ #include #include +#include #include "../src/hps.h" class QuantumState { public: - uint16_t n_elecs; - std::unordered_set orbs_from; - std::unordered_set orbs_to; -}; + unsigned n_elecs; + std::unordered_set orbs_from; + std::unordered_set orbs_to; -namespace hps { -template -class Serializer { - public: - static void serialize(const QuantumState& qs, OutputBuffer& ob) { - Serializer::serialize(qs.n_elecs, ob); - Serializer, B>::serialize(qs.orbs_from, ob); - Serializer, B>::serialize(qs.orbs_to, ob); + template + void serialize(B& buf) const { + buf << n_elecs << orbs_from << orbs_to; } - static void parse(QuantumState& qs, InputBuffer& ib) { - Serializer::parse(qs.n_elecs, ib); - Serializer, B>::parse(qs.orbs_from, ib); - Serializer, B>::parse(qs.orbs_to, ib); + + template + void parse(B& buf) { + buf >> n_elecs >> orbs_from >> orbs_to; } }; -} // namespace hps int main() { QuantumState qs; @@ -33,7 +27,7 @@ int main() { qs.orbs_from.insert({11, 22}); qs.orbs_to.insert({44, 66}); - std::string serialized = hps::serialize_to_string(qs); + std::string serialized = hps::to_string(qs); std::cout << "size (B): " << serialized.size() << std::endl; // size (B): 7 diff --git a/example/file_stream.cc b/example/file_stream.cc index d50a86d..1dc878b 100644 --- a/example/file_stream.cc +++ b/example/file_stream.cc @@ -9,13 +9,13 @@ int main() { std::ofstream out_file("data.log", std::ofstream::binary); std::ifstream in_file("data.log", std::ifstream::binary); - hps::serialize_to_stream(data, out_file); + hps::to_stream(data, out_file); out_file.close(); in_file.seekg(0, in_file.end); size_t serialized_size = in_file.tellg(); in_file.seekg(0, in_file.beg); - auto parsed = hps::parse_from_stream>(in_file); + auto parsed = hps::from_stream>(in_file); assert(parsed == data); diff --git a/googletest b/googletest new file mode 160000 index 0000000..ec44c6c --- /dev/null +++ b/googletest @@ -0,0 +1 @@ +Subproject commit ec44c6c1675c25b9827aacd08c02433cccde7780 diff --git a/src/basic_type/float_serializer.h b/src/basic_type/float_serializer.h index 390873c..11b3a07 100644 --- a/src/basic_type/float_serializer.h +++ b/src/basic_type/float_serializer.h @@ -9,12 +9,12 @@ namespace hps { template class Serializer::value, void>::type> { public: - static void serialize(const T& num, OutputBuffer& ob) { + static void serialize(const T& num, B& ob) { const char* num_ptr = reinterpret_cast(&num); ob.write(num_ptr, sizeof(num)); } - static void parse(T& num, InputBuffer& ib) { + static void parse(T& num, B& ib) { char* num_ptr = reinterpret_cast(&num); ib.read(num_ptr, sizeof(num)); } diff --git a/src/basic_type/float_serializer_test.cc b/src/basic_type/float_serializer_test.cc index 46a8458..b268e96 100644 --- a/src/basic_type/float_serializer_test.cc +++ b/src/basic_type/float_serializer_test.cc @@ -2,43 +2,44 @@ #include #include #include +#include "../buffer/buffer.h" TEST(FloatSerializerTest, Zero) { const double input = 0; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); double output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(FloatSerializerTest, Float) { const float input = 3.3; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); float output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(FloatSerializerTest, Double) { const double input = 3.3; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); double output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } @@ -46,16 +47,16 @@ TEST(FloatSerializerTest, TwoDoubles) { const double input1 = 3.2; const double input2 = -4.4; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input1, ob); - hps::Serializer::serialize(input2, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input1, ob); + hps::Serializer::serialize(input2, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); double output1; double output2; - hps::Serializer::parse(output1, ib); - hps::Serializer::parse(output2, ib); + hps::Serializer::parse(output1, ib); + hps::Serializer::parse(output2, ib); EXPECT_EQ(input1, output1); EXPECT_EQ(input2, output2); } diff --git a/src/basic_type/int_serializer.h b/src/basic_type/int_serializer.h index cb8f11c..eb101de 100644 --- a/src/basic_type/int_serializer.h +++ b/src/basic_type/int_serializer.h @@ -14,14 +14,14 @@ class Serializer< B, typename std::enable_if::value && std::is_integral::value, void>::type> { public: - static void serialize(const T& num, OutputBuffer& ob) { + static void serialize(const T& num, B& ob) { const size_t n_bits = sizeof(num) * 8; using UT = typename std::make_unsigned::type; UT zigzaged_num = (num << 1) ^ (num >> (n_bits - 1)); Serializer::serialize(zigzaged_num, ob); } - static void parse(T& num, InputBuffer& ib) { + static void parse(T& num, B& ib) { using UT = typename std::make_unsigned::type; UT zigzaged_num; Serializer::parse(zigzaged_num, ib); diff --git a/src/basic_type/int_serializer_test.cc b/src/basic_type/int_serializer_test.cc index 4388929..b78bb78 100644 --- a/src/basic_type/int_serializer_test.cc +++ b/src/basic_type/int_serializer_test.cc @@ -2,107 +2,108 @@ #include #include #include +#include "../buffer/buffer.h" TEST(IntSerializerTest, Zero) { const int input = 0; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); int output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(IntSerializerTest, SmallPositiveInt) { const int input = 22; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); int output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(IntSerializerTest, SmallNegativeInt) { const int input = -33; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); int output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(IntSerializerTest, MaxInt) { const int input = std::numeric_limits::max(); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); int output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(IntSerializerTest, MinInt) { const int input = std::numeric_limits::min(); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); int output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(IntSerializerTest, MaxLongLong) { const long long input = std::numeric_limits::max(); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); long long output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(IntSerializerTest, MinLongLong) { const long long input = std::numeric_limits::min(); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); long long output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(IntSerializerTest, LargeNegativeLongLong) { const long long input = -7363025924956902506ll; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); long long output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } diff --git a/src/basic_type/string_serializer.h b/src/basic_type/string_serializer.h index 3d49b1a..e07ec3c 100644 --- a/src/basic_type/string_serializer.h +++ b/src/basic_type/string_serializer.h @@ -10,13 +10,13 @@ namespace hps { template class Serializer { public: - static void serialize(const std::string& str, OutputBuffer& ob) { + static void serialize(const std::string& str, B& ob) { const size_t n_bytes = str.size(); Serializer::serialize(n_bytes, ob); ob.write(str.data(), n_bytes); } - static void parse(std::string& str, InputBuffer& ib) { + static void parse(std::string& str, B& ib) { size_t n_bytes; Serializer::parse(n_bytes, ib); str.resize(n_bytes); diff --git a/src/basic_type/string_serializer_test.cc b/src/basic_type/string_serializer_test.cc index e6c15d6..3cededa 100644 --- a/src/basic_type/string_serializer_test.cc +++ b/src/basic_type/string_serializer_test.cc @@ -1,4 +1,5 @@ #include "string_serializer.h" +#include "../buffer/buffer.h" #include #include #include @@ -6,25 +7,25 @@ TEST(StringSerializerTest, ShortString) { const std::string input = "test"; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::string output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(StringSerializerTest, EmptyString) { const std::string input = ""; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::string output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } diff --git a/src/basic_type/uint_serializer.h b/src/basic_type/uint_serializer.h index 25b80e4..9348186 100644 --- a/src/basic_type/uint_serializer.h +++ b/src/basic_type/uint_serializer.h @@ -12,7 +12,7 @@ namespace hps { template class Serializer::value, void>::type> { public: - static void serialize(const T& num, OutputBuffer& ob) { + static void serialize(const T& num, B& ob) { if (num == 0) { ob.write_char(0); return; @@ -30,7 +30,7 @@ class Serializer::value, void> } } - static void parse(T& num, InputBuffer& ib) { + static void parse(T& num, B& ib) { num = 0; size_t n_shifts = 0; char ch; diff --git a/src/basic_type/uint_serializer_test.cc b/src/basic_type/uint_serializer_test.cc index 92d5f1f..a4a0139 100644 --- a/src/basic_type/uint_serializer_test.cc +++ b/src/basic_type/uint_serializer_test.cc @@ -2,59 +2,60 @@ #include #include #include +#include "../buffer/buffer.h" TEST(UintSerializerTest, Zero) { const unsigned int input = 0; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); unsigned int output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(UintSerializerTest, SmallUint) { const unsigned int input = 22; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); unsigned int output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(UintSerializerTest, LargeUint) { const unsigned int input = 3333; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); unsigned int output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } TEST(UintSerializerTest, FewUints) { std::stringstream ss; - hps::OutputBuffer ob(ss); + hps::StreamOutputBuffer ob(ss); const size_t N = 10; for (size_t i = 0; i < N; i++) { - hps::Serializer::serialize(i, ob); + hps::Serializer::serialize(i, ob); } ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); for (size_t i = 0; i < N; i++) { unsigned int output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(i, output); } } @@ -62,12 +63,12 @@ TEST(UintSerializerTest, FewUints) { TEST(UintSerializerTest, MaxUint) { const unsigned long long input = std::numeric_limits::max(); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); unsigned long long output; - hps::Serializer::parse(output, ib); + hps::Serializer::parse(output, ib); EXPECT_EQ(input, output); } diff --git a/src/benchmark/double_array_test.cc b/src/benchmark/double_array_test.cc index 4c945d4..3fe3700 100644 --- a/src/benchmark/double_array_test.cc +++ b/src/benchmark/double_array_test.cc @@ -139,8 +139,8 @@ TEST(DoubleArrayBenchmarkLargeTest, HPS) { auto constructed_time = high_resolution_clock::now(); // Serialize and parse. - const std::string serialized = hps::serialize_to_string(origin); - auto parsed = hps::parse_from_string>(serialized); + const std::string serialized = hps::to_string(origin); + auto parsed = hps::from_string>(serialized); auto finish_time = high_resolution_clock::now(); diff --git a/src/benchmark/map_test.cc b/src/benchmark/map_test.cc index 0032eaf..617f83f 100644 --- a/src/benchmark/map_test.cc +++ b/src/benchmark/map_test.cc @@ -102,8 +102,8 @@ TEST(MapBenchmarkLargeTest, HPS) { auto constructed_time = high_resolution_clock::now(); // Serialize and parse. - const std::string serialized = hps::serialize_to_string(origin); - auto parsed = hps::parse_from_string>(serialized); + const std::string serialized = hps::to_string(origin); + auto parsed = hps::from_string>(serialized); auto finish_time = high_resolution_clock::now(); diff --git a/src/benchmark/sparse_matrix_test.cc b/src/benchmark/sparse_matrix_test.cc index 355a7de..0ebbdf0 100644 --- a/src/benchmark/sparse_matrix_test.cc +++ b/src/benchmark/sparse_matrix_test.cc @@ -174,8 +174,8 @@ TEST(SparseMatrixBenchmarkLargeTest, HPS) { // Serialize and parse. std::string serialized; - hps::serialize_to_string(origin, serialized); - auto parsed = hps::parse_from_string(serialized); + hps::to_string(origin, serialized); + auto parsed = hps::from_string(serialized); auto finish_time = high_resolution_clock::now(); diff --git a/src/buffer/char_array_input_buffer.h b/src/buffer/char_array_input_buffer.h index 45fc609..1dbaceb 100644 --- a/src/buffer/char_array_input_buffer.h +++ b/src/buffer/char_array_input_buffer.h @@ -1,14 +1,13 @@ #pragma once #include -#include "input_buffer.h" +#include "../serializer.h" namespace hps { -template <> -class InputBuffer { +class CharArrayInputBuffer { public: - InputBuffer(const char* arr) : arr(arr) { pos = 0; } + CharArrayInputBuffer(const char* arr) : arr(arr) { pos = 0; } void read(char* content, size_t length) { strncpy(content, &arr[pos], length); @@ -21,6 +20,12 @@ class InputBuffer { return ch; } + template + CharArrayInputBuffer& operator>>(T& t) { + Serializer::parse(t, *this); + return *this; + } + private: const char* const arr; diff --git a/src/buffer/input_buffer.h b/src/buffer/input_buffer.h deleted file mode 100644 index af63c83..0000000 --- a/src/buffer/input_buffer.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -namespace hps { - -template -class InputBuffer { - InputBuffer() = delete; - InputBuffer(const InputBuffer&) = delete; -}; - -} // namespace hps diff --git a/src/buffer/output_buffer.h b/src/buffer/output_buffer.h deleted file mode 100644 index fd4515c..0000000 --- a/src/buffer/output_buffer.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -namespace hps { - -template -class OutputBuffer { - OutputBuffer() = delete; - OutputBuffer(const OutputBuffer&) = delete; -}; - -} // namespace hps diff --git a/src/buffer/stream.h b/src/buffer/stream.h deleted file mode 100644 index 2dd8c10..0000000 --- a/src/buffer/stream.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -namespace hps { - -class Stream {}; - -} // namespace hps diff --git a/src/buffer/stream_input_buffer.h b/src/buffer/stream_input_buffer.h index e08b96a..4a59a57 100644 --- a/src/buffer/stream_input_buffer.h +++ b/src/buffer/stream_input_buffer.h @@ -2,17 +2,15 @@ #include #include -#include "input_buffer.h" -#include "stream.h" +#include "../serializer.h" namespace hps { constexpr size_t STREAM_INPUT_BUFFER_SIZE = 1 << 16; -template <> -class InputBuffer { +class StreamInputBuffer { public: - InputBuffer(std::istream& stream) : stream(&stream) { + StreamInputBuffer(std::istream& stream) : stream(&stream) { stream.seekg(0, stream.beg); load(); } @@ -42,6 +40,12 @@ class InputBuffer { return ch; } + template + StreamInputBuffer& operator>>(T& t) { + Serializer::parse(t, *this); + return *this; + } + private: std::istream* const stream; diff --git a/src/buffer/stream_output_buffer.h b/src/buffer/stream_output_buffer.h index 9a8f53a..287250c 100644 --- a/src/buffer/stream_output_buffer.h +++ b/src/buffer/stream_output_buffer.h @@ -2,17 +2,15 @@ #include #include -#include "output_buffer.h" -#include "stream.h" +#include "../serializer.h" namespace hps { constexpr size_t STREAM_OUTPUT_BUFFER_SIZE = 1 << 16; -template <> -class OutputBuffer { +class StreamOutputBuffer { public: - OutputBuffer(std::ostream& stream) : stream(&stream) { pos = 0; } + StreamOutputBuffer(std::ostream& stream) : stream(&stream) { pos = 0; } void write(const char* content, size_t length) { if (pos + length > STREAM_OUTPUT_BUFFER_SIZE) { @@ -42,6 +40,12 @@ class OutputBuffer { pos = 0; } + template + StreamOutputBuffer& operator<<(const T& t) { + Serializer::serialize(t, *this); + return *this; + } + private: std::ostream* const stream; diff --git a/src/buffer/string_input_buffer.h b/src/buffer/string_input_buffer.h index f9820a5..9158303 100644 --- a/src/buffer/string_input_buffer.h +++ b/src/buffer/string_input_buffer.h @@ -1,14 +1,13 @@ #pragma once #include -#include "input_buffer.h" +#include "../serializer.h" namespace hps { -template <> -class InputBuffer { +class StringInputBuffer { public: - InputBuffer(const std::string& str) : str(&str) { pos = 0; } + StringInputBuffer(const std::string& str) : str(&str) { pos = 0; } void read(char* content, size_t length) { str->copy(content, length, pos); @@ -21,6 +20,12 @@ class InputBuffer { return ch; } + template + StringInputBuffer& operator>>(T& t) { + Serializer::parse(t, *this); + return *this; + } + private: const std::string* const str; diff --git a/src/buffer/string_output_buffer.h b/src/buffer/string_output_buffer.h index 3c784f2..deb862c 100644 --- a/src/buffer/string_output_buffer.h +++ b/src/buffer/string_output_buffer.h @@ -1,16 +1,15 @@ #pragma once #include -#include "output_buffer.h" +#include "../serializer.h" namespace hps { constexpr size_t STRING_OUTPUT_BUFFER_SIZE = 1 << 10; -template <> -class OutputBuffer { +class StringOutputBuffer { public: - OutputBuffer(std::string& str) : str(&str) { pos = 0; } + StringOutputBuffer(std::string& str) : str(&str) { pos = 0; } void write(const char* content, size_t length) { if (pos + length > STRING_OUTPUT_BUFFER_SIZE) { @@ -40,6 +39,12 @@ class OutputBuffer { pos = 0; } + template + StringOutputBuffer& operator<<(const T& t) { + Serializer::serialize(t, *this); + return *this; + } + private: std::string* const str; diff --git a/src/container/array_serializer.h b/src/container/array_serializer.h index 98e8c71..ac74b0f 100644 --- a/src/container/array_serializer.h +++ b/src/container/array_serializer.h @@ -14,13 +14,13 @@ class Serializer< B, typename std::enable_if::value, void>::type> { public: - static void serialize(const std::array& container, OutputBuffer& ob) { + static void serialize(const std::array& container, B& ob) { for (const T& elem : container) { Serializer::serialize(elem, ob); } } - static void parse(std::array& container, InputBuffer& ib) { + static void parse(std::array& container, B& ib) { for (size_t i = 0; i < N; i++) { Serializer::parse(container[i], ib); } @@ -33,12 +33,12 @@ class Serializer< B, typename std::enable_if::value, void>::type> { public: - static void serialize(const std::array& container, OutputBuffer& ob) { + static void serialize(const std::array& container, B& ob) { const char* num_ptr = reinterpret_cast(container.data()); ob.write(num_ptr, N * sizeof(T)); } - static void parse(std::array& container, InputBuffer& ib) { + static void parse(std::array& container, B& ib) { char* num_ptr = reinterpret_cast(container.data()); ib.read(num_ptr, N * sizeof(T)); } @@ -47,7 +47,7 @@ class Serializer< template class Serializer, B> { public: - static void serialize(const std::array& container, OutputBuffer& ob) { + static void serialize(const std::array& container, B& ob) { if (N == 0) return; char chunk = 0; size_t n_chunk_elems = 0; @@ -68,7 +68,7 @@ class Serializer, B> { } } - static void parse(std::array& container, InputBuffer& ib) { + static void parse(std::array& container, B& ib) { char chunk = ib.read_char(); size_t n_chunk_elems = 0; for (size_t i = 0; i < N; i++) { diff --git a/src/container/array_serializer_test.cc b/src/container/array_serializer_test.cc index c2927be..996d645 100644 --- a/src/container/array_serializer_test.cc +++ b/src/container/array_serializer_test.cc @@ -3,43 +3,44 @@ #include #include #include "../basic_type/basic_type.h" +#include "../buffer/buffer.h" TEST(ArraySerializerTest, FewInts) { std::array input{{3, 444, -33}}; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::array output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::ElementsAre(3, 444, -33)); } TEST(ArraySerializerTest, FewDoubles) { std::array input{{3, 4.44, -3.3}}; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::array output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::ElementsAre(3, 4.44, -3.3)); } TEST(ArraySerializerTest, FewBools) { std::array input{{true, false, true}}; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::array output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::ElementsAre(true, false, true)); EXPECT_THAT(ss.str(), testing::SizeIs(testing::Eq(1))); } diff --git a/src/container/deque_serializer.h b/src/container/deque_serializer.h index 1225e34..b3901c8 100644 --- a/src/container/deque_serializer.h +++ b/src/container/deque_serializer.h @@ -11,14 +11,14 @@ namespace hps { template class Serializer, B> { public: - static void serialize(const std::deque& container, OutputBuffer& ob) { + static void serialize(const std::deque& container, B& ob) { Serializer::serialize(container.size(), ob); for (const T& elem : container) { Serializer::serialize(elem, ob); } } - static void parse(std::deque& container, InputBuffer& ib) { + static void parse(std::deque& container, B& ib) { size_t n_elems; Serializer::parse(n_elems, ib); container.clear(); diff --git a/src/container/deque_serializer_test.cc b/src/container/deque_serializer_test.cc index 54a7453..8782c16 100644 --- a/src/container/deque_serializer_test.cc +++ b/src/container/deque_serializer_test.cc @@ -3,17 +3,18 @@ #include #include #include "../basic_type/basic_type.h" +#include "../buffer/buffer.h" TEST(DequeSerializerTest, NoElements) { std::deque input; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::deque output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::IsEmpty()); } @@ -23,12 +24,12 @@ TEST(DequeSerializerTest, FewElements) { input.push_back(0); input.push_back(-133); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::deque output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::ElementsAre(3, 0, -133)); } diff --git a/src/container/list_serializer.h b/src/container/list_serializer.h index c92f156..af89887 100644 --- a/src/container/list_serializer.h +++ b/src/container/list_serializer.h @@ -11,14 +11,14 @@ namespace hps { template class Serializer, B> { public: - static void serialize(const std::list& container, OutputBuffer& ob) { + static void serialize(const std::list& container, B& ob) { Serializer::serialize(container.size(), ob); for (const T& elem : container) { Serializer::serialize(elem, ob); } } - static void parse(std::list& container, InputBuffer& ib) { + static void parse(std::list& container, B& ib) { size_t n_elems; Serializer::parse(n_elems, ib); container.clear(); diff --git a/src/container/list_serializer_test.cc b/src/container/list_serializer_test.cc index c296b33..a870be4 100644 --- a/src/container/list_serializer_test.cc +++ b/src/container/list_serializer_test.cc @@ -3,17 +3,18 @@ #include #include #include "../basic_type/basic_type.h" +#include "../buffer/buffer.h" TEST(ListSerializerTest, NoElements) { std::list input; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::list output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::IsEmpty()); } @@ -23,12 +24,12 @@ TEST(ListSerializerTest, FewElements) { input.push_back(0); input.push_back(-133); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::list output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::ElementsAre(3, 0, -133)); } diff --git a/src/container/map_serializer.h b/src/container/map_serializer.h index 254f7bd..3c26545 100644 --- a/src/container/map_serializer.h +++ b/src/container/map_serializer.h @@ -11,7 +11,7 @@ namespace hps { template class Serializer, B> { public: - static void serialize(const std::map& container, OutputBuffer& ob) { + static void serialize(const std::map& container, B& ob) { Serializer::serialize(container.size(), ob); for (const auto& elem : container) { Serializer::serialize(elem.first, ob); @@ -19,7 +19,7 @@ class Serializer, B> { } } - static void parse(std::map& container, InputBuffer& ib) { + static void parse(std::map& container, B& ib) { size_t n_elems; Serializer::parse(n_elems, ib); container.clear(); diff --git a/src/container/map_serializer_test.cc b/src/container/map_serializer_test.cc index 07471a5..02736a3 100644 --- a/src/container/map_serializer_test.cc +++ b/src/container/map_serializer_test.cc @@ -3,17 +3,18 @@ #include #include #include "../basic_type/basic_type.h" +#include "../buffer/buffer.h" TEST(MapSerializerTest, NoElements) { std::map input; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::map output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::IsEmpty()); } @@ -23,13 +24,13 @@ TEST(MapSerializerTest, FewElements) { input["bb"] = 0; input["cc"] = -333; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::map output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_EQ(output.size(), input.size()); EXPECT_THAT(output, testing::Contains(testing::Key("aa"))); EXPECT_THAT(output, testing::Contains(testing::Key("bb"))); diff --git a/src/container/pair_serializer.h b/src/container/pair_serializer.h index 0089f7e..8eafdff 100644 --- a/src/container/pair_serializer.h +++ b/src/container/pair_serializer.h @@ -10,12 +10,12 @@ namespace hps { template class Serializer, B> { public: - static void serialize(const std::pair& container, OutputBuffer& ob) { + static void serialize(const std::pair& container, B& ob) { Serializer::serialize(container.first, ob); Serializer::serialize(container.second, ob); } - static void parse(std::pair& container, InputBuffer& ib) { + static void parse(std::pair& container, B& ib) { Serializer::parse(container.first, ib); Serializer::parse(container.second, ib); } diff --git a/src/container/pair_serializer_test.cc b/src/container/pair_serializer_test.cc index c592f47..5a14234 100644 --- a/src/container/pair_serializer_test.cc +++ b/src/container/pair_serializer_test.cc @@ -2,17 +2,18 @@ #include #include #include "../basic_type/basic_type.h" +#include "../buffer/buffer.h" TEST(PairSerializerTest, SinglePair) { std::pair input("aa", 3.5); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::pair output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_EQ(output.first, input.first); EXPECT_EQ(output.second, input.second); } diff --git a/src/container/set_serializer.h b/src/container/set_serializer.h index 3f1cff0..a987b0d 100644 --- a/src/container/set_serializer.h +++ b/src/container/set_serializer.h @@ -10,14 +10,14 @@ namespace hps { template class Serializer, B> { public: - static void serialize(const std::set& container, OutputBuffer& ob) { + static void serialize(const std::set& container, B& ob) { Serializer::serialize(container.size(), ob); for (const T& elem : container) { Serializer::serialize(elem, ob); } } - static void parse(std::set& container, InputBuffer& ib) { + static void parse(std::set& container, B& ib) { size_t n_elems; Serializer::parse(n_elems, ib); container.clear(); diff --git a/src/container/set_serializer_test.cc b/src/container/set_serializer_test.cc index 8088539..d6f9dcd 100644 --- a/src/container/set_serializer_test.cc +++ b/src/container/set_serializer_test.cc @@ -3,17 +3,18 @@ #include #include #include "../basic_type/basic_type.h" +#include "../buffer/buffer.h" TEST(SetSerializerTest, NoElements) { std::set input; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::set output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::IsEmpty()); } @@ -23,12 +24,12 @@ TEST(SetSerializerTest, FewElements) { input.insert(0); input.insert(-133); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::set output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::UnorderedElementsAre(3, 0, -133)); } diff --git a/src/container/unique_ptr_serializer.h b/src/container/unique_ptr_serializer.h index ac27476..2aa2a0c 100644 --- a/src/container/unique_ptr_serializer.h +++ b/src/container/unique_ptr_serializer.h @@ -9,7 +9,7 @@ namespace hps { template class Serializer, B> { public: - static void serialize(const std::unique_ptr& container, OutputBuffer& ob) { + static void serialize(const std::unique_ptr& container, B& ob) { if (container) { ob.write_char(1); Serializer::serialize(*container, ob); @@ -18,7 +18,7 @@ class Serializer, B> { } } - static void parse(std::unique_ptr& container, InputBuffer& ib) { + static void parse(std::unique_ptr& container, B& ib) { if (ib.read_char()) { if (!container) { container.reset(new T()); diff --git a/src/container/unique_ptr_serializer_test.cc b/src/container/unique_ptr_serializer_test.cc index 7f31a89..2f413b9 100644 --- a/src/container/unique_ptr_serializer_test.cc +++ b/src/container/unique_ptr_serializer_test.cc @@ -3,29 +3,30 @@ #include #include #include "../basic_type/basic_type.h" +#include "../buffer/buffer.h" TEST(UniquePtrSerializerTest, NoElement) { std::unique_ptr input; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::unique_ptr output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_FALSE(output); } TEST(UniquePtrSerializerTest, HasElement) { std::unique_ptr input(new std::string("aaa")); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::unique_ptr output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_EQ(*(output.get()), "aaa"); } diff --git a/src/container/unordered_map_serializer.h b/src/container/unordered_map_serializer.h index 9842790..864c9f1 100644 --- a/src/container/unordered_map_serializer.h +++ b/src/container/unordered_map_serializer.h @@ -11,7 +11,7 @@ namespace hps { template class Serializer, B> { public: - static void serialize(const std::unordered_map& container, OutputBuffer& ob) { + static void serialize(const std::unordered_map& container, B& ob) { Serializer::serialize(container.size(), ob); for (const auto& elem : container) { Serializer::serialize(elem.first, ob); @@ -19,7 +19,7 @@ class Serializer, B> { } } - static void parse(std::unordered_map& container, InputBuffer& ib) { + static void parse(std::unordered_map& container, B& ib) { size_t n_elems; Serializer::parse(n_elems, ib); container.clear(); diff --git a/src/container/unordered_map_serializer_test.cc b/src/container/unordered_map_serializer_test.cc index 57f7883..a58ad65 100644 --- a/src/container/unordered_map_serializer_test.cc +++ b/src/container/unordered_map_serializer_test.cc @@ -3,17 +3,18 @@ #include #include #include "../basic_type/basic_type.h" +#include "../buffer/buffer.h" TEST(UnorderedMapSerializerTest, NoElements) { std::unordered_map input; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::unordered_map output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::IsEmpty()); } @@ -23,13 +24,13 @@ TEST(UnorderedMapSerializerTest, FewElements) { input["bb"] = 0; input["cc"] = -333; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::unordered_map output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_EQ(output.size(), input.size()); EXPECT_THAT(output, testing::Contains(testing::Key("aa"))); EXPECT_THAT(output, testing::Contains(testing::Key("bb"))); diff --git a/src/container/unordered_set_serializer.h b/src/container/unordered_set_serializer.h index c7bf2e8..d352c35 100644 --- a/src/container/unordered_set_serializer.h +++ b/src/container/unordered_set_serializer.h @@ -10,14 +10,14 @@ namespace hps { template class Serializer, B> { public: - static void serialize(const std::unordered_set& container, OutputBuffer& ob) { + static void serialize(const std::unordered_set& container, B& ob) { Serializer::serialize(container.size(), ob); for (const T& elem : container) { Serializer::serialize(elem, ob); } } - static void parse(std::unordered_set& container, InputBuffer& ib) { + static void parse(std::unordered_set& container, B& ib) { size_t n_elems; Serializer::parse(n_elems, ib); container.clear(); diff --git a/src/container/unordered_set_serializer_test.cc b/src/container/unordered_set_serializer_test.cc index 78ff786..d13d058 100644 --- a/src/container/unordered_set_serializer_test.cc +++ b/src/container/unordered_set_serializer_test.cc @@ -3,17 +3,18 @@ #include #include #include "../basic_type/basic_type.h" +#include "../buffer/buffer.h" TEST(UnorderedSetSerializerTest, NoElements) { std::unordered_set input; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::unordered_set output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::IsEmpty()); } @@ -23,12 +24,12 @@ TEST(UnorderedSetSerializerTest, FewElements) { input.insert(0); input.insert(-133); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::unordered_set output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::UnorderedElementsAre(3, 0, -133)); } diff --git a/src/container/vector_serializer.h b/src/container/vector_serializer.h index 301efdd..d3fbce6 100644 --- a/src/container/vector_serializer.h +++ b/src/container/vector_serializer.h @@ -18,14 +18,14 @@ class Serializer< typename std:: enable_if::value && !hps::is_unique_ptr::value, void>::type> { public: - static void serialize(const std::vector& container, OutputBuffer& ob) { + static void serialize(const std::vector& container, B& ob) { Serializer::serialize(container.size(), ob); for (const T& elem : container) { Serializer::serialize(elem, ob); } } - static void parse(std::vector& container, InputBuffer& ib) { + static void parse(std::vector& container, B& ib) { size_t n_elems; Serializer::parse(n_elems, ib); container.resize(n_elems); @@ -41,14 +41,14 @@ class Serializer< B, typename std::enable_if::value, void>::type> { public: - static void serialize(const std::vector& container, OutputBuffer& ob) { + static void serialize(const std::vector& container, B& ob) { const size_t n_elems = container.size(); Serializer::serialize(n_elems, ob); const char* num_ptr = reinterpret_cast(container.data()); ob.write(num_ptr, n_elems * sizeof(T)); } - static void parse(std::vector& container, InputBuffer& ib) { + static void parse(std::vector& container, B& ib) { size_t n_elems; Serializer::parse(n_elems, ib); container.resize(n_elems); @@ -60,7 +60,7 @@ class Serializer< template class Serializer, B> { public: - static void serialize(const std::vector& container, OutputBuffer& ob) { + static void serialize(const std::vector& container, B& ob) { const size_t n_elems = container.size(); Serializer::serialize(n_elems, ob); char chunk = 0; @@ -82,7 +82,7 @@ class Serializer, B> { } } - static void parse(std::vector& container, InputBuffer& ib) { + static void parse(std::vector& container, B& ib) { size_t n_elems; Serializer::parse(n_elems, ib); container.resize(n_elems); @@ -102,7 +102,7 @@ class Serializer, B> { template class Serializer>, B> { public: - static void serialize(const std::vector>& container, OutputBuffer& ob) { + static void serialize(const std::vector>& container, B& ob) { const size_t n_elems = container.size(); std::vector exist(n_elems, false); for (size_t i = 0; i < n_elems; i++) { @@ -116,7 +116,7 @@ class Serializer>, B> { } } - static void parse(std::vector>& container, InputBuffer& ib) { + static void parse(std::vector>& container, B& ib) { std::vector exist; Serializer, B>::parse(exist, ib); const size_t n_elems = exist.size(); diff --git a/src/container/vector_serializer_test.cc b/src/container/vector_serializer_test.cc index a95de4a..d544d43 100644 --- a/src/container/vector_serializer_test.cc +++ b/src/container/vector_serializer_test.cc @@ -3,17 +3,18 @@ #include #include #include "../basic_type/basic_type.h" +#include "../buffer/buffer.h" TEST(VectorSerializerTest, NoElements) { std::vector input; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::vector output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::IsEmpty()); } @@ -23,13 +24,13 @@ TEST(VectorSerializerTest, FewElements) { input.push_back(0); input.push_back(-133); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::vector output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_THAT(output, testing::ElementsAre(3, 0, -133)); } @@ -38,13 +39,13 @@ TEST(VectorSerializerTest, VectorOfVector) { input.push_back(std::vector({4, 44})); input.push_back(std::vector({0})); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer>, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer>, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::vector> output; - hps::Serializer>, hps::Stream>::parse(output, ib); + hps::Serializer>, hps::StreamInputBuffer>::parse(output, ib); EXPECT_EQ(output.size(), 2); EXPECT_THAT(output[0], testing::ElementsAre(4, 44)); EXPECT_THAT(output[1], testing::ElementsAre(0)); @@ -56,13 +57,13 @@ TEST(VectorSerializerTest, BoolElements) { input.resize(n_elems); for (int i = 0; i < n_elems; i++) input[i] = (i < 10 || i % 10 == 0) ? 1 : 0; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::vector output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_EQ(input, output); EXPECT_THAT(ss.str(), testing::SizeIs(testing::Le((n_elems << 3) + 4))); } @@ -73,13 +74,13 @@ TEST(VectorSerializerTest, UniquePtrElements) { input.resize(n_elems); for (int i = 0; i < n_elems; i += 3) input[i].reset(new int(i >> 4)); std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer>, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer>, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::vector> output; - hps::Serializer>, hps::Stream>::parse(output, ib); + hps::Serializer>, hps::StreamInputBuffer>::parse(output, ib); EXPECT_EQ(input.size(), output.size()); for (int i = 0; i < n_elems; i++) { if (input[i]) { @@ -98,13 +99,13 @@ TEST(VectorSerializerLargeTest, ManyIntElements) { input.resize(n_elems); for (int i = 0; i < n_elems; i++) input[i] = i; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::vector output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_EQ(input.size(), output.size()); for (int i = 0; i < 10; i++) EXPECT_EQ(input[i], output[i]); } @@ -115,13 +116,13 @@ TEST(VectorSerializerLargeTest, ManyDoubleElements) { input.resize(n_elems); for (int i = 0; i < n_elems; i++) input[i] = i; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::vector output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_EQ(input.size(), output.size()); for (int i = 0; i < 10; i++) EXPECT_EQ(input[i], output[i]); } @@ -132,13 +133,13 @@ TEST(VectorSerializerLargeTest, ManyStringElements) { input.resize(n_elems); for (int i = 0; i < n_elems; i++) input[i] = "fadczioupekljaiou"; std::stringstream ss; - hps::OutputBuffer ob(ss); - hps::Serializer, hps::Stream>::serialize(input, ob); + hps::StreamOutputBuffer ob(ss); + hps::Serializer, hps::StreamOutputBuffer>::serialize(input, ob); ob.flush(); - hps::InputBuffer ib(ss); + hps::StreamInputBuffer ib(ss); std::vector output; - hps::Serializer, hps::Stream>::parse(output, ib); + hps::Serializer, hps::StreamInputBuffer>::parse(output, ib); EXPECT_EQ(input.size(), output.size()); for (int i = 0; i < 10; i++) EXPECT_EQ(input[i], output[i]); } diff --git a/src/hps.h b/src/hps.h index 769c4b1..c32b1c2 100644 --- a/src/hps.h +++ b/src/hps.h @@ -3,6 +3,7 @@ #include #include #include "basic_type/basic_type.h" +#include "buffer/buffer.h" #include "container/container.h" #include "serializer.h" @@ -10,74 +11,74 @@ namespace hps { // Serialize data t to an STL ostream. template -void serialize_to_stream(const T& t, std::ostream& stream) { - OutputBuffer ob(stream); - Serializer::serialize(t, ob); +void to_stream(const T& t, std::ostream& stream) { + StreamOutputBuffer ob(stream); + ob << t; ob.flush(); } // Parse from an STL istream and save to the data t passed in. // Recommended for repeated use inside a loop. template -void parse_from_stream(T& t, std::istream& stream) { - InputBuffer ib(stream); - Serializer::parse(t, ib); +void from_stream(std::istream& stream, T& t) { + StreamInputBuffer ib(stream); + ib >> t; } // Parse from an STL istream and return the data. template -T parse_from_stream(std::istream& stream) { +T from_stream(std::istream& stream) { T t; - parse_from_stream(t, stream); + from_stream(stream, t); return t; } // Serialize data t to the STL string passed in. // Recommended for repeated use inside a loop. template -void serialize_to_string(const T& t, std::string& str) { - OutputBuffer ob(str); - Serializer::serialize(t, ob); +void to_string(const T& t, std::string& str) { + StringOutputBuffer ob(str); + ob << t; ob.flush(); } // Serialize data t to an STL string and return it. template -std::string serialize_to_string(const T& t) { +std::string to_string(const T& t) { std::string str; - serialize_to_string(t, str); + to_string(t, str); return str; } // Parse from an STL string and save to the data t passed in. // Recommended for repeated use inside a loop. template -void parse_from_string(T& t, const std::string& str) { - InputBuffer ib(str); - Serializer::parse(t, ib); +void from_string(const std::string& str, T& t) { + StringInputBuffer ib(str); + ib >> t; } // Parse from an STL string and return the data. template -T parse_from_string(const std::string& str) { +T from_string(const std::string& str) { T t; - parse_from_string(t, str); + from_string(str, t); return t; } // Parse from a char array and save to the data t passed in. // Recommended for repeated use inside a loop. template -void parse_from_char_array(T& t, const char* arr) { - InputBuffer ib(arr); - Serializer::parse(t, ib); +void from_char_array(const char* arr, T& t) { + CharArrayInputBuffer ib(arr); + ib >> t; } // Parse from a char array and return the data. template -T parse_from_char_array(const char* arr) { +T from_char_array(const char* arr) { T t; - parse_from_char_array(t, arr); + from_char_array(arr, t); return t; } diff --git a/src/hps_test.cc b/src/hps_test.cc index 235dd3e..a45964d 100644 --- a/src/hps_test.cc +++ b/src/hps_test.cc @@ -5,22 +5,22 @@ TEST(HpsTest, ToAndFromStream) { const int input = 22; std::stringstream ss; - hps::serialize_to_stream(input, ss); - const int output = hps::parse_from_stream(ss); + hps::to_stream(input, ss); + const int output = hps::from_stream(ss); EXPECT_EQ(input, output); } TEST(HpsTest, ToAndFromString) { const double input = 1.1; - std::string str = hps::serialize_to_string(input); - const double output = hps::parse_from_string(str); + std::string str = hps::to_string(input); + const double output = hps::from_string(str); EXPECT_EQ(input, output); } TEST(HpsTest, FromCharArray) { const double input = 1.1; - std::string str = hps::serialize_to_string(input); - const double output = hps::parse_from_char_array(str.data()); + std::string str = hps::to_string(input); + const double output = hps::from_char_array(str.data()); EXPECT_EQ(input, output); } @@ -29,8 +29,8 @@ TEST(HpsLargeTest, LargeIntVectorToAndFromString) { const int n_elems = 1 << 25; input.resize(n_elems); for (int i = 0; i < n_elems; i++) input[i] = i; - const std::string str = hps::serialize_to_string(input); - std::vector output = hps::parse_from_string>(str); + const std::string str = hps::to_string(input); + std::vector output = hps::from_string>(str); EXPECT_EQ(input.size(), output.size()); for (int i = 0; i < 10; i++) EXPECT_EQ(input[i], output[i]); } @@ -40,8 +40,8 @@ TEST(HpsLargeTest, LargeDoubleVectorToAndFromString) { const int n_elems = 1 << 25; input.resize(n_elems); for (int i = 0; i < n_elems; i++) input[i] = i; - const std::string str = hps::serialize_to_string(input); - std::vector output = hps::parse_from_string>(str); + const std::string str = hps::to_string(input); + std::vector output = hps::from_string>(str); EXPECT_EQ(input.size(), output.size()); for (int i = 0; i < 10; i++) EXPECT_EQ(input[i], output[i]); } @@ -51,8 +51,8 @@ TEST(HpsLargeTest, LargeStringVectorToAndFromString) { const int n_elems = 1 << 23; input.resize(n_elems); for (int i = 0; i < n_elems; i++) input[i] = "fadczioupekljaiou"; - const std::string str = hps::serialize_to_string(input); - std::vector output = hps::parse_from_string>(str); + const std::string str = hps::to_string(input); + std::vector output = hps::from_string>(str); EXPECT_EQ(input.size(), output.size()); for (int i = 0; i < 10; i++) EXPECT_EQ(input[i], output[i]); } @@ -61,8 +61,8 @@ TEST(HpsLargeTest, RepeatedToAndFromString) { const std::vector input(1 << 11, 3.3); std::vector output; for (size_t i = 0; i < 1 << 18; i++) { - const std::string str = hps::serialize_to_string(input); - output = hps::parse_from_string>(str); + const std::string str = hps::to_string(input); + output = hps::from_string>(str); } EXPECT_EQ(input, output); } @@ -89,13 +89,13 @@ namespace hps { template class Serializer { public: - static void serialize(const CustomType& obj, OutputBuffer& ob) { + static void serialize(const CustomType& obj, B& ob) { Serializer::serialize(obj.num, ob); Serializer, B>::serialize(obj.vec, ob); Serializer::serialize(obj.str, ob); } - static void parse(CustomType& obj, InputBuffer& ib) { + static void parse(CustomType& obj, B& ib) { Serializer::parse(obj.num, ib); Serializer, B>::parse(obj.vec, ib); Serializer::parse(obj.str, ib); @@ -110,8 +110,8 @@ TEST(HpsTest, CustomType) { input.vec.push_back(2.2); input.vec.push_back(-4.4); input.set_str("aa"); - const std::string& str = hps::serialize_to_string(input); - const CustomType& output = hps::parse_from_string(str); + const std::string& str = hps::to_string(input); + const CustomType& output = hps::from_string(str); EXPECT_EQ(output.num, input.num); EXPECT_THAT(output.vec, testing::ElementsAre(2.2, -4.4)); EXPECT_EQ(output.get_str(), "aa"); @@ -123,8 +123,8 @@ TEST(HpsTest, CustomTypeVector) { input[0].vec.push_back(2.2); input[0].vec.push_back(-4.4); input[0].set_str("aa"); - const std::string& str = hps::serialize_to_string(input); - const std::vector& output = hps::parse_from_string>(str); + const std::string& str = hps::to_string(input); + const std::vector& output = hps::from_string>(str); EXPECT_EQ(output[0].num, 3); EXPECT_THAT(output[0].vec, testing::ElementsAre(2.2, -4.4)); EXPECT_EQ(output[0].get_str(), "aa"); diff --git a/src/serializer.h b/src/serializer.h index 863551f..5b0738c 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -4,22 +4,15 @@ #include #include #include -#include "buffer/buffer.h" namespace hps { template class Serializer { public: - static void serialize(const T&, OutputBuffer&) { default_handler(); } + static void serialize(const T& t, B& buf) { t.serialize(buf); } - static void parse(T&, InputBuffer&) { default_handler(); } - - private: - static void default_handler() { - const std::string type_name = typeid(T).name(); - throw std::logic_error("Serializer not found for type " + type_name); - } + static void parse(T& t, B& buf) { t.parse(buf); } }; } // namespace hps