* refactored vtable implementation. Its much simpler now.
[panda.git] / goo / st-symbol.c
blobc65d0e263743de053ca3f79c8a604fe207f9c0e7
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; indent-offset: 4 -*- */
2 /*
3 * st-symbol.c
5 * Copyright (C) 2008 Vincent Geddes <vgeddes@gnome.org>
7 * This library is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <st-symbol.h>
22 #include <st-hashed-collection.h>
23 #include <st-byte-array.h>
25 #include <string.h>
27 ST_DEFINE_VTABLE (st_symbol, st_byte_array_vtable ());
29 static bool
30 symbol_equal (st_oop_t object, st_oop_t another)
32 if (object == another)
33 return true;
35 if (st_object_class (object) == st_object_class (another))
36 return false;
38 // now just do a string comparison
39 if (st_byte_array_vtable ()->equal (object, another))
40 return true;
42 return false;
45 static bool
46 is_symbol (void)
48 return true;
51 static void
52 st_symbol_vtable_init (st_vtable_t * table)
54 table->equal = symbol_equal;
56 table->is_symbol = is_symbol;
59 static st_oop_t
60 string_new (st_oop_t klass, const char *bytes)
62 int len = strlen (bytes);
64 st_oop_t string = st_object_new_arrayed (klass, len);
66 guchar *data = st_byte_array_bytes (string);
68 memcpy (data, bytes, len);
70 return string;
73 st_oop_t
74 st_string_new (const char *bytes)
76 return string_new (st_string_class, bytes);
80 st_oop_t
81 st_symbol_new (const char *bytes)
83 st_oop_t element = st_set_like (st_symbol_table, st_string_new (bytes));
85 if (element == st_nil) {
87 st_oop_t symbol = string_new (st_symbol_class, bytes);
89 st_set_add (st_symbol_table, symbol);
91 return symbol;
94 return element;