increasing usage of p<>, minor bugs fixed [code still broken]
[quarnos.git] / manes / manec.cpp
blobcbd42f834759b1c6da4403351186518cd3d1671e
1 /* Quarn OS / Manes
3 * Manes 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 #include "creator.h"
24 #include "manec.h"
25 #include "type.h"
26 #include "error.h"
27 #include "root.h"
28 #include "driver.h"
29 #include "factory.h"
30 #include "abstract.h"
31 #include "services/kernel_state.h"
33 using namespace manes;
35 p<manec> manec::instance = (manec*)0;
37 void manec::connect_manes(p<root> root_mgr) {
38 main = root_mgr;
39 types = main->get_component(component_name((type_name)"factory",0))->get<factory>();
41 usermode = false;
44 void manec::connect_manes() {
45 manec *main;
47 __asm__ ("int $0x32": "=a" (main));
49 instance = main;
52 p<manec> manec::get() {
53 if (!instance.valid())
54 instance = new manec;
56 return instance;
59 p<services::kernel_state> manec::state() {
60 if (!kstate.valid())
61 kstate = get<services::kernel_state>("/kernel_state");
62 return kstate;
65 bool manec::far_calls() {
66 return usermode;
69 error *manec::err_msg(const string x) {
70 return state()->new_error(x);
74 p<component> manec::new_component(const type_name &name) {
75 return main->new_component(name);
78 p<component> manec::get_component(const component_name &obj) {
79 return main->get_component(obj);
82 p<component> manec::get_component(const string cname) {
83 return get_component(component_name((type_name)cname,0));
86 p<component> manec::get_by_path(const string &tpath) {
87 char *path = new char[strlen(tpath) + 1];
88 strcpy(path, tpath);
90 int i = 1, j = 1;
92 char *dep_res = (char*)0;
93 char *main_res;
94 char *specific_name = (char*)0;
95 int num = 0;
97 main_res = &path[1];
98 while(1) {
99 if (!path[i]) {
100 main_res = &(path[j]);
101 break;
103 if (path[i] == ',') {
104 path[i] = 0;
105 i++;
106 if (string::is_digit(path[i]))
107 for (; path[i] && path[i] != '/'; i++)
108 num = num * 10 + path[i] - '0';
109 else
110 specific_name = &path[i];
111 } else if (path[i] == '/') {
112 path[i] = 0;
113 dep_res = &(path[j]);
114 i++;
115 j = i;
117 for (; path[i] && path[i] != ',' && path[i] != '/'; i++);
118 if (!path[i])
119 break;
122 if (!specific_name)
123 return get_component(component_name(type_name(main_res), num));
124 else
125 return get_component(component_name(type_name(main_res), specific_name));
128 void manec::register_type(const string me, const string parent, type::root_type tpe, delegate<p<implementation> > deleg) {
129 type_name bus_name(me, true);
130 register_type(new type(tpe, (type_name)parent, bus_name, deleg));
133 void manec::register_driver(const string me,const string parent,type::root_type tpe, delegate<p<implementation> > deleg, delegate<bool, p<resources::did> > check) {
134 type_name bus_name(me, true);
135 register_type(new driver(tpe, (type_name)parent, bus_name, deleg, check));
136 main->type_added(bus_name);
139 void manec::register_abstract(const string me, const string parent) {
140 type_name abstr_name(me, true);
141 register_type(new abstract(type::device, (type_name)parent, abstr_name));
144 void manec::register_type(p<type> tpe) {
145 assert("manager: incorrect type pointer", tpe == 0);
147 types->register_impl(tpe);
150 p<type> manec::get_type(const type_name &tpe) {
151 return types->get_component(component_name((type_name)"type",tpe.get_textname()))->get<type>();
154 p<type> manec::get_driver(p<resources::did> id) {
155 for (int i = 0; i < types->count(); i++) {
156 p<type> drv = types->get_component(component_name((type_name)"driver", i))->get<type>();
157 if (drv.cast<driver>().valid() && drv.cast<driver>()->check_device(id))
158 return drv;
161 return (type*)0;
164 arch::irq_op *manec::get_low() {
165 return &irqs;
168 p<root> manec::get_root() {
169 return main;
173 p<factory> manec::get_factory() {
174 return types;