// -*- C++ -*- /*****************************************************************************\ * Copyright (c) 2004 Mark Aylett * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * * "Software"), to deal in the Software without restriction, including * * without limitation the rights to use, copy, modify, merge, publish, * * distribute, sublicense, and/or sell copies of the Software, and to permit * * persons to whom the Software is furnished to do so, subject to the * * following conditions: * * * * The above copyright notice and this permission notice shall be included * * in all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * * USE OR OTHER DEALINGS IN THE SOFTWARE. * \*****************************************************************************/ /** * \file mar_stream_cpp.h * \brief TODO */ #ifndef INCLUDED_MAR_STREAM_CPP #define INCLUDED_MAR_STREAM_CPP #ifndef INCLUDED_MAR_STREAMBUF_CPP #include "mar_streambuf_cpp.h" #endif // INCLUDED_MAR_STREAMBUF_CPP #ifndef INCLUDED_MAR_TYPES_CPP #include "mar_types_cpp.h" #endif // INCLUDED_MAR_TYPES_CPP #if !defined(__GNUC__) || __GNUC__ < 3 #define MAR_BASIC_ISTREAM basic_istream #define MAR_BASIC_OSTREAM basic_ostream #define MAR_BASIC_IOSTREAM basic_iostream #else // __GNUC__ >= 3 /** * See GCC Bug C++/16171 */ #define MAR_BASIC_ISTREAM basic_istream_ #define MAR_BASIC_OSTREAM basic_ostream_ #define MAR_BASIC_IOSTREAM basic_iostream_ #endif // __GNUC__ >= 3 namespace mar { const std::streamsize bufsize(1024); const struct memory_type { } memory = memory_type(); template > class MAR_BASIC_ISTREAM : public std::basic_istream { public: typedef charT char_type; typedef char_traitsT char_traits_type; typedef basic_streambuf streambuf_type; private: typedef std::basic_istream base_type; streambuf_type streambuf_; public: explicit MAR_BASIC_ISTREAM(const handle& h, std::streamsize size = bufsize) : base_type(0), streambuf_(h, std::ios_base::in, bufsize) { rdbuf(&streambuf_); if (!streambuf_.is_open()) base_type::setstate(std::ios_base::failbit); } explicit MAR_BASIC_ISTREAM(const char* path, int flags = rdonly, mode_t mode = 0444, std::streamsize size = bufsize) : base_type(0), streambuf_(archive::open(path, flags, mode), std::ios_base::in, bufsize) { rdbuf(&streambuf_); if (!streambuf_.is_open()) base_type::setstate(std::ios_base::failbit); } int close() { return streambuf_.close(); } bool is_open() const { return streambuf_.is_open(); } archive retain() { return streambuf_.retain(); } const archive& retain() const { return streambuf_.retain(); } }; template > class MAR_BASIC_OSTREAM : public std::basic_ostream { public: typedef charT char_type; typedef char_traitsT char_traits_type; typedef basic_streambuf streambuf_type; private: typedef std::basic_ostream base_type; streambuf_type streambuf_; public: explicit MAR_BASIC_OSTREAM(const handle& h, std::streamsize size = bufsize) : base_type(0), streambuf_(h, std::ios_base::out, bufsize) { rdbuf(&streambuf_); if (!streambuf_.is_open()) base_type::setstate(std::ios_base::failbit); } explicit MAR_BASIC_OSTREAM(const char* path, int flags = wronly | creat, mode_t mode = 0664, std::streamsize size = bufsize) : base_type(0), streambuf_(archive::open(path, flags, mode), std::ios_base::out, bufsize) { rdbuf(&streambuf_); if (!streambuf_.is_open()) base_type::setstate(std::ios_base::failbit); } int close() { return streambuf_.close(); } bool is_open() const { return streambuf_.is_open(); } archive retain() { return streambuf_.retain(); } const archive& retain() const { return streambuf_.retain(); } }; template > class MAR_BASIC_IOSTREAM :public std::basic_iostream { public: typedef charT char_type; typedef char_traitsT char_traits_type; typedef basic_streambuf streambuf_type; private: typedef std::basic_iostream base_type; streambuf_type streambuf_; public: explicit MAR_BASIC_IOSTREAM(const memory_type&, std::streamsize size = bufsize) : base_type(0), streambuf_(archive::create(), std::ios_base::in | std::ios_base::out, size) { rdbuf(&streambuf_); if (!streambuf_.is_open()) base_type::setstate(std::ios_base::failbit); } explicit MAR_BASIC_IOSTREAM(const handle& h, std::streamsize size = bufsize) : base_type(0), streambuf_(h, std::ios_base::in | std::ios_base::out, size) { rdbuf(&streambuf_); if (!streambuf_.is_open()) base_type::setstate(std::ios_base::failbit); } explicit MAR_BASIC_IOSTREAM(const char* path, int flags = rdwr | creat, mode_t mode = 0664, std::streamsize size = bufsize) : base_type(0), streambuf_(archive::open(path, flags, mode), std::ios_base::in | std::ios_base::out, size) { rdbuf(&streambuf_); if (!streambuf_.is_open()) base_type::setstate(std::ios_base::failbit); } int close() { return streambuf_.close(); } bool is_open() const { return streambuf_.is_open(); } archive retain() { return streambuf_.retain(); } const archive& retain() const { return streambuf_.retain(); } }; typedef MAR_BASIC_ISTREAM istream; typedef MAR_BASIC_OSTREAM ostream; typedef MAR_BASIC_IOSTREAM iostream; } #endif // INCLUDED_MAR_STREAM_CPP