StateFS
 All Classes Files Functions Variables Typedefs Enumerations Groups Pages
util.h
1 #ifndef _STATEFS_UTIL_H_
2 #define _STATEFS_UTIL_H_
3 
4 #include <statefs/provider.h>
5 
6 #ifndef container_of
7 #define container_of(ptr, type, member) \
8  ((type *)( (char *)(ptr) - offsetof(type, member) ))
9 #endif
10 
11 EXTERN_C_BEGIN
12 
13 #define STATEFS_META(name, type, value) { name, STATEFS_##type(value) }
14 #define STATEFS_META_END { NULL, {} }
15 
16 static inline void statefs_node_release(struct statefs_node *node)
17 {
18  if (node && node->release)
19  node->release(node);
20 }
21 
22 static inline void statefs_ns_release(struct statefs_namespace *ns)
23 {
24  if (ns)
25  statefs_node_release(&ns->node);
26 }
27 
28 static inline void statefs_provider_release(struct statefs_provider *p)
29 {
30  if (p)
31  statefs_ns_release(&p->root);
32 }
33 
34 static inline intptr_t statefs_first(struct statefs_branch const* self)
35 {
36  return (self->first ? self->first(self) : 0);
37 }
38 
39 static inline void statefs_next(struct statefs_branch const* self, intptr_t *p)
40 {
41  if (self->next)
42  self->next(self, p);
43  else
44  *p = 0;
45 }
46 
47 static inline void statefs_branch_release
48 (struct statefs_branch const* self, intptr_t p)
49 {
50  if (self && self->release)
51  self->release(self, p);
52 }
53 
54 static inline struct statefs_node * statefs_get
55 (struct statefs_branch const* self, intptr_t p)
56 {
57  return (self->get ? self->get(self, p) : NULL);
58 }
59 
60 static inline struct statefs_property * statefs_prop_get
61 (struct statefs_branch const* self, intptr_t iter)
62 {
63  struct statefs_node *n = statefs_get(self, iter);
64  return (n && n->type == statefs_node_prop
65  ? container_of(n, struct statefs_property, node)
66  : NULL);
67 }
68 
69 static inline struct statefs_namespace *
70 statefs_node2ns(struct statefs_node *n)
71 {
72  return (n && n->type | statefs_node_ns
73  ? container_of(n, struct statefs_namespace, node)
74  : NULL);
75 }
76 
77 static inline struct statefs_namespace * statefs_ns_get
78 (struct statefs_branch const* self, intptr_t iter)
79 {
80  return statefs_node2ns(statefs_get(self, iter));
81 }
82 
83 static inline struct statefs_property * statefs_prop_find
84 (struct statefs_namespace const* self, char const *name)
85 {
86  struct statefs_node *res = self->branch.find(&self->branch, name);
87  return ( (res && res->type == statefs_node_prop)
88  ? container_of(res, struct statefs_property, node)
89  : NULL );
90 }
91 
92 static inline struct statefs_namespace * statefs_ns_find
93 (struct statefs_namespace *self, char const *name)
94 {
95  struct statefs_node *res = self->branch.find(&self->branch, name);
96  return ( (res && res->type | statefs_node_ns)
97  ? container_of(res, struct statefs_namespace, node)
98  : NULL );
99 }
100 
101 static inline char const* statefs_prop_name(struct statefs_property const* p)
102 {
103  return p->node.name;
104 }
105 
106 EXTERN_C_END
107 
108 #endif // _STATEFS_UTIL_H_