soma-io  5.0.5
writer.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef SOMAIO_IO_WRITER_H
35 #define SOMAIO_IO_WRITER_H
36 //--- soma-io ----------------------------------------------------------------
38 //--- cartobase --------------------------------------------------------------
40 //----------------------------------------------------------------------------
41 
42 namespace soma
43 {
44  class DataSource;
45  class DataSourceInfo;
46 
48  {
49  public:
50  GenericWriter();
52  GenericWriter( const std::string & filename );
53  GenericWriter( std::ostream & stream );
54  virtual ~GenericWriter();
55 
64  template <typename T>
65  bool write( const T & obj, carto::Object options = carto::none() );
66 
67  virtual std::string writtenObjectType() const = 0;
68 
72  void attach( const std::string & filename );
73  void attach( std::ostream & stream );
74 
76  void flush();
78  void close();
79 
80  protected:
82  };
83 
84 
85  // Specialize write<Object> to be the same as write<GenericObject>
86  template <> inline bool GenericWriter::write( const carto::Object & obj,
87  carto::Object options )
88  {
89  return write( *obj, options );
90  }
91 
92 
102  template<class T> class Writer : public GenericWriter
103  {
104  public:
105  /* Note: constructors used to be inline, but this triggered a
106  compiler/linker bug on MacOS 10.5, so we moved them to non-inline.
107  */
108  Writer();
110  Writer( const std::string& filename );
111  Writer( std::ostream & stream );
112  virtual ~Writer();
113 
128  virtual bool write( const T & obj,
129  carto::Object options = carto::none(),
130  int passbegin = 1 , int passend = 4 );
131 
132  virtual std::string writtenObjectType() const;
133  };
134 
135 
136  // Specialize Writer<Object> to be the same as Writer<GenericObject>
137  template <> inline bool Writer<carto::Object>::write(
138  const carto::Object & obj, carto::Object options,
139  int passbegin, int passend )
140  {
142  return w.write( *obj, options, passbegin, passend );
143  }
144 
145  extern template class Writer<carto::GenericObject>;
146  extern template class Writer<carto::Object>;
147 }
148 
149 template <class T>
150 inline soma::GenericWriter &
151 operator << ( soma::GenericWriter & writer, const T & thing )
152 {
153  writer.write( thing );
154  return writer;
155 }
156 
157 
158 template <class T>
159 inline soma::Writer<T> &
160 operator << ( soma::Writer<T> & writer, const T & thing )
161 {
162  writer.write( thing );
163  return writer;
164 }
165 
166 
167 #endif
const carto::rc_ptr< DataSource > dataSource() const
virtual std::string writtenObjectType() const =0
Generic writer for every format of Aims object.
Definition: writer.h:102
void flush()
flush the writing DataSource (if still open)
Definition: allocator.h:48
void close()
close the writing DataSource
virtual bool write(const T &obj, carto::Object options=carto::none(), int passbegin=1, int passend=4)
Finds the correct format and writes the object.
Definition: writer_d.h:120
carto::rc_ptr< DataSourceInfo > _datasourceinfo
Definition: writer.h:81
void attach(carto::rc_ptr< DataSource > ds)
std::ostream & operator<<(std::ostream &os, const MemoryAllocator &thing)
virtual ~GenericWriter()
Object none()
bool write(const T &obj, carto::Object options=carto::none())
Finds the correct format and writes the object.
Definition: writer_d.h:59