aimsdata  5.0.5
Neuroimaging data handling
aimsGraphW_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 AIMS_IO_AIMSGRAPHW_D_H
35 #define AIMS_IO_AIMSGRAPHW_D_H
36 
37 #include <aims/io/aimsGraphW.h>
38 #include <aims/graph/graphmanip.h>
39 #include <aims/io/writer.h>
40 #include <aims/io/datatypecode.h>
41 #include <aims/io/finder.h>
42 #include <aims/data/data.h>
43 #include <graph/graph/graph.h>
44 #include <cartobase/smart/rcptr.h>
47 #include <list>
48 #include <vector>
49 
50 namespace aims
51 {
52 
54  {
57  std::string filename;
58  std::string directory;
59  std::string elemfname;
60  int elemindex;
66  std::map<std::string,AimsGraphWriter::Wrapper *> globalobjects;
67  std::map<std::string, bool> globalmodified;
69  bool delayindex;
70  bool modified;
71  bool saveall;
73  };
74 
75 
77  : colors( new std::map<std::string,std::vector<int> > )
78  {
79  }
80 
81 
83  {
84  std::map<std::string,AimsGraphWriter::Wrapper *>::iterator i, e
85  = globalobjects.end();
86  for ( i=globalobjects.begin(); i!=e; ++i )
87  {
88  // i->second->destroy(); ? no, object can be kept in graph
89  delete i->second;
90  }
91  }
92 
93 
94  template<typename T> bool
96  {
97  return wp.write( *data );
98  }
99 
100 
101  template<typename T> int
103  {
104  typename T::const_iterator i = data->begin(), e = data->end();
105  int n = 1;
106  if( i != e && i->first == 0 )
107  n = 0;
108  for ( ; i!=e && i->first==n; ++i, ++n ) {}
109  return n;
110  }
111 
112 
113  template<typename T> std::string
115  {
116  return ::carto::DataTypeCode<T>().objectType();
117  }
118 
119 
120  template<typename T> std::string
122  {
123  return carto::DataTypeCode<T>().dataType();
124  }
125 
126 
127  template<class T>
128  bool AimsGraphWriter::write( const T & obj ) const
129  {
130  // write in file or in global object
131  if ( d->mode == Local )
132  {
133  if ( d->saveall || ( d->modified && !obj.empty() ) )
134  {
135  std::string dir = carto::FileUtil::dirname( d->elemfname );
136  if( carto::FileUtil::fileStat( dir ).find( '+' )
137  == std::string::npos )
138  {
139  carto::Directory cd( dir );
140  cd.makedirs();
141  }
142  Writer<T> w( d->elemfname );
143  return w.write( obj );
144  }
145  return true;
146  }
147  else
148  {
149  Wrapper *& glob = d->globalobjects[ d->elemcode.global_filename ];
150  if ( !glob )
151  {
152  //cout << "create " << d->elemcode.global_filename << endl;
153  carto::rc_ptr<T> gobject;
154  if ( !d->elemcode.global_attribute.empty()
155  && info.graph->getProperty( d->elemcode.global_attribute,
156  gobject ) )
157  glob = new ObjectWrapper<T>( gobject.get() );
158  else
159  {
160  glob = new ObjectWrapper<T>( new T );
161  T* data = ((ObjectWrapper<T> *) glob)->data;
162  DataManip::adjustVoxelSize( *data, obj );
163  DataManip::setHeaderInfo( *data, *info.graph );
164  }
165  }
166  if( !d->delayindex && d->mode == Global )
167  {
168  /*cout << "insert index " << d->elemindex << " in "
169  << d->elemcode.global_filename << endl;*/
170  DataManip::insertElement( *((ObjectWrapper<T> *) glob)->data,
171  d->elemindex, obj );
172  }
173  }
174 
175  return true;
176  }
177 
178 
179  template<class T>
180  bool AimsGraphWriter::defaultTakeObject( Process & p, const std::string &,
181  Finder & )
182  {
183  AimsGraphWriter & wp = (AimsGraphWriter &) p;
184  ElementInfo & info = wp.info;
185  carto::rc_ptr<T> obj;
186 
187  try
188  {
189  if ( !info.element->getProperty( info.attribute, obj ) )
190  return false;
191  info.object = new ObjectWrapper<T>( obj.get() );
192  return true;
193  }
194  catch( std::exception & e )
195  {
196  std::cerr << e.what() << std::endl;
197  return false;
198  }
199  }
200 
201 }
202 
203 #endif
std::map< std::string, AimsGraphWriter::Wrapper * > globalobjects
Definition: aimsGraphW_d.h:66
virtual std::string objectType() const
Definition: aimsGraphW_d.h:114
STL namespace.
Describe how Aims objects are stored in graph objects.
Definition: graphmanip.h:64
The class for EcatSino data write operation.
Definition: border.h:44
static std::string fileStat(const std::string &)
carto::rc_ptr< std::map< std::string, std::vector< int > > > colors
Definition: aimsGraphW_d.h:72
static bool defaultTakeObject(Process &, const std::string &, Finder &)
Definition: aimsGraphW_d.h:180
virtual bool write(const T &obj, bool ascii=false, const std::string *format=0)
Finds the correct format and writes the object.
Definition: writer_d.h:108
std::map< std::string, bool > globalmodified
Definition: aimsGraphW_d.h:67
Generic finder / checker for all data objects and file formats This will replace the old AimsFinder...
Definition: finder.h:117
AimsGraphWriter::SavingMode prevmode
Definition: aimsGraphW_d.h:65
static std::string dirname(const std::string &)
void makedirs() const
AimsGraphWriter::SavingMode oldmode
Definition: aimsGraphW_d.h:63
AimsGraphWriter::SavingMode mode
Definition: aimsGraphW_d.h:62
AimsGraphWriter::SavingMode newmode
Definition: aimsGraphW_d.h:64
T * get() const
Generic writer for every format of Aims object.
Definition: writer.h:92
virtual std::string dataType() const
Definition: aimsGraphW_d.h:121
void adjustVoxelSize(T &, const T &)
utility function to copy voxel sizes from an object to another (if applicable)
void insertElement(T &dest, int index, const T &src)
insert a sub-element in a container data (time bucket/mesh/texture, or volume of labels...)
Link mechanism between the Finder and a process operating on arbitrary data types.
Definition: process.h:199
carto::AttributedObject * element
Definition: aimsGraphW.h:101
void setHeaderInfo(T &obj, const carto::GenericObject &srcheader)