summaryrefslogtreecommitdiffstats
path: root/common/clientapi.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/clientapi.h')
-rw-r--r--common/clientapi.h25
1 files changed, 4 insertions, 21 deletions
diff --git a/common/clientapi.h b/common/clientapi.h
index 4948c59..748baa6 100644
--- a/common/clientapi.h
+++ b/common/clientapi.h
@@ -108,7 +108,7 @@ public:
108 108
109class FacadeFactory { 109class FacadeFactory {
110public: 110public:
111 typedef std::function<void*(bool &externallyManaged)> FactoryFunction; 111 typedef std::function<std::shared_ptr<void>()> FactoryFunction;
112 112
113 //FIXME: proper singleton implementation 113 //FIXME: proper singleton implementation
114 static FacadeFactory &instance() 114 static FacadeFactory &instance()
@@ -126,17 +126,13 @@ public:
126 void registerFacade(const QByteArray &resource) 126 void registerFacade(const QByteArray &resource)
127 { 127 {
128 const QByteArray typeName = ApplicationDomain::getTypeName<DomainType>(); 128 const QByteArray typeName = ApplicationDomain::getTypeName<DomainType>();
129 mFacadeRegistry.insert(key(resource, typeName), [](bool &externallyManaged){ return new Facade; }); 129 mFacadeRegistry.insert(key(resource, typeName), [](){ return std::shared_ptr<Facade>(new Facade); });
130 } 130 }
131 131
132 /* 132 /*
133 * Allows the registrar to register a specific instance. 133 * Allows the registrar to register a specific instance.
134 * 134 *
135 * Primarily for testing. 135 * Primarily for testing.
136 * The facade factory takes ovnership of the pointer and typically deletes the instance via shared pointer.
137 * Supplied factory functions should therefore always return a new pointer (i.e. via clone())
138 *
139 * FIXME the factory function should really be returning QSharedPointer<void>, which doesn't work (std::shared_pointer<void> would though). That way i.e. a test could keep the object alive until it's done. As a workaround the factory function can define wether it manages the lifetime of the facade itself.
140 */ 136 */
141 template<class DomainType, class Facade> 137 template<class DomainType, class Facade>
142 void registerFacade(const QByteArray &resource, const FactoryFunction &customFactoryFunction) 138 void registerFacade(const QByteArray &resource, const FactoryFunction &customFactoryFunction)
@@ -155,25 +151,12 @@ public:
155 mFacadeRegistry.clear(); 151 mFacadeRegistry.clear();
156 } 152 }
157 153
158 static void doNothingDeleter(void *)
159 {
160 qWarning() << "Do nothing";
161 }
162
163 template<class DomainType> 154 template<class DomainType>
164 std::shared_ptr<StoreFacade<DomainType> > getFacade(const QByteArray &resource) 155 std::shared_ptr<StoreFacade<DomainType> > getFacade(const QByteArray &resource)
165 { 156 {
166 const QByteArray typeName = ApplicationDomain::getTypeName<DomainType>(); 157 const QByteArray typeName = ApplicationDomain::getTypeName<DomainType>();
167 auto factoryFunction = mFacadeRegistry.value(key(resource, typeName)); 158 if (auto factoryFunction = mFacadeRegistry.value(key(resource, typeName))) {
168 if (factoryFunction) { 159 return std::static_pointer_cast<StoreFacade<DomainType> >(factoryFunction());
169 bool externallyManaged = false;
170 auto ptr = static_cast<StoreFacade<DomainType>* >(factoryFunction(externallyManaged));
171 if (externallyManaged) {
172 //Allows tests to manage the lifetime of injected facades themselves
173 return std::shared_ptr<StoreFacade<DomainType> >(ptr, doNothingDeleter);
174 } else {
175 return std::shared_ptr<StoreFacade<DomainType> >(ptr);
176 }
177 } 160 }
178 qWarning() << "Failed to find facade for resource: " << resource << " and type: " << typeName; 161 qWarning() << "Failed to find facade for resource: " << resource << " and type: " << typeName;
179 return std::shared_ptr<StoreFacade<DomainType> >(); 162 return std::shared_ptr<StoreFacade<DomainType> >();