CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
llvm_model_library.hpp
1 #ifndef CPPAD_CG_LLVM_MODEL_LIBRARY_INCLUDED
2 #define CPPAD_CG_LLVM_MODEL_LIBRARY_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2013 Ciengis
6  * Copyright (C) 2018 Joao Leal
7  *
8  * CppADCodeGen is distributed under multiple licenses:
9  *
10  * - Eclipse Public License Version 1.0 (EPL1), and
11  * - GNU General Public License Version 3 (GPL3).
12  *
13  * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
14  * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
15  * ----------------------------------------------------------------------------
16  * Author: Joao Leal
17  */
18 
19 namespace CppAD {
20 namespace cg {
21 
22 template<class Base>
23 class LlvmModel;
24 
30 template<class Base>
31 class LlvmModelLibrary : public FunctorModelLibrary<Base> {
32 protected:
33  std::set<LlvmModel<Base>*> _models;
34 public:
35  inline virtual ~LlvmModelLibrary() {
36  // do not call clean-up here
37  // cleanUp() must be called by the subclass (before destruction of the execution engine...)
38  }
39 
40  virtual std::unique_ptr<LlvmModel<Base>> modelLlvm(const std::string& modelName) {
41  std::unique_ptr<LlvmModel<Base>> m;
42  typename std::set<std::string>::const_iterator it = this->_modelNames.find(modelName);
43  if (it == this->_modelNames.end()) {
44  return m;
45  }
46  m.reset(new LlvmModel<Base> (this, modelName));
47  _models.insert(m.get());
48  return m;
49  }
50 
51  std::unique_ptr<FunctorGenericModel<Base>> modelFunctor(const std::string& modelName) override final {
52  return std::unique_ptr<FunctorGenericModel<Base>>(modelLlvm(modelName).release());
53  }
54 
55 protected:
56  inline LlvmModelLibrary() = default;
57 
58  inline void cleanUp() {
59  for (LlvmModel<Base>* model : _models) {
60  model->modelLibraryClosed();
61  }
62 
63  if(this->_onClose != nullptr) {
64  (*this->_onClose)();
65  this->_onClose = nullptr;
66  }
67  }
68 
69  virtual void destroyed(LlvmModel<Base>* model) {
70  _models.erase(model);
71  }
72 
73  friend class LlvmModel<Base>;
74 };
75 
76 } // END cg namespace
77 } // END CppAD namespace
78 
79 #endif
std::unique_ptr< FunctorGenericModel< Base > > modelFunctor(const std::string &modelName) override final
std::unique_ptr< GenericModel< Base > > model(const std::string &modelName) override final