model_factory.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. //
  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. #pragma once
  15. #include <iostream>
  16. #include <map>
  17. #include <memory>
  18. #include <string>
  19. #include "model_deploy/common/include/base_model.h"
  20. #include "model_deploy/ppdet/include/det_model.h"
  21. #include "model_deploy/ppseg/include/seg_model.h"
  22. #include "model_deploy/ppclas/include/clas_model.h"
  23. #include "model_deploy/paddlex/include/x_model.h"
  24. namespace PaddleDeploy {
  25. typedef Model* (*NewInstance)();
  26. class ModelFactory {
  27. private:
  28. static std::map<std::string, NewInstance> model_map_;
  29. public:
  30. static Model* CreateObject(const std::string &name);
  31. static void Register(const std::string &name, NewInstance model);
  32. };
  33. class Register {
  34. public:
  35. Register(const std::string &name, NewInstance func) {
  36. ModelFactory::Register(name, func);
  37. }
  38. };
  39. #define REGISTER_CLASS(model_type, class_name) \
  40. class class_name##Register { \
  41. public: \
  42. static Model* newInstance() { \
  43. std::cerr << "REGISTER_CLASS:" << #model_type << std::endl; \
  44. return new class_name(#model_type); \
  45. } \
  46. \
  47. private: \
  48. static Register reg_; \
  49. }; \
  50. Register class_name##Register::reg_(#model_type, \
  51. class_name##Register::newInstance);
  52. } // namespace PaddleDeploy