2 * (c) 2010 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3 * Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
6 * This file is part of TUD:OS and distributed under the terms of the
7 * GNU General Public License 2.
8 * Please see the COPYING-GPL-2 file for details.
15 #include <l4/cxx/avl_map>
16 #include <l4/cxx/hlist>
20 #include "hw_device.h"
21 #include "type_matcher.h"
26 template< typename VI, typename HW >
27 class Generic_type_factory
28 : public Type_matcher<Generic_type_factory<VI, HW>, VI*>
31 typedef Generic_type_factory<VI, HW> Self;
34 virtual VI *do_match(HW *f) = 0;
37 explicit Generic_type_factory(std::type_info const *type)
38 : Type_matcher<Self, VI*>(type) {}
42 typedef Generic_type_factory<Dev_feature, Hw::Dev_feature> Feature_factory;
43 typedef Generic_type_factory<Resource, Resource> Resource_factory;
45 class Dev_factory : public Type_matcher<Dev_factory, Device *>
48 virtual Device *vcreate() = 0;
49 virtual Device *do_match(Hw::Device *f) = 0;
51 typedef cxx::Avl_map<std::string, Dev_factory *> Name_map;
54 explicit Dev_factory(std::type_info const *type)
55 : Type_matcher<Dev_factory, Device *>(type)
58 static Name_map &name_map()
60 static Name_map _name_map;
65 static Device *create(std::string const &_class);
66 static Device *create(Hw::Device *f)
70 Dev_factory(Dev_factory const &);
71 void operator = (Dev_factory const &);
75 template< typename VI, typename HW_BASE, typename HW, typename BASE >
76 class Generic_factory_t : public BASE
80 Generic_factory_t() : BASE(&typeid(HW)) {}
82 VI *do_match(HW_BASE *dev)
86 printf("WARNING: device '%s' already assigned to another virtual bus.\n",
91 if (HW* h = dynamic_cast<HW*>(dev))
93 // dev->inc_ref_count();
102 template< typename VI, typename HW >
103 class Feature_factory_t
104 : public Generic_factory_t<VI, Hw::Dev_feature, HW, Feature_factory >
107 template< typename VI, typename HW >
108 class Resource_factory_t
109 : public Generic_factory_t<VI, Resource, HW, Resource_factory >
112 template< typename V_DEV, typename HW_DEV = void >
113 class Dev_factory_t : public Dev_factory
116 typedef HW_DEV Hw_dev;
119 Dev_factory_t() : Dev_factory(&typeid(Hw_dev))
123 Device *do_match(Hw::Device *dev)
125 if (dev->ref_count())
126 printf("WARNING: device '%s' already assigned to another virtual bus.\n",
129 if (!dynamic_cast<HW_DEV const*>(dev))
132 Device *d = new V_dev(static_cast<Hw_dev*>(dev));
133 dev->inc_ref_count();
142 template< typename V_DEV >
143 class Dev_factory_t<V_DEV, void> : public Dev_factory
149 explicit Dev_factory_t(std::string const &_class) : Dev_factory(0)
150 { name_map()[_class] = this; }
153 Device *do_match(Hw::Device *)
157 { return new V_dev; }