rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
estring.h
Go to the documentation of this file.
1 #ifndef __estring_h__
2 #define __estring_h__
3 
4 #include <string>
5 
6 #include "asserts.h"
7 #include "types.h"
8 
9 /* TODO: Make estring more OO
10 
11  1) Make an estring_value base class
12  2) Make an estring_value class for each type that inherits from the base
13  class
14  3) Revamp estring to use smart pointers to allocate the correct version of
15  estring value based on the value being stored.
16  */
17 
19 {
20 public:
21  typedef union {
22  unsigned int ui;
23  int i;
24  unsigned short us;
25  short s;
26  unsigned long ul;
27  long l;
28  unsigned long long ull;
29  long long ll;
30  char const * char_ptr;
31  void * void_ptr;
32  float f;
33  double d;
34  } value_type;
35 
36  estring_value();
37 
39 
40  void clear(void);
41  estring_value& operator=(const estring_value& a_estring_value);
42 
43 };
44 
45 /** An extended string class.
46 
47  Estring is a derivative of std::string with extra functionality added in
48  order to fascilitate (a) the conversion to strings from other types, (b)
49  special formatting of those converted strings, and (c) the conversion back
50  from a string to some other type.
51  */
52 class estring : public std::string
53 {
54 public:
55  /** The type from which estring inherits, std::string. */
56  typedef std::string value_type;
57 
58  /** The size type. */
59  typedef value_type::size_type size_type;
60 
61  /** Alignment values for formatted strings. */
62  enum alignment {
63  /** Left-justified */
65  /** Right-justified */
67  /** Centered */
69  };
70 
71  /** The type last assigned. */
73  /** std::string */
75  /** unsigned int */
77  /** int */
79  /** unsigned short*/
81  /** short */
83  /** unsigned long */
85  /** long */
87  /** unsigned long long */
89  /** long long */
91  /** float */
93  /** double */
95  /** char* */
97  /** void* */
99  /** unknown */
101  };
102 
103  void init(void);
104  void clear(void);
105  void reset(void);
106  estring();
107 
108  //
109  // String Formatting
110  //
111  size_type width(const size_type a_l);
112  size_type width(void) const;
113  alignment align(const alignment a_alignment);
114  alignment align(void) const;
115  char left_fillchar(const char a_char);
116  char left_fillchar(void) const;
117  char right_fillchar(const char a_char);
118  char right_fillchar(void) const;
119  void fillchar(const char a_char);
120  value_type fmt_str(void);
122  const size_type a_width,
123  const alignment a_alignment,
124  const char a_left_fill,
125  const char a_right_fill
126  );
127 
128  // String formatting for float and double
130  size_type precision(void) const;
131 
132  // Formatting for conversion from different bases
133  const unsigned int base(const unsigned int a_base);
134  const unsigned int base(void) const;
135 
136  // estring
137  const set_from_type& get_from_type(void) const;
138  const estring_value& get_from_value(void) const;
139  estring(const estring& a_estr);
140  estring& assign(const estring& a_estr);
141  estring& operator=(const estring& a_estr);
142  estring& lower(void);
143  estring& upper(void);
144 
145  // char
146  estring(const char a_char);
147  estring& assign(const char a_char);
148  estring& operator=(const char a_char);
149 
150  // string
151  estring(const value_type& a_string);
152  estring& assign(const value_type& a_string);
153  estring& operator=(const value_type& a_string);
154 
155  // unsigned int
156  estring(const unsigned int a_int);
157  estring& assign(const unsigned int a_int);
158  estring& operator=(const unsigned int a_int);
159  operator unsigned int() const;
160 
161  // int
162  estring(const int a_int);
163  estring& assign(const int a_int);
164  estring& operator=(const int a_int);
165  operator int() const;
166 
167  // unsigned short
168  estring(const unsigned short a_short);
169  estring& assign(const unsigned short a_short);
170  estring& operator=(const unsigned short a_short);
171  operator unsigned short() const;
172 
173  // short
174  estring(const short a_short);
175  estring& assign(const short a_short);
176  estring& operator=(const short a_short);
177  operator short() const;
178 
179  // unsigned long
180  estring(const unsigned long a_long);
181  estring& assign(const unsigned long a_long);
182  estring& operator=(const unsigned long a_long);
183  operator unsigned long() const;
184 
185  // long
186  estring(const long a_long);
187  estring& assign(const long a_long);
188  estring& operator=(const long a_long);
189  operator long() const;
190 
191  // unsigned long long
192  estring(const unsigned long long a_long);
193  estring& assign(const unsigned long long a_long);
194  estring& operator=(const unsigned long long a_long);
195  operator unsigned long long() const;
196 
197  // long long
198  estring(const long long a_long);
199  estring& assign(const long long a_long);
200  estring& operator=(const long long a_long);
201  operator long long() const;
202 
203  // char*
204  estring(char const * a_ptr);
205  estring& assign(char const * a_ptr);
206  estring& operator=(char const * a_ptr);
207  operator char const *() const;
208 
209  // void*
210  estring(void* const a_ptr);
211  estring& assign(void* const a_ptr);
212  estring& operator=(void* const a_ptr);
213  operator void*() const;
214 
215  // float
216  estring(const float a_float);
217  estring& assign(const float a_float);
218  estring& operator=(const float a_float);
219  estring(const float a_float, unsigned a_precision,
220  unsigned int a_base = 10);
221  estring& assign(const float a_float, unsigned a_precision,
222  unsigned int a_base = 10);
223  operator float() const;
224 
225  // double
226  estring(const double a_double);
227  estring& assign(const double a_double);
228  estring& operator=(const double a_double);
229  estring(const double a_double, unsigned a_precision,
230  unsigned int a_base = 10);
231  estring& assign(const double a_double, unsigned a_precision,
232  unsigned int a_base = 10);
233  operator double() const;
234 
235 private:
236  /** The current fractional number precision */
238  /** The current numerical base */
239  unsigned int m_base;
240  /** The alphabet used for any base from 2 to 36 */
241  static const char *m_alphabet;
242  /** The length of the alphabet */
243  static const size_t m_alphabet_len;
244  /** The current formatting width */
246  /** The current formatting alignment */
248  /** The current left-hand fill character */
250  /** The current right-hand fill character */
252  /** The current value type */
254  /** The current value */
256 
257  template <class T>
258  void T_fraction_to_strings(const T& a_t,
259  value_type& a_ws, value_type& a_fs);
260 
261  template <class T>
262  void T_integral_to_string(const T& a_t, value_type& a_str);
263 
264  template <class T>
265  void T_string_to_integral(const value_type& a_str, T& a_t) const;
266 
267  template <class T>
268  void T_string_to_signed_integral(const value_type& a_str, T& a_t) const;
269 
270  template <class T>
271  void T_string_to_fractional(const value_type& a_str, T& a_t) const;
272 };
273 
274 #endif
static const size_t m_alphabet_len
The length of the alphabet.
Definition: estring.h:243
Left-justified.
Definition: estring.h:64
set_from_type m_type
The current value type.
Definition: estring.h:253
void clear(void)
Erase the string value.
Definition: estring.cc:414
Basic types definitions and templates.
unsigned short us
Definition: estring.h:24
An extended string class.
Definition: estring.h:52
value_type value
Definition: estring.h:38
void reset(void)
Erase and reinitialize.
Definition: estring.cc:421
void clear(void)
Definition: estring.cc:21
void T_string_to_integral(const value_type &a_str, T &a_t) const
Helper member template function to convert a string to an integral type.
Definition: estring.cc:197
const set_from_type & get_from_type(void) const
Retrieve the type of value being held by this estring.
Definition: estring.cc:810
estring & assign(const estring &a_estr)
Assignment for estring objects.
Definition: estring.cc:853
Right-justified.
Definition: estring.h:66
estring_value & operator=(const estring_value &a_estring_value)
Definition: estring.cc:26
char m_left_fillchar
The current left-hand fill character.
Definition: estring.h:249
unsigned long ul
Definition: estring.h:26
unsigned long long ull
Definition: estring.h:28
char m_right_fillchar
The current right-hand fill character.
Definition: estring.h:251
unsigned short
Definition: estring.h:80
void T_integral_to_string(const T &a_t, value_type &a_str)
Helper member template function to convert an integral type to an estring.
Definition: estring.cc:59
alignment align(void) const
Retrieve the set alignment for formatted strings.
Definition: estring.cc:486
static const char * m_alphabet
The alphabet used for any base from 2 to 36.
Definition: estring.h:241
char const * char_ptr
Definition: estring.h:30
Centered.
Definition: estring.h:68
unsigned long long
Definition: estring.h:88
alignment m_alignment
The current formatting alignment.
Definition: estring.h:247
size_type width(void) const
Retrieve the set width for formatted strings.
Definition: estring.cc:461
void init(void)
Initialize the estring object.
Definition: estring.cc:401
size_type m_precision
The current fractional number precision.
Definition: estring.h:237
value_type fmt_str(void)
Generate a formatted string.
Definition: estring.cc:688
char left_fillchar(void) const
Retrieve the fill character used to padd the left side of a formatted string.
Definition: estring.cc:620
estring & operator=(const estring &a_estr)
Assignment operator for estring objects.
Definition: estring.cc:878
void fillchar(const char a_char)
Set the fill character used for padding both the left and right side of a formatted string...
Definition: estring.cc:662
void T_string_to_fractional(const value_type &a_str, T &a_t) const
Helper member template function to convert a string to a fractional.
Definition: estring.cc:313
const estring_value & get_from_value(void) const
Retrieve the typeless_value being held by this estring.
Definition: estring.cc:821
alignment
Alignment values for formatted strings.
Definition: estring.h:62
set_from_type
The type last assigned.
Definition: estring.h:72
estring & lower(void)
Convert all characters to lowercase.
Definition: estring.cc:891
void T_fraction_to_strings(const T &a_t, value_type &a_ws, value_type &a_fs)
Helper member template function to convert a fractional type to an estring.
Definition: estring.cc:101
std::string value_type
The type from which estring inherits, std::string.
Definition: estring.h:56
size_type precision(void) const
Retrieve the set precision used in fractional conversions.
Definition: estring.cc:521
value_type::size_type size_type
The size type.
Definition: estring.h:59
estring()
Default constructor.
Definition: estring.cc:431
void T_string_to_signed_integral(const value_type &a_str, T &a_t) const
Helper member template function to convert a string to a signed integral.
Definition: estring.cc:277
estring & upper(void)
Convert all characters to uppercase.
Definition: estring.cc:908
estring_value m_value
The current value.
Definition: estring.h:255
std::string
Definition: estring.h:74
unsigned long
Definition: estring.h:84
size_type m_width
The current formatting width.
Definition: estring.h:245
char right_fillchar(void) const
Retrieve the fill character used to padd the right side of a formatted string.
Definition: estring.cc:651
const unsigned int base(void) const
Retrieve the base used in numeric conversions.
Definition: estring.cc:589
unsigned int m_base
The current numerical base.
Definition: estring.h:239