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

byte.h

Go to the documentation of this file.
00001 // -*- Socket Streams -*-
00002 
00003 // Copyright (C) 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 
00030 #include <string>
00031 
00032 namespace ss {
00033 
00039         class byte {
00040         public:
00041 
00042                 byte () : value(0) {}
00043 
00044                 byte (byte const& that) : value(that.value) {}
00045 
00046                 byte (int i) : value(static_cast<unsigned char>(i)) {}
00047 
00048                 byte
00049                 operator= (byte const& that) {
00050                         value = that.value;
00051                         return *this;
00052                 }
00053 
00054                 bool
00055                 operator== (byte const& that) const {
00056                         return value == that.value;
00057                 }
00058 
00059                 bool
00060                 operator< (byte const& that) const {
00061                         return value < that.value;
00062                 }
00063 
00064                 operator unsigned int() const {
00065                         return static_cast<unsigned int>(value);
00066                 }
00067 
00068         private:
00069                 unsigned char value;
00070         };
00071 
00072 }
00073 
00074 namespace std {
00075 
00076         template <>
00077         struct char_traits<ss::byte> {
00078 
00079                 typedef ss::byte char_type;
00080 
00081                 typedef unsigned int int_type;
00082 
00083                 typedef streampos pos_type;
00084                 typedef streamoff off_type;
00085                 typedef mbstate_t state_type;
00086 
00087                 static void
00088                 assign (char_type& c1, char_type const& c2) {
00089                         c1 = c2;
00090                 }
00091 
00092                 static bool
00093                 eq (char_type const& c1, char_type const& c2) {
00094                         return c1 == c2;
00095                 }
00096 
00097                 static bool
00098                 lt (char_type const& c1, char_type const& c2) {
00099                         return c1 < c2;
00100                 }
00101 
00102                 static int
00103                 compare (char_type const* s1, char_type const* s2, size_t n) {
00104                         for (size_t i = 0; i < n; ++i)
00105                                 if (!eq(s1[i], s2[i]))
00106                                         return lt(s1[i], s2[i]) ? -1 : 1;
00107                         return 0;
00108                 }
00109 
00110                 static size_t
00111                 length (char_type const* s) {
00112                         char_type const* p = s;
00113                         while (*p) ++p;
00114                         return (p - s);
00115                 }
00116 
00117                 static char_type const*
00118                 find (char_type const* s, size_t n, char_type const& c) {
00119                         for (char_type const* p = s; size_t(p - s) < n; ++p)
00120                                 if (*p == c)
00121                                         return p;
00122                         return 0;
00123                 }
00124 
00125                 static char_type*
00126                 move (char_type* s1, char_type const* s2, size_t n) {
00127                         return reinterpret_cast<char_type*>(
00128                                 memmove(s1, s2, n * sizeof(char_type))
00129                         );
00130                 }
00131 
00132                 static char_type*
00133                 copy (char_type* s1, char_type const* s2, size_t n) {
00134                         return reinterpret_cast<char_type*>(
00135                                 memcpy(s1, s2, n * sizeof(char_type))
00136                         );
00137                 }
00138 
00139                 static char_type*
00140                 assign (char_type* s, size_t n, char_type const& c) {
00141                         for  (char_type* p = s; p < s + n; ++p)
00142                                 assign(*p, c);
00143                         return s;
00144                 }
00145 
00146                 static char_type
00147                 to_char_type (int_type const& c) { return char_type(c); }
00148 
00149                 static int_type
00150                 to_int_type (char_type const& c) { return int_type(c); }
00151 
00152                 static bool
00153                 eq_int_type (int_type const& c1, int_type const& c2) {
00154                         return c1 == c2;
00155                 }
00156 
00157                 static int_type
00158                 eof () { return static_cast<int_type>(-1); }
00159 
00160                 static int_type
00161                 not_eof (int_type const& c) {
00162                         return eq_int_type(c, eof()) ? int_type(0) : c;
00163                 }
00164 
00165         };
00166 
00167 }
00168 

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