Cadabra
Computer algebra system for field theory problems
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
exec-stream.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2004 Artem Khodush
3 
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7 1. Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13 
14 3. The name of the author may not be used to endorse or promote products
15 derived from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef exec_stream_h
30 #define exec_stream_h
31 
32 #include <string>
33 #include <stdexcept>
34 #include <istream>
35 #include <ostream>
36 #include <vector>
37 
39 public:
40  exec_stream_t();
41  exec_stream_t( std::string const & program, std::string const & arguments );
42  template< class iterator > exec_stream_t( std::string const & program, iterator args_begin, iterator args_end );
43 
45 
47 
48  void set_buffer_limit( int stream_kind, std::size_t size );
49 
50  typedef unsigned long timeout_t;
51  void set_wait_timeout( int stream_kind, timeout_t milliseconds );
52 
53  void set_binary_mode( int stream_kind );
54  void set_text_mode( int stream_kind );
55 
56  void start( std::string const & program, std::string const & arguments );
57  template< class iterator > void start( std::string const & program, iterator args_begin, iterator args_end );
58  void start( std::string const & program, char const * arg1, char const * arg2 ); // to compensate for damage from the previous one
59  void start( std::string const & program, char * arg1, char * arg2 );
60 
61  bool close_in();
62  bool close();
63  void kill();
64  int exit_code();
65 
66  std::ostream & in();
67  std::istream & out();
68  std::istream & err();
69 
70  typedef unsigned long error_code_t;
71 
72  class error_t : public std::exception {
73  public:
74  error_t( std::string const & msg );
75  error_t( std::string const & msg, error_code_t code );
76  ~error_t() throw();
77  virtual char const * what() const throw();
78  protected:
79  error_t();
80  void compose( std::string const & msg, error_code_t code );
81 
82  std::string m_msg;
83  };
84 
85 private:
86  exec_stream_t( exec_stream_t const & );
88 
89  struct impl_t;
90  friend struct impl_t;
92 
93  void exceptions( bool enable );
94 
95 // helpers for template member functions
96  void new_impl();
97 
98  class next_arg_t {
99  public:
100  virtual ~next_arg_t()
101  {
102  }
103 
104  virtual std::string const * next()=0;
105  };
106 
107  template< class iterator > class next_arg_impl_t : public next_arg_t {
108  public:
109  next_arg_impl_t( iterator args_begin, iterator args_end )
110  : m_args_i( args_begin ), m_args_end( args_end )
111  {
112  }
113 
114  virtual std::string const * next()
115  {
116  if( m_args_i==m_args_end ) {
117  return 0;
118  }else {
119  m_arg=*m_args_i;
120  ++m_args_i;
121  return &m_arg;
122  }
123  }
124 
125  private:
126  iterator m_args_i;
127  iterator m_args_end;
128  std::string m_arg;
129  };
130 
131  void start( std::string const & program, next_arg_t & next_arg );
132 };
133 
134 template< class iterator > inline exec_stream_t::exec_stream_t( std::string const & program, iterator args_begin, iterator args_end )
135 {
136  new_impl();
137  exceptions( true );
138  start( program, args_begin, args_end );
139 }
140 
141 template< class iterator > inline void exec_stream_t::start( std::string const & program, iterator args_begin, iterator args_end )
142 {
143  exec_stream_t::next_arg_impl_t< iterator > next_arg( args_begin, args_end );
144  start( program, next_arg );
145 }
146 
147 inline void exec_stream_t::start( std::string const & program, char const * arg1, char const * arg2 )
148 {
149  std::vector< std::string > args;
150  args.push_back( std::string( arg1 ) );
151  args.push_back( std::string( arg2 ) );
152  start( program, args.begin(), args.end() );
153 }
154 
155 inline void exec_stream_t::start( std::string const & program, char * arg1, char * arg2 )
156 {
157  std::vector< std::string > args;
158  args.push_back( std::string( arg1 ) );
159  args.push_back( std::string( arg2 ) );
160  start( program, args.begin(), args.end() );
161 }
162 
163 #endif
error_t()
Definition: exec-stream.cc:436
Definition: exec-stream.h:98
Definition: exec-stream.h:46
iterator m_args_end
Definition: exec-stream.h:127
Definition: exec-stream.h:46
Definition: exec-stream.h:107
void exceptions(bool enable)
Definition: exec-stream.cc:401
Definition: exec-stream.h:72
void new_impl()
Definition: exec-stream.cc:372
unsigned long timeout_t
Definition: exec-stream.h:50
virtual char const * what() const
Definition: exec-stream.cc:454
exec_stream_t & operator=(exec_stream_t const &)
std::ostream & in()
Definition: exec-stream.cc:386
virtual ~next_arg_t()
Definition: exec-stream.h:100
~error_t()
Definition: exec-stream.cc:450
std::string m_msg
Definition: exec-stream.h:82
std::istream & out()
Definition: exec-stream.cc:391
exec_stream_t()
Definition: exec-stream.cc:359
void set_binary_mode(int stream_kind)
void set_text_mode(int stream_kind)
void start(std::string const &program, std::string const &arguments)
virtual std::string const * next()
Definition: exec-stream.h:114
friend struct impl_t
Definition: exec-stream.h:89
void compose(std::string const &msg, error_code_t code)
Definition: exec-stream.cc:460
virtual std::string const * next()=0
std::string m_arg
Definition: exec-stream.h:128
Definition: exec-stream.h:46
void set_wait_timeout(int stream_kind, timeout_t milliseconds)
Definition: exec-stream.h:46
std::istream & err()
Definition: exec-stream.cc:396
Definition: exec-stream.h:38
iterator m_args_i
Definition: exec-stream.h:126
void set_buffer_limit(int stream_kind, std::size_t size)
impl_t * m_impl
Definition: exec-stream.h:91
~exec_stream_t()
Definition: exec-stream.cc:377
unsigned long error_code_t
Definition: exec-stream.h:70
Definition: exec-stream.h:46
stream_kind_t
Definition: exec-stream.h:46
next_arg_impl_t(iterator args_begin, iterator args_end)
Definition: exec-stream.h:109