Sauce-0.10.1: sauce/internal/type_id.h Source File

Sauce-0.10.1 A C++ Dependency Injection Framework

- Main Page
- Classes
- Files

- File List

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
10 namespace sauce {
11 namespace internal {
12
23 typedef void (*TypeSignature)();
24
28 template<typename Type>
29 void TypeSignatureFactory() {}
30
34 class TypeId {
35 TypeSignature signature;
36
37 TypeId():
38 signature(NULL) {}
39
40 protected:
41
42 explicit TypeId(TypeSignature const & signature):
43 signature(signature) {}
44
45 public:
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
69 template<typename Type>
70 TypeId typeIdOf();
71
75 template<typename Type>
76 class ResolvedTypeId: public TypeId {
77 friend TypeId typeIdOf<Type>();
78
79 ResolvedTypeId():
80 TypeId(&TypeSignatureFactory<Type>) {}
81
82 public:
83
84 void throwOutOfScopeException() const {
85 throw OutOfScopeExceptionFor<Type>();
86 }
87 };
88
92 template<typename Type>
93 TypeId typeIdOf() {
94 return ResolvedTypeId<Type>();
95 }
96
100 typedef std::pair<TypeId, std::string> NamedTypeId;
101
105 template<typename Type>
106 NamedTypeId namedTypeIdOf(std::string const name) {
107 return std::make_pair(typeIdOf<Type>(), name);
108 }
109
113 typedef std::set<NamedTypeId> TypeIds;
114
115 }
116
117 namespace i = ::sauce::internal;
118
119 }
120
121 #endif // SAUCE_INTERNAL_TYPE_ID_H_