]> www.ginac.de Git - ginac.git/blobdiff - ginac/utils.h
* Zapped header dependency from <strstream>.
[ginac.git] / ginac / utils.h
index 5f34d02534bcf04e02708853dab8b40adcf5d703..6edea866c16f22c2e0d533f0a42f2513b88d31df 100644 (file)
 #ifndef __GINAC_UTILS_H__
 #define __GINAC_UTILS_H__
 
-#include <strstream>
+#include "config.h"
+
 #include <string>
 #include <stdexcept>
-#include "config.h"
+#if defined(HAVE_SSTREAM)
+#include <sstream>
+#elif defined(HAVE_STRSTREAM)
+#include <strstream>
+#else
+#error Need either sstream or strstream
+#endif
 #include "assertion.h"
 
 namespace GiNaC {
@@ -36,9 +43,15 @@ namespace GiNaC {
 template<class T>
 std::string ToString(const T & t)
 {
+#if defined(HAVE_SSTREAM)
+       std::ostringstream buf;
+       buf << t << std::ends;
+       return buf.str();
+#else
        char buf[256];
        std::ostrstream(buf,sizeof(buf)) << t << std::ends;
        return buf;
+#endif
 }
 
 /** Exception class thrown by classes which provide their own series expansion
@@ -243,6 +256,76 @@ const ex & _ex60(void);
 const numeric & _num120(void);    //  120
 const ex & _ex120(void);
 
+
+// Helper macros for class implementations (mostly useful for trivial classes)
+
+#define DEFAULT_COPY(classname) \
+void classname::copy(const classname & other) \
+{ \
+       inherited::copy(other); \
+}
+
+#define DEFAULT_DESTROY(classname) \
+void classname::destroy(bool call_parent) \
+{ \
+       if (call_parent) \
+               inherited::destroy(call_parent); \
+}
+
+#define DEFAULT_CTORS(classname) \
+classname::classname() : inherited(TINFO_##classname) \
+{ \
+       debugmsg(#classname " default constructor", LOGLEVEL_CONSTRUCT); \
+} \
+DEFAULT_COPY(classname) \
+DEFAULT_DESTROY(classname)
+
+#define DEFAULT_UNARCHIVE(classname) \
+ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
+{ \
+       return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
+}
+
+#define DEFAULT_ARCHIVING(classname) \
+classname::classname(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) \
+{ \
+       debugmsg(#classname " constructor from archive_node", LOGLEVEL_CONSTRUCT); \
+} \
+DEFAULT_UNARCHIVE(classname) \
+void classname::archive(archive_node &n) const \
+{ \
+       inherited::archive(n); \
+}
+
+#define DEFAULT_COMPARE(classname) \
+int classname::compare_same_type(const basic & other) const \
+{ \
+       /* by default, the objects are always identical */ \
+       return 0; \
+}
+
+#define DEFAULT_PRINT(classname, text) \
+void classname::print(const print_context & c, unsigned level) const \
+{ \
+       debugmsg(#classname " print", LOGLEVEL_PRINT); \
+       if (is_of_type(c, print_tree)) \
+               inherited::print(c, level); \
+       else \
+               c.s << text; \
+}
+
+#define DEFAULT_PRINT_LATEX(classname, text, latex) \
+void classname::print(const print_context & c, unsigned level) const \
+{ \
+       debugmsg(#classname " print", LOGLEVEL_PRINT); \
+       if (is_of_type(c, print_tree)) \
+               inherited::print(c, level); \
+       else if (is_of_type(c, print_latex)) \
+               c.s << latex; \
+       else \
+               c.s << text; \
+}
+
 } // namespace GiNaC
 
 #endif // ndef __GINAC_UTILS_H__