soma-io 6.0.6
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
44namespace {
45
46template<typename T>
47void
48genericHelper( 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>();
53 soma::AsciiDataSourceTraits<T>::write( ds, x );
54}
55
56
57template<>
58void 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>();
64 soma::AsciiDataSourceTraits<int>::write( ds, x );
65}
66
67
68template<>
69void 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>();
75 soma::AsciiDataSourceTraits<unsigned>::write( ds, x );
76}
77
78
79template<>
80void genericHelper<std::string>( const carto::GenericObject & obj,
81 soma::PythonWriter & w, int, bool )
82{
83 w.writeString( *w.dataSource(), obj.getString() );
84}
85
86
87template<>
88void 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
96template<>
97void 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
105template<>
106void genericHelper<int16_t>( const carto::GenericObject & obj,
107 soma::PythonWriter & w, int, bool )
108{
109 soma::DataSource & ds = *w.dataSource();
110 soma::AsciiDataSourceTraits<int16_t>::write(
111 ds, int16_t( obj.getScalar() ) );
112}
113
114
115template<>
116void genericHelper<uint16_t>( const carto::GenericObject & obj,
117 soma::PythonWriter & w, int, bool )
118{
119 soma::DataSource & ds = *w.dataSource();
120 soma::AsciiDataSourceTraits<uint16_t>::write(
121 ds, uint16_t( obj.getScalar() ) );
122}
123
124
125template<>
126void genericHelper<int32_t>( const carto::GenericObject & obj,
127 soma::PythonWriter & w, int, bool )
128{
129 soma::DataSource & ds = *w.dataSource();
130 soma::AsciiDataSourceTraits<int32_t>::write(
131 ds, int32_t( obj.getScalar() ) );
132}
133
134
135template<>
136void genericHelper<uint32_t>( const carto::GenericObject & obj,
137 soma::PythonWriter & w, int, bool )
138{
139 soma::DataSource & ds = *w.dataSource();
140 soma::AsciiDataSourceTraits<uint32_t>::write(
141 ds, uint32_t( obj.getScalar() ) );
142}
143
144
145template<typename T>
146void 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 );
174 soma::AsciiDataSourceTraits<std::string>::write( ds, ind );
175
176 std::string synt;
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 );
215 soma::AsciiDataSourceTraits<std::string>::write( ds, ind );
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 );
220 soma::AsciiDataSourceTraits<std::string>::write( ds, " : " );
221 w.write( im->second, indent, "", "", writeInternals );
222 }
223 ds.putch( sep );
224 soma::AsciiDataSourceTraits<std::string>::write( ds, ind );
225 ds.putch( '}' );
226}
227
228
229template<>
230void dictHelper<std::string>( const carto::GenericObject & obj,
231 soma::PythonWriter & w,
232 int indent, bool writeInternals )
233{
234 // std::cout << "dictHelper<string>\n";
235
236 soma::DataSource & ds = *w.dataSource();
239 carto::Object im;
240
241 bool first = true, hassyntax = false;
242 std::string ind, ind2;
243 int i;
244 char sep = '\n';
245
246 if( w.singleLine() )
247 sep = ' ';
248 else
249 {
250 for( i=0; i<indent-1; ++i )
251 ind += " ";
252 ind2 = ind + " ";
253 if( indent > 0 )
254 ind += " ";
255 ++indent;
256 }
257
258 ds.putch( '{' );
259 ds.putch( sep );
260 soma::AsciiDataSourceTraits<std::string>::write( ds, ind );
261
262 std::string synt;
264 if( si && si->hasSyntax() )
265 {
266 synt = si->getSyntax();
267 hassyntax = true;
268 }
269 else
270 {
271 try
272 {
273 carto::Object sx = obj.getProperty( "__syntax__" );
274 if( sx.get() )
275 {
276 synt = sx->GenericObject::value<std::string>();
277 hassyntax = true;
278 }
279 }
280 catch( ... )
281 {
282 }
283 }
284
285 if( hassyntax )
286 {
287 soma::AsciiDataSourceTraits<std::string>::write( ds, "'__syntax__' : " );
288 w.writeString( ds, synt );
289 first = false;
290 }
291
292 std::string key;
293 im=y.objectIterator();
294 while( im->isValid() )
295 {
296 try
297 {
298 key = im->key();
299 if( ( writeInternals || !w.isInternal( "", key ) )
300 && ( key != "__syntax__" || !hassyntax ) )
301 {
302 if( first )
303 first = false;
304 else
305 {
306 ds.putch( ',' );
307 ds.putch( sep );
308 soma::AsciiDataSourceTraits<std::string>::write( ds, ind );
309 }
310 w.writeString( ds, key );
311 soma::AsciiDataSourceTraits<std::string>::write( ds, " : " );
312 w.write( im->currentValue(), indent, "", key, writeInternals );
313 }
314 }
315 catch( ... )
316 {
317 try
318 {
319 long key = im->intKey();
320 if( first )
321 first = false;
322 else
323 {
324 ds.putch( ',' );
325 ds.putch( sep );
326 soma::AsciiDataSourceTraits<std::string>::write( ds, ind );
327 }
328 soma::AsciiDataSourceTraits<long>::write( ds, key );
329 soma::AsciiDataSourceTraits<std::string>::write( ds, " : " );
330 w.write( *im->currentValue(), indent, "", "", writeInternals );
331 }
332 catch( ... )
333 {
334 carto::Object key = im->keyObject();
335 if( first )
336 first = false;
337 else
338 {
339 ds.putch( ',' );
340 ds.putch( sep );
341 soma::AsciiDataSourceTraits<std::string>::write( ds, ind );
342 }
343 w.write( key, indent, "", "", writeInternals );
344 soma::AsciiDataSourceTraits<std::string>::write( ds, " : " );
345 w.write( *im->currentValue(), indent, "", "", writeInternals );
346 }
347 }
348 im->next();
349 }
350
351 ds.putch( sep );
352 soma::AsciiDataSourceTraits<std::string>::write( ds, ind2 );
353 ds.putch( '}' );
354}
355
356
357void rcDictHelper( const carto::GenericObject & obj, soma::PythonWriter & w, int indent,
358 bool writeInternals )
359{
360 soma::DataSource & ds = *w.dataSource();
361 const std::map<carto::Object, carto::Object> & x
362 = obj.value<std::map<carto::Object, carto::Object> >();
363 std::map<carto::Object, carto::Object>::const_iterator im, em = x.end();
364 bool first = true, hassyntax = false;
365 std::string ind, ind2;
366 int i;
367 char sep = '\n';
368
369 if( w.singleLine() )
370 sep = ' ';
371 else
372 {
373 for( i=0; i<indent-1; ++i )
374 ind += " ";
375 ind2 = ind + " ";
376 if( indent > 0 )
377 ind += " ";
378 ++indent;
379 }
380
381 ds.putch( '{' );
382 ds.putch( sep );
383 soma::AsciiDataSourceTraits<std::string>::write( ds, ind );
384
385 std::string synt;
387 if( si && si->hasSyntax() )
388 {
389 synt = si->getSyntax();
390 hassyntax = true;
391 }
392 else
393 {
394 try
395 {
396 carto::Object sx = obj.getProperty( "__syntax__" );
397 if( sx.get() )
398 {
399 synt = sx->GenericObject::value<std::string>();
400 hassyntax = true;
401 }
402 }
403 catch( ... )
404 {
405 }
406 }
407
408 if( hassyntax )
409 {
410 soma::AsciiDataSourceTraits<std::string>::write( ds, "'__syntax__' : " );
411 w.writeString( ds, synt );
412 first = false;
413 }
414
415 for( im=x.begin(); im!=em; ++im )
416 {
417 if( first )
418 first = false;
419 else
420 {
421 ds.putch( ',' );
422 ds.putch( sep );
423 soma::AsciiDataSourceTraits<std::string>::write( ds, ind );
424 }
425 w.write( im->first, indent, "", "", writeInternals );
426 soma::AsciiDataSourceTraits<std::string>::write( ds, " : " );
427 w.write( im->second, indent, "", "", writeInternals );
428 }
429
430 ds.putch( sep );
431 soma::AsciiDataSourceTraits<std::string>::write( ds, ind2 );
432 ds.putch( '}' );
433}
434
435
436void listHelper( const carto::GenericObject & obj, soma::PythonWriter & w,
437 int indent, bool writeInternals )
438{
439 soma::DataSource & ds = *w.dataSource();
440 carto::Object it;
441 bool first = true;
442
443 ds.putch( '[' );
444 ++indent;
445 for( it=obj.objectIterator(); it->isValid(); it->next() )
446 {
447 if( first )
448 first = false;
449 else
450 ds.putch( ',' );
451 ds.putch( ' ' );
452 w.write( it->currentValue(), indent, "", "", writeInternals );
453 }
454 ds.putch( ' ' );
455 ds.putch( ']' );
456}
457
458} // namespace (internal linkage)
459
460template<typename T>
461void
463 soma::PythonWriter & w,
464 int ind,
465 bool writeInternals )
466{
467 soma::DataSource & ds = *w.dataSource();
468 carto::Object ix = obj.objectIterator();
469 carto::Object de;
470 bool first = true;
471
472 ds.putch( '[' );
473 for( ix=obj.objectIterator(); ix->isValid(); ix->next() )
474 {
475 if( first )
476 {
477 first = false;
478 ds.putch( ' ' );
479 }
480 else
481 {
482 ds.putch( ',' );
483 ds.putch( ' ' );
484 }
485 de = ix->currentValue();
486 genericHelper<typename T::value_type>( *de, w, ind, writeInternals );
487 }
488 ds.putch( ' ' );
489 ds.putch( ']' );
490}
491
492#endif
493
virtual bool getProperty(const std::string &key, Object &value) const=0
const T & value() const
virtual Object objectIterator() const=0
Object reference(Object &value)
virtual double getScalar() const=0
virtual std::string getString() const=0
virtual bool hasSyntax() const=0
virtual std::string getSyntax() const=0
T * get() const
virtual int putch(int ch)=0
bool singleLine() const
carto::rc_ptr< DataSource > dataSource()
void writeString(DataSource &, std::string)
utility function
static void genericSequenceHelper(const carto::GenericObject &obj, PythonWriter &w, int ind, bool writeInternals)
void write(const carto::GenericObject &object, bool writeInternals=false, bool writevariable=true)
Writes a generic object to stream.
bool isInternal(const std::string &syntax, const std::string &semantic)