implemented operator-> for the iterators
[ginac.git] / ginac / class_info.h
1 /** @file class_info.h
2  *
3  *  Helper templates to provide per-class information for class hierarchies. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #ifndef __GINAC_CLASS_INFO_H__
24 #define __GINAC_CLASS_INFO_H__
25
26 #include <cstddef> // for size_t
27 #include <vector>
28 #include <map>
29 #include <string>
30 #include <iostream>
31 #include <iomanip>
32 #include <stdexcept>
33
34 namespace GiNaC {
35
36 // OPT is the class that stores the actual per-class data. It must provide
37 // get_name(), get_parent_name() and get_id() members.
38
39 template <class OPT>
40 class class_info {
41 public:
42         class_info(const OPT & o) : options(o), next(first), parent(NULL)
43         {
44                 first = this;
45                 parents_identified = false;
46         }
47
48         /** Get pointer to class_info of parent class (or NULL). */
49         class_info *get_parent() const
50         {
51                 identify_parents();
52                 return parent;
53         }
54
55         /** Find class_info by name. */
56         static const class_info *find(const std::string &class_name);
57
58         /** Dump class hierarchy to std::cout. */
59         static void dump_hierarchy(bool verbose = false);
60
61         OPT options;
62
63 private:
64         struct tree_node {
65                 tree_node(class_info *i) : info(i) {}
66                 void add_child(tree_node *n) { children.push_back(n); }
67
68                 std::vector<tree_node *> children;
69                 class_info *info;
70         };
71
72         static void dump_tree(tree_node *n, const std::string & prefix, bool verbose);
73         static void identify_parents();
74
75         static class_info *first;
76         class_info *next;
77         mutable class_info *parent;
78
79         static bool parents_identified;
80 };
81
82 template <class OPT>
83 const class_info<OPT> *class_info<OPT>::find(const std::string &class_name)
84 {
85         // Use a map for faster lookup. The registered_class_info list doesn't
86         // change at run-time, so it's sufficient to construct the map once
87         // on the first trip through this function.
88         typedef std::map<std::string, const class_info *> name_map_type;
89         static name_map_type name_map;
90         static bool name_map_initialized = false;
91
92         if (!name_map_initialized) {
93                 // Construct map
94                 const class_info *p = first;
95                 while (p) {
96                         name_map[p->options.get_name()] = p;
97                         p = p->next;
98                 }
99                 name_map_initialized = true;
100         }
101
102         typename name_map_type::const_iterator it = name_map.find(class_name);
103         if (it == name_map.end())
104                 throw (std::runtime_error("class '" + class_name + "' not registered"));
105         else
106                 return it->second;
107 }
108
109 template <class OPT>
110 void class_info<OPT>::dump_tree(tree_node *n, const std::string & prefix, bool verbose)
111 {
112         std::string name = n->info->options.get_name();
113         std::cout << name;
114         if (verbose)
115                 std::cout << " [ID 0x" << std::hex << std::setw(8) << std::setfill('0') << n->info->options.get_id() << std::dec << "]" << std::endl;
116
117         size_t num_children = n->children.size();
118         if (num_children) {
119                 for (size_t i = 0; i < num_children; ++i) {
120                         if (verbose) {
121                                 std::cout << prefix << " +- ";
122                                 if (i == num_children - 1)
123                                         dump_tree(n->children[i], prefix + "    ", verbose);
124                                 else
125                                         dump_tree(n->children[i], prefix + " |  ", verbose);
126                         } else {
127                                 std::string spaces(name.size(), ' ');
128                                 if (i > 0)
129                                         std::cout << prefix << spaces;
130                                 if (num_children == 1)
131                                         std::cout << " --- ";
132                                 else if (i > 0)
133                                         std::cout << "  +- ";
134                                 else
135                                         std::cout << " -+- ";
136                                 if (i == num_children - 1)
137                                         dump_tree(n->children[i], prefix + spaces + "     ", verbose);
138                                 else
139                                         dump_tree(n->children[i], prefix + spaces + "  |  ", verbose);
140                         }
141                 }
142         } else if (!verbose)
143                 std::cout << std::endl;
144 }
145
146 template <class OPT>
147 void class_info<OPT>::dump_hierarchy(bool verbose)
148 {
149         identify_parents();
150
151         // Create tree nodes for all class_infos
152         std::vector<tree_node> tree;
153         for (class_info *p = first; p; p = p->next)
154                 tree.push_back(tree_node(p));
155
156         // Identify children for all nodes and find the root
157         tree_node *root = NULL;
158         for (typename std::vector<tree_node>::iterator i = tree.begin(); i != tree.end(); ++i) {
159                 class_info *p = i->info->get_parent();
160                 if (p) {
161                         for (typename std::vector<tree_node>::iterator j = tree.begin(); j != tree.end(); ++j) {
162                                 if (j->info == p) {
163                                         j->add_child(&*i);
164                                         break;
165                                 }
166                         }
167                 } else
168                         root = &*i;
169         }
170
171         // Print hierarchy tree starting at the root
172         dump_tree(root, "", verbose);
173 }
174
175 template <class OPT>
176 void class_info<OPT>::identify_parents()
177 {
178         if (!parents_identified) {
179                 for (class_info *p = first; p; p = p->next) {
180                         const char *parent_name = p->options.get_parent_name();
181                         for (class_info *q = first; q; q = q->next) {
182                                 if (strcmp(q->options.get_name(), parent_name) == 0) {
183                                         p->parent = q;
184                                         break;
185                                 }
186                         }
187                 }
188                 parents_identified = true;
189         }
190 }
191
192 template <class OPT> class_info<OPT> *class_info<OPT>::first = NULL;
193 template <class OPT> bool class_info<OPT>::parents_identified = false;
194
195 } // namespace GiNaC
196
197 #endif // ndef __GINAC_CLASS_INFO_H__