2 * @file mystring.cpp Example of extending GiNaC: writing new classes
8 #include <ginac/ginac.h>
11 using namespace GiNaC;
13 class mystring : public basic
15 GINAC_DECLARE_REGISTERED_CLASS(mystring, basic)
17 mystring(const string &s);
18 ex eval() const override;
23 void do_print(const print_context &c, unsigned level = 0) const;
27 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(mystring, basic,
28 print_func<print_context>(&mystring::do_print))
31 mystring::mystring() { }
32 mystring::mystring(const string &s) : str(s) { }
35 int mystring::compare_same_type(const basic &other) const
37 const mystring &o = static_cast<const mystring &>(other);
38 int cmpval = str.compare(o.str);
48 void mystring::do_print(const print_context &c, unsigned level) const
50 // print_context::s is a reference to an ostream
51 c.s << '\"' << str << '\"';
55 * evaluation: all strings automatically converted to lowercase with
56 * non-alphabetic characters stripped, and empty strings removed
58 ex mystring::eval() const
61 for (size_t i=0; i<str.length(); i++) {
63 if (c >= 'A' && c <= 'Z')
64 new_str += tolower(c);
65 else if (c >= 'a' && c <= 'z')
69 if (new_str.length() == 0)
72 return mystring(new_str).hold();
75 int main(int argc, char** argv)
77 ex e = mystring("Hello, world!");
78 cout << is_a<mystring>(e) << endl;
79 cout << ex_to<basic>(e).class_name() << endl;
81 ex another = pow(mystring("One string"), 2*sin(Pi-mystring("Another string")));
82 cout << another << endl;