1 /* 2 * Copyright (c) 2021 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 HOS_CAMERA_CREATE_SENSOR_FACTORY_H 17 #define HOS_CAMERA_CREATE_SENSOR_FACTORY_H 18 19 #include "isensor.h" 20 #include "device_manager_adapter.h" 21 #include <map> 22 23 namespace OHOS::Camera { 24 typedef std::shared_ptr<ISensor> (*createClass) (void); 25 class CreateSensorFactory { 26 public: ~CreateSensorFactory()27 virtual ~CreateSensorFactory(){}; GetSensorByName(std::string className)28 std::shared_ptr<ISensor> GetSensorByName(std::string className) 29 { 30 std::map<std::string, createClass>::const_iterator iter; 31 32 iter = senorMap_.find(className); 33 if (iter == senorMap_.end()) { 34 return NULL; 35 } else { 36 return iter->second(); 37 } 38 }; RegistClass(std::string name,createClass method)39 void RegistClass(std::string name, createClass method) 40 { 41 senorMap_.insert(std::pair<std::string, createClass>(name, method)); 42 }; SharedSensorFactory()43 static CreateSensorFactory& SharedSensorFactory() 44 { 45 static CreateSensorFactory sensorFactory_; 46 return sensorFactory_; 47 }; 48 49 private: CreateSensorFactory()50 CreateSensorFactory(){}; 51 std::map<std::string, createClass> senorMap_; 52 }; 53 54 class SensorDynamicClass { 55 public: SensorDynamicClass(std::string name,createClass method)56 SensorDynamicClass(std::string name, createClass method) 57 { 58 CreateSensorFactory::SharedSensorFactory().RegistClass(name, method); 59 }; ~SensorDynamicClass()60 ~SensorDynamicClass() {} 61 }; 62 63 #define DECLARE_SENSOR(className) \ 64 private: \ 65 static SensorDynamicClass* className##_; \ 66 \ 67 public: \ 68 static std::shared_ptr<ISensor> createInstance() \ 69 { \ 70 return std::static_pointer_cast<ISensor>(std::make_shared<className>()); \ 71 } 72 73 #define IMPLEMENT_SENSOR(className) \ 74 SensorDynamicClass* className::className##_ = new SensorDynamicClass(#className, &className::createInstance); 75 76 #define GetSensorFactory CreateSensorFactory::SharedSensorFactory() 77 } // namespace OHOS::Camera 78 #endif 79