Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
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 // Even faster autostring
72 #ifndef ELM_AUTOSTR_FAST_NO
73 
74 template <class T>
76  { return autostr << str << value; }
77 template <class T>
78 inline AutoString& operator<<(const String& str, const T& value)
79  { return autostr << str << value; }
80 
81 template <class T>
82 inline io::StringInput operator>>(const string& str, T& val)
83  { io::StringInput in(str); in >> val; return in; }
84 
85 template <class T>
87  { io::StringInput in(str); in >> val; return in; }
88 
89 #endif // ELM_AUTOSTR_FAST_NO
90 
91 } // elm
92 
93 #endif // ELM_STRING_AUTOSTRING_H
elm::autostr
AutoStringStartup autostr
Definition: string_AutoString.cpp:32
elm::operator<<
AutoString & operator<<(CString str, const T &value)
Definition: AutoString.h:75
elm::cout
io::Output cout
value
elm::string
String string
Definition: String.h:149
elm::AutoStringStartup
Definition: AutoString.h:57
elm::CString
Definition: CString.h:17
elm::AutoString::operator<<
AutoString & operator<<(const T &value)
Definition: AutoString.h:37
elm::t::in
typename type_info< T >::in_t in
Definition: type_info.h:283
elm::AutoStringStartup::operator<<
AutoString & operator<<(const T &value)
Definition: AutoString.h:60
elm::_
AutoStringStartup & _
Definition: debug_CrashHandler.cpp:232
elm
Definition: adapter.h:26
elm::AutoString
Definition: AutoString.h:34
elm::io::endl
const EOL endl
Definition: io_Output.cpp:880
elm::StringBuffer::toString
String toString()
Definition: StringBuffer.h:25
elm::String
Definition: String.h:30
elm::io::StringInput
Definition: StringInput.h:30
elm::operator>>
io::StringInput operator>>(const string &str, T &val)
Definition: AutoString.h:82
elm::str
string str(const char *s)
Definition: String.h:150