soma-io  5.0.5
pythonwriter_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_WRITER_PYTHONWRITER_D_H
35 #define SOMAIO_WRITER_PYTHONWRITER_D_H
36 
37 //--- soma-io ----------------------------------------------------------------
42 //----------------------------------------------------------------------------
43 
44 namespace {
45 
46 template<typename T>
47 void
48 genericHelper( const carto::GenericObject & obj, soma::PythonWriter & w, int, bool )
49 {
50  soma::DataSource & ds = *w.dataSource();
51  // using carto::GenericObject:: avoids a bug in gcc-2.96
52  const T & x = obj.GenericObject::value<T>();
54 }
55 
56 
57 template<>
58 void genericHelper<char>( const carto::GenericObject & obj, soma::PythonWriter & w, int,
59  bool )
60 {
61  soma::DataSource & ds = *w.dataSource();
62  // using carto::GenericObject:: avoids a bug in gcc-2.96
63  const char & x = obj.GenericObject::value<char>();
65 }
66 
67 
68 template<>
69 void genericHelper<unsigned char>( const carto::GenericObject & obj,
70  soma::PythonWriter & w, int, bool )
71 {
72  soma::DataSource & ds = *w.dataSource();
73  // using carto::GenericObject:: avoids a bug in gcc-2.96
74  const unsigned char & x = obj.GenericObject::value<unsigned char>();
76 }
77 
78 
79 template<>
80 void genericHelper<std::string>( const carto::GenericObject & obj,
81  soma::PythonWriter & w, int, bool )
82 {
83  w.writeString( *w.dataSource(), obj.getString() );
84 }
85 
86 
87 template<>
88 void genericHelper<double>( const carto::GenericObject & obj,
89  soma::PythonWriter & w, int, bool )
90 {
91  soma::DataSource & ds = *w.dataSource();
92  soma::AsciiDataSourceTraits<double>::write( ds, obj.getScalar() );
93 }
94 
95 
96 template<>
97 void genericHelper<float>( const carto::GenericObject & obj,
98  soma::PythonWriter & w, int, bool )
99 {
100  soma::DataSource & ds = *w.dataSource();
101  soma::AsciiDataSourceTraits<float>::write( ds, float( obj.getScalar() ) );
102 }
103 
104 
105 template<>
106 void genericHelper<int16_t>( const carto::GenericObject & obj,
107  soma::PythonWriter & w, int, bool )
108 {
109  soma::DataSource & ds = *w.dataSource();
111  ds, int16_t( obj.getScalar() ) );
112 }
113 
114 
115 template<>
116 void genericHelper<uint16_t>( const carto::GenericObject & obj,
117  soma::PythonWriter & w, int, bool )
118 {
119  soma::DataSource & ds = *w.dataSource();
121  ds, uint16_t( obj.getScalar() ) );
122 }
123 
124 
125 template<>
126 void genericHelper<int32_t>( const carto::GenericObject & obj,
127  soma::PythonWriter & w, int, bool )
128 {
129  soma::DataSource & ds = *w.dataSource();
131  ds, int32_t( obj.getScalar() ) );
132 }
133 
134 
135 template<>
136 void genericHelper<uint32_t>( const carto::GenericObject & obj,
137  soma::PythonWriter & w, int, bool )
138 {
139  soma::DataSource & ds = *w.dataSource();
141  ds, uint32_t( obj.getScalar() ) );
142 }
143 
144 
145 template<typename T>
146 void dictHelper( const carto::GenericObject & obj, soma::PythonWriter & w, int indent,
147  bool writeInternals )
148 {
149  // std::cout << "dictHelper<" << carto::DataTypeCode<T>::name() << ">\n";
150 
151  soma::DataSource & ds = *w.dataSource();
152  const std::map<T, carto::Object>
153  & x = obj.GenericObject::value<std::map<T, carto::Object> >();
154  typename std::map<T, carto::Object>::const_iterator im, em = x.end();
155  bool first = true, hassyntax = false;
156  std::string ind, ind2;
157  int i;
158  char sep = '\n';
159 
160  if( w.singleLine() )
161  sep = ' ';
162  else
163  {
164  for( i=0; i<indent-1; ++i )
165  ind += " ";
166  ind2 = ind + " ";
167  if( indent > 0 )
168  ind += " ";
169  ++indent;
170  }
171 
172  ds.putch( '{' );
173  ds.putch( sep );
175 
176  std::string synt;
177  const carto::SyntaxedInterface *si
178  = obj.GenericObject::getInterface<carto::SyntaxedInterface>();
179  if( si && si->hasSyntax() )
180  {
181  synt = si->getSyntax();
182  hassyntax = true;
183  }
184  else
185  {
186  try
187  {
188  carto::Object sx = obj.getProperty( "__syntax__" );
189  if( sx.get() )
190  {
191  synt = sx->GenericObject::value<std::string>();
192  hassyntax = true;
193  }
194  }
195  catch( ... )
196  {
197  }
198  }
199 
200  if( hassyntax )
201  {
202  soma::AsciiDataSourceTraits<std::string>::write( ds, "'__syntax__' : " );
203  w.writeString( ds, synt );
204  first = false;
205  }
206 
207  for( im=x.begin(); im!=em; ++im )
208  {
209  if( first )
210  first = false;
211  else
212  {
213  ds.putch( ',' );
214  ds.putch( sep );
216  }
217  // const_cast: just to avoid referencing a const T
218  carto::Object key = carto::Object::reference( const_cast<T &>( im->first ) );
219  w.write( key, indent, "", "", writeInternals );
221  w.write( im->second, indent, "", "", writeInternals );
222  }
223  ds.putch( sep );
225  ds.putch( '}' );
226 }
227 
228 
229 template<>
230 void dictHelper<std::string>( const carto::GenericObject & obj, soma::PythonWriter & w,
231  int indent, bool writeInternals )
232 {
233  // std::cout << "dictHelper<string>\n";
234 
235  soma::DataSource & ds = *w.dataSource();
237  & y = *obj.getInterface<carto::IterableInterface>();
238  carto::Object im;
239 
240  bool first = true, hassyntax = false;
241  std::string ind, ind2;
242  int i;
243  char sep = '\n';
244 
245  if( w.singleLine() )
246  sep = ' ';
247  else
248  {
249  for( i=0; i<indent-1; ++i )
250  ind += " ";
251  ind2 = ind + " ";
252  if( indent > 0 )
253  ind += " ";
254  ++indent;
255  }
256 
257  ds.putch( '{' );
258  ds.putch( sep );
260 
261  std::string synt;
262  const carto::SyntaxedInterface *si = obj.getInterface<carto::SyntaxedInterface>();
263  if( si && si->hasSyntax() )
264  {
265  synt = si->getSyntax();
266  hassyntax = true;
267  }
268  else
269  {
270  try
271  {
272  carto::Object sx = obj.getProperty( "__syntax__" );
273  if( sx.get() )
274  {
275  synt = sx->GenericObject::value<std::string>();
276  hassyntax = true;
277  }
278  }
279  catch( ... )
280  {
281  }
282  }
283 
284  if( hassyntax )
285  {
286  soma::AsciiDataSourceTraits<std::string>::write( ds, "'__syntax__' : " );
287  w.writeString( ds, synt );
288  first = false;
289  }
290 
291  std::string key;
292  im=y.objectIterator();
293  while( im->isValid() )
294  {
295  try
296  {
297  key = im->key();
298  if( ( writeInternals || !w.isInternal( "", key ) )
299  && ( key != "__syntax__" || !hassyntax ) )
300  {
301  if( first )
302  first = false;
303  else
304  {
305  ds.putch( ',' );
306  ds.putch( sep );
308  }
309  w.writeString( ds, key );
311  w.write( im->currentValue(), indent, "", key, writeInternals );
312  }
313  }
314  catch( ... )
315  {
316  long key = im->intKey();
317  if( first )
318  first = false;
319  else
320  {
321  ds.putch( ',' );
322  ds.putch( sep );
324  }
327  w.write( *im->currentValue(), indent, "", "", writeInternals );
328  }
329  im->next();
330  }
331 
332  ds.putch( sep );
334  ds.putch( '}' );
335 }
336 
337 
338 void rcDictHelper( const carto::GenericObject & obj, soma::PythonWriter & w, int indent,
339  bool writeInternals )
340 {
341  soma::DataSource & ds = *w.dataSource();
342  const std::map<carto::Object, carto::Object> & x
343  = obj.value<std::map<carto::Object, carto::Object> >();
344  std::map<carto::Object, carto::Object>::const_iterator im, em = x.end();
345  bool first = true, hassyntax = false;
346  std::string ind, ind2;
347  int i;
348  char sep = '\n';
349 
350  if( w.singleLine() )
351  sep = ' ';
352  else
353  {
354  for( i=0; i<indent-1; ++i )
355  ind += " ";
356  ind2 = ind + " ";
357  if( indent > 0 )
358  ind += " ";
359  ++indent;
360  }
361 
362  ds.putch( '{' );
363  ds.putch( sep );
365 
366  std::string synt;
368  if( si && si->hasSyntax() )
369  {
370  synt = si->getSyntax();
371  hassyntax = true;
372  }
373  else
374  {
375  try
376  {
377  carto::Object sx = obj.getProperty( "__syntax__" );
378  if( sx.get() )
379  {
380  synt = sx->GenericObject::value<std::string>();
381  hassyntax = true;
382  }
383  }
384  catch( ... )
385  {
386  }
387  }
388 
389  if( hassyntax )
390  {
391  soma::AsciiDataSourceTraits<std::string>::write( ds, "'__syntax__' : " );
392  w.writeString( ds, synt );
393  first = false;
394  }
395 
396  for( im=x.begin(); im!=em; ++im )
397  {
398  if( first )
399  first = false;
400  else
401  {
402  ds.putch( ',' );
403  ds.putch( sep );
405  }
406  w.write( im->first, indent, "", "", writeInternals );
408  w.write( im->second, indent, "", "", writeInternals );
409  }
410 
411  ds.putch( sep );
413  ds.putch( '}' );
414 }
415 
416 
417 void listHelper( const carto::GenericObject & obj, soma::PythonWriter & w,
418  int indent, bool writeInternals )
419 {
420  soma::DataSource & ds = *w.dataSource();
421  carto::Object it;
422  bool first = true;
423 
424  ds.putch( '[' );
425  ++indent;
426  for( it=obj.objectIterator(); it->isValid(); it->next() )
427  {
428  if( first )
429  first = false;
430  else
431  ds.putch( ',' );
432  ds.putch( ' ' );
433  w.write( it->currentValue(), indent, "", "", writeInternals );
434  }
435  ds.putch( ' ' );
436  ds.putch( ']' );
437 }
438 
439 } // namespace (internal linkage)
440 
441 template<typename T>
442 void
444  soma::PythonWriter & w,
445  int ind,
446  bool writeInternals )
447 {
448  soma::DataSource & ds = *w.dataSource();
449  carto::Object ix = obj.objectIterator();
450  carto::Object de;
451  bool first = true;
452 
453  ds.putch( '[' );
454  for( ix=obj.objectIterator(); ix->isValid(); ix->next() )
455  {
456  if( first )
457  {
458  first = false;
459  ds.putch( ' ' );
460  }
461  else
462  {
463  ds.putch( ',' );
464  ds.putch( ' ' );
465  }
466  de = ix->currentValue();
467  genericHelper<typename T::value_type>( *de, w, ind, writeInternals );
468  }
469  ds.putch( ' ' );
470  ds.putch( ']' );
471 }
472 
473 #endif
474 
carto::rc_ptr< DataSource > dataSource()
Definition: pythonwriter.h:84
virtual bool getProperty(const std::string &key, Object &value) const=0
virtual bool hasSyntax() const=0
static Object reference(T &value)
void writeString(DataSource &, std::string)
utility function
Abstraction layer for various data sources (file, buffer, socket...).
Definition: datasource.h:64
static bool write(DataSource &ds, const T &item)
bool singleLine() const
Definition: pythonwriter.h:112
static void genericSequenceHelper(const carto::GenericObject &obj, PythonWriter &w, int ind, bool writeInternals)
virtual Object objectIterator() const=0
const T & value() const
GenericObject * get() const
virtual int putch(int ch)=0
void write(const carto::GenericObject &object, bool writeInternals=false, bool writevariable=true)
Writes a generic object to stream.
virtual std::string getSyntax() const=0