1 if (($#ARGV!=0) and ($#ARGV!=1)) {
2 die 'usage: container.pl type [maxargs] (type=lst or exprseq)';
5 if ($ARGV[0] eq 'lst') {
7 } elsif ($ARGV[0] eq 'exprseq') {
10 die 'only lst and exprseq supported';
16 $maxargs=16; # must be greater or equal than the value used in function.pl
19 if ($type eq 'exprseq') {
21 # settings for exprseq
30 } elsif ($type eq 'lst') {
42 die "invalid type $type";
45 $CONTAINER_UC=uc(${CONTAINER});
46 $STLT="ex".$STLHEADER;
49 $RESERVE_IMPLEMENTATION="#define RESERVE(s,size) (s).reserve(size)";
51 $RESERVE_IMPLEMENTATION="#define RESERVE(s,size) // no reserve needed for ${STLHEADER}";
55 $PREPEND_INTERFACE=<<END_OF_PREPEND_INTERFACE;
56 virtual ${CONTAINER} & prepend(const ex & b);
57 virtual ${CONTAINER} & remove_first(void);
58 END_OF_PREPEND_INTERFACE
60 $PREPEND_IMPLEMENTATION=<<END_OF_PREPEND_IMPLEMENTATION;
61 ${CONTAINER} & ${CONTAINER}::prepend(const ex & b)
63 ensure_if_modifiable();
67 ${CONTAINER} & ${CONTAINER}::remove_first(void)
69 ensure_if_modifiable();
73 END_OF_PREPEND_IMPLEMENTATION
75 $PREPEND_INTERFACE=" // no prepend possible for ${CONTAINER}";
76 $PREPEND_IMPLEMENTATION="";
80 $LET_OP_IMPLEMENTATION=<<END_OF_LET_OP_IMPLEMENTATION
81 ex & ${CONTAINER}::let_op(int i)
84 GINAC_ASSERT(i<nops());
86 ${STLT}::iterator it=seq.begin();
87 for (int j=0; j<i; j++) {
92 END_OF_LET_OP_IMPLEMENTATION
94 $LET_OP_IMPLEMENTATION="// ${CONTAINER}::let_op() will be implemented by user elsewhere";
98 my ($seq_template,$n,$separator)=@_;
102 for ($N=1; $N<=$n; $N++) {
103 $res .= eval('"' . $seq_template . '"');
111 sub generate_from_to {
112 my ($template,$seq_template1,$seq_separator1,$seq_template2,
113 $seq_separator2,$from,$to)=@_;
117 for ($N=$from; $N<=$to; $N++) {
118 $SEQ1=generate_seq($seq_template1,$N,$seq_separator1);
119 $SEQ2=generate_seq($seq_template2,$N,$seq_separator2);
120 $res .= eval('"' . $template . '"');
121 $SEQ1=''; # to avoid main::SEQ1 used only once warning
122 $SEQ2=''; # same as above
128 my ($template,$seq_template1,$seq_separator1,$seq_template2,
130 return generate_from_to($template,$seq_template1,$seq_separator1,
131 $seq_template2,$seq_separator2,1,$maxargs);
134 $constructors_interface=generate(
135 ' explicit ${CONTAINER}(${SEQ1});'."\n",
136 'const ex & param${N}',', ','','');
138 $constructors_implementation=generate(
139 <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}',', ',' seq.push_back(param${N});',"\n");
140 ${CONTAINER}::${CONTAINER}(${SEQ1}) : basic(TINFO_${CONTAINER})
142 debugmsg(\"${CONTAINER} ctor from ${N}*ex\",LOGLEVEL_CONSTRUCT);
146 END_OF_CONSTRUCTORS_IMPLEMENTATION
148 $interface=<<END_OF_INTERFACE;
149 /** \@file ${CONTAINER}.h
151 * Definition of GiNaC's ${CONTAINER}. */
154 * This file was generated automatically by container.pl.
155 * Please do not modify it directly, edit the perl script instead!
156 * container.pl options: \$CONTAINER=${CONTAINER}
157 * \$STLHEADER=${STLHEADER}
158 * \$reserve=${reserve}
159 * \$prepend=${prepend}
161 * \$open_bracket=${open_bracket}
162 * \$close_bracket=${close_bracket}
163 * \$maxargs=${maxargs}
165 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
167 * This program is free software; you can redistribute it and/or modify
168 * it under the terms of the GNU General Public License as published by
169 * the Free Software Foundation; either version 2 of the License, or
170 * (at your option) any later version.
172 * This program is distributed in the hope that it will be useful,
173 * but WITHOUT ANY WARRANTY; without even the implied warranty of
174 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
175 * GNU General Public License for more details.
177 * You should have received a copy of the GNU General Public License
178 * along with this program; if not, write to the Free Software
179 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
182 #ifndef __GINAC_${CONTAINER_UC}_H__
183 #define __GINAC_${CONTAINER_UC}_H__
185 #include <${STLHEADER}>
187 // CINT needs <algorithm> to work properly with <vector> and <list>
196 typedef std::${STLHEADER}<ex> ${STLT};
198 class ${CONTAINER} : public basic
200 GINAC_DECLARE_REGISTERED_CLASS(${CONTAINER}, basic)
203 ${CONTAINER}(${STLT} const & s, bool discardable=0);
204 ${CONTAINER}(${STLT} * vp); // vp will be deleted
205 ${constructors_interface}
208 void print(const print_context & c, unsigned level = 0) const;
209 unsigned precedence(void) const {return 10;}
210 bool info(unsigned inf) const;
211 unsigned nops() const;
213 ex map(map_function & f) const;
214 ex eval(int level=0) const;
215 ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
217 bool is_equal_same_type(const basic & other) const;
218 unsigned return_type(void) const;
220 // new virtual functions which can be overridden by derived classes
222 virtual ${CONTAINER} & append(const ex & b);
223 virtual ${CONTAINER} & remove_last(void);
226 virtual void printseq(const print_context & c, char openbracket, char delim,
227 char closebracket, unsigned this_precedence,
228 unsigned upper_precedence = 0) const;
229 virtual ex this${CONTAINER}(${STLT} const & v) const;
230 virtual ex this${CONTAINER}(${STLT} * vp) const;
233 bool is_canonical() const;
234 ${STLT} evalchildren(int level) const;
235 ${STLT} * subschildren(const lst & ls, const lst & lr, bool no_pattern = false) const;
243 /** Return the ${CONTAINER} object handled by an ex. Deprecated: use ex_to<${CONTAINER}>().
244 * This is unsafe: you need to check the type first. */
245 inline const ${CONTAINER} &ex_to_${CONTAINER}(const ex &e)
247 return static_cast<const ${CONTAINER} &>(*e.bp);
250 /** Specialization of is_exactly_a<${CONTAINER}>(obj) for ${CONTAINER} objects. */
251 template<> inline bool is_exactly_a<${CONTAINER}>(const basic & obj)
253 return obj.tinfo()==TINFO_${CONTAINER};
256 inline ${CONTAINER} &ex_to_nonconst_${CONTAINER}(const ex &e)
258 return static_cast<${CONTAINER} &>(*e.bp);
263 #endif // ndef __GINAC_${CONTAINER_UC}_H__
267 $implementation=<<END_OF_IMPLEMENTATION;
268 /** \@file ${CONTAINER}.cpp
270 * Implementation of GiNaC's ${CONTAINER}. */
273 * This file was generated automatically by container.pl.
274 * Please do not modify it directly, edit the perl script instead!
275 * container.pl options: \$CONTAINER=${CONTAINER}
276 * \$STLHEADER=${STLHEADER}
277 * \$reserve=${reserve}
278 * \$prepend=${prepend}
280 * \$open_bracket=${open_bracket}
281 * \$close_bracket=${close_bracket}
282 * \$maxargs=${maxargs}
284 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
286 * This program is free software; you can redistribute it and/or modify
287 * it under the terms of the GNU General Public License as published by
288 * the Free Software Foundation; either version 2 of the License, or
289 * (at your option) any later version.
291 * This program is distributed in the hope that it will be useful,
292 * but WITHOUT ANY WARRANTY; without even the implied warranty of
293 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
294 * GNU General Public License for more details.
296 * You should have received a copy of the GNU General Public License
297 * along with this program; if not, write to the Free Software
298 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
304 #include "${CONTAINER}.h"
308 #include "debugmsg.h"
312 GINAC_IMPLEMENT_REGISTERED_CLASS(${CONTAINER}, basic)
314 ${RESERVE_IMPLEMENTATION}
317 // default ctor, dtor, copy ctor assignment operator and helpers
322 ${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER})
324 debugmsg("${CONTAINER} default ctor",LOGLEVEL_CONSTRUCT);
329 void ${CONTAINER}::copy(${CONTAINER} const & other)
331 inherited::copy(other);
335 void ${CONTAINER}::destroy(bool call_parent)
338 if (call_parent) inherited::destroy(call_parent);
347 ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) : basic(TINFO_${CONTAINER})
349 debugmsg("${CONTAINER} ctor from ${STLT}", LOGLEVEL_CONSTRUCT);
351 seq.swap(const_cast<${STLT} &>(s));
357 ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER})
359 debugmsg("${CONTAINER} ctor from ${STLT} *",LOGLEVEL_CONSTRUCT);
365 ${constructors_implementation}
371 /** Construct object from archive_node. */
372 ${CONTAINER}::${CONTAINER}(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
374 debugmsg("${CONTAINER} ctor from archive_node", LOGLEVEL_CONSTRUCT);
375 for (unsigned int i=0; true; i++) {
377 if (n.find_ex("seq", e, sym_lst, i))
384 /** Unarchive the object. */
385 ex ${CONTAINER}::unarchive(const archive_node &n, const lst &sym_lst)
387 return (new ${CONTAINER}(n, sym_lst))->setflag(status_flags::dynallocated);
390 /** Archive the object. */
391 void ${CONTAINER}::archive(archive_node &n) const
393 inherited::archive(n);
394 ${STLT}::const_iterator i = seq.begin(), iend = seq.end();
402 // functions overriding virtual functions from bases classes
407 void ${CONTAINER}::print(const print_context & c, unsigned level) const
409 debugmsg("${CONTAINER} print", LOGLEVEL_PRINT);
411 if (is_a<print_tree>(c)) {
413 c.s << std::string(level, ' ') << class_name()
414 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
415 << ", nops=" << nops()
417 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
418 for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit)
419 cit->print(c, level + delta_indent);
420 c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
423 // always print brackets around seq, ignore upper_precedence
424 printseq(c, '${open_bracket}', ',', '${close_bracket}', precedence(), precedence()+1);
428 // ${CONTAINER}::info() will be implemented by user elsewhere";
430 unsigned ${CONTAINER}::nops() const
435 ${LET_OP_IMPLEMENTATION}
437 ex ${CONTAINER}::map(map_function & f) const
440 RESERVE(s,seq.size());
441 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
445 return this${CONTAINER}(s);
448 ex ${CONTAINER}::eval(int level) const
453 return this${CONTAINER}(evalchildren(level));
456 ex ${CONTAINER}::subs(const lst & ls, const lst & lr, bool no_pattern) const
458 ${STLT} *vp = subschildren(ls, lr, no_pattern);
460 return this${CONTAINER}(vp).bp->basic::subs(ls, lr, no_pattern);
462 return basic::subs(ls, lr, no_pattern);
467 int ${CONTAINER}::compare_same_type(const basic & other) const
469 GINAC_ASSERT(is_a<${CONTAINER}>(other));
470 ${CONTAINER} const & o = static_cast<const ${CONTAINER} &>(other);
472 ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
473 it2 = o.seq.begin(), it2end = o.seq.end();
475 while (it1 != it1end && it2 != it2end) {
476 int cmpval = it1->compare(*it2);
482 return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
485 bool ${CONTAINER}::is_equal_same_type(const basic & other) const
487 GINAC_ASSERT(is_a<${CONTAINER}>(other));
488 ${CONTAINER} const &o = static_cast<const ${CONTAINER} &>(other);
490 if (seq.size() != o.seq.size())
493 ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
496 while (it1 != it1end) {
497 if (!it1->is_equal(*it2))
505 unsigned ${CONTAINER}::return_type(void) const
507 return return_types::noncommutative_composite;
511 // new virtual functions which can be overridden by derived classes
516 ${CONTAINER} & ${CONTAINER}::append(const ex & b)
518 ensure_if_modifiable();
523 ${CONTAINER} & ${CONTAINER}::remove_last(void)
525 ensure_if_modifiable();
530 ${PREPEND_IMPLEMENTATION}
534 void ${CONTAINER}::printseq(const print_context & c, char openbracket, char delim,
535 char closebracket, unsigned this_precedence,
536 unsigned upper_precedence) const
538 if (this_precedence <= upper_precedence)
541 if (seq.size() != 0) {
542 ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
544 while (it != itend) {
545 it->print(c, this_precedence);
549 it->print(c, this_precedence);
552 if (this_precedence <= upper_precedence)
556 ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const
558 return ${CONTAINER}(v);
561 ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
563 return ${CONTAINER}(vp);
567 // non-virtual functions in this class
576 bool ${CONTAINER}::is_canonical() const
578 if (seq.size()<=1) { return 1; }
580 ${STLT}::const_iterator it=seq.begin();
581 ${STLT}::const_iterator it_last=it;
582 for (++it; it!=seq.end(); it_last=it, ++it) {
583 if ((*it_last).compare(*it)>0) {
584 if ((*it_last).compare(*it)>0) {
585 std::cout << *it_last << ">" << *it << "\\n";
594 ${STLT} ${CONTAINER}::evalchildren(int level) const
597 RESERVE(s,seq.size());
602 if (level == -max_recursion_level) {
603 throw(std::runtime_error("max recursion level reached"));
606 for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
607 s.push_back((*it).eval(level));
612 ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr, bool no_pattern) const
614 // returns a NULL pointer if nothing had to be substituted
615 // returns a pointer to a newly created epvector otherwise
616 // (which has to be deleted somewhere else)
618 ${STLT}::const_iterator last=seq.end();
619 ${STLT}::const_iterator cit=seq.begin();
621 const ex & subsed_ex=(*cit).subs(ls,lr,no_pattern);
622 if (!are_ex_trivially_equal(*cit,subsed_ex)) {
624 // something changed, copy seq, subs and return it
625 ${STLT} *s=new ${STLT};
626 RESERVE(*s,seq.size());
628 // copy parts of seq which are known not to have changed
629 ${STLT}::const_iterator cit2=seq.begin();
634 // copy first changed element
635 s->push_back(subsed_ex);
639 s->push_back((*cit2).subs(ls,lr,no_pattern));
647 return 0; // nothing has changed
652 END_OF_IMPLEMENTATION
654 print "Creating interface file ${CONTAINER}.h...";
655 open OUT,">${CONTAINER}.h" or die "cannot open ${CONTAINER}.h";
656 print OUT $interface;
660 print "Creating implementation file ${CONTAINER}.cpp...";
661 open OUT,">${CONTAINER}.cpp" or die "cannot open ${CONTAINER}.cpp";
662 print OUT $implementation;