Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AutoString.h
1 /*
2  * $Id$
3  * AutoString and InputString class interfaces
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2007-09, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_STRING_AUTOSTRING_H
23 #define ELM_STRING_AUTOSTRING_H
24 
25 #include <elm/string/StringBuffer.h>
26 #include <elm/io/StringInput.h>
27 #ifdef ELM_TEST_AUTOSTR
28 # include <elm/io.h>
29 #endif
30 
31 namespace elm {
32 
33 // AutoString class
34 class AutoString {
35 public:
36  template <class T>
37  inline AutoString& operator<<(const T& value)
38  { buf << value; return *this; }
39 
40  inline operator const string(void) {
41  String result = buf.toString();
42  delete this;
43  return result;
44  }
45 
46  // Debugging
47  #ifdef ELM_TEST_AUTOSTR
48  inline AutoString(void) { cout << "allocated " << this << io::endl; }
49  inline ~AutoString(void) { cout << "fried " << this << io::endl; }
50  #endif
51 
52 private:
53  StringBuffer buf;
54 };
55 
56 // AutoStringStartup class
58 public:
59  template <class T>
60  inline AutoString& operator<<(const T& value) {
61  AutoString *str = new AutoString();
62  return (*str) << value;
63  }
64 };
65 
66 // autostr object
67 extern AutoStringStartup autostr;
68 extern AutoStringStartup &_;
69 
70 
71 // StringInput class
72 /*class StringInput {
73 public:
74  StringInput(const char *str);
75  StringInput(const string& str);
76  StringInput(cstring str);
77  ~StringInput(void);
78  inline StringInput(StringInput& _in) { in.setStream(_in.in.stream()); _in.in.setStream(io::in); }
79  inline StringInput& operator=(StringInput& _in) { in.setStream(_in.in.stream()); _in.in.setStream(io::in); return *this; }
80  template <class T> StringInput& operator>>(T& val) { in.operator>>(val); return *this; }
81 private:
82  io::Input in;
83 };*/
84 
85 
86 // Even faster autostring
87 #ifndef ELM_AUTOSTR_FAST_NO
88 
89 template <class T>
90 inline AutoString& operator<<(CString str, const T& value)
91  { return autostr << str << value; }
92 template <class T>
93 inline AutoString& operator<<(const String& str, const T& value)
94  { return autostr << str << value; }
95 
96 template <class T>
97 inline io::StringInput operator>>(const string& str, T& val)
98  { io::StringInput in(str); in >> val; return in; }
99 
100 template <class T>
101 inline io::StringInput operator>>(cstring str, T& val)
102  { io::StringInput in(str); in >> val; return in; }
103 
104 #endif // ELM_AUTOSTR_FAST_NO
105 
106 } // elm
107 
108 #endif // ELM_STRING_AUTOSTRING_H
Definition: CString.h:17
const char endl
Definition: Output.h:221
AutoString & operator<<(const T &value)
Definition: AutoString.h:37
sys::SystemInStream & in
Definition: system_SystemIO.cpp:95
AutoStringStartup & _
Definition: debug_CrashHandler.cpp:221
value_t value(CString name, int value)
Definition: rtti.h:40
String string
Definition: String.h:123
AutoStringStartup autostr
Definition: string_AutoString.cpp:32
AutoString & operator<<(const T &value)
Definition: AutoString.h:60
Definition: AutoString.h:57
Definition: String.h:31
AutoString & operator<<(CString str, const T &value)
Definition: AutoString.h:90
Definition: StringInput.h:30
Definition: AutoString.h:34
io::StringInput operator>>(const string &str, T &val)
Definition: AutoString.h:97
io::Output cout
String toString(void)
Definition: StringBuffer.h:37