rvm 1.08
|
00001 #ifndef __estring_h__ 00002 #define __estring_h__ 00003 00004 #include <string> 00005 00006 #include "asserts.h" 00007 #include "types.h" 00008 00009 /* TODO: Make estring more OO 00010 00011 1) Make an estring_value base class 00012 2) Make an estring_value class for each type that inherits from the base 00013 class 00014 3) Revamp estring to use smart pointers to allocate the correct version of 00015 estring value based on the value being stored. 00016 */ 00017 00018 class estring_value 00019 { 00020 public: 00021 typedef union { 00022 unsigned int ui; 00023 int i; 00024 unsigned short us; 00025 short s; 00026 unsigned long ul; 00027 long l; 00028 unsigned long long ull; 00029 long long ll; 00030 char const * char_ptr; 00031 void * void_ptr; 00032 float f; 00033 double d; 00034 } value_type; 00035 00036 estring_value(); 00037 00038 value_type value; 00039 00040 void clear(void); 00041 estring_value& operator=(const estring_value& a_estring_value); 00042 00043 }; 00044 00045 /** An extended string class. 00046 00047 Estring is a derivative of std::string with extra functionality added in 00048 order to fascilitate (a) the conversion to strings from other types, (b) 00049 special formatting of those converted strings, and (c) the conversion back 00050 from a string to some other type. 00051 */ 00052 class estring : public std::string 00053 { 00054 public: 00055 /** The type from which estring inherits, std::string. */ 00056 typedef std::string value_type; 00057 00058 /** The size type. */ 00059 typedef value_type::size_type size_type; 00060 00061 /** Alignment values for formatted strings. */ 00062 enum alignment { 00063 /** Left-justified */ 00064 left, 00065 /** Right-justified */ 00066 right, 00067 /** Centered */ 00068 center 00069 }; 00070 00071 /** The type last assigned. */ 00072 enum set_from_type { 00073 /** std::string */ 00074 type_string, 00075 /** unsigned int */ 00076 type_unsigned_int, 00077 /** int */ 00078 type_int, 00079 /** unsigned short*/ 00080 type_unsigned_short, 00081 /** short */ 00082 type_short, 00083 /** unsigned long */ 00084 type_unsigned_long, 00085 /** long */ 00086 type_long, 00087 /** unsigned long long */ 00088 type_unsigned_long_long, 00089 /** long long */ 00090 type_long_long, 00091 /** float */ 00092 type_float, 00093 /** double */ 00094 type_double, 00095 /** char* */ 00096 type_char_ptr, 00097 /** void* */ 00098 type_void_ptr, 00099 /** unknown */ 00100 type_unknown 00101 }; 00102 00103 void init(void); 00104 void clear(void); 00105 void reset(void); 00106 estring(); 00107 00108 // 00109 // String Formatting 00110 // 00111 size_type width(const size_type a_l); 00112 size_type width(void) const; 00113 alignment align(const alignment a_alignment); 00114 alignment align(void) const; 00115 char left_fillchar(const char a_char); 00116 char left_fillchar(void) const; 00117 char right_fillchar(const char a_char); 00118 char right_fillchar(void) const; 00119 void fillchar(const char a_char); 00120 value_type fmt_str(void); 00121 value_type fmt_str( 00122 const size_type a_width, 00123 const alignment a_alignment, 00124 const char a_left_fill, 00125 const char a_right_fill 00126 ); 00127 00128 // String formatting for float and double 00129 size_type precision(size_type a_p); 00130 size_type precision(void) const; 00131 00132 // Formatting for conversion from different bases 00133 const unsigned int base(const unsigned int a_base); 00134 const unsigned int base(void) const; 00135 00136 // estring 00137 const set_from_type& get_from_type(void) const; 00138 const estring_value& get_from_value(void) const; 00139 estring(const estring& a_estr); 00140 estring& assign(const estring& a_estr); 00141 estring& operator=(const estring& a_estr); 00142 estring& lower(void); 00143 estring& upper(void); 00144 00145 // char 00146 estring(const char a_char); 00147 estring& assign(const char a_char); 00148 estring& operator=(const char a_char); 00149 00150 // string 00151 estring(const value_type& a_string); 00152 estring& assign(const value_type& a_string); 00153 estring& operator=(const value_type& a_string); 00154 00155 // unsigned int 00156 estring(const unsigned int a_int); 00157 estring& assign(const unsigned int a_int); 00158 estring& operator=(const unsigned int a_int); 00159 operator unsigned int() const; 00160 00161 // int 00162 estring(const int a_int); 00163 estring& assign(const int a_int); 00164 estring& operator=(const int a_int); 00165 operator int() const; 00166 00167 // unsigned short 00168 estring(const unsigned short a_short); 00169 estring& assign(const unsigned short a_short); 00170 estring& operator=(const unsigned short a_short); 00171 operator unsigned short() const; 00172 00173 // short 00174 estring(const short a_short); 00175 estring& assign(const short a_short); 00176 estring& operator=(const short a_short); 00177 operator short() const; 00178 00179 // unsigned long 00180 estring(const unsigned long a_long); 00181 estring& assign(const unsigned long a_long); 00182 estring& operator=(const unsigned long a_long); 00183 operator unsigned long() const; 00184 00185 // long 00186 estring(const long a_long); 00187 estring& assign(const long a_long); 00188 estring& operator=(const long a_long); 00189 operator long() const; 00190 00191 // unsigned long long 00192 estring(const unsigned long long a_long); 00193 estring& assign(const unsigned long long a_long); 00194 estring& operator=(const unsigned long long a_long); 00195 operator unsigned long long() const; 00196 00197 // long long 00198 estring(const long long a_long); 00199 estring& assign(const long long a_long); 00200 estring& operator=(const long long a_long); 00201 operator long long() const; 00202 00203 // char* 00204 estring(char const * a_ptr); 00205 estring& assign(char const * a_ptr); 00206 estring& operator=(char const * a_ptr); 00207 operator char const *() const; 00208 00209 // void* 00210 estring(void* const a_ptr); 00211 estring& assign(void* const a_ptr); 00212 estring& operator=(void* const a_ptr); 00213 operator void*() const; 00214 00215 // float 00216 estring(const float a_float); 00217 estring& assign(const float a_float); 00218 estring& operator=(const float a_float); 00219 estring(const float a_float, unsigned a_precision, 00220 unsigned int a_base = 10); 00221 estring& assign(const float a_float, unsigned a_precision, 00222 unsigned int a_base = 10); 00223 operator float() const; 00224 00225 // double 00226 estring(const double a_double); 00227 estring& assign(const double a_double); 00228 estring& operator=(const double a_double); 00229 estring(const double a_double, unsigned a_precision, 00230 unsigned int a_base = 10); 00231 estring& assign(const double a_double, unsigned a_precision, 00232 unsigned int a_base = 10); 00233 operator double() const; 00234 00235 private: 00236 /** The current fractional number precision */ 00237 size_type m_precision; 00238 /** The current numerical base */ 00239 unsigned int m_base; 00240 /** The alphabet used for any base from 2 to 36 */ 00241 static const char *m_alphabet; 00242 /** The length of the alphabet */ 00243 static const size_t m_alphabet_len; 00244 /** The current formatting width */ 00245 size_type m_width; 00246 /** The current formatting alignment */ 00247 alignment m_alignment; 00248 /** The current left-hand fill character */ 00249 char m_left_fillchar; 00250 /** The current right-hand fill character */ 00251 char m_right_fillchar; 00252 /** The current value type */ 00253 set_from_type m_type; 00254 /** The current value */ 00255 estring_value m_value; 00256 00257 template <class T> 00258 void T_fraction_to_strings(const T& a_t, 00259 value_type& a_ws, value_type& a_fs); 00260 00261 template <class T> 00262 void T_integral_to_string(const T& a_t, value_type& a_str); 00263 00264 template <class T> 00265 void T_string_to_integral(const value_type& a_str, T& a_t) const; 00266 00267 template <class T> 00268 void T_string_to_signed_integral(const value_type& a_str, T& a_t) const; 00269 00270 template <class T> 00271 void T_string_to_fractional(const value_type& a_str, T& a_t) const; 00272 }; 00273 00274 #endif