Salome HOME
DCQ : Merge with Ecole Ete a6.
[modules/med.git] / src / MEDMEM / MEDMEM_STRING.hxx
1 # ifndef MEDMEM_STRING_HXX
2 # define MEDMEM_STRING_HXX
3
4 using namespace std;
5
6 # include <string>
7 # include <strstream>
8
9 /*!
10  A class to generate string from any type:
11
12  here is a simple use case
13       STRING msgErr;
14       msgErr << "This Object cannot be instanciated by now ! Try "<< 11 << "times just to see what happens ";
15       throw SALOME_EXCEPTION (LOCALIZED(msgErr)) ;
16                                                                 */
17 namespace MEDMEM {
18 class STRING : public string
19 {
20
21 private :
22   ostrstream _s ;
23
24 public :
25   
26   STRING::STRING() :string(), _s() 
27    {
28    }
29
30   STRING::~STRING()
31    {
32         _s.freeze(false);
33    }
34
35   STRING::operator const char * () const 
36    {     
37         return const_cast <const char *> (this->c_str()) ;
38    }
39
40    template <class T> STRING::STRING( const T &valeur ) : string(), _s() 
41    {
42         _s.freeze(false);
43
44         _s << valeur ;
45         _s << ends;
46
47         this->string::operator =( _s.str());  // freeze is true by now
48    }
49
50    template <class T> STRING & STRING::operator<<( const T &valeur )
51    {
52   
53         if ( _s.pcount() )
54         {
55                 _s.seekp(-1, ios::cur); // Back up before NULL
56                 _s.rdbuf()->freeze(0); // Unfreeze it
57         }
58
59         _s << valeur ;
60         _s << ends;
61
62         this->string::operator = ( _s.str() ) ;  // freeze is true by now
63         _s.freeze( false ) ;
64         return *this ;
65
66
67    }
68 } ;
69 } ;
70
71 # endif