source: trunk/MagicSoft/Mars/mbase/MString.cc@ 7462

Last change on this file since 7462 was 5713, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 3.8 KB
Line 
1/*====================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 3/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MString
28// =======
29//
30// If you are using root Form() command you must be aware of the fact that
31// it uses a global buffer. This buffer is recreated depending on the
32// length which is necessary to form the string. This recreation is not
33// thread safe and it might result in crashes if used in multi-threaded
34// environments.
35//
36// To get around this problem MString implements a Print() member function
37// which form a string like Form does. This formation is done in a
38// internal buffer. Because this buffer must be recreated and
39// deleted each time Print() is called this might be slower than Form().
40// The advantage is, that the buffer is not global and a call to Print()
41// from different threads is safe. However accessing the same
42// M/TString-object must still be locked with a mutex.
43//
44/////////////////////////////////////////////////////////////////////////////
45#include "MString.h"
46
47ClassImp(MString);
48
49// --------------------------------------------------------------------------
50//
51// Thread safe replacement for Form, use it like:
52//
53// MString string;
54// string.Print("MyString has %d bytes", 128);
55//
56// As a special feature the function returns the reference to the MString
57// so that you can directly work with it, eg.
58//
59// string.Print(" MyString has %d bytes ", 128).Strip(TString::kBoth);
60//
61MString &MString::Print(const char *fmt, va_list &ap)
62{
63 Int_t n=256;
64
65 char *ret=0;
66
67 while (1)
68 {
69 ret = new char[n+1];
70 Int_t sz = vsnprintf(ret, n, fmt, ap);
71 if (sz<=n)
72 break;
73
74 n *= 2;
75 delete [] ret;
76 };
77
78 va_end(ap);
79
80 *static_cast<TString*>(this) = ret;
81
82 delete [] ret;
83
84 return *this;
85}
86
87// --------------------------------------------------------------------------
88//
89// Thread safe replacement for Form, use it like:
90//
91// MString string;
92// string.Print("MyString has %d bytes", 128);
93//
94// As a special feature the function returns the reference to the MString
95// so that you can directly work with it, eg.
96//
97// string.Print(" MyString has %d bytes ", 128).Strip(TString::kBoth);
98//
99MString &MString::Print(const char *fmt, ...)
100{
101 va_list ap;
102 va_start(ap, fmt);
103
104 return Print(fmt, ap);
105}
106
107// --------------------------------------------------------------------------
108//
109// Thread safe replacement for Form, use it like:
110//
111// MString string;
112// string.Print("MyString has %d bytes", 128);
113//
114// As a special feature the function returns the reference to the MString
115// so that you can directly work with it, eg.
116//
117// string.Print(" MyString has %d bytes ", 128).Strip(TString::kBoth);
118//
119// The static version of this function returns a copy(!) of the resulting
120// M/TString.
121//
122MString MString::Form(const char *fmt, ...)
123{
124 va_list ap;
125 va_start(ap, fmt);
126
127 MString ret;
128 ret.Print(fmt, ap);
129 return ret;
130}
Note: See TracBrowser for help on using the repository browser.