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

Last change on this file since 4909 was 4081, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 2.7 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, ...)
62{
63 va_list ap;
64 va_start(ap, fmt);
65
66 Int_t n=256;
67
68 char *ret=0;
69
70 while (1)
71 {
72 ret = new char[n+1];
73 Int_t sz = vsnprintf(ret, n, fmt, ap);
74 if (sz<=n)
75 break;
76
77 n *= 2;
78 delete [] ret;
79 };
80
81 va_end(ap);
82
83 *static_cast<TString*>(this) = ret;
84
85 delete [] ret;
86
87 return *this;
88}
Note: See TracBrowser for help on using the repository browser.