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