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.
14 #include <l4/cxx/avl_map>
19 #include "hw_device.h"
24 template< typename VI, typename HW >
25 class Generic_type_factory
28 virtual VI *vcreate(HW *f) = 0;
29 virtual ~Generic_type_factory() {}
31 typedef cxx::Avl_map<std::type_info const *, Generic_type_factory *> Type_map;
34 static Type_map &type_map()
41 static VI *create(HW *f);
44 typedef Generic_type_factory<Dev_feature, Hw::Dev_feature> Feature_factory;
47 : public Generic_type_factory<Device, Hw::Device>
50 virtual Device *vcreate() = 0;
52 typedef cxx::Avl_map<std::string, Dev_factory *> Name_map;
55 static Name_map &name_map()
57 static Name_map _name_map;
62 using Generic_type_factory<Device, Hw::Device>::create;
63 static Device *create(std::string const &_class);
67 template< typename VI, typename HW_BASE, typename HW, typename BASE >
68 class Generic_factory_t : public BASE
72 { BASE::type_map()[&typeid(HW)] = this; }
75 VI *vcreate(HW_BASE *dev)
79 printf("WARNING: device '%s' already assigned to an other virtual bus.\n",
84 if (HW* h = dynamic_cast<HW*>(dev))
86 // dev->inc_ref_count();
95 template< typename VI, typename HW >
96 class Feature_factory_t
97 : public Generic_factory_t<VI, Hw::Dev_feature, HW, Feature_factory >
100 template< typename V_DEV, typename HW_DEV = void >
101 class Dev_factory_t : public Dev_factory
104 typedef HW_DEV Hw_dev;
109 type_map()[&typeid(Hw_dev)] = this;
113 virtual Device *vcreate(Hw::Device *dev)
115 if (dev->ref_count())
116 printf("WARNING: device '%s' already assigned to an other virtual bus.\n",
119 Device *d = new V_dev(static_cast<Hw_dev*>(dev));
120 dev->inc_ref_count();
124 virtual Device *vcreate()
129 template< typename V_DEV >
130 class Dev_factory_t<V_DEV, void> : public Dev_factory
136 explicit Dev_factory_t(std::string const &_class)
138 name_map()[_class] = this;
142 virtual Device *vcreate(Hw::Device *)
145 virtual Device *vcreate()
147 Device *d = new V_dev();
154 template< typename VI, typename HW >
156 Generic_type_factory<VI, HW>::create(HW *f)
161 Type_map &tm = type_map();
162 typename Type_map::const_iterator i = tm.find(&typeid(*f));
165 printf("WARNING: cannot fabricate buddy object for '%s'\n",
170 return i->second->vcreate(f);