1 /**
2  * \file    local.i
3  * \brief   Perl-specific SWIG directives for wrapping libSBML API
4  * \author  TBI {xtof,raim}@tbi.univie.ac.at
5  *
6  * Copyright 2004 TBI
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 2.1 of the License, or
11  * any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
15  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
16  * documentation provided hereunder is on an "as is" basis, and the
17  * California Institute of Technology and Japan Science and Technology
18  * Corporation have no obligations to provide maintenance, support,
19  * updates, enhancements or modifications.  In no event shall the
20  * California Institute of Technology or the Japan Science and Technology
21  * Corporation be liable to any party for direct, indirect, special,
22  * incidental or consequential damages, including lost profits, arising
23  * out of the use of this software and its documentation, even if the
24  * California Institute of Technology and/or Japan Science and Technology
25  * Corporation have been advised of the possibility of such damage.  See
26  * the GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with this library; if not, write to the Free Software Foundation,
30  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31  *
32  * The original code contained here was initially developed by:
33  *
34  *     Christoph Flamm and Rainer Machne
35  *
36  * Contributor(s):
37  */
38 
39 /**
40  * Convert SBase, SimpleSpeciesReference and Rule objects into the most specific type possible.
41  */
42 %typemap(out) SBase*, SimpleSpeciesReference*, Rule*, SBasePlugin*, SBMLExtension*, SBMLNamespaces*, SBMLConverter*, Reaction*
43 {
44   ST(argvi) = SWIG_NewPointerObj(SWIG_as_voidptr($1), GetDowncastSwigType($1),
45                                  $owner | %newpointer_flags);
46   argvi++;
47 }
48 
49 %typemap(out) ASTBasePlugin*
50 {
51   ST(argvi) = SWIG_NewPointerObj(SWIG_as_voidptr($1), GetDowncastSwigType($1),
52                                  $owner | %newpointer_flags);
53   argvi++;
54 }
55 
56 
57 
58 /**
59  * typemap to handle functions which take a FILE*
60  */
61 %typemap(in) FILE * {
62   if (SvOK($input)) /* check for undef */
63         $1 = PerlIO_findFILE(IoIFP(sv_2io($input)));
64   else  $1 = NULL;
65 }
66 
67 /**
68  * By default, returned boolean false (C++) is converted to "" (Perl) in
69  * SWIG 1.3.31.
70  * The following typemap converts returned boolean value to 0 (false) or
71  * 1 (true) like C/C++ for compatibility.
72  */
73 %typemap(out) bool
74 {
75    ST(argvi) = sv_newmortal();
76    sv_setiv(ST(argvi++), (IV) $1);
77 }
78 
79 /**
80  * Wraps standard output streams
81  */
82 
83 %include "std_string.i"
84 
85 %{
86 #include <iostream>
87 #include <sstream>
88 %}
89 
90 %rename(COUT) cout;
91 %rename(CERR) cerr;
92 %rename(CLOG) clog;
93 
94 namespace std
95 {
96 %immutable;
97 extern std::ostream cout;
98 extern std::ostream cerr;
99 extern std::ostream clog;
100 %mutable;
101 }
102 
103 /**
104  * Wraps std::ostream by implementing two simple wrapper classes.
105  *
106  * 1) OStream wraps std::cout, std::cerr, and std::clog.
107  *    The following public final static variables are provied in
108  *    libsbml class like in C++.
109  *
110  *    1. public final static OStream cout;
111  *    2. public final static OStream cerr;
112  *    3. public final static OStream clog;
113  *
114  * 2) OStringStream (derived class of OStream) wraps std::ostringstream.
115  *
116  * These wrapper classes provide only the minimum functions.
117  *
118  * (sample code) -----------------------------------------------------
119  *
120  * 1. wraps std::cout
121  *
122  *    my $xos = new LibSBML::XMLOutputStream($LibSBML::COUT);
123  *
124  * 2. wraps std::cerr
125  *
126  *    my $rd = new LibSBML::SBMLReader
127  *    my $d = $rd->readSBML("foo.xml");
128  *    if ( $d->getNumErrors() > 0) {
129  *       $d->printErrors($LibSBML::CERR);
130  *    }
131  * 3. wraps std::ostringstream
132  *
133  *    my $oss = new LibSBML::OStringStream();
134  *    my $xos = new XMLOutputStream($oss->get_ostream());
135  *    my $p   = new LibSBML::Parameter("p", 3.31);
136  *    $p->write($xos);
137  *    $oss->endl();
138  *    print $oss->str();
139  *
140  */
141 
142 %include "OStream.h"
143 
144 %{
145 #include "OStream.cpp"
146 %}
147 
148 /**
149  * Renames functions whose name is Perl keyword
150  *
151  */
152 
153 %include list_of_fix.i
154 %include list_get_fix.i
155 
156 /**
157  * Wraps the SBMLConstructorException
158  *
159  * The SBMLConstructorException (C++ class) is converted into
160  * Perl exception.
161  *
162  * For example, the exception can be catched in Perl as follows:
163  *
164  *  -----------------------------------------------------------------
165  *  eval
166  *  {
167  *    $m = new LibSBML::Model($level,$version);
168  *  };
169  *  if ($@)
170  *  {
171  *    warn $@; # print error message
172  *    $m = new LibSBML::Model(2,4);
173  *  }
174  *  -----------------------------------------------------------------
175  */
176 
177 %define SBMLCONSTRUCTOR_EXCEPTION(SBASE_CLASS_NAME)
178 %exception SBASE_CLASS_NAME {
179   try {
180     $action
181   }
catch(SBMLConstructorException & e)182   catch (SBMLConstructorException &e){
183     croak("%s", e.what());
184   }
catch(SBMLExtensionException & e)185   catch (SBMLExtensionException &e){
186     croak("%s", e.what());
187   }
188 }
189 %enddef
190 
191 SBMLCONSTRUCTOR_EXCEPTION(Compartment)
192 SBMLCONSTRUCTOR_EXCEPTION(CompartmentType)
193 SBMLCONSTRUCTOR_EXCEPTION(Constraint)
194 SBMLCONSTRUCTOR_EXCEPTION(Delay)
195 SBMLCONSTRUCTOR_EXCEPTION(Event)
196 SBMLCONSTRUCTOR_EXCEPTION(EventAssignment)
197 SBMLCONSTRUCTOR_EXCEPTION(FunctionDefinition)
198 SBMLCONSTRUCTOR_EXCEPTION(InitialAssignment)
199 SBMLCONSTRUCTOR_EXCEPTION(KineticLaw)
200 SBMLCONSTRUCTOR_EXCEPTION(Model)
201 SBMLCONSTRUCTOR_EXCEPTION(Parameter)
202 SBMLCONSTRUCTOR_EXCEPTION(Priority)
203 SBMLCONSTRUCTOR_EXCEPTION(LocalParameter)
204 SBMLCONSTRUCTOR_EXCEPTION(Reaction)
205 SBMLCONSTRUCTOR_EXCEPTION(AssignmentRule)
206 SBMLCONSTRUCTOR_EXCEPTION(AlgebraicRule)
207 SBMLCONSTRUCTOR_EXCEPTION(RateRule)
208 SBMLCONSTRUCTOR_EXCEPTION(Species)
209 SBMLCONSTRUCTOR_EXCEPTION(SpeciesReference)
210 SBMLCONSTRUCTOR_EXCEPTION(ModifierSpeciesReference)
211 SBMLCONSTRUCTOR_EXCEPTION(SpeciesType)
212 SBMLCONSTRUCTOR_EXCEPTION(StoichiometryMath)
213 SBMLCONSTRUCTOR_EXCEPTION(Trigger)
214 SBMLCONSTRUCTOR_EXCEPTION(Unit)
215 SBMLCONSTRUCTOR_EXCEPTION(UnitDefinition)
216 SBMLCONSTRUCTOR_EXCEPTION(SBMLDocument)
217 SBMLCONSTRUCTOR_EXCEPTION(SBMLNamespaces)
218 SBMLCONSTRUCTOR_EXCEPTION(SBMLExtensionNamespaces)
219 
220 SBMLCONSTRUCTOR_EXCEPTION(ListOf)
221 SBMLCONSTRUCTOR_EXCEPTION(ListOfCompartments)
222 SBMLCONSTRUCTOR_EXCEPTION(ListOfCompartmentTypes)
223 SBMLCONSTRUCTOR_EXCEPTION(ListOfConstraints)
224 SBMLCONSTRUCTOR_EXCEPTION(ListOfEventAssignments)
225 SBMLCONSTRUCTOR_EXCEPTION(ListOfEvents)
226 SBMLCONSTRUCTOR_EXCEPTION(ListOfFunctionDefinitions)
227 SBMLCONSTRUCTOR_EXCEPTION(ListOfInitialAssignments)
228 SBMLCONSTRUCTOR_EXCEPTION(ListOfParameters)
229 SBMLCONSTRUCTOR_EXCEPTION(ListOfLocalParameters)
230 SBMLCONSTRUCTOR_EXCEPTION(ListOfReactions)
231 SBMLCONSTRUCTOR_EXCEPTION(ListOfRules)
232 SBMLCONSTRUCTOR_EXCEPTION(ListOfSpecies)
233 SBMLCONSTRUCTOR_EXCEPTION(ListOfSpeciesReferences)
234 SBMLCONSTRUCTOR_EXCEPTION(ListOfSpeciesTypes)
235 SBMLCONSTRUCTOR_EXCEPTION(ListOfUnitDefinitions)
236 SBMLCONSTRUCTOR_EXCEPTION(ListOfUnits)
237 
238 /**
239  * Wraps the XMLConstructorException
240  *
241  * The XMLConstructorException (C++ class) is converted into
242  * Perl exception.
243  *
244  * For example, the exception can be catched in Perl as follows:
245  *
246  *  -----------------------------------------------------------------
247  *  eval
248  *  {
249  *    $m = new LibSBML::XMLAttributes(invalid arguments);
250  *  };
251  *  if ($@)
252  *  {
253  *    warn $@; # print error message
254  *  }
255  *  -----------------------------------------------------------------
256  */
257 
258 %define XMLCONSTRUCTOR_EXCEPTION(SBASE_CLASS_NAME)
259 %exception SBASE_CLASS_NAME {
260   try {
261     $action
262   }
catch(XMLConstructorException & e)263   catch (XMLConstructorException &e){
264     croak("%s", e.what());
265   }
266 }
267 %enddef
268 
269 XMLCONSTRUCTOR_EXCEPTION(XMLAttributes)
270 XMLCONSTRUCTOR_EXCEPTION(XMLError)
271 XMLCONSTRUCTOR_EXCEPTION(XMLNamespaces)
272 XMLCONSTRUCTOR_EXCEPTION(XMLNode)
273 XMLCONSTRUCTOR_EXCEPTION(XMLOutputStream)
274 XMLCONSTRUCTOR_EXCEPTION(XMLToken)
275 XMLCONSTRUCTOR_EXCEPTION(XMLTripple)
276 
277 %include "local-packages.i"
278