sffostream.h 7.6 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
/*! \file sffostream.h
 * \brief SFF output stream (prototypes)
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
6
 * $Id$
thomas.forbriger's avatar
thomas.forbriger committed
7
8
9
10
 * \author Thomas Forbriger
 * \date 10/02/2004
 * 
 * SFF output stream (prototypes)
thomas.forbriger's avatar
thomas.forbriger committed
11
12
 *
 * ----
13
 * libsffxx is free software; you can redistribute it and/or modify
thomas.forbriger's avatar
thomas.forbriger committed
14
15
16
17
18
19
20
21
22
23
24
25
26
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
thomas.forbriger's avatar
thomas.forbriger committed
27
28
29
30
31
 * 
 * Copyright (c) 2004 by Thomas Forbriger (BFO Schiltach) 
 * 
 * REVISIONS and CHANGES 
 *  - 10/02/2004   V1.0   Thomas Forbriger
32
 *  - 21/04/2004   V1.1   some improvements - provide definition of use
33
34
 *  - 08/09/2004   V1.2   notice: documentaion gave wrong order for
 *                        SFFostream operators
thomas.forbriger's avatar
thomas.forbriger committed
35
36
37
38
 *  - 27/07/2005   V1.3   use a deep copy - that's the save way to postpone
 *                        the writing of a time series, it decouples our copy
 *                        from references hold in the calling program (which
 *                        might modify the samples)
thomas.forbriger's avatar
thomas.forbriger committed
39
40
 *  - 28/04/2006   V1.4   added using declaration for standard output streams
 *                        for debug code
thomas.forbriger's avatar
thomas.forbriger committed
41
42
43
44
45
46
47
48
 * 
 * ============================================================================
 */

// include guard
#ifndef TF_SFFOSTREAM_H_VERSION

#define TF_SFFOSTREAM_H_VERSION \
thomas.forbriger's avatar
thomas.forbriger committed
49
  "TF_SFFOSTREAM_H   V1.4"
thomas.forbriger's avatar
thomas.forbriger committed
50
#define TF_SFFOSTREAM_H_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
51
  "$Id$"
thomas.forbriger's avatar
thomas.forbriger committed
52
53
54

#include<sffxx.h>
#include<iostream>
thomas.forbriger's avatar
thomas.forbriger committed
55
#include<new>
thomas.forbriger's avatar
thomas.forbriger committed
56
57
58
59
60
61
62

namespace sff {

namespace helper {

  class ostream_manager {
    public:
63
      ostream_manager(std::ostream& os, const bool& debug=false);
thomas.forbriger's avatar
thomas.forbriger committed
64
65
66
67
68
69
70
71
72
      void setfileheader(const FileHeader&);
      void settraceheader(const TraceHeader&);
      void setinfo(const INFO&);
      void setwid2(const WID2&);
      void setsrce(const SRCE&);
      void setfree(const FREE&);
      void setnormmode(const Enormmode&);
      void nextislast();
      void clearlast();
73
      TraceHeader traceheader() const;
thomas.forbriger's avatar
thomas.forbriger committed
74
75
76
      const FileHeader& fileheader() const;
      const Enormmode& normmode() const;
    protected:
77
      //! flush file header to output (if not done yet)
thomas.forbriger's avatar
thomas.forbriger committed
78
79
80
81
      void flushheader();
      std::ostream& Mos;
    private:
      FileHeader Mfileheader;
82
      TraceHeader Mtraceheader;
thomas.forbriger's avatar
thomas.forbriger committed
83
84
85
      bool Mlast;
      bool Mfileheaderwritten;
      bool Mfilefreedefined;
86
      bool Mtraceinprogress;
thomas.forbriger's avatar
thomas.forbriger committed
87
      Enormmode Mnormmode;
88
89
    protected:
      bool Mdebug;
thomas.forbriger's avatar
thomas.forbriger committed
90
91
92
93
  }; // class ostream_manager

} // namespace helper

94
95
96
97
98
99
100
101
102
103
104
105
  /*! SFF output stream
   *
   * \note
   * You have to call the member function setseries() first upon writing a new
   * trace. This will check for a pending trace to be flushed to disc. If
   * there is one, it will be written to file together with all stored header
   * information first. Then the passed series will be copied to a local
   * member data. Notice, that this will be a shallow copy, when using
   * aff::Series objects. The data may still be modified through another
   * handle from the outside, before it is written to the file.
   * After setting the series itself, header information should be passed
   * through the inherited member functions provided by ostream_manager.
106
107
108
109
110
111
   *
   * \deprecated
   * The use of class SFFostream is deprecated.
   * It should be replaced by the corresponding output class in libdatrwxx.
   * SFFostream will no longer be maintained.
   * Thomas Forbriger, 22.3.2013.
112
   */
thomas.forbriger's avatar
thomas.forbriger committed
113
114
115
116
117
118
  template<class C>
  class SFFostream: public helper::ostream_manager {
    public:
      typedef C Tseries;
      typedef typename C::Tcoc Tcseries;
      typedef helper::ostream_manager Tbase;
119
120
      SFFostream(std::ostream& os, const bool& debug=false): 
        Tbase(os, debug), Mhasseries(false) { }
thomas.forbriger's avatar
thomas.forbriger committed
121
122
123
124
125
126
127
      ~SFFostream()
      { 
        if (!Mhasseries) throw
          Terror("ERROR (~SFFostream): no waveform!");
        this->nextislast();
        write();
      }
128
      //! finish previous trace and start with a new one
thomas.forbriger's avatar
thomas.forbriger committed
129
      void setseries(const Tcseries& series);
thomas.forbriger's avatar
thomas.forbriger committed
130
    private:
thomas.forbriger's avatar
thomas.forbriger committed
131
      void write();
thomas.forbriger's avatar
thomas.forbriger committed
132
133
134
135
      Tcseries Mseries;
      bool Mhasseries;
  }; // class SFFostream

thomas.forbriger's avatar
thomas.forbriger committed
136
137
138
139
140
141
142
  /*----------------------------------------------------------------------*/

  template<class C>
  inline
  void
  SFFostream<C>::write()
  {
thomas.forbriger's avatar
thomas.forbriger committed
143
144
    using std::cout;
    using std::endl;
thomas.forbriger's avatar
thomas.forbriger committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    // flush file header (in case we have to write the first trace)
    this->flushheader();
    if (Mhasseries) 
    {
      Mos << OutputWaveform<Tseries>(Mseries, 
                                     this->traceheader(),
                                     this->normmode());
      Mhasseries=false;
      if (Mdebug) 
      { 
        cout << "DEBUG (SFFostream::write): trace written:" << endl; 
        cout << "      " << this->traceheader().wid2().line().substr(0,70) 
          << endl;
        cout << "      index range: " << Mseries.f() << " - "  
                                      << Mseries.l() << "; ";
        cout << "some values: " << Mseries(Mseries.f()) 
                        << ", " << Mseries(Mseries.f()+1)
                        << ", " << Mseries(Mseries.f()+2) 
                        << ", " << Mseries(Mseries.f()+3) 
                        << endl;
      }
    }
    if (Mdebug) { cout << "DEBUG (SFFostream::write): finished" << endl; }
  } // SFFostream<C>::write()

  /*----------------------------------------------------------------------*/

  template<class C>
  inline
  void
  SFFostream<C>::setseries(const Tcseries& series)
  {
thomas.forbriger's avatar
thomas.forbriger committed
177
178
    using std::cout;
    using std::endl;
thomas.forbriger's avatar
thomas.forbriger committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    this->write();
    // using a deep copy here is the save way!
    Mseries=series.copyout();
    Mhasseries=true;
    if (Mdebug) 
    { 
      cout << "DEBUG (SFFostream::setseries) finished:" << endl; 
      cout << "      index range: " << Mseries.f() << " - "  
                                    << Mseries.l() << endl;
      cout << "      some values: " << Mseries(Mseries.f()) 
                     << ", " << Mseries(Mseries.f()+1)
                     << ", " << Mseries(Mseries.f()+2) 
                     << ", " << Mseries(Mseries.f()+3) 
                     << endl;
    }
  } // SFFostream<C>::setseries(const Tcseries& series)

  /*----------------------------------------------------------------------*/

198
  //! FIRST(!) operator to be called for each trace
thomas.forbriger's avatar
thomas.forbriger committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  template<class C>
  SFFostream<C>& operator<<(SFFostream<C>& os, const typename C::Tcoc& c)
  { os.setseries(c); return(os); }

  template<class C>
  SFFostream<C>& operator<<(SFFostream<C>& os, const WID2& wid2)
  { os.setwid2(wid2); return(os); }

  template<class C>
  SFFostream<C>& operator<<(SFFostream<C>& os, const INFO& info)
  { os.setinfo(info); return(os); }

  template<class C>
  SFFostream<C>& operator<<(SFFostream<C>& os, const FREE& free)
  { os.setfree(free); return(os); }

  template<class C>
  SFFostream<C>& operator<<(SFFostream<C>& os, const SRCE& srce)
  { os.setsrce(srce); return(os); }

  template<class C>
  SFFostream<C>& operator<<(SFFostream<C>& os, const FileHeader& fh)
  { os.setfileheader(fh); return(os); }

  template<class C>
  SFFostream<C>& operator<<(SFFostream<C>& os, const TraceHeader& th)
  { os.settraceheader(th); return(os); }

} // namespace sff

#endif // TF_SFFOSTREAM_H_VERSION (includeguard)

/* ----- END OF sffostream.h ----- */