Garfield++ v1r0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
1#ifndef STRING_SWITCHING_H
2#define STRING_SWITCHING_H
3/*
4This is the file for indication which String class should be used by
5the wcpplib library. This library does not depend on very sophicticated
6operations with strings. The strings are used for inclusion of notations
7or character names in various objects and also for keeping the file names.
8The typical string operations used in wcpplib are
9initialization from characters array,
10convertion to characters array,
11initialization from integer number,
12and addition of strings.
13Such simple operations are either supported by any library or some of them
14can be easily added, as done below with STL.
15Since the wcpplib was started before the standard library was firmly
16established, and because there were many other libraries and it was not
17clear which of them will predominate,
18the source texts of wcpplib reference the string type by
19symbolic name "String" (which seem to be unexisting in all known libraries
20except GNU library),
21and this type can and must be redefined right here, in this file,
22to actual name of string class of actually applied library (except GNU library,
23for which it should not be redefined at all).
24
25There are currently 3 options.
26One is String class from gnu C++ library.
27(This library seems to be not supported anymore).
28Another is HepString from CLHEP library.
29(This collaboration have decided to eliminate strings in favour to std.)
30The last is standard library string.
31The standard library appears to be the most inconvenient (!) in supporting
32the strings. It does not support conversion from integers and long integers
33to strings, and it does not support the default conversion of strings to
34char*. Therefore when the support of other strings dissappeared,
35I had to edit the programs to pass to this "standard".
36The first type of conversions is performed by little function long_to_String()
37defined as inline below. The second type of conversions is performed
38in program with the use of macro USE_STLSTRING and c_str() member function
39like this:
40
41Example of program:
42#ifdef USE_STLSTRING
43 ifstream fh(hfile.c_str()); // use standard library
44#else
45 ifstream fh(hfile); // most of other libraries
46#endif
47
48Not convenient! Hope there was real reasons to forbid default conversions in
49standard library.
50
51
52Copyright (c) 2001 I. B. Smirnov
53
54Permission to use, copy, modify, distribute and sell this file
55and its documentation for any purpose is hereby granted without fee,
56provided that the above copyright notice, this permission notice,
57and notices about any modifications of the original text
58appear in all copies and in supporting documentation.
59It is provided "as is" without express or implied warranty.
60*/
61
62//#define USE_HEPSTRING // use HepString
63#define USE_STLSTRING // use standard library
64 // otherwise use gnu C++ String
65
66#ifdef USE_HEPSTRING
67
68#include "CLHEP/String/Strings.h" // HepString
69typedef HepString String;
70
71#elif defined(USE_STLSTRING)
72
73#include <string>
74//typedef string String;
75typedef std::string String;
76
77#else // use String from gnu C++ lib
78#include <String.h> // gnu C++ String
79#endif
80
81// For any library:
82#include <iostream>
83
84//#define STRSTREAM_AVAILABLE
85#ifdef STRSTREAM_AVAILABLE
86#include <strstream.h> // valid for old systems
87#else
88#include <sstream> // good for new ones
89#endif
90
91// In <string> initialization from integer is absent
92// In HepString it is present.
93// So as to write code not dependent on the type of strings used,
94// or to quickly modify the old code rich with String(n)
95// I have defined this macro.
96// The old String(n) should be converted to long_to_String(n)
97#ifdef USE_HEPSTRING
98inline String long_to_String(int n) { return String(n); }
99// call of overloaded `HepString(long int &)' is ambiguous !!
100// perhaps there are HepString::HepString(char) and HepString::HepString(int)
101#else
102inline String long_to_String(long n)
103 // This worked well for old standards.
104 // In new ones strstream has dissapeared.
105 //{ std::ostrstream s; s << n <<'\0'; return String(s.str()); }
106 {
107 std::ostringstream s;
108 s << n;
109 return String(s.str());
110}
111//{ std::ostringstream s; s << n <<'\0'; return String(s.str()); }
112
113inline String double_to_String(double d)
114 // This worked well for old standards.
115 // In new ones strstream has dissapeared.
116 //{ std::ostrstream s; s << n <<'\0'; return String(s.str()); }
117 {
118 std::ostringstream s;
119 s << d;
120 return String(s.str());
121}
122//{ std::ostringstream s; s << n <<'\0'; return String(s.str()); }
123#endif
124
125// puts one \n at the end of string stream:
126
127inline void put_one_n(std::ostringstream& ost) {
128 long qost = ost.str().length();
129 if (qost > 0) {
130 if (ost.str()[qost - 1] == '\n') { // nothing
131 } else {
132 ost << '\n';
133 }
134 } else
135 ost << '\n';
136}
137
138#endif
String double_to_String(double d)
Definition: String.h:113
void put_one_n(std::ostringstream &ost)
Definition: String.h:127
String long_to_String(long n)
Definition: String.h:102
std::string String
Definition: String.h:75