C++ Logo

sg16

Advanced search

Re: [SG16-Unicode] std::byte based I/O library

From: Lyberta <lyberta_at_[hidden]>
Date: Sat, 16 Feb 2019 03:37:00 +0000
Lyberta:
> I guess I'm gonna write a synopsis of my design so we can have a more
> tangible discussion.


namespace std::io
{

class format final // bikeshed name
{
public:
 constexpr format(std::endian endianness = std::endian::native);
 constexpr std::endian get_endianness() const noexcept;
 constexpr void set_endianness(std::endian new_endianness) noexcept;
 // TODO: Floating point and strings
};

class stream_base
{
 stream_base(format f = {});
 virtual ~stream_base() = default;
 format& get_format() noexcept;
 const format& get_format() const noexcept;
 virtual bool is_good() const = 0;
 virtual bool is_eof() const = 0;
 virtual bool is_fail() const = 0;
 virtual bool is_bad() const = 0;
 explicit operator bool() const;
 
 // Do we define a new strong enum instead of std::ios_base::iostate?
 virtual std::ios_base::iostate get_state() const = 0;
 virtual void add_to_state(std::ios_base::iostate state) = 0;
 virtual void set_state(std::ios_base::iostate state) = 0;
 void clear_state();
};

class input_stream : public virtual stream_base
{
public:
 input_stream(format f = {});
 virtual std::streamsize get_read_position() = 0;
 virtual void set_read_position(std::streamsize position) = 0;
 
 // Do we define a new strong enum instead of std::ios_base::seekdir?
 virtual void seek_read_position(std::streamoff offset,
  std::ios_base::seekdir direction) = 0;
 virtual void read(std::span<std::byte> buffer) = 0;
};

class output_stream : public virtual stream_base
{
public:
 output_stream(format f = {});
 virtual std::streamsize get_write_position() = 0;
 virtual void set_write_position(std::streamsize position) = 0;

 // Do we define a new strong enum instead of std::ios_base::seekdir?
 virtual void seek_write_position(std::streamoff offset,
  std::ios_base::seekdir direction) = 0;
 virtual void write(std::span<const std::byte> buffer) = 0;
};

class stream : public input_stream, public output_stream
{
public:
 stream(format f = {});
};

namespace customization_points
{

void read(input_stream& s, Integral auto& var);
template <typename T>
 requires is_floating_point_v<T>
void read(input_stream& s, T& var);
void read(input_stream& s, std::span<std::byte> buffer);
template <typename Traits, typename Allocator>
void read(input_stream& s, std::basic_string<char, Traits, Allocator>&
buffer);


void write(output_stream& s, Integral auto var);
template <typename T>
 requires is_floating_point_v<T>
void write(output_stream& s, T var);
void write(output_stream& s, std::span<const std::byte> buffer);
void write(output_stream& s, std::string_view buffer);

struct read_customization_point
{
 template <typename T>
 void operator()(input_stream& s, T& var);
};

struct write_customization_point
{
 template <typename T>
 void operator()(output_stream& s, const T& var);
};

}

inline customization_points::read_customization_point read;
inline customization_points::write_customization_point write;

}


Received on 2019-02-16 04:47:17