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>
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, bool warn = true);
44 typedef Generic_type_factory<Dev_feature, Hw::Dev_feature> Feature_factory;
45 typedef Generic_type_factory<Resource, Resource> Resource_factory;
48 : public Generic_type_factory<Device, Hw::Device>
51 virtual Device *vcreate() = 0;
53 typedef cxx::Avl_map<std::string, Dev_factory *> Name_map;
56 static Name_map &name_map()
58 static Name_map _name_map;
63 using Generic_type_factory<Device, Hw::Device>::create;
64 static Device *create(std::string const &_class);
68 template< typename VI, typename HW_BASE, typename HW, typename BASE >
69 class Generic_factory_t : public BASE
73 { BASE::type_map()[&typeid(HW)] = this; }
76 VI *vcreate(HW_BASE *dev)
80 printf("WARNING: device '%s' already assigned to an other virtual bus.\n",
85 if (HW* h = dynamic_cast<HW*>(dev))
87 // dev->inc_ref_count();
96 template< typename VI, typename HW >
97 class Feature_factory_t
98 : public Generic_factory_t<VI, Hw::Dev_feature, HW, Feature_factory >
101 template< typename VI, typename HW >
102 class Resource_factory_t
103 : public Generic_factory_t<VI, Resource, HW, Resource_factory >
106 template< typename V_DEV, typename HW_DEV = void >
107 class Dev_factory_t : public Dev_factory
110 typedef HW_DEV Hw_dev;
115 type_map()[&typeid(Hw_dev)] = this;
119 virtual Device *vcreate(Hw::Device *dev)
121 if (dev->ref_count())
122 printf("WARNING: device '%s' already assigned to an other virtual bus.\n",
125 Device *d = new V_dev(static_cast<Hw_dev*>(dev));
126 dev->inc_ref_count();
130 virtual Device *vcreate()
135 template< typename V_DEV >
136 class Dev_factory_t<V_DEV, void> : public Dev_factory
142 explicit Dev_factory_t(std::string const &_class)
144 name_map()[_class] = this;
148 virtual Device *vcreate(Hw::Device *)
151 virtual Device *vcreate()
153 Device *d = new V_dev();
160 template< typename VI, typename HW >
162 Generic_type_factory<VI, HW>::create(HW *f, bool warn)
167 Type_map &tm = type_map();
168 typename Type_map::const_iterator i = tm.find(&typeid(*f));
172 d_printf(DBG_WARN, "WARNING: cannot fabricate buddy object for '%s'\n",
177 return i->second->vcreate(f);