1 /* 2 * %injeqt copyright begin% 3 * Copyright 2014 Rafał Malinowski (rafal.przemyslaw.malinowski@gmail.com) 4 * %injeqt copyright end% 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21 #pragma once 22 23 #include <injeqt/injeqt.h> 24 25 #include "internal.h" 26 #include "provider.h" 27 #include "default-constructor-method.h" 28 29 /** 30 * @file 31 * @brief Contains classes and functions for representing provider working on default constructor. 32 */ 33 34 namespace injeqt { namespace internal { 35 36 /** 37 * @brief Provider that returns default-constructed object. 38 * 39 * This provider implementation will return object using default constructor of some type. 40 * Its provided_type() returns type of object that contains passed constructor. Its required_types() 41 * returns empty set of types no other objects are required for construction. 42 * 43 * Once created, object will be stored inside and return on subsequents calls to provide(injector_core &). 44 * This provider has ownershipd over created object and will destroy it at own destruction. 45 */ 46 class INJEQT_INTERNAL_API provider_by_default_constructor final : public provider 47 { 48 49 public: 50 /** 51 * @brief Create provider instance with default constructor to call. 52 * @param constructor constructor method used to create object 53 * @pre !constructor.is_empty() 54 */ 55 explicit provider_by_default_constructor(default_constructor_method constructor); 56 virtual ~provider_by_default_constructor(); 57 58 provider_by_default_constructor(provider_by_default_constructor &&x) = delete; 59 provider_by_default_constructor & operator = (provider_by_default_constructor &&x) = delete; 60 61 /** 62 * @return default_constructor_method::object_type() of object passed to construtor 63 */ 64 virtual const type & provided_type() const override; 65 66 /** 67 * @return object created by default constructor 68 * @post result != nullptr 69 * @post implements(type{result->metaObject()}, provided_type()) 70 * @throw instantiation_failed if instantiation of provided type failed 71 * 72 * If object was not yet created the constructor() method is called and object is stored 73 * in internal cache. Then object from cache is returned. 74 */ 75 virtual QObject * provide(injector_core &i) override; 76 77 /** 78 * @return empty set of object - this provider does not require another object to instantiate 79 */ required_types()80 virtual types required_types() const override { return types{}; } 81 82 /** 83 * @return true 84 * 85 * Objects created by injector will have its dependencies resolved. 86 */ 87 virtual bool require_resolving() const override; 88 89 /** 90 * @return constructor object passed in constructor 91 */ 92 const default_constructor_method & constructor() const; 93 94 private: 95 default_constructor_method _constructor; 96 std::unique_ptr<QObject> _object; 97 98 }; 99 100 }} 101