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