]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_STRING.hxx
Salome HOME
sources v1.2
[modules/med.git] / src / MEDMEM / MEDMEM_STRING.hxx
1 //  MED MEDMEM : MED files in memory
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : MEDMEM_STRING.hxx
25 //  Module : MED
26
27 # ifndef MEDMEM_STRING_HXX
28 # define MEDMEM_STRING_HXX
29
30 using namespace std;
31
32 # include <string>
33 # include <strstream>
34
35 /*!
36  A class to generate string from any type:
37
38  here is a simple use case
39       STRING msgErr;
40       msgErr << "This Object cannot be instanciated by now ! Try "<< 11 << "times just to see what happens ";
41       throw SALOME_EXCEPTION (LOCALIZED(msgErr)) ;
42                                                                 */
43 class STRING : public string
44 {
45
46 private :
47   ostrstream _s ;
48
49 public :
50   
51   STRING::STRING() :string(), _s() 
52    {
53    }
54
55   STRING::~STRING()
56    {
57         _s.freeze(false);
58    }
59
60   STRING::operator const char * () const 
61    {     
62         return const_cast <const char *> (this->c_str()) ;
63    }
64
65    template <class T> STRING::STRING( const T &valeur ) : string(), _s() 
66    {
67         _s.freeze(false);
68
69         _s << valeur ;
70         _s << ends;
71
72         this->string::operator =( _s.str());  // freeze is true by now
73    }
74
75    template <class T> STRING & STRING::operator<<( const T &valeur )
76    {
77   
78         if ( _s.pcount() )
79         {
80                 _s.seekp(-1, ios::cur); // Back up before NULL
81                 _s.rdbuf()->freeze(0); // Unfreeze it
82         }
83
84         _s << valeur ;
85         _s << ends;
86
87         this->string::operator = ( _s.str() ) ;  // freeze is true by now
88         _s.freeze( false ) ;
89         return *this ;
90
91
92    }
93 } ;
94
95 # endif