blockfactory  0.8
A framework for wrapping algorithms for dataflow programming
GeneratedCodeWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * GNU Lesser General Public License v2.1 or any later version.
7  */
8 
9 #ifndef BLOCKFACTORY_CODER_GENERATEDCODEWRAPPER_H
10 #define BLOCKFACTORY_CODER_GENERATEDCODEWRAPPER_H
11 
12 #ifndef MODEL
13 #error "MODEL option not specified"
14 #endif
15 
16 #include <iostream>
17 #include <memory>
18 #include <string>
19 
20 namespace blockfactory {
21  namespace coder {
22  template <typename T>
24  } // namespace coder
25 } // namespace blockfactory
26 
27 template <typename T>
29 {
30 private:
31  std::unique_ptr<T> m_model;
32  std::string m_modelName;
33  unsigned m_numSampleTimes;
34 
35  bool modelFailed() const;
36 
37 public:
38  GeneratedCodeWrapper(const std::string& modelName = {}, const unsigned& numSampleTimes = 0);
39  ~GeneratedCodeWrapper() = default;
40 
41  bool initialize();
42  bool step();
43  bool terminate();
44 
45  // double* getOutput(const unsigned& index) const;
46 
47  std::string getErrors() const;
48  // std::string getWarnings() const;
49 };
50 
51 template <typename T>
53 {
54  if (m_model) {
55  if (m_model->getRTM()) {
56  if (!m_model->getRTM()->errorStatus)
57  return false;
58  }
59  }
60  return true;
61 }
62 
63 template <typename T>
65  const unsigned& numSampleTimes)
66  : m_modelName(modelName)
67  , m_numSampleTimes(numSampleTimes)
68 {}
69 
70 template <typename T>
72 {
73  if (m_model) {
74  m_model.reset();
75  }
76 
77  m_model = std::make_unique<T>();
78  m_model->initialize();
79 
80  if (modelFailed()) {
81  return false;
82  }
83 
84  return true;
85 }
86 
87 template <typename T>
89 {
90  if (!m_model) {
91  return false;
92  }
93 
94  m_model->step();
95 
96  if (modelFailed()) {
97  return false;
98  }
99 
100  return true;
101 }
102 
103 template <typename T>
105 {
106  if (!m_model) {
107  return false;
108  }
109 
110  m_model->terminate();
111 
112  if (modelFailed()) {
113  return false;
114  }
115 
116  return true;
117 }
118 
119 template <typename T>
121 {
122  if (!m_model) {
123  return {};
124  }
125 
126  if (modelFailed()) {
127  return {m_model->getRTM()->errorStatus};
128  }
129 
130  return {};
131 }
132 
133 #endif // BLOCKFACTORY_CODER_GENERATEDCODEWRAPPER_H
bool modelFailed() const
Definition: GeneratedCodeWrapper.h:52
bool terminate()
Definition: GeneratedCodeWrapper.h:104
std::unique_ptr< T > m_model
Definition: GeneratedCodeWrapper.h:31
Definition: GeneratedCodeWrapper.h:23
bool step()
Definition: GeneratedCodeWrapper.h:88
Definition: Block.h:17
GeneratedCodeWrapper(const std::string &modelName={}, const unsigned &numSampleTimes=0)
Definition: GeneratedCodeWrapper.h:64
std::string getErrors() const
Definition: GeneratedCodeWrapper.h:120
std::string m_modelName
Definition: GeneratedCodeWrapper.h:32
unsigned m_numSampleTimes
Definition: GeneratedCodeWrapper.h:33
bool initialize()
Definition: GeneratedCodeWrapper.h:71