comparison C++/Pack.h @ 268:b5d795389387

Pack: * Remove read / write from path, it makes no sense because you need to read all in one shot. * Implement arrays via readArray / writeArray using iterators. Task: #302
author David Demelier <markand@malikania.fr>
date Wed, 15 Oct 2014 21:29:20 +0200
parents bc9b5e7421a7
children 7433ebe6a8b0
comparison
equal deleted inserted replaced
267:bc9b5e7421a7 268:b5d795389387
97 * @throw std::runtime_exception on error 97 * @throw std::runtime_exception on error
98 */ 98 */
99 template <typename T, typename... Args> 99 template <typename T, typename... Args>
100 static void write(std::ostream &out, Endian endian, const T &value, const Args&... args) 100 static void write(std::ostream &out, Endian endian, const T &value, const Args&... args)
101 { 101 {
102 T ret = convert(value, endian); 102 auto ret = convert(value, endian);
103 out.write(reinterpret_cast<const char *>(&ret), TypeInfo<T>::size); 103 out.write(reinterpret_cast<const char *>(&ret), TypeInfo<T>::size);
104 write(out, endian, args...);
105 }
106
107 /**
108 * Write binary data to the file.
109 *
110 * @param path the path to the file
111 * @param endian the endian mode
112 * @param args the arguments
113 * @throw std::runtime_exception on error
114 */
115 template <typename... Args>
116 static void write(const std::string &path, Endian endian, const Args&... args)
117 {
118 std::ofstream out;
119
120 out.open(path, std::ios_base::binary);
121 if (!out.is_open())
122 throw std::runtime_error("Can't open file for writing");
123
124 write(out, endian, args...); 104 write(out, endian, args...);
125 } 105 }
126 106
127 /** 107 /**
128 * Read nothing, stop recursion. 108 * Read nothing, stop recursion.
146 value = convert(value, endian); 126 value = convert(value, endian);
147 read(in, endian, args...); 127 read(in, endian, args...);
148 } 128 }
149 129
150 /** 130 /**
151 * Read binary data from the file. 131 * Read array and store it to the output iterator. Because the container
152 * 132 * is not allocated, we use an integer based size to determine the
153 * @param path the path to the file 133 * number of bytes to read.
154 * @param endian the endian mode 134 *
155 * @param args the arguments 135 * @param in the input stream
156 * @throw std::runtime_exception on error 136 * @param endian the endian mode
157 */ 137 * @param out the output it
158 template <typename... Args> 138 * @param count the number of bytes to read
159 static void read(const std::string &path, Endian endian, Args&... args) 139 * @throw std::runtime_error on error
160 { 140 */
161 std::ifstream in; 141 template <typename OutputIt>
162 142 static void readArray(std::istream &in, Endian endian, OutputIt out, unsigned count)
163 in.open(path, std::ios_base::binary); 143 {
164 if (!in.is_open()) 144 typename OutputIt::container_type::value_type byte;
165 throw std::runtime_error("Can't open file for reading"); 145
166 146 for (unsigned i = 0; i < count; ++i) {
167 read(in, endian, args...); 147 read(in, endian, byte);
148 *out++ = byte;
149 }
150 }
151
152 /**
153 * Read a container from an input iterator and write it to output stream.
154 *
155 * @param out the output stream
156 * @param endian the endian mode
157 * @param in the input iterator
158 * @param count the number of bytes to write
159 * @throw std::runtime_error on error
160 */
161 template <typename InputIt>
162 static void writeArray(std::ostream &out, Endian endian, InputIt in, unsigned count)
163 {
164 typename std::iterator_traits<InputIt>::value_type byte;
165
166 for (unsigned i = 0; i < count; ++i) {
167 byte = *in++;
168 write(out, endian, byte);
169 }
168 } 170 }
169 }; 171 };
170 172
171 template <> 173 template <>
172 struct Pack::TypeInfo<uint8_t> { 174 struct Pack::TypeInfo<uint8_t> {