Sauce-0.10.1
A C++ Dependency Injection Framework
type_id.h
1#ifndef SAUCE_INTERNAL_TYPE_ID_H_
2#define SAUCE_INTERNAL_TYPE_ID_H_
3
4#include <set>
5#include <string>
6#include <utility>
7
8#include <sauce/exceptions.h>
9
10namespace sauce {
11namespace internal {
12
23typedef void (*TypeSignature)();
24
28template<typename Type>
29void TypeSignatureFactory() {}
30
34class TypeId {
35 TypeSignature signature;
36
37 TypeId():
38 signature(NULL) {}
39
40protected:
41
42 explicit TypeId(TypeSignature const & signature):
43 signature(signature) {}
44
45public:
46
47 virtual ~TypeId() {}
48
49 bool operator==(TypeId const & id) const {
50 return signature == id.signature;
51 }
52
53 bool operator!=(TypeId const & id) const {
54 return signature != id.signature;
55 }
56
57 bool operator<(TypeId const & id) const {
58 return signature < id.signature;
59 }
60
64 virtual void throwOutOfScopeException() const {
65 throw OutOfScopeException();
66 }
67};
68
69template<typename Type>
70TypeId typeIdOf();
71
75template<typename Type>
76class ResolvedTypeId: public TypeId {
77 friend TypeId typeIdOf<Type>();
78
80 TypeId(&TypeSignatureFactory<Type>) {}
81
82public:
83
86 }
87};
88
92template<typename Type>
93TypeId typeIdOf() {
94 return ResolvedTypeId<Type>();
95}
96
100typedef std::pair<TypeId, std::string> NamedTypeId;
101
105template<typename Type>
106NamedTypeId namedTypeIdOf(std::string const name) {
107 return std::make_pair(typeIdOf<Type>(), name);
108}
109
113typedef std::set<NamedTypeId> TypeIds;
114
115}
116
117namespace i = ::sauce::internal;
118
119}
120
121#endif // SAUCE_INTERNAL_TYPE_ID_H_
Thrown when a provision is requested outside of its given, bound scope.
Definition: exceptions.h:85
Thrown when a provision is requested outside of its bound scope.
Definition: exceptions.h:75
The TypeId derived class that acknowledges the hidden type.
Definition: type_id.h:76
void throwOutOfScopeException() const
Throw an OutOfScopeException appropriate for the hidden type, assuming it is a Scope.
Definition: type_id.h:84
A TypeSignature equipped with specific helper methods dealing in the hidden type.
Definition: type_id.h:34
virtual void throwOutOfScopeException() const
Throw an OutOfScopeException appropriate for the hidden type, assuming it is a Scope.
Definition: type_id.h:64