soma-io  5.0.5
formatreader_d.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_READER_FORMATREADER_D_H
35 #define SOMAIO_READER_FORMATREADER_D_H
36 //--- soma io ----------------------------------------------------------------
38 #include <soma-io/reader/formatreader.h> // class declaration
39 #include <soma-io/datasource/datasource.h> // for templating rc_ptr
40 #include <soma-io/utilities/creator.h> // used by setup() and create()
41 #include <soma-io/allocator/allocator.h> // AllocatorContext
42 //--- cartobase --------------------------------------------------------------
43 #include <cartobase/object/object.h> // header, options
44 #include <cartobase/smart/rcptr.h> // reference counting pointer
45 //--- system -----------------------------------------------------------------
46 #include <memory>
47 //--- debug ------------------------------------------------------------------
50 #define localMsg( message ) cartoCondMsg( 4, message, "FORMATREADER" )
51 // localMsg must be undef at end of file
52 //----------------------------------------------------------------------------
53 
54 namespace soma
55 {
56  //==========================================================================
57  // C O N S T R U C T O R S
58  //==========================================================================
59  template<typename T>
61  {
62  }
63 
64  //==========================================================================
65  // N E W M E T H O D S
66  //==========================================================================
67  template<typename T>
70  const AllocatorContext & context,
71  carto::Object options )
72  {
73  dsi = checkDataSourceInfo( dsi, options );
74  // copy context with compatible mmap mode
75  localMsg("Copying allocator context...")
76  AllocatorContext ac( context.accessMode(), dsi, context.useFactor() );
77  setup( obj, dsi->header(), ac, options );
78  read( obj, dsi, ac, options );
79  }
80 
81  template<typename T>
83  const AllocatorContext & context,
84  carto::Object options )
85  {
86  localMsg("Checking data source info...")
87  dsi = checkDataSourceInfo( dsi, options );
88  // copy context with compatible mmap mode
89  AllocatorContext ac( context.accessMode(), dsi, context.useFactor() );
90  localMsg("Allocating memory...")
91  std::unique_ptr<T> objp( create( dsi->header(), ac, options ) );
92  T *obj = objp.get();
93  localMsg("Reading data...")
94  read( *obj, dsi, ac, options );
95  objp.release();
96  return obj;
97  }
98 
99  /*** read ******************************************************************
100  * This method is totally abstract and should be implemented in a format-
101  * specific FormatReader.
102  * This implem is only used for FormatReader<GenericObject>
103  **************************************************************************/
104  template<typename T>
105  void FormatReader<T>::read( T & /* obj */,
107  const AllocatorContext & context,
108  carto::Object /* options */ )
109  {
110  throw carto::invalid_format_error( "format reader not implemented yet...",
111  context.dataSource() ?
112  context.dataSource()->url() : "" );
113  }
114 
115 
116  template<typename T>
119  {
120  localMsg("Checking data source informations...")
121  // copy options dict to avoid modifying it
122  // (it would kill thread safety)
123  carto::Object new_options( options->clone() );
124  if( new_options.isNone() )
125  new_options = carto::Object::value( carto::PropertySet() );
126  new_options->setProperty( "format", formatID() );
128  // check with exact format (pass 0 only)
129  DataSourceInfo new_dsi = dsil.check( *dsi, new_options, 0, 0 );
130  // FIXME: this copy is suboptimal. dsil.check() should work with rc_ptr
131  return carto::rc_ptr<DataSourceInfo>( new DataSourceInfo( new_dsi ) );
132  }
133 
134  //==========================================================================
135  // S T I L L U S E D
136  //==========================================================================
137 
138  template<typename T>
140  const AllocatorContext & context,
141  carto::Object options )
142  {
143  localMsg("Creating object ...");
144  return Creator<T>::create( header, context, options );
145  }
146 
147  template<typename T>
148  void FormatReader<T>::setup( T & obj, carto::Object header,
149  const AllocatorContext & context,
150  carto::Object options )
151  {
152  localMsg("Setting object ...");
153  Creator<T>::setup( obj, header, context, options );
154  }
155 
156 }
157 
158 #undef localMsg
159 #endif
virtual T * create(carto::Object header, const AllocatorContext &context, carto::Object options)
create the object to be read, bind the allocation context.
DataAccess accessMode() const
Definition: allocator.h:296
#define localMsg(message)
Allocation context.
Definition: allocator.h:260
const carto::rc_ptr< DataSource > dataSource() const
Definition: allocator.h:301
virtual void read(T &obj, carto::rc_ptr< DataSourceInfo > dsi, const AllocatorContext &context, carto::Object options)
Reads part or all of the object obj.
static void setup(T &obj, carto::Object header, const AllocatorContext &context, carto::Object options)
Setup (modify) an existing object according to the given header (maybe resize it etc.).
Definition: creator.h:86
virtual ~FormatReader()
Definition: allocator.h:48
virtual void setupAndRead(T &obj, carto::rc_ptr< DataSourceInfo > dsi, const AllocatorContext &context, carto::Object options)
Full reading procedure, for an already existing object.
DataSourceInfo check(DataSourceInfo dsi, carto::Object options=carto::none(), int passbegin=1, int passend=3)
Finds the right format checker.
Generic information retreiver / checker for all data sources and file formats.
virtual carto::rc_ptr< DataSourceInfo > checkDataSourceInfo(carto::rc_ptr< DataSourceInfo > dsi, carto::Object options)
Checks that dsi has been identified with the matching format checker.
static Object value()
virtual T * createAndRead(carto::rc_ptr< DataSourceInfo > dsi, const AllocatorContext &context, carto::Object options)
Factory mode: creates an object and reads it.
float useFactor() const
static T * create(carto::Object header, const AllocatorContext &context, carto::Object options)
Create a new object according to the given header.
Definition: creator.h:76
Informative object used by IO system.
virtual void setup(T &obj, carto::Object header, const AllocatorContext &context, carto::Object options)
setup an existing object (for resizing or reallocation for instance).
const carto::Object & header() const