]> www.ginac.de Git - ginac.git/blob - doc/examples/mystring.cpp
[bugfix] remainder_in_ring: using exact division is plain wrong.
[ginac.git] / doc / examples / mystring.cpp
1 /**
2  * @file mystring.cpp Example of extending GiNaC: writing new classes
3  */
4 #include <iostream>
5 #include <string>   
6 #include <stdexcept>
7 using namespace std;
8
9 #include <ginac/ginac.h>
10 using namespace GiNaC;
11
12 class mystring : public basic
13 {
14         GINAC_DECLARE_REGISTERED_CLASS(mystring, basic)
15 public:
16         mystring(const string &s);
17         mystring(const char *s);
18         ex eval(int level) const;
19
20 private:
21         string str;
22
23 protected:
24         void do_print(const print_context &c, unsigned level = 0) const;
25
26 };
27
28 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(mystring, basic,
29   print_func<print_context>(&mystring::do_print))
30
31 // ctors
32 mystring::mystring() : inherited(&mystring::tinfo_static) { }
33 mystring::mystring(const string &s) : inherited(&mystring::tinfo_static), str(s) { }
34 mystring::mystring(const char *s) : inherited(&mystring::tinfo_static), str(s) { }
35
36 // comparison
37 int mystring::compare_same_type(const basic &other) const
38 {
39         const mystring &o = static_cast<const mystring &>(other);
40         int cmpval = str.compare(o.str);
41         if (cmpval == 0)
42                 return 0;
43         else if (cmpval < 0)
44                 return -1;
45         else
46                 return 1;
47 }
48
49 // archiving/unarchiving
50 mystring::mystring(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
51 {
52         n.find_string("string", str);
53 }
54
55 void mystring::archive(archive_node &n) const
56 {
57         inherited::archive(n);
58         n.add_string("string", str);
59 }
60
61 ex mystring::unarchive(const archive_node &n, lst &sym_lst)
62 {
63         return (new mystring(n, sym_lst))->setflag(status_flags::dynallocated);
64 }
65
66 // printing
67 void mystring::do_print(const print_context &c, unsigned level) const
68 {
69         // print_context::s is a reference to an ostream
70         c.s << '\"' << str << '\"';
71 }
72
73 /**
74  * evaluation: all strings automatically converted to lowercase with
75  * non-alphabetic characters stripped, and empty strings removed
76  */
77 ex mystring::eval(int level) const
78 {
79         string new_str;
80         for (size_t i=0; i<str.length(); i++) {
81                 char c = str[i];
82                 if (c >= 'A' && c <= 'Z') 
83                         new_str += tolower(c);
84                 else if (c >= 'a' && c <= 'z')
85                         new_str += c;
86         }
87
88         if (new_str.length() == 0)
89                 return 0;
90         else
91                 return mystring(new_str).hold();
92 }
93
94 int main(int argc, char** argv)
95 {
96         ex e = mystring("Hello, world!");
97         cout << is_a<mystring>(e) << endl;
98         cout << ex_to<basic>(e).class_name() << endl;
99         cout << e << endl;
100         ex another = pow(mystring("One string"), 2*sin(Pi-mystring("Another string")));
101         cout << another << endl;
102         return 0;
103 }