24 # error C++ compiler required
27 #ifndef GMIO_SUPPORT_STREAM_CPP_H
28 #define GMIO_SUPPORT_STREAM_CPP_H
31 #include "../gmio_core/stream.h"
37 template<
typename CHAR,
typename TRAITS>
41 template<
typename CHAR,
typename TRAITS>
55 template<
typename STREAM>
56 bool stream_cpp_at_end(
void* cookie)
58 STREAM* s =
static_cast<STREAM*
>(cookie);
62 template<
typename STREAM>
63 int stream_cpp_error(
void* cookie)
65 STREAM* s =
static_cast<STREAM*
>(cookie);
69 template<
typename STREAM>
70 size_t istream_cpp_read(
71 void* cookie,
void* ptr,
size_t item_size,
size_t item_count)
73 STREAM* s =
static_cast<STREAM*
>(cookie);
74 s->read(static_cast<char*>(ptr), item_size * item_count);
75 return static_cast<size_t>(s->gcount() / item_size);
78 template<
typename STREAM>
79 size_t ostream_cpp_write(
80 void* cookie,
const void* ptr,
size_t item_size,
size_t item_count)
82 STREAM* s =
static_cast<STREAM*
>(cookie);
83 s->write(static_cast<const char*>(ptr), item_size * item_count);
85 return item_size * item_count;
88 template<
typename STREAM>
91 STREAM* s =
static_cast<STREAM*
>(cookie);
92 std::streampos pos = s->tellg();
93 s->seekg(0, std::ios_base::beg);
94 std::streampos begin_pos = s->tellg();
95 s->seekg(0, std::ios_base::end);
96 std::streampos end_pos = s->tellg();
97 s->seekg(pos, std::ios_base::beg);
98 return end_pos - begin_pos;
101 template<
typename STREAM>
104 STREAM* s =
static_cast<STREAM*
>(cookie);
105 std::streampos pos = s->tellp();
106 s->seekp(0, std::ios_base::beg);
107 std::streampos begin_pos = s->tellp();
108 s->seekp(0, std::ios_base::end);
109 std::streampos end_pos = s->tellp();
110 s->seekp(pos, std::ios_base::beg);
111 return end_pos - begin_pos;
116 std::memcpy(&pos->
cookie[0], &spos,
sizeof(std::streampos));
122 std::memcpy(&spos, &pos->
cookie[0],
sizeof(std::streampos));
126 template<
typename STREAM>
129 copy_cpp_streampos(pos, static_cast<STREAM*>(cookie)->tellg());
133 template<
typename STREAM>
136 static_cast<STREAM*
>(cookie)->seekg(to_cpp_streampos(pos));
140 template<
typename STREAM>
143 copy_cpp_streampos(pos, static_cast<STREAM*>(cookie)->tellp());
147 template<
typename STREAM>
148 static int ostream_cpp_set_pos(
void* cookie,
const gmio_streampos* pos)
150 static_cast<STREAM*
>(cookie)->seekp(to_cpp_streampos(pos));
154 template<
typename STREAM>
155 void stream_cpp_init_common(STREAM* s,
gmio_stream* stream)
159 stream->
func_at_end = gmio::internal::stream_cpp_at_end<STREAM>;
160 stream->
func_error = gmio::internal::stream_cpp_error<STREAM>;
166 template<
typename CHAR,
typename TRAITS>
169 typedef std::basic_istream<CHAR, TRAITS> CppStream;
171 gmio::internal::stream_cpp_init_common(s, &stream);
172 stream.
func_size = gmio::internal::istream_cpp_size<CppStream>;
173 stream.
func_read = gmio::internal::istream_cpp_read<CppStream>;
174 stream.
func_get_pos = gmio::internal::istream_cpp_get_pos<CppStream>;
175 stream.
func_set_pos = gmio::internal::istream_cpp_set_pos<CppStream>;
179 template<
typename CHAR,
typename TRAITS>
182 typedef std::basic_ostream<CHAR, TRAITS> CppStream;
184 gmio::internal::stream_cpp_init_common(s, &stream);
185 stream.
func_size = gmio::internal::ostream_cpp_size<CppStream>;
186 stream.
func_write = gmio::internal::ostream_cpp_write<CppStream>;
187 stream.
func_get_pos = gmio::internal::ostream_cpp_get_pos<CppStream>;
188 stream.
func_set_pos = gmio::internal::ostream_cpp_set_pos<CppStream>;
gmio_stream gmio_istream_cpp(std::basic_istream< CHAR, TRAITS > *s)
Returns a gmio_stream for C++ input stream (cookie will hold s )
#define GMIO_INLINE
Expands to the C compiler specific inline keyword (if any)
Definition: global.h:156
int(* func_get_pos)(void *cookie, struct gmio_streampos *pos)
Pointer on a function that retrieves the current position in the stream.
Definition: stream.h:125
bool(* func_at_end)(void *cookie)
Pointer on a function that checks end-of-stream indicator.
Definition: stream.h:79
size_t(* func_write)(void *cookie, const void *ptr, size_t size, size_t count)
Pointer on a function that writes block of data to stream.
Definition: stream.h:115
gmio_stream gmio_ostream_cpp(std::basic_ostream< CHAR, TRAITS > *s)
Returns a gmio_stream for C++ output stream (cookie will hold s )
int(* func_set_pos)(void *cookie, const struct gmio_streampos *pos)
Pointer on a function that restores the current position in the stream to pos.
Definition: stream.h:133
size_t(* func_read)(void *cookie, void *ptr, size_t size, size_t count)
Pointer on a function that reads block of data from stream.
Definition: stream.h:102
Specifies a position within a stream.
Definition: streampos.h:41
int64_or_long gmio_streamsize_t
Type able to represent the size(in bytes) of a stream.
Definition: stream.h:44
uint8_t cookie[GMIO_STREAMPOS_COOKIE_SIZE]
Stores the actual(concrete) stream position object.
Definition: streampos.h:44
Stream that can get input from an arbitrary data source or can write output to an arbitrary data sink...
Definition: stream.h:65
Global declarations for the support module.
void * cookie
Opaque pointer on the user stream, passed as first argument to hook functions.
Definition: stream.h:69
int(* func_error)(void *cookie)
Pointer on a function that checks error indicator.
Definition: stream.h:89
gmio_streamsize_t(* func_size)(void *cookie)
Pointer on a function that returns the size(in bytes) of the stream.
Definition: stream.h:118
struct gmio_stream gmio_stream_null()
Returns a null stream.