factory.h 6.1 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include <map>
#include <string>
#include <utility>
W
wangguibao 已提交
19
#include "common/inner_common.h"
W
serving  
wangguibao 已提交
20
#include "glog/raw_logging.h"
W
wangguibao 已提交
21 22 23 24 25
namespace baidu {
namespace paddle_serving {
namespace predictor {

//////////////// DECLARE INTERFACE ////////////////
W
wangguibao 已提交
26 27 28 29 30 31 32 33 34 35
#define DECLARE_FACTORY_OBJECT(D, B)                                         \
  static int regist(const std::string& tag) {                                \
    FactoryDerive<D, B>* factory = new (std::nothrow) FactoryDerive<D, B>(); \
    if (factory == NULL ||                                                   \
        FactoryPool<B>::instance().register_factory(tag, factory) != 0) {    \
      RAW_LOG_FATAL("Failed regist factory: %s in macro!", #D);              \
      return -1;                                                             \
    }                                                                        \
    return 0;                                                                \
  }
W
wangguibao 已提交
36 37

#define PDS_STR_CAT(a, b) PDS_STR_CAT_I(a, b)
W
wangguibao 已提交
38
#define PDS_STR_CAT_I(a, b) a##b
W
wangguibao 已提交
39

W
wangguibao 已提交
40 41 42 43 44
#define DEFINE_FACTORY_OBJECT(D)                                           \
  __attribute__((constructor)) static void PDS_STR_CAT(GlobalRegistObject, \
                                                       __LINE__)(void) {   \
    D::regist(#D);                                                         \
  }
W
wangguibao 已提交
45 46 47

//////////////// REGISTER INTERFACE ////////////////

W
wangguibao 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#define REGIST_FACTORY_OBJECT_IMPL(D, B)                                       \
  __attribute__((constructor)) static void PDS_STR_CAT(GlobalRegistObject,     \
                                                       __LINE__)(void) {       \
    ::baidu::paddle_serving::predictor::FactoryDerive<D, B>* factory = new (   \
        ::std::nothrow)::baidu::paddle_serving::predictor::FactoryDerive<D,    \
                                                                         B>(); \
    if (factory == NULL ||                                                     \
        ::baidu::paddle_serving::predictor::FactoryPool<B>::instance()         \
                .register_factory(#D, factory) != 0) {                         \
      RAW_LOG_FATAL("Failed regist factory: %s->%s in macro!", #D, #B);        \
      return;                                                                  \
    }                                                                          \
    return;                                                                    \
  }

#define REGIST_FACTORY_OBJECT_IMPL_WITH_NAME(D, B, N)                          \
  __attribute__((constructor)) static void PDS_STR_CAT(GlobalRegistObject,     \
                                                       __LINE__)(void) {       \
    ::baidu::paddle_serving::predictor::FactoryDerive<D, B>* factory = new (   \
        ::std::nothrow)::baidu::paddle_serving::predictor::FactoryDerive<D,    \
                                                                         B>(); \
    if (factory == NULL ||                                                     \
        ::baidu::paddle_serving::predictor::FactoryPool<B>::instance()         \
                .register_factory(N, factory) != 0) {                          \
      RAW_LOG_FATAL(                                                           \
          "Failed regist factory: %s->%s, tag: %s in macro!", #D, #B, N);      \
      return;                                                                  \
    }                                                                          \
    RAW_LOG_WARNING(                                                           \
        "Succ regist factory: %s->%s, tag: %s in macro!", #D, #B, N);          \
    return;                                                                    \
  }

template <typename B>
W
wangguibao 已提交
82
class FactoryBase {
W
wangguibao 已提交
83 84 85
 public:
  virtual B* gen() = 0;
  virtual void del(B* obj) = 0;
W
wangguibao 已提交
86 87
};

W
wangguibao 已提交
88
template <typename D, typename B>
W
wangguibao 已提交
89
class FactoryDerive : public FactoryBase<B> {
W
wangguibao 已提交
90 91
 public:
  B* gen() { return new (std::nothrow) D(); }
W
wangguibao 已提交
92

W
wangguibao 已提交
93
  void del(B* obj) { delete dynamic_cast<D*>(obj); }
W
wangguibao 已提交
94 95
};

W
wangguibao 已提交
96
template <typename B>
W
wangguibao 已提交
97
class FactoryPool {
W
wangguibao 已提交
98 99 100 101 102 103 104 105 106 107 108 109
 public:
  static FactoryPool<B>& instance() {
    static FactoryPool<B> singleton;
    return singleton;
  }

  int register_factory(const std::string& tag, FactoryBase<B>* factory) {
    typename std::map<std::string, FactoryBase<B>*>::iterator it =
        _pool.find(tag);
    if (it != _pool.end()) {
      RAW_LOG_FATAL("Insert duplicate with tag: %s", tag.c_str());
      return -1;
W
wangguibao 已提交
110 111
    }

W
wangguibao 已提交
112 113 114 115 116
    std::pair<typename std::map<std::string, FactoryBase<B>*>::iterator, bool>
        r = _pool.insert(std::make_pair(tag, factory));
    if (!r.second) {
      RAW_LOG_FATAL("Failed insert new factory with: %s", tag.c_str());
      return -1;
W
wangguibao 已提交
117 118
    }

W
wangguibao 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    RAW_LOG_INFO("Succ insert one factory, tag: %s, base type %s",
                 tag.c_str(),
                 typeid(B).name());

    return 0;
  }

  B* generate_object(const std::string& tag) {
    typename std::map<std::string, FactoryBase<B>*>::iterator it =
        _pool.find(tag);
    if (it == _pool.end() || it->second == NULL) {
      RAW_LOG_FATAL("Not found factory pool, tag: %s, pool size %u",
                    tag.c_str(),
                    _pool.size());
      return NULL;
W
wangguibao 已提交
134 135
    }

W
wangguibao 已提交
136 137
    return it->second->gen();
  }
W
wangguibao 已提交
138

W
wangguibao 已提交
139 140 141 142 143
  template <typename D>
  void return_object(B* object) {
    FactoryDerive<D, B> factory;
    factory.del(object);
  }
W
wangguibao 已提交
144

W
wangguibao 已提交
145 146 147
 private:
  std::map<std::string, FactoryBase<B>*> _pool;
};
W
wangguibao 已提交
148

W
wangguibao 已提交
149 150 151
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu
W
wangguibao 已提交
152

W
wangguibao 已提交
153
/* vim: set expandtab ts=2 sw=2 sts=2 tw=100: */