]> www.ginac.de Git - cln.git/blob - src/base/symbol/cl_symbol.cc
* All Files have been modified for inclusion of namespace cln;
[cln.git] / src / base / symbol / cl_symbol.cc
1 // class cl_symbol.
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 CL_PROVIDE(cl_symbol)
7
8 // Specification.
9 #include "cln/symbol.h"
10
11
12 // Implementation.
13
14 #include "cl_hashuniqweak.h"
15
16 namespace cln {
17
18 inline const cl_string hashkey (const cl_symbol& sym)
19 {
20         return (cl_string)sym;
21 }
22
23 // A symbol points to a string, so to convert cl_string -> cl_symbol, we just
24 // take the pointer and put it into a cl_symbol.
25 inline cl_symbol::cl_symbol (struct hashuniq * null, const cl_string& s)
26         : cl_rcpointer (as_cl_private_thing(s)) { unused null; }
27
28 typedef cl_htuniqentry<cl_string,cl_symbol> cl_htentry_from_string_to_symbol;
29
30 typedef cl_heap_weak_hashtable_uniq<cl_string,cl_symbol> cl_heap_hashtable_from_string_to_symbol;
31
32 typedef _cl_hashtable_iterator<cl_htentry_from_string_to_symbol> cl_hashtable_from_string_to_symbol_iterator;
33
34 static void cl_hashtable_from_string_to_symbol_destructor (cl_heap* pointer)
35 {
36 #if (defined(__mips__) || defined(__mips64__)) && !defined(__GNUC__) // workaround SGI CC bug
37         (*(cl_heap_hashtable_from_string_to_symbol*)pointer).~cl_heap_weak_hashtable_uniq();
38 #else
39         (*(cl_heap_hashtable_from_string_to_symbol*)pointer).~cl_heap_hashtable_from_string_to_symbol();
40 #endif
41 }
42
43 cl_class cl_class_hashtable_from_string_to_symbol = {
44         cl_hashtable_from_string_to_symbol_destructor,
45         0
46 };
47
48 struct cl_ht_from_string_to_symbol : public cl_gcpointer {
49         // Constructors.
50         cl_ht_from_string_to_symbol ();
51         cl_ht_from_string_to_symbol (const cl_ht_from_string_to_symbol&);
52         // Assignment operators.
53         cl_ht_from_string_to_symbol& operator= (const cl_ht_from_string_to_symbol&);
54         // Iterator.
55         cl_hashtable_from_string_to_symbol_iterator iterator () const
56         { return ((cl_heap_hashtable_from_string_to_symbol*)pointer)->iterator(); }
57         // Lookup.
58         cl_symbol * get (const cl_string& s) const;
59         // Store.
60         void put (const cl_string& s) const;
61 };
62
63 // These are not inline, because they tend to duplicate a lot of template code.
64
65 cl_ht_from_string_to_symbol::cl_ht_from_string_to_symbol ()
66 {
67         var cl_heap_hashtable_from_string_to_symbol* ht = new cl_heap_hashtable_from_string_to_symbol ();
68         ht->refcount = 1;
69         ht->type = &cl_class_hashtable_from_string_to_symbol;
70         pointer = ht;
71 }
72
73 cl_symbol * cl_ht_from_string_to_symbol::get (const cl_string& s) const
74 {
75         return ((cl_heap_hashtable_from_string_to_symbol*)pointer)->get(s);
76 }
77
78 void cl_ht_from_string_to_symbol::put (const cl_string& s) const
79 {
80         ((cl_heap_hashtable_from_string_to_symbol*)pointer)->put(s);
81 }
82
83 // The global symbol table.
84 static cl_ht_from_string_to_symbol symbol_table;
85
86 // Create or lookup a symbol from its name.
87 cl_symbol::cl_symbol (const cl_string& s)
88 {
89         var cl_symbol * sym_in_table;
90         sym_in_table = symbol_table.get(s);
91         if (!sym_in_table) {
92                 symbol_table.put(s);
93                 sym_in_table = symbol_table.get(s);
94                 if (!sym_in_table)
95                         cl_abort();
96         }
97         var cl_heap* p = sym_in_table->heappointer;
98         cl_inc_pointer_refcount(p);
99         pointer = p;
100 }
101
102 }  // namespace cln
103
104 CL_PROVIDE_END(cl_symbol)