2 * @file mystring.cpp Example of extending GiNaC: writing new classes
9 #include <ginac/ginac.h>
10 using namespace GiNaC;
12 class mystring : public basic
14 GINAC_DECLARE_REGISTERED_CLASS(mystring, basic)
16 mystring(const string &s);
17 ex eval(int level) const override;
22 void do_print(const print_context &c, unsigned level = 0) const;
26 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(mystring, basic,
27 print_func<print_context>(&mystring::do_print))
30 mystring::mystring() { }
31 mystring::mystring(const string &s) : str(s) { }
34 int mystring::compare_same_type(const basic &other) const
36 const mystring &o = static_cast<const mystring &>(other);
37 int cmpval = str.compare(o.str);
47 void mystring::do_print(const print_context &c, unsigned level) const
49 // print_context::s is a reference to an ostream
50 c.s << '\"' << str << '\"';
54 * evaluation: all strings automatically converted to lowercase with
55 * non-alphabetic characters stripped, and empty strings removed
57 ex mystring::eval(int level) const
60 for (size_t i=0; i<str.length(); i++) {
62 if (c >= 'A' && c <= 'Z')
63 new_str += tolower(c);
64 else if (c >= 'a' && c <= 'z')
68 if (new_str.length() == 0)
71 return mystring(new_str).hold();
74 int main(int argc, char** argv)
76 ex e = mystring("Hello, world!");
77 cout << is_a<mystring>(e) << endl;
78 cout << ex_to<basic>(e).class_name() << endl;
80 ex another = pow(mystring("One string"), 2*sin(Pi-mystring("Another string")));
81 cout << another << endl;