sockstream.h

Go to the documentation of this file.
00001 /*
00002     socklibpp library
00003     Copyright (C) 2005  Daniel K. O. <danielosmari at users.sf.net>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 
00021 
00022 #ifndef SOCKSTREAM_H
00023 #define SOCKSTREAM_H
00024 
00025 
00026 
00033 #include "sockbuf.h"
00034 
00035 #include <iosfwd>
00036 
00037 
00038 namespace socklibpp {
00039 
00040 /* TODO (DanielKO#1#): Add exceptions to these high-level classes */
00041 
00042 
00057 template <typename _CharT, typename _Traits = std::char_traits<_CharT> >
00058 class basic_sockstream : public std::basic_iostream<_CharT, _Traits>
00059 {
00060 public:
00061         // Types:
00062         typedef _CharT                                  char_type;
00063         typedef _Traits                                 traits_type;
00064 
00065         typedef typename traits_type::int_type          int_type;
00066         typedef typename traits_type::pos_type          pos_type;
00067         typedef typename traits_type::off_type          off_type;
00068 
00069 private:
00070         // Non-standard Types:
00071         typedef basic_sockbuf<_CharT, _Traits>          __sockbuf_type;
00072         typedef std::basic_iostream<char_type, traits_type>  __iostream_type;
00073         
00074         __sockbuf_type  _M_sockbuf;
00075 
00076 public:
00077         basic_sockstream()
00078         {
00079                 this->init(&_M_sockbuf);
00080         }
00081 
00082 
00083         explicit
00084         basic_sockstream(const sock& sock_)
00085         : _M_sockbuf(sock_)
00086         {
00087                 this->init(&_M_sockbuf);
00088         }
00089 
00095         explicit
00096         basic_sockstream(const addr_in sin_)
00097         : _M_sockbuf(sin_)
00098         {
00099                 this->init(&_M_sockbuf);
00100         }
00101         
00102         virtual ~basic_sockstream()
00103         { }
00104 
00106         bool is_connected() const throw() { return _M_sockbuf.is_connected(); }
00107         
00109         bool
00110         connect(const addr_in& sin_)
00111         {
00112                 return _M_sockbuf.connect(sin_);
00113         }
00114         
00116         bool
00117         connect(const std::string& addr_, uint16_t port_)
00118         {
00119                 return _M_sockbuf.connect(addr_, port_);
00120         }
00121         
00123         bool
00124         close()
00125         {
00126                 return _M_sockbuf.close();
00127         }
00128 };
00129 
00130 
00131 
00138 typedef basic_sockstream<char> sockstream;
00139 
00140 
00141 
00142 
00148 std::ostream& crlf(std::ostream& o_)
00149 {
00150         o_.put(o_.widen('\x00d'));
00151         o_.put(o_.widen('\x00a'));
00152         o_.flush();
00153         return o_;
00154 }
00155 
00156 
00162 std::ostream& lfcr(std::ostream& o_)
00163 {
00164         o_.put(o_.widen('\x00a'));
00165         o_.put(o_.widen('\x00d'));
00166         o_.flush();
00167         return o_;
00168 }
00169 
00170 } // namespace socklibpp
00171 
00172 
00173 
00174 #endif

Generated on Thu Jan 18 19:26:35 2007 for socklib++ by  doxygen 1.5.1