38 # error C++ compiler required
41 #ifndef GMIO_SUPPORT_STREAM_CPP_H
42 #define GMIO_SUPPORT_STREAM_CPP_H
45 #include "../gmio_core/stream.h"
51 template<
typename CHAR,
typename TRAITS>
55 template<
typename CHAR,
typename TRAITS>
69 template<
typename STREAM>
70 bool stream_cpp_at_end(
void* cookie)
72 STREAM* s =
static_cast<STREAM*
>(cookie);
76 template<
typename STREAM>
77 int stream_cpp_error(
void* cookie)
79 STREAM* s =
static_cast<STREAM*
>(cookie);
83 template<
typename STREAM>
84 size_t istream_cpp_read(
85 void* cookie,
void* ptr,
size_t item_size,
size_t item_count)
87 STREAM* s =
static_cast<STREAM*
>(cookie);
88 s->read(static_cast<char*>(ptr), item_size * item_count);
89 return static_cast<size_t>(s->gcount() / item_size);
92 template<
typename STREAM>
93 size_t ostream_cpp_write(
94 void* cookie,
const void* ptr,
size_t item_size,
size_t item_count)
96 STREAM* s =
static_cast<STREAM*
>(cookie);
97 s->write(static_cast<const char*>(ptr), item_size * item_count);
99 return item_size * item_count;
102 template<
typename STREAM>
105 STREAM* s =
static_cast<STREAM*
>(cookie);
106 std::streampos pos = s->tellg();
107 s->seekg(0, std::ios_base::beg);
108 std::streampos begin_pos = s->tellg();
109 s->seekg(0, std::ios_base::end);
110 std::streampos end_pos = s->tellg();
111 s->seekg(pos, std::ios_base::beg);
112 return end_pos - begin_pos;
115 template<
typename STREAM>
118 STREAM* s =
static_cast<STREAM*
>(cookie);
119 std::streampos pos = s->tellp();
120 s->seekp(0, std::ios_base::beg);
121 std::streampos begin_pos = s->tellp();
122 s->seekp(0, std::ios_base::end);
123 std::streampos end_pos = s->tellp();
124 s->seekp(pos, std::ios_base::beg);
125 return end_pos - begin_pos;
130 std::memcpy(&pos->
cookie[0], &spos,
sizeof(std::streampos));
136 std::memcpy(&spos, &pos->
cookie[0],
sizeof(std::streampos));
140 template<
typename STREAM>
143 copy_cpp_streampos(pos, static_cast<STREAM*>(cookie)->tellg());
147 template<
typename STREAM>
150 static_cast<STREAM*
>(cookie)->seekg(to_cpp_streampos(pos));
154 template<
typename STREAM>
157 copy_cpp_streampos(pos, static_cast<STREAM*>(cookie)->tellp());
161 template<
typename STREAM>
162 static int ostream_cpp_set_pos(
void* cookie,
const gmio_streampos* pos)
164 static_cast<STREAM*
>(cookie)->seekp(to_cpp_streampos(pos));
168 template<
typename STREAM>
169 void stream_cpp_init_common(STREAM* s,
gmio_stream* stream)
173 stream->
func_at_end = gmio::internal::stream_cpp_at_end<STREAM>;
174 stream->
func_error = gmio::internal::stream_cpp_error<STREAM>;
180 template<
typename CHAR,
typename TRAITS>
183 typedef std::basic_istream<CHAR, TRAITS> CppStream;
185 gmio::internal::stream_cpp_init_common(s, &stream);
186 stream.
func_size = gmio::internal::istream_cpp_size<CppStream>;
187 stream.
func_read = gmio::internal::istream_cpp_read<CppStream>;
188 stream.
func_get_pos = gmio::internal::istream_cpp_get_pos<CppStream>;
189 stream.
func_set_pos = gmio::internal::istream_cpp_set_pos<CppStream>;
193 template<
typename CHAR,
typename TRAITS>
196 typedef std::basic_ostream<CHAR, TRAITS> CppStream;
198 gmio::internal::stream_cpp_init_common(s, &stream);
199 stream.
func_size = gmio::internal::ostream_cpp_size<CppStream>;
200 stream.
func_write = gmio::internal::ostream_cpp_write<CppStream>;
201 stream.
func_get_pos = gmio::internal::ostream_cpp_get_pos<CppStream>;
202 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:170
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:139
bool(* func_at_end)(void *cookie)
Pointer on a function that checks end-of-stream indicator.
Definition: stream.h:93
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:129
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:147
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:116
Specifies a position within a stream.
Definition: streampos.h:55
int64_or_long gmio_streamsize_t
Type able to represent the size(in bytes) of a stream.
Definition: stream.h:58
uint8_t cookie[GMIO_STREAMPOS_COOKIE_SIZE]
Stores the actual(concrete) stream position object.
Definition: streampos.h:58
Stream that can get input from an arbitrary data source or can write output to an arbitrary data sink...
Definition: stream.h:79
Global declarations for the support module.
void * cookie
Opaque pointer on the user stream, passed as first argument to hook functions.
Definition: stream.h:83
int(* func_error)(void *cookie)
Pointer on a function that checks error indicator.
Definition: stream.h:103
gmio_streamsize_t(* func_size)(void *cookie)
Pointer on a function that returns the size(in bytes) of the stream.
Definition: stream.h:132
struct gmio_stream gmio_stream_null()
Returns a null stream.