Improved container support

This commit is contained in:
Dreaded_X 2019-12-14 00:56:01 +01:00
parent 35cd2873cd
commit 42ca9fefff
3 changed files with 67 additions and 9 deletions

View File

@ -47,6 +47,8 @@ namespace io {
return value;
}
template <typename T> requires std::is_convertible_v<std::string, T>
T read(std::istream& is, size_t length) {
std::vector<char> t(length);
@ -62,14 +64,13 @@ namespace io {
return read<T>(is, length);
}
// @todo We need to support std::array
// Extra concept that checks if the size is static
template <Container T>
T read(std::istream& is, size_t length) {
// @todo Properly constuct the container
T v(length);
for (size_t i = 0; i < length; ++i) {
v[i] = io::read<typename T::value_type>(is);
for (auto& e : v) {
e = io::read<typename T::value_type>(is);
}
return v;
}
@ -79,4 +80,14 @@ namespace io {
size_t length = io::read<size_t>(is);
return read<T>(is, length);
}
// Static sized container
template <Container T> requires requires (T a) { std::tuple_size<T>::value; }
T read(std::istream& is) {
T v;
for (size_t i = 0; i < std::tuple_size<T>::value; ++i) {
v[i] = io::read<typename T::value_type>(is);
}
return v;
}
}

View File

@ -2,6 +2,7 @@
#include "helper.h"
#include <iostream>
#include <cstdint>
#include <tuple>
namespace io {
// This exists to allow for specialization and clearer errors
@ -59,6 +60,7 @@ namespace io {
os << s;
}
// @todo Merge this into one function with the proper default automatically
template <Container T>
void write(std::ostream& os, T value, bool store_length = true) {
if (store_length) {
@ -68,4 +70,14 @@ namespace io {
write<typename T::value_type>(os, entry);
}
}
template <Container T> requires requires (T a) { std::tuple_size<T>::value; }
void write(std::ostream& os, T value, bool store_length = false) {
if (store_length) {
write<size_t>(os, value.size());
}
for (const auto& entry : value) {
write<typename T::value_type>(os, entry);
}
}
}

View File

@ -2,7 +2,9 @@
#include <vector>
#include <array>
#include <list>
#include <fstream>
#include <experimental/source_location>
struct A {
int b;
@ -29,12 +31,25 @@ int main() {
io::write(f, "test");
std::array<char, 2> a = {'a', 'b'};
// Does not store length by default
io::write(f, a);
std::array<std::string, 2> b = {"Hello", "World"};
io::write(f, b);
std::array b = {"Hello", "World"};
// Explicitly store length
io::write(f, b, true);
std::vector<int> c = {1, 2, 3, 4, 5};
// Stores length by default
io::write(f, c);
std::list d = {6, 7, 8, 9};
// Stores length by default
io::write(f, d);
io::write(f, A{4});
std::array e = {A{1}, A{5}, A{3}};
io::write(f, e, true);
}
{
@ -43,19 +58,39 @@ int main() {
std::cout << io::read<size_t>(f) << '\n';
std::cout << io::read<std::string>(f) << '\n';
// auto v = io::read<std::array<char, 2>>(f);
auto a = io::read<std::vector<char>>(f);
// Length is implied by the array
auto a = io::read<std::array<char,2>>(f);
for (auto& v : a) {
std::cout << v << ' ';
}
std::cout << '\n';
// Length is read from file (optionally can be provided as parameter)
auto b = io::read<std::vector<std::string>>(f);
for (auto& v : b) {
std::cout << v << ' ';
}
std::cout << '\n';
auto c = io::read<std::vector<int>>(f);
for (auto& v : c) {
std::cout << v << ' ';
}
std::cout << '\n';
auto d = io::read<std::list<int>>(f);
for (auto& v : d) {
std::cout << v << ' ';
}
std::cout << '\n';
std::cout << io::read<A>(f).b << '\n';
auto e = io::read<std::list<int>>(f);
for (auto& v : e) {
std::cout << v << ' ';
}
std::cout << '\n';
}
}