Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

resolver.h

Go to the documentation of this file.
00001 // -*- Socket Streams -*-
00002 
00003 // Copyright (C) 2003, 2004, 2005 Pedro LamarĂ£o <pedro.lamarao@mndfck.org>
00004 
00005 // This file is part of the Socket Streams Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU Lesser General Public License as published by the
00008 // Free Software Foundation; either version 2.1 of the License
00009 // or (at your option) any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU Lesser General Public License for more details.
00015 
00016 // You should have received a copy of the GNU Lesser General Public License along
00017 // with this library; see the file COPYING. If not, write to the Free
00018 // Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
00019 // USA
00020 
00066 #ifndef SOCKETSTREAM_RESOLVER_H
00067 #define SOCKETSTREAM_RESOLVER_H
00068 
00069 #include <socketstream/config.h>
00070 
00071 #ifndef WIN32
00072 # include <netdb.h>
00073 #endif
00074 
00075 #include <socketstream/sockets_base.h>
00076 #include <socketstream/address_exception.h>
00077 
00078 namespace ss
00079 {
00080 
00086         class SOCKETSTREAM_API resolver_result_iterator
00087         {
00088         public:
00089 
00090                 // Assignable
00091                 resolver_result_iterator (resolver_result_iterator const& that)
00092                 : _M_node(that._M_node) {}
00093 
00094                 resolver_result_iterator&
00095                 operator= (resolver_result_iterator const& that) {
00096                         _M_node = that._M_node;
00097                         return *this;
00098                 }
00099 
00100                 // EqualityComparable
00101                 bool
00102                 operator== (resolver_result_iterator const& that) const {
00103                         return _M_node == that._M_node;
00104                 }
00105 
00106                 bool
00107                 operator!= (resolver_result_iterator const& that) const {
00108                         return _M_node != that._M_node;
00109                 }
00110 
00111                 // DefaultConstructible
00112                 resolver_result_iterator () : _M_node(0) {}
00113 
00114                 // TrivialIterator
00115 
00116                 addrinfo const&
00117                 operator* () const {
00118                         return *_M_node;
00119                 }
00120 
00121                 addrinfo const*
00122                 operator-> () {
00123                         return _M_node;
00124                 }
00125 
00126                 // InputIterator
00127                 resolver_result_iterator&
00128                 operator++ () {
00129                         _M_node = _M_node->ai_next;
00130                         return *this;
00131                 }
00132 
00133                 resolver_result_iterator
00134                 operator++ (int) {
00135                         resolver_result_iterator i(_M_node);
00136                         _M_node = _M_node->ai_next;
00137                         return i;
00138                 }
00139 
00140                 // etc.
00141 
00142                 resolver_result_iterator (addrinfo* node) : _M_node(node) {}
00143 
00144         private:
00145                 addrinfo* _M_node;
00146         };
00147 
00151         class SOCKETSTREAM_API resolver_result
00152         {
00153         public:
00154 
00155                 // Assignable
00156                 resolver_result () : _M_head(0) {}
00157 
00158                 resolver_result (resolver_result const& that)
00159                 : _M_head(that._M_head) {}
00160 
00161                 resolver_result&
00162                 operator= (resolver_result const& that) {
00163                         _M_head = that._M_head;
00164                         return *this;
00165                 }
00166 
00167                 // Container
00168                 typedef addrinfo* value_type;
00169                 typedef resolver_result_iterator iterator;
00170                 typedef resolver_result_iterator const_iterator;
00171                 typedef value_type& reference;
00172                 typedef value_type const& const_reference;
00173                 typedef value_type* pointer;
00174                 typedef size_t difference_type;
00175                 typedef size_t size_type;
00176 
00177                 iterator begin () { return iterator(_M_head); }
00178 
00179                 const_iterator begin () const {
00180                         return const_iterator(_M_head);
00181                 }
00182 
00183                 iterator end () { return iterator(); }
00184 
00185                 const_iterator end () const { return const_iterator(); }
00186 
00187                 size_type size () const {
00188                         size_type result = 0;
00189                         for (addrinfo* i = _M_head; i != 0; i = i->ai_next)
00190                                 ++result;
00191                         return result;
00192                 }
00193 
00194                 size_type max_size () const { return this->size(); }
00195 
00196                 bool empty () const { return (_M_head == 0); }
00197 
00198                 void swap (resolver_result& that) {
00199                         addrinfo* tmp = _M_head;
00200                         _M_head = that._M_head;
00201                         that._M_head = tmp;
00202                 }
00203 
00204                 // EqualityComparable
00205                 bool
00206                 operator== (resolver_result const& that) const {
00207                         if (this->size() != that.size())
00208                                 return false;
00209                         // FIXME: Every element must be equal.
00210                 }
00211 
00212                 bool
00213                 operator!= (resolver_result const& that) const {
00214                         return (*this) != that;
00215                 }
00216 
00217                 // LessThanComparable
00218                 bool
00219                 operator< (resolver_result const& that) const {
00220                         return (*this) != that; // FIXME
00221                 }
00222 
00223                 bool
00224                 operator> (resolver_result const& that) const {
00225                         return *this < that ? false : true;
00226                 }
00227 
00228                 bool
00229                 operator<= (resolver_result const& that) const {
00230                         return *this < that or *this == that;
00231                 }
00232 
00233                 bool
00234                 operator>= (resolver_result const& that) const {
00235                         return *this > that or *this == that;
00236                 }
00237 
00238                 // ForwardContainer
00239 
00240                 // etc.
00241                 resolver_result (addrinfo* head) : _M_head(head) {}
00242 
00243                 ~resolver_result () {
00244                         if (_M_head)
00245                                 ::freeaddrinfo(_M_head);
00246                 }
00247 
00248         private:
00249                 addrinfo* _M_head;
00250         };
00251 
00255         class SOCKETSTREAM_API resolver
00256         {
00257         public:
00258 
00264                 resolver ();
00265 
00271                 ~resolver ();
00272 
00278                 resolver& family (sockets_base::family value);
00279 
00285                 resolver& style (sockets_base::style value);
00286 
00292                 resolver& flags (int value);
00293 
00299                 void reset ();
00300 
00309                 void resolve (char const* host, char const* service);
00310 
00316                 resolver_result const& result () const { return _M_result; }
00317 
00318         private:
00319 
00320                 // Not CopyConstructible.
00321                 resolver (resolver const&);
00322 
00323                 // Not Assignable.
00324                 resolver& operator= (resolver const&);
00325 
00326                 void initialize_hint ();
00327 
00328                 void reset_hint ();
00329 
00330                 addrinfo* _M_hint;
00331 
00332                 resolver_result _M_result;
00333 
00334         };
00335 
00336 }
00337 
00338 #endif // SOCKETSTREAM_RESOLVER_H

Generated on Sat May 21 21:25:32 2005 for Socket Streams Library by  doxygen 1.4.3