Make is_class_loaded without any arguments fail loudly
[gitmo/Mouse.git] / xs-src / mouse_instance.xs
CommitLineData
43165725 1#include "mouse.h"\r
2\r
3#define CHECK_INSTANCE(instance) STMT_START{ \
4 if(!(SvROK(instance) && SvTYPE(SvRV(instance)) == SVt_PVHV)){ \
5 croak("Invalid object for instance managers"); \
6 } \
7 } STMT_END
8
9SV*
10mouse_instance_create(pTHX_ HV* const stash) {
11 assert(stash);
12 return sv_bless( newRV_noinc((SV*)newHV()), stash );
13}
14
15SV*
16mouse_instance_clone(pTHX_ SV* const instance) {
17 HV* proto;
18 assert(instance);
19
20 CHECK_INSTANCE(instance);
21 proto = newHVhv((HV*)SvRV(instance));
22 return sv_bless( newRV_noinc((SV*)proto), SvSTASH(SvRV(instance)) );
23}
24
25bool
26mouse_instance_has_slot(pTHX_ SV* const instance, SV* const slot) {
27 assert(instance);
28 assert(slot);
29 CHECK_INSTANCE(instance);
30 return hv_exists_ent((HV*)SvRV(instance), slot, 0U);
31}
32
33SV*
34mouse_instance_get_slot(pTHX_ SV* const instance, SV* const slot) {
35 HE* he;
36 assert(instance);
37 assert(slot);
38 CHECK_INSTANCE(instance);
39 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
40 return he ? HeVAL(he) : NULL;
41}
42
43SV*
44mouse_instance_set_slot(pTHX_ SV* const instance, SV* const slot, SV* const value) {
45 HE* he;
46 SV* sv;
47 assert(instance);
48 assert(slot);
49 assert(value);
50 CHECK_INSTANCE(instance);
51 he = hv_fetch_ent((HV*)SvRV(instance), slot, TRUE, 0U);
52 sv = HeVAL(he);
53 sv_setsv_mg(sv, value);
54 return sv;
55}
56
57SV*
58mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot) {
59 assert(instance);
60 assert(slot);
61 CHECK_INSTANCE(instance);
62 return hv_delete_ent((HV*)SvRV(instance), slot, 0, 0U);
63}
64
65void
66mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot) {
67 HE* he;
68 assert(instance);
69 assert(slot);
70 CHECK_INSTANCE(instance);
71 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
72 if(he){
73 sv_rvweaken(HeVAL(he));
74 }
75}
76\r