1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_SINGLETON_DELEGATOR_H 17 #define OHOS_SINGLETON_DELEGATOR_H 18 19 #include "singleton_container.h" 20 21 #define MOCKABLE virtual 22 23 namespace OHOS { 24 namespace Rosen { 25 template<class T> 26 class SingletonDelegator { 27 public: SingletonDelegator()28 SingletonDelegator() 29 { 30 nameT = __PRETTY_FUNCTION__; 31 nameT = nameT.substr(nameT.find("T = ")); 32 nameT = nameT.substr(sizeof("T ="), nameT.length() - sizeof("T = ")); 33 SingletonContainer::GetInstance().SetSingleton(nameT, &T::GetInstance()); 34 } 35 ~SingletonDelegator() = default; 36 37 template<class S> Dep()38 S& Dep() 39 { 40 std::string nameS = __PRETTY_FUNCTION__; 41 nameS = nameS.substr(nameS.find("S = ")); 42 nameS = nameS.substr(sizeof("S ="), nameS.length() - sizeof("S = ")); 43 44 auto ret = SingletonContainer::Get<S>(); 45 SingletonContainer::GetInstance().DependOn(nameT, nameS); 46 return ret; 47 } 48 49 private: 50 std::string nameT; 51 }; 52 } // namespace Rosen 53 } // namespace OHOS 54 55 #endif // FRAMEWORKS_WM_INCLUDE_SINGLETON_DELEGATOR_H 56