-
Notifications
You must be signed in to change notification settings - Fork 6
/
ioc.h
513 lines (448 loc) · 18.2 KB
/
ioc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* ioc.h - An implementation of a IOC dependency injection
* engine
*
* Copyright (c) 2012 Nicholas A. Smith ([email protected])
* Distributed under the Boost software license 1.0,
* see boost.org for a copy.
*/
#ifndef IOC_H
#define IOC_H
#include <stdlib.h>
#include <typeinfo>
#include <map>
#include <string>
#include <cstring>
#include <memory>
#include <typeindex>
namespace ioc
{
// Constant identifiers
static const std::string
ioc_type_name_registration = "IOC Container";
static const std::string
unnamed_type_name_registration = "Unnamed registration";
class container;
// ifactory is the base interface for a factory
// type. CreateItem returns a void * which can
// then be reinterpret_cast'd to the required type.
class ifactory
{
public:
virtual ~ifactory(){}
virtual const std::type_info &get_type() const = 0;
virtual const std::string &get_name() const = 0;
virtual void* create_item() const = 0;
};
// BaseFatory extends ifactory to provide some standard
// functionality that is required by most concrete
// factoy types.
template<typename I>
class base_factory : public ifactory
{
private:
std::string name;
virtual I *internal_create_item() const = 0;
public:
base_factory( const std::string &name_in )
: ifactory(), name( name_in )
{
}
~base_factory()
{
}
const std::type_info &get_type() const
{
return typeid(I);
}
const std::string &get_name() const
{
return name;
}
void *create_item() const
{
return static_cast<void *>( internal_create_item() );
}
};
template<size_t index>
struct recursive_resolve_impl;
template<>
struct recursive_resolve_impl<0>
{
template<typename resolver_type, typename t, typename callable_type>
static t *resolve(resolver_type &resolver, callable_type callable)
{
return callable();
}
};
template<size_t i>
struct recursive_resolve_impl
{
template<typename resolver_type, typename t,
typename callable_type, typename ...argtypes>
static t *resolve(resolver_type &resolver, callable_type callable)
{
return callable(resolver.template resolve<argtypes>()...);
}
};
struct recursive_resolve
{
template<typename t, typename resolver_type,
typename callable_type, typename ...argtypes>
static t *resolve(resolver_type &resolver, callable_type callable)
{
return recursive_resolve_impl<sizeof...(argtypes)>
::template resolve<resolver_type, t, callable_type, argtypes...>(resolver, callable);
}
};
// DelegateFactory allows delegate objects or routines to be
// supplied and called for object construction. All delegate
// arguments are resolved by the resolver before being send
// to the delegate instance.
template<typename I, typename callable, typename ...argtypes>
class delegate_factory : public base_factory<I>
{
private:
ioc::container &container_obj;
callable callable_obj;
I *internal_create_item() const
{
// Resolve all variables for construction.
// If there is an error during resolution
// then the Resolver will de-allocate any
// already resolved objects for us.
//auto args =
// tuple_resolve::
// resolve<ioc::container, argtypes...>( container_obj );
//I *result = tuple_unwrap::call( callable_obj, args );
I *result = recursive_resolve
::resolve<I, ioc::container, callable, argtypes...>(container_obj, callable_obj);
return result;
}
public:
delegate_factory( const std::string &name_in,
ioc::container &container_in, const
callable &callable_obj_in )
: base_factory<I>( name_in ), container_obj( container_in ),
callable_obj( callable_obj_in )
{
}
~delegate_factory()
{
}
};
// ResolvableFactory extends DelegateFactory by supplying
// a standard function which can be used to instantiate
// and return an instance of a specific type.
template<typename I, typename T, typename ...argtypes>
class resolvable_factory
: public delegate_factory<I, I* (*)( std::shared_ptr<argtypes>...),
argtypes...>
{
private:
static I *creator(std::shared_ptr<argtypes>... args)
{
return new T(args...);
}
public:
typedef I *(func_type)(std::shared_ptr<argtypes>...);
resolvable_factory(
const std::string &name_in,
ioc::container &container_in )
: delegate_factory<I, I *(*)(std::shared_ptr<argtypes>...), argtypes...>
( name_in, container_in, resolvable_factory::creator )
{
}
~resolvable_factory()
{
}
};
// isntance_factory stores an instance of the required type.
// create_item simply returns the stored instance.
// It should be noted that there is no guard around the instance
// to stop it being deleted by some other object once it has
// been resolved.
template<typename I>
class instance_factory
: public base_factory<I>
{
private:
std::shared_ptr<I> instance;
I *internal_create_item() const
{
return instance.get();
}
public:
instance_factory( const std::string &name_in, std::shared_ptr<I> instance_in )
: base_factory<I>( name_in ), instance( instance_in )
{
}
~instance_factory()
{
}
};
// Registration exception classes
class registration_exception : public std::exception
{
private:
std::string type_name;
std::string registration_name;
std::string error;
public:
registration_exception( const std::string &type_name_in,
const std::string ®istration_name_in )
: std::exception(), type_name( type_name_in ),
registration_name( registration_name_in )
{
error = std::string( "Previous registration of type (Type: " ) +
type_name + std::string( " , " ) + registration_name +
std::string( ")" );
}
~registration_exception() throw()
{
}
const std::string &get_type_name() const
{
return type_name;
}
const std::string &get_registration_name() const
{
return registration_name;
}
const char *what() const throw()
{
return error.c_str();
}
};
// Container. All object types are registered with the container
// at run-time and can then be resolved. Resolver supports
// constructor injection.
class container
{
private:
template<typename T>
struct ellided_deleter
{
void operator()(T *val)
{
// Shhhhh, don't actually delete the ptr.
}
};
typedef ellided_deleter<container> container_deleter;
// Internal map of registered types -> map of named instances of
// type factories.
typedef std::map<std::string, ifactory*> named_factory;
typedef std::map<std::type_index, named_factory> registration_types;
registration_types types;
std::shared_ptr<container> self;
static inline void destroy_factory( ifactory *factory )
{
if( factory )
{
delete factory;
factory = NULL;
}
}
// Registration helper
template<typename F, typename I, typename ...argtypes>
void register_with_name_template( const std::string &name_in,
argtypes... args )
{
if( type_is_registered<I>( name_in ) )
{
// Throw an exception as we cannot register a type
// which has already been registered
throw registration_exception( typeid(I).name(),
name_in );
}
F *new_factory = new F( name_in, args... );
types[std::type_index(typeid(I))][name_in] = new_factory;
}
// Resolve factory for interface. If that fails then return NULL.
template<typename I>
const ifactory *resolve_factory() const
{
// Lookup interface type. If it cannot be found return
// the default for that type.
ifactory *result = NULL;
registration_types::const_iterator i = types.find(std::type_index(typeid(I)));
if( i != types.end() )
{
const named_factory candidates =
i->second;
result = (candidates.begin())->second;
}
return result;
}
// Resolve factory for interface type by name.
// If that fails then return NULL.
template<typename I>
ifactory *
resolve_factory_by_name( const std::string &name_in ) const
{
// Lookup interface type. If it cannot be found return
// the default for that type.
ifactory *result = NULL;
registration_types::const_iterator i = types.find(std::type_index(typeid(I)));
if( i != types.end() )
{
// We've got the type registered but we now need to look
// up the named version.
const named_factory::const_iterator c =
i->second.find(name_in);
if( c != i->second.end() )
{
result = c->second;
}
}
return result;
}
public:
container() : self(this, container_deleter())
{
// Register our special shared_ptr which will not
// delete if a container is resolved.
this->register_instance<container>(self);
}
~container()
{
// Destroy all factories
for( registration_types::reverse_iterator i = types.rbegin();
i != types.rend(); ++i )
{
for(named_factory::reverse_iterator j = i->second.rbegin();
j != i->second.rend(); ++j)
{
destroy_factory( j->second );
}
i->second.clear();
}
types.clear();
}
// Check if a factory to create a gievn interface
// already exists
template<typename I>
bool type_is_registered( const std::string &name_in ) const
{
const ifactory *f = resolve_factory_by_name<I>( name_in );
return f ? true : false;
}
template<typename I>
bool type_is_registered() const
{
const ifactory *f = resolve_factory<I>();
return f ? true : false;
}
template<typename I, typename callable, typename ...argtypes>
void register_delegate_with_name( const std::string &name_in,
callable call_obj )
{
// Create a functor which returns an Interface type
// but actually news a Concretion.
typedef delegate_factory<I, callable, argtypes...>
factorytype;
register_with_name_template<factorytype, I,
ioc::container &, callable>( name_in, *this, call_obj );
}
template<typename I, typename callable, typename ...argtypes>
void register_delegate( callable call_obj )
{
// Register nameless delegate constructor
register_delegate_with_name<I, callable, argtypes...>(
unnamed_type_name_registration, call_obj );
}
template<typename I, typename T, typename ...argtypes>
void register_type_with_name( const std::string &name_in )
{
typedef resolvable_factory<I, T, argtypes...> factorytype;
register_with_name_template<factorytype, I,
ioc::container &>( name_in, *this );
}
template<typename I, typename T, typename ...argtypes>
void register_type()
{
// Register nameless constructor object
register_type_with_name<I, T, argtypes...>(
unnamed_type_name_registration );
}
template<typename I>
void register_instance_with_name( const std::string &name_in,
std::shared_ptr<I> instance_in )
{
// Create instance constuctor and register in our type list
typedef instance_factory<I> factorytype;
register_with_name_template<factorytype, I, std::shared_ptr<I>>(
name_in,
instance_in );
}
template<typename I>
void register_instance( std::shared_ptr<I> instance_in )
{
register_instance_with_name<I>(
unnamed_type_name_registration, instance_in );
}
// Resolve interface type. If that fails then return NULL.
template<typename I>
std::shared_ptr<I> resolve() const
{
I *result = NULL;
const ifactory *factory = resolve_factory<I>();
if( factory )
{
result = reinterpret_cast<I *>( factory->create_item() );
}
return std::shared_ptr<I>(result);
}
// Resolve interface type by name. If that fails then return NULL.
template<typename I>
std::shared_ptr<I> resolve_by_name( const std::string &name_in ) const
{
I *result = NULL;
const ifactory *factory =
resolve_factory_by_name<I>( name_in );
if( factory )
{
result = reinterpret_cast<I *>( factory->create_item() );
}
return std::shared_ptr<I>(result);
}
// Destroy all factories implementing the given interface
template<typename I>
bool remove_registration()
{
bool result = false;
registration_types::iterator i = types.find(std::type_index(typeid(I)));
if( i != types.end() )
{
for( named_factory::iterator j = i->second.begin();
j != i->second.end(); ++j )
{
destroy_factory( j->second );
}
types.erase(i);
result = true;
}
return result;
}
// Destroy the first named factory which creates an
// interface
template<typename I>
bool remove_registration_by_name( const std::string &name_in )
{
bool result = false;
registration_types::iterator i = types.find(std::type_index(typeid(I)));
if( i != types.end() )
{
named_factory::iterator j = i->second.find(name_in);
if( j != i->second.end() )
{
destroy_factory( j->second );
i->second.erase( j );
result = true;
}
}
return result;
}
}; // namespace IOC
};
#endif // IOC_H