cbase coverage


File: src/object/private/o_base.c
Date: 2024-07-26 20:57:32
Lines:
30/30
100.0%
Functions:
9/9
100.0%
Branches:
4/4
100.0%

Line Branch Exec Source
1 #include <stdlib.h>
2
3 #include "object.h"
4 #include "o_internal.h"
5
6 /**
7 * @brief Allocates and sets up object
8 *
9 * @param type The type of this object
10 * @param data Refrence to this ojects data
11 * @param connect_destroy when the object is destroyed, should the data be
12 * destroyed too?
13 * @return object_t* The object
14 */
15 31 object_t* object_create(
16 const type_t* type,
17 void* data,
18 bool connect_destroy
19 ) {
20 31 object_t* self = (object_t*)malloc(sizeof(*_self));
21 31 _type = type;
22 31 _data = data;
23 31 _connect_destroy = connect_destroy;
24 31 return (object_t*)_self;
25 }
26
27 /**
28 * @brief Destroy this object
29 *
30 * @param self The object
31 */
32 31 void object_destroy(object_t* self) {
33
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 17 times.
31 if (_connect_destroy)
34 14 type_destroy(_type, _data);
35 31 free(_self);
36 31 }
37
38 /**
39 * @brief Returns a copy of this object
40 *
41 * @param self The object
42 * @return void* A copy of this object
43 */
44 1 object_t* object_copy(const void* self) {
45 1 return object_create(_type, type_copy(_type, _data), true);
46 }
47
48 /**
49 * @brief Returns refrence to this ojects data
50 *
51 * @param self The object
52 * @return void* Refrence to this ojects data
53 */
54 2 void* object_data(const object_t* self) {
55 2 return _data;
56 }
57
58 /**
59 * @brief Returns the type of this object
60 *
61 * @param self The object
62 * @return const type_t* The type of this object
63 */
64 2 const type_t* object_data_type(const object_t* self) {
65 2 return _type;
66 }
67
68 /**
69 * @brief Returns the identifier of this object's type
70 *
71 * @param self The object
72 * @return const char* The identifier of this object's type
73 */
74 2 const char* object_identifier(const object_t* self) {
75 2 return type_identifier(_type);
76 }
77
78 /**
79 * @brief Returns the representation of this object
80 *
81 * @param self The object
82 * @return rope_t* The representation of this object
83 */
84 4 rope_t* object_repr(const object_t* self) {
85 4 return type_repr(_type, _data);
86 }
87
88 /**
89 * @brief Returns the hash of this object
90 *
91 * @param self The object
92 * @return uint64_t The hash of this object
93 */
94 4 uint64_t object_hash(const object_t* self) {
95 4 return type_hash(_type, _data);
96 }
97
98 /**
99 * @brief Returns the comparison of this object
100 *
101 * @param self The object
102 * @param other The object to compare to
103 * @return int
104 * >0: self > other
105 * <0: self < other
106 * =0: self = other
107 *
108 * @note if comparison functions are not the same, returns pointer comparison
109 */
110 6 int object_cmp(const object_t* self, const object_t* other) {
111
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (_type->cmp != ((__object_t*)other)->type->cmp)
112 3 return type_cmp(ptr_type, _data, ((__object_t*)other)->data);
113 3 return type_cmp(_type, _data, ((__object_t*)other)->data);
114 }
115
116 /**
117 * @brief Prints this object
118 *
119 * @param f The file to write to
120 * @param self The object
121 */
122 1 void __object_print(FILE* f, const object_t* self) {
123 1 __type_print(f, _type, _data);
124 1 }
125