List type and generic base type, char wrapper type
This commit is contained in:
parent
647f739d08
commit
471cf679cd
6 changed files with 124 additions and 5 deletions
45
src/object-type.c
Normal file
45
src/object-type.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
#include "object-type.h"
|
||||
|
||||
int ObjectType_Create(ObjectType* self, StringView name, ObjectType* supertype, size_t attribute_count_guess, allocator_t* allocator)
|
||||
{
|
||||
struct HashMapConfig config = HashMap_DefaultConfig();
|
||||
config.allocator = allocator;
|
||||
int hashmap_code = HashMap_Create(&self->attributes, &config, attribute_count_guess * (1 / config.load_factor) + 1);
|
||||
if (hashmap_code) {
|
||||
return hashmap_code;
|
||||
}
|
||||
self->name = name;
|
||||
self->reference_count = 0;
|
||||
supertype->reference_count++;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void ObjectType_Destroy(ObjectType* self)
|
||||
{
|
||||
self->supertype->reference_count--;
|
||||
// TODO: Emit a warning when there are still references?
|
||||
HashMap_Destroy(&self->attributes);
|
||||
}
|
||||
|
||||
int ObjectType_DefineObjectAttribute(ObjectType* self, ObjectType* attribute_type, StringView attribute_name)
|
||||
{
|
||||
ObjectTypeAttribute attribute = {
|
||||
.name = attribute_name,
|
||||
VALUETYPE_OBJECT,
|
||||
attribute_type,
|
||||
HashMap_Size(&self->attributes),
|
||||
};
|
||||
return HashMap_Put(&self->attributes, attribute_name.source, attribute_name.length, &attribute);
|
||||
}
|
||||
|
||||
int ObjectType_DefinePrimitiveAttribute(ObjectType* self, enum ValueType attribute_type, StringView attribute_name)
|
||||
{
|
||||
ObjectTypeAttribute attribute = {
|
||||
.name = attribute_name,
|
||||
attribute_type,
|
||||
NULL,
|
||||
HashMap_Size(&self->attributes),
|
||||
};
|
||||
return HashMap_Put(&self->attributes, attribute_name.source, attribute_name.length, &attribute);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue