]> www.ginac.de Git - ginac.git/blob - doc/examples/mystring.cpp
Don't force every algebraic class to implement archiving/unarchiving.
[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() { }
33 mystring::mystring(const string &s) :  str(s) { }
34 mystring::mystring(const char *s) :  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 void mystring::read_archive(const archive_node &n, lst &sym_lst)
51 {
52         inherited::read_archive(n, sym_lst);
53         n.find_string("string", str);
54 }
55
56 void mystring::archive(archive_node &n) const
57 {
58         inherited::archive(n);
59         n.add_string("string", str);
60 }
61
62 ex mystring::unarchive(const archive_node &n, lst &sym_lst)
63 {
64         return (new mystring(n, sym_lst))->setflag(status_flags::dynallocated);
65 }
66
67 // printing
68 void mystring::do_print(const print_context &c, unsigned level) const
69 {
70         // print_context::s is a reference to an ostream
71         c.s << '\"' << str << '\"';
72 }
73
74 /**
75  * evaluation: all strings automatically converted to lowercase with
76  * non-alphabetic characters stripped, and empty strings removed
77  */
78 ex mystring::eval(int level) const
79 {
80         string new_str;
81         for (size_t i=0; i<str.length(); i++) {
82                 char c = str[i];
83                 if (c >= 'A' && c <= 'Z') 
84                         new_str += tolower(c);
85                 else if (c >= 'a' && c <= 'z')
86                         new_str += c;
87         }
88
89         if (new_str.length() == 0)
90                 return 0;
91         else
92                 return mystring(new_str).hold();
93 }
94
95 int main(int argc, char** argv)
96 {
97         ex e = mystring("Hello, world!");
98         cout << is_a<mystring>(e) << endl;
99         cout << ex_to<basic>(e).class_name() << endl;
100         cout << e << endl;
101         ex another = pow(mystring("One string"), 2*sin(Pi-mystring("Another string")));
102         cout << another << endl;
103         return 0;
104 }