2 * @file mystring.cpp Example of extending GiNaC: writing new classes
10 #include <ginac/ginac.h>
13 using namespace GiNaC;
15 class mystring : public basic
17 GINAC_DECLARE_REGISTERED_CLASS(mystring, basic)
19 mystring(const string &s);
20 ex eval() const override;
25 void do_print(const print_context &c, unsigned level = 0) const;
29 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(mystring, basic,
30 print_func<print_context>(&mystring::do_print))
33 mystring::mystring() { }
34 mystring::mystring(const string &s) : str(s) { }
37 int mystring::compare_same_type(const basic &other) const
39 const mystring &o = static_cast<const mystring &>(other);
40 int cmpval = str.compare(o.str);
50 void mystring::do_print(const print_context &c, unsigned level) const
52 // print_context::s is a reference to an ostream
53 c.s << '\"' << str << '\"';
57 * evaluation: all strings automatically converted to lowercase with
58 * non-alphabetic characters stripped, and empty strings removed
60 ex mystring::eval() const
63 for (size_t i=0; i<str.length(); i++) {
65 if (c >= 'A' && c <= 'Z')
66 new_str += tolower(c);
67 else if (c >= 'a' && c <= 'z')
71 if (new_str.length() == 0)
74 return mystring(new_str).hold();
77 int main(int argc, char** argv)
79 ex e = mystring("Hello, world!");
80 cout << is_a<mystring>(e) << endl;
81 cout << ex_to<basic>(e).class_name() << endl;
83 ex another = pow(mystring("One string"), 2*sin(Pi-mystring("Another string")));
84 cout << another << endl;