abstract types used in device/resource system
[quarnos.git] / manes / manec.h
blobb74abd2c3770ba9066e5d0112ac17c2f551a1000
1 /* Quarn OS / Manes
3 * Managed Execution Controller
5 * Copyright (C) 2008-2009 Pawel Dziepak
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef _MANEC_H_
24 #define _MANEC_H_
26 #include "component.h"
27 #include "libs/pointer.h"
28 #include "arch/low/irq.h"
30 namespace resources {
31 class did;
34 namespace services {
35 class kernel_state;
38 namespace manes {
39 class error;
40 class root;
41 class factory;
43 /* Managed Execution Controller */
44 class manec {
45 private:
46 root *main;
47 factory *types;
49 services::kernel_state *kstate;
51 arch::irq_op irqs;
53 bool usermode;
55 static manec *instance;
56 manec(){}
57 public:
59 virtual p<component> new_component(const type_name &name);
60 virtual p<component> get_component(const component_name &name);
61 virtual p<component> get_component(const char *cname);
63 /**
64 * @brief Get component specified by path.
66 * @details This method parses given path and then uses multiple invocations
67 * of get_component method to get specified component. This is the syntax
68 * of component path:
69 * @code
70 * /type,id/type,id/type,id
71 * @endcode
72 * @param path Path to the requested component.
73 * @return Requested component.
75 virtual p<component> get_by_path(const string &path);
78 /**
79 * @brief Get certain interface of the specific component.
81 * @details This is a part of simplified interface of Manes manager.
82 * It gets specified component using method get_by_path, then narrows
83 * it to the needed interface T.
84 * @param path Path to the requested component.
85 * @return Requested interface of the component.
87 template<typename T>
88 p<T> get(const string &path);
90 virtual void register_type(const char*,const char*,type::root_type, delegate<implementation*>*);
91 virtual void register_driver(const char*, const char*, type::root_type, delegate<implementation*>*, delegate<bool, resources::did*>);
93 /**
94 * @brief Register new type in Manes.
96 * @details When given a pointer to a completely initialized type this function
97 * inserts that type into internal Manes database, so that it is available
98 * for all clients and operations on components of that type are possible.
99 * @param newtype Pointer to newly created type.
101 virtual void register_type(type *newtype);
104 * @brief Get type from Manes.
106 * @details This method looks for requested type in internal Manes database.
107 * An error will occur if type wasn't previously registered.
108 * @param name Name of the requested type.
109 * @return Requested type.
111 virtual type *get_type(type_name name);
113 virtual type *get_driver(resources::did *);
115 virtual arch::irq_op *get_low();
118 virtual void connect_manes(root *root_mgr);
119 static void connect_manes();
121 static manec *get();
123 virtual root *get_root();
124 virtual factory *get_factory();
126 virtual services::kernel_state *state();
128 virtual error *err_msg(const char *x);
130 bool far_calls();
132 template<typename T>
133 void register_type(const char*, const char*, type::root_type);
135 template<typename T>
136 void register_driver(const char*, const char*, type::root_type, delegate<bool, resources::did*>);
138 virtual void register_abstract(const char*, const char*);
141 template<typename T, typename U>
142 T *create_object() {
143 return (T*)new U();
146 template<typename T>
147 void manec::register_type(const char *me, const char *parent, type::root_type tpe) {
148 delegate<implementation *> *bus_create = new delegate<implementation *>;
149 bus_create->function(create_object<implementation, T>);
150 register_type(me, parent, tpe, bus_create);
152 /* type_name bus_name(me, true);
153 delegate<implementation *> *bus_create = new delegate<implementation *>;
154 bus_create->function(create_object<implementation, T>);
155 manec::get()->register_type(new type(tpe, (type_name)parent, bus_name, *bus_create));*/
158 template<typename T>
159 void manec::register_driver(const char *me, const char *parent, type::root_type tpe, delegate<bool, resources::did*> check) {
160 delegate<implementation *> *bus_create = new delegate<implementation *>;
161 bus_create->function(create_object<implementation, T>);
162 register_driver(me, parent, tpe, bus_create, check);
165 template<typename T>
166 p<T> manec::get(const string &path) {
167 return get_by_path(path)->get<T>();
170 template<typename T>
171 T *component::get() const {
172 if (!manec::get()->far_calls())
173 return dynamic_cast<T*>(get_implementation());
174 else
175 return (T*)get_stub();
180 #include "error.h"
182 #endif