Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include <string>
  2. #include <fstream>
  3. #include <map>
  4. #include <type_traits>
  5. #include <vector>
  6. #include <iostream>
  7.  
  8.  
  9. class BinaryOutput
  10. {
  11. public:
  12. BinaryOutput(const std::string &file_name) : file(file_name, std::ios::binary) {};
  13. template<class T>
  14. BinaryOutput& operator<<(const T &value)
  15. {
  16. write(file, value);
  17. return *this;
  18. }
  19. private:
  20. std::ofstream file;
  21.  
  22. template<class T, typename std::enable_if<std::is_same<T,
  23. std::vector< typename T::value_type,
  24. typename T::allocator_type >>::value>::type* = nullptr>
  25. static void write(std::ostream &stream, const T &container)
  26. {
  27. const std::size_t size = container.size();
  28. stream.write(reinterpret_cast<const char*>(&size), sizeof(std::size_t));
  29. for (const auto &elem : container)
  30. {
  31. write(stream, elem);
  32. }
  33. }
  34.  
  35.  
  36. template<class T,
  37. typename std::enable_if< std::is_fundamental<T>::value >::type* = nullptr>
  38. static void write(std::ostream &stream, const T &fundamental)
  39. {
  40. stream.write(reinterpret_cast<const char*>(&fundamental), sizeof(fundamental));
  41. }
  42.  
  43. static void write(std::ostream &stream, const std::string &str)
  44. {
  45. const std::size_t size = str.size();
  46. stream.write(reinterpret_cast<const char*>(&size), sizeof(std::size_t));
  47. stream.write(str.c_str(), size);
  48. }
  49. };
  50.  
  51.  
  52. class BinaryInput
  53. {
  54. public:
  55. BinaryInput(const std::string &file_name) : file(file_name, std::ios::binary) {};
  56. template<class T>
  57. BinaryInput& operator>>(T &value)
  58. {
  59. read(file, value);
  60. return *this;
  61. }
  62. private:
  63. std::ifstream file;
  64.  
  65. template<class T, typename std::enable_if<std::is_same<T,
  66. std::vector< typename T::value_type,
  67. typename T::allocator_type >>::value>::type* = nullptr>
  68. static void read(std::istream &stream, T &container)
  69. {
  70. std::size_t size;
  71. stream.read(reinterpret_cast<char*>(&size), sizeof(std::size_t));
  72. container.resize(size);
  73. for (auto &elem : container)
  74. {
  75. read(stream, elem);
  76. }
  77. }
  78.  
  79. template<class T,
  80. typename std::enable_if< std::is_fundamental<T>::value >::type* = nullptr>
  81. static void read(std::istream &stream, T &fundamental)
  82. {
  83. stream.read(reinterpret_cast<char*>(&fundamental), sizeof(fundamental));
  84. }
  85.  
  86. static void read(std::istream &stream, std::string &str)
  87. {
  88. std::size_t size;
  89. stream.read(reinterpret_cast<char*>(&size), sizeof(std::size_t));
  90. str.resize(size);
  91. stream.read(const_cast<char*>(str.c_str()), size);
  92. }
  93.  
  94. };
  95.  
  96.  
  97. int main()
  98. {
  99. {
  100. BinaryOutput out("bin");
  101. int val1 = 10;
  102. double val2 = 20;
  103. float val3 = 30;
  104. long val4 = 40;
  105.  
  106. out << val1 << val2 << val3 << val4;
  107. }
  108. {
  109. BinaryInput in("bin");
  110. int val1;
  111. double val2;
  112. float val3;
  113. long val4;
  114.  
  115. in >> val1 >> val2 >> val3 >> val4;
  116. std::cout << "Fundamentals:\n" << val1 << '\n' << val2 << '\n' << val3 << '\n' << val4 << std::endl;
  117. }
  118.  
  119.  
  120. {
  121. BinaryOutput out("bin");
  122. std::vector<int> val1{ 1, 2, 3 };
  123. std::vector<double> val2{ 4.42342344, 5.654654654635, 6.4234234125 };
  124. std::vector<float> val3{ 7.634f, 8.12321f, 9.56546f };
  125. std::vector<std::string> val4{ "lol", "kek" , "ololo" };
  126.  
  127. out << val1 << val2 << val3 << val4;
  128. }
  129. {
  130. BinaryInput out("bin");
  131. std::vector<int> val1;
  132. std::vector<double> val2;
  133. std::vector<float> val3;
  134. std::vector<std::string> val4;
  135.  
  136. out >> val1 >> val2 >> val3 >> val4;
  137. std::cout << "Vectors:\n";
  138. for (int i = 0; i < val4.size(); ++i)
  139. {
  140. std::cout << val1[i] << ' ' << val2[i] << ' ' << val3[i] << ' ' << val4[i] << std::endl;
  141. }
  142. }
  143. return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement