X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fbasic.h;h=a8062c15c81d4460e86f571a4ecb636d3c9df5f0;hp=b629f066d26bcc72d89de1a39723892f10b6617b;hb=65f2693a0948d1db0bc68d7656c64e1fed91c158;hpb=00d612ce3789ba9240ade1b8bf06b26c326bd8f2 diff --git a/ginac/basic.h b/ginac/basic.h index b629f066..a8062c15 100644 --- a/ginac/basic.h +++ b/ginac/basic.h @@ -33,6 +33,7 @@ #include #include // for typeid #include +#include namespace GiNaC { @@ -126,7 +127,12 @@ public: // only const functions please (may break reference counting) /** Create a clone of this object on the heap. One can think of this as * simulating a virtual copy constructor which is needed for instance by * the refcounted construction of an ex from a basic. */ - virtual basic * duplicate() const { return new basic(*this); } + virtual basic * duplicate() const + { + basic * bp = new basic(*this); + bp->setflag(status_flags::dynallocated); + return bp; + } // evaluation virtual ex eval(int level = 0) const; @@ -296,7 +302,6 @@ protected: mutable unsigned hashvalue; ///< hash value }; - // global variables extern int max_recursion_level; @@ -318,6 +323,31 @@ inline bool is_exactly_a(const basic & obj) return typeid(T) == typeid(obj); } +/** Constructs a new (class basic or derived) B object on the heap. + * + * This function picks the object's ctor based on the given argument types. + * + * This helps the constructor of ex from basic (or a derived class B) because + * then the constructor doesn't have to duplicate the object onto the heap. + * See ex::construct_from_basic(const basic &) for more information. + */ +template +inline B & dynallocate(Args &&... args) +{ + return const_cast(static_cast((new B(std::forward(args)...))->setflag(status_flags::dynallocated))); +} +/** Constructs a new (class basic or derived) B object on the heap. + * + * This function is needed for GiNaC classes which have public ctors from + * initializer lists of expressions (which are not a type and not captured + * by the variadic template version). + */ +template +inline B & dynallocate(std::initializer_list il) +{ + return const_cast(static_cast((new B(il))->setflag(status_flags::dynallocated))); +} + } // namespace GiNaC #endif // ndef GINAC_BASIC_H