Diffstat (limited to 'include/sitecing/component_so.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | include/sitecing/component_so.h | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/include/sitecing/component_so.h b/include/sitecing/component_so.h new file mode 100644 index 0000000..3239d4a --- a/dev/null +++ b/include/sitecing/component_so.h | |||
@@ -0,0 +1,159 @@ | |||
1 | #ifndef __SITECING_COMPONENT_SO_H | ||
2 | #define __SITECING_COMPONENT_SO_H | ||
3 | |||
4 | #include <sys/types.h> | ||
5 | #include <sys/stat.h> | ||
6 | #include <string> | ||
7 | #include <map> | ||
8 | #include <list> | ||
9 | #include "sitecing/acomponent.h" | ||
10 | |||
11 | /** | ||
12 | * @file | ||
13 | * @brief Classes related to the .so components handling. | ||
14 | */ | ||
15 | |||
16 | namespace sitecing { | ||
17 | using namespace std; | ||
18 | |||
19 | /** | ||
20 | * The 'class' component object. | ||
21 | */ | ||
22 | class component_so { | ||
23 | public: | ||
24 | /** | ||
25 | * The type of the component instantiating function. | ||
26 | */ | ||
27 | typedef acomponent *(*egg_t)(); | ||
28 | /** | ||
29 | * Type for storing the list of instances and the reference counts. | ||
30 | */ | ||
31 | typedef map<acomponent*,int> used_chickens_t; | ||
32 | /** | ||
33 | * The type for storing the list of unused instances. | ||
34 | */ | ||
35 | typedef list<acomponent*> free_chickens_t; | ||
36 | |||
37 | /** | ||
38 | * The .so file name. | ||
39 | */ | ||
40 | string sofile; | ||
41 | /** | ||
42 | * The stat structure for the .so loaded. | ||
43 | */ | ||
44 | struct stat stso; | ||
45 | /** | ||
46 | * The dloaded .so handle. | ||
47 | */ | ||
48 | void *dl; | ||
49 | /** | ||
50 | * Pointer to the instatiator function. | ||
51 | */ | ||
52 | egg_t egg; | ||
53 | /** | ||
54 | * The list of instances in use. | ||
55 | */ | ||
56 | used_chickens_t chickens_used; | ||
57 | /** | ||
58 | * The list of unused instances. | ||
59 | */ | ||
60 | free_chickens_t chickens_free; | ||
61 | |||
62 | /** | ||
63 | * @param soname the .so file name | ||
64 | */ | ||
65 | component_so(const string& soname); | ||
66 | ~component_so(); | ||
67 | /** | ||
68 | * Check whether the loaded .so is in sync with the disk file. | ||
69 | */ | ||
70 | bool is_uptodate() const; | ||
71 | |||
72 | /** | ||
73 | * @todo TODO: wish I could remember -- document me. | ||
74 | */ | ||
75 | acomponent* allocate_chicken(); | ||
76 | /** | ||
77 | * @todo TODO: wish I could remember -- document me. | ||
78 | */ | ||
79 | void allocate_chicken(acomponent *ac); | ||
80 | /** | ||
81 | * @todo TODO: wish I could remember -- document me. | ||
82 | */ | ||
83 | void deallocate_chicken(acomponent *ac); | ||
84 | }; | ||
85 | |||
86 | /** | ||
87 | * The component instance container. | ||
88 | */ | ||
89 | class so_component { | ||
90 | public: | ||
91 | /** | ||
92 | * Pointer to the component 'class'. | ||
93 | */ | ||
94 | component_so *hen; | ||
95 | /** | ||
96 | * The instance in question. | ||
97 | */ | ||
98 | acomponent* ac; | ||
99 | |||
100 | so_component() | ||
101 | : hen(0), ac(0) { } | ||
102 | /** | ||
103 | * @param h the 'class' object. | ||
104 | * @param scif pointer to the interface to the site-C-ing core. | ||
105 | */ | ||
106 | so_component(component_so *h,sitecing_interface *scif); | ||
107 | /** | ||
108 | * Copy constructor | ||
109 | * @param s source instance. | ||
110 | */ | ||
111 | so_component(const so_component& s) | ||
112 | : hen(0), ac(0) { attach(s); } | ||
113 | ~so_component() { detach(); } | ||
114 | |||
115 | /** | ||
116 | * Assignment operator. | ||
117 | * @param s source instance. | ||
118 | */ | ||
119 | so_component& operator=(const so_component& s) { | ||
120 | attach(s); return *this; | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * @todo TODO: wish I could remember the details -- document me. | ||
125 | * @param h the 'class' object. | ||
126 | * @param a the instance to be attached. | ||
127 | */ | ||
128 | void attach(component_so *h,acomponent *a); | ||
129 | /** | ||
130 | * @todo TODO: wish I could remember the details -- document me. | ||
131 | * @param s the source instance. | ||
132 | */ | ||
133 | void attach(const so_component& s) { attach(s.hen,s.ac); } | ||
134 | /** | ||
135 | * @todo TODO: wish I could remember the details -- document me. | ||
136 | */ | ||
137 | void detach(); | ||
138 | }; | ||
139 | |||
140 | /** | ||
141 | * The typed component instance container template. | ||
142 | * @param CT the component class. | ||
143 | */ | ||
144 | template<typename CT> | ||
145 | class so_component_t : public sitecing::so_component { | ||
146 | public: | ||
147 | /** | ||
148 | * @param s The untyped instance container. | ||
149 | */ | ||
150 | so_component_t(const so_component& s) | ||
151 | : so_component(s) { } | ||
152 | CT* operator->() { | ||
153 | return static_cast<CT*>(ac->__the_most_derived_this()); | ||
154 | } | ||
155 | }; | ||
156 | |||
157 | } | ||
158 | |||
159 | #endif /* __SITECING_COMPONENT_SO_H */ | ||