soma-io  5.1.2
pythonwriter.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_H
35 #define SOMAIO_WRITER_PYTHONWRITER_H
36 //--- soma-io ----------------------------------------------------------------
39 //--- cartobase --------------------------------------------------------------
43 //--- system -----------------------------------------------------------------
44 #include <fstream>
45 //----------------------------------------------------------------------------
46 
47 namespace carto
48 {
49  class GenericObject;
50  class Object;
51 }
52 
53 namespace soma
54 {
55 
57  {
58  public:
59  typedef void (*Helper)( const carto::GenericObject &, PythonWriter & w,
60  int indent, bool writeInternals );
61  typedef std::map<std::string, Helper> HelperSet;
62  typedef void (*CatchFunction)( PythonWriter &, std::exception &,
63  const carto::Object & );
64 
65  PythonWriter( const std::string& filename,
66  const carto::SyntaxSet& rules = carto::SyntaxSet(),
67  const HelperSet& helpers = HelperSet() );
69  const HelperSet& helpers = HelperSet() );
72  const carto::SyntaxSet& rules = carto::SyntaxSet(),
73  const HelperSet& helpers = HelperSet() );
74  virtual ~PythonWriter();
75 
76  void open(const std::string& filename);
78  void attach( std::ostream & s );
80  void close();
81  virtual std::string name() const;
82  bool operator ! () const;
83  bool is_open() const;
84  carto::rc_ptr<DataSource> dataSource() { return _datasource; }
85 
87  const HelperSet & helpers() const;
89  const carto::SyntaxSet & syntaxes() const;
91 
93  void write( const carto::GenericObject & object,
94  bool writeInternals = false,
95  bool writevariable = true );
96  void write( const carto::Object & object,
97  bool writeInternals = false,
98  bool writevariable = true );
100  void write( const carto::GenericObject & object, int indent,
101  const std::string & syntax = "",
102  const std::string & semantic = "",
103  bool writeInternals = false );
105  void write( const carto::Object & object, int indent,
106  const std::string & syntax = "",
107  const std::string & semantic = "",
108  bool writeInternals = false );
109  bool isInternal( const std::string & syntax,
110  const std::string & semantic );
111  void setSingleLineMode( bool x ) { _singleLine = x; }
112  bool singleLine() const { return _singleLine; }
115  void setCatchFunction( CatchFunction f );
116  char quoteCharacter() const { return _quoteChar; }
117  void setQuoteCharacter( char q ) { _quoteChar = q; }
118  std::string noneString() const { return _noneString; }
119  void setNoneString( const std::string & none ) { _noneString = none; }
120 
122  void writeString( DataSource &, std::string );
123 
124  template<typename T>
125  static void genericSequenceHelper( const carto::GenericObject & obj,
126  PythonWriter & w, int ind,
127  bool writeInternals );
128 
129  protected:
130  void init( const HelperSet & helpers );
131 
136  CatchFunction _catchFunction;
138  std::string _noneString;
139  };
140 } // namespace soma
141 
142 namespace carto {
145 }
146 
147 #endif
#define CARTOBASE_API
Abstraction layer for various data sources (file, buffer, socket...).
Definition: datasource.h:65
PythonWriter(const carto::SyntaxSet &rules=carto::SyntaxSet(), const HelperSet &helpers=HelperSet())
HelperSet & helpers()
carto::SyntaxSet & syntaxes()
void setQuoteCharacter(char q)
Definition: pythonwriter.h:117
void write(const carto::Object &object, int indent, const std::string &syntax="", const std::string &semantic="", bool writeInternals=false)
Writes an element according to the syntax.
const carto::SyntaxSet & syntaxes() const
bool singleLine() const
Definition: pythonwriter.h:112
CatchFunction _catchFunction
Definition: pythonwriter.h:136
void write(const carto::GenericObject &object, int indent, const std::string &syntax="", const std::string &semantic="", bool writeInternals=false)
Writes an element according to the syntax.
void writeString(DataSource &, std::string)
utility function
PythonWriter(const std::string &filename, const carto::SyntaxSet &rules=carto::SyntaxSet(), const HelperSet &helpers=HelperSet())
void open(const std::string &filename)
virtual std::string name() const
std::map< std::string, Helper > HelperSet
Definition: pythonwriter.h:61
PythonWriter(carto::rc_ptr< DataSource > ds, const carto::SyntaxSet &rules=carto::SyntaxSet(), const HelperSet &helpers=HelperSet())
PythonWriter takes ownership of the DataSource.
void setCatchFunction(CatchFunction f)
if set, the catch function is used instead of throwing the regular exception during writing
void write(const carto::Object &object, bool writeInternals=false, bool writevariable=true)
void setSingleLineMode(bool x)
Definition: pythonwriter.h:111
bool is_open() const
void attach(carto::rc_ptr< DataSource > ds)
carto::rc_ptr< DataSource > dataSource()
Definition: pythonwriter.h:84
void(* Helper)(const carto::GenericObject &, PythonWriter &w, int indent, bool writeInternals)
Definition: pythonwriter.h:59
const HelperSet & helpers() const
helper functions read basic data type
void setNoneString(const std::string &none)
Definition: pythonwriter.h:119
void init(const HelperSet &helpers)
carto::rc_ptr< DataSource > _datasource
Definition: pythonwriter.h:133
std::string _noneString
Definition: pythonwriter.h:138
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)
virtual ~PythonWriter()
char quoteCharacter() const
Definition: pythonwriter.h:116
carto::SyntaxSet _rules
Definition: pythonwriter.h:132
std::string noneString() const
Definition: pythonwriter.h:118
void attach(std::ostream &s)
attach to an existing (and open) stream
std::map< std::string, Syntax > SyntaxSet
Object none()
Definition: allocator.h:49
#define DECLARE_GENERIC_OBJECT_TYPE(T)