ifw-ccf  3.0.0-pre2
recipeBase.hpp
Go to the documentation of this file.
1 
5 #ifndef CCF_COMMON_RECIPE_BASE_HPP_H_
6 #define CCF_COMMON_RECIPE_BASE_HPP_H_
7 
8 #include <cpl.h>
9 #include <clipm.h>
10 
11 #include <ccf/common/base.hpp>
12 #include <ccf/common/db.hpp>
13 #include <ccf/common/setup.hpp>
14 #include <ccf/common/dataFrame.hpp>
15 
16 
17 namespace ccf::common {
18 
21  template <class SRC_IM_TYPE, class TRG_IM_TYPE>
22  void ConvertImage(SRC_IM_TYPE* image_buffer,
23  uint32_t pixels,
24  TRG_IM_TYPE** target_buf,
25  uint32_t* target_buf_size) {
26  LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
27  *target_buf_size = (pixels * sizeof(TRG_IM_TYPE));
28  *target_buf = (TRG_IM_TYPE*)malloc(*target_buf_size);
29  ccf::AssertPtr(target_buf, "CPL Image Tmp Buffer", CCFLOC);
30  SRC_IM_TYPE* im_ptr = image_buffer;
31  TRG_IM_TYPE* target_buf_ptr = *target_buf;
32  for (uint32_t n = 0; n < pixels; n++) {
33  *target_buf_ptr++ = static_cast<TRG_IM_TYPE>(*im_ptr++);
34  }
35  }
36 
40  void PrepCplImage(const ccf::common::DataFrame& frame,
41  cpl_image** image,
42  ccf::DataTypes* target_data_type,
43  uint32_t* target_buffer_size);
44 
45 
47  class RecipeBase: public Base {
48  public:
49 
51  static const std::string& GenId(const std::string& proc_thread_name,
52  const std::string& recipe_name);
53 
55  template <class TYPE>
56  static void AddRecipeFactoryObj(TYPE& recipe_factory_obj) {
57  LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
58 
59  if (recipe_factory_obj.GetClassName() == "") {
60  CCFTHROW("Must define a classname for a Processing Recipe Plug-In");
61  }
62 
63  BEGIN_CRIT_SEC("ccf::common::RecipeBase") {
64  if (s_recipe_factory_objects.find(recipe_factory_obj.GetClassName()) !=
65  s_recipe_factory_objects.end()) {
66  CCFTHROW(fmt::format("Factory object with classname: {} already exists",
67  recipe_factory_obj.GetClassName()));
68  }
69  LOG4CPLUS_INFO(Logger(), "Registering Processing Recipe factory object. Classname: " <<
70  recipe_factory_obj.GetClassName());
71  s_recipe_factory_objects[recipe_factory_obj.GetClassName()] = std::make_shared<TYPE>();
72  } END_CRIT_SEC();
73 
74  }
75 
77  static const std::map<std::string, std::shared_ptr<RecipeBase>>& GetRecipeFactoryObjs();
78 
84  static void CreateRecipeObj(const std::string& class_name,
85  const std::string& proc_thread_name,
86  const std::string& recipe_name,
87  std::shared_ptr<RecipeBase>& new_obj);
88 
93  static void GetRecipeObj(const std::string& class_name,
94  const std::string& proc_thread_name,
95  std::shared_ptr<RecipeBase>& recipe_obj,
96  const bool initialise = true);
97 
99  static const std::map<std::string, std::shared_ptr<RecipeBase>>& GetRecipeObjs();
100 
102  static bool HasRecipeObj(const std::string& class_name,
103  const std::string& proc_thread_name,
104  std::shared_ptr<RecipeBase>& recipe_obj);
105 
108  // static void FreeRecipeObjs();
109 
112  // static void FreeRecipeFactoryObjs();
113 
116  RecipeBase(const std::string& proc_thread_name,
117  const std::string& recipe_name);
118 
119  RecipeBase();
120 
121  virtual ~RecipeBase();
122 
124  void Initialise();
125 
127  virtual void InitialiseUser();
128 
130  bool GetInitialised() const;
131 
133  const std::string& GetRecipeId() const;
134 
136  const std::string& GetProcThreadName() const;
137 
139  const std::string& GetRecipeName() const;
140 
142  const std::string& GetDbPath() const;
143 
145  void Process(DataFrame& frame);
146 
148  void Enable();
149 
152  virtual void EnableUser();
153 
156  void Disable();
157 
160  virtual void DisableUser();
161 
163  bool GetEnabled() const;
164 
166  virtual void ProcessUser(DataFrame& frame);
167 
169  virtual void CreateObjectUser(const std::string& proc_thread_name,
170  const std::string& recipe_name,
171  std::shared_ptr<RecipeBase>& new_object);
172 
174  void SetStatus(const ProcStatus status);
175 
177  ProcStatus GetStatus() const;
178 
179  // TODO: Add:
180  // void ResetDbStatus() const;
181  // void UpdateDbStatus() const;
182  // std::string ToString() const;
183  // void Dismantle();
184  // virtual void DismantleUser();
185 
186  // TODO: Add + invoke when a setup command is received.
187  // void HandleSetup();
188  // void HandleSetupUser();
189 
190  protected:
191 
193  void SetEnabled(const bool enabled);
194 
196  void DisableRecipe();
197 
198  private:
199  static std::map<std::string, std::shared_ptr<RecipeBase>> s_recipe_factory_objects;
200  static std::map<std::string, std::shared_ptr<RecipeBase>> s_recipe_objects;
201  static bool s_cpl_initialised;
202 
203  bool m_initialised;
204  bool m_enabled;
205  double m_delay;
206 
207  std::string m_recipe_id;
208  std::string m_recipe_name;
209  std::string m_proc_thread_name;
210 
211  ProcStatus m_recipe_status;
212 
213  std::string m_db_base_name;
214  };
215 
216 }
217 
218 #endif // CCF_COMMON_RECIPE_BASE_HPP_H_
#define CCFLOC
Macro generating a location identifier: "<file>:<line>:<function>:<thread>".
Definition: base.hpp:441
#define CCFTHROW(msg)
Guard for TRACE logs. Ensures log text is only generated when the given log level is enabled.
Definition: base.hpp:489
Class to be used as parent all CCF classes.
Definition: base.hpp:151
Frame class used to store the data and metadata for one frames received from the camera.
Definition: dataFrame.hpp:17
Processing Recipe base class. All recipes shall be derived from this class.
Definition: recipeBase.hpp:47
virtual void DisableUser()
Definition: recipeBase.cpp:257
const std::string & GetDbPath() const
Generate DB path for this recipe.
Definition: recipeBase.cpp:165
RecipeBase()
Definition: recipeBase.cpp:118
static void AddRecipeFactoryObj(TYPE &recipe_factory_obj)
Static method to register a Data Publisher factory object in the internal registry.
Definition: recipeBase.hpp:56
static const std::map< std::string, std::shared_ptr< RecipeBase > > & GetRecipeObjs()
Get reference to all Processing Recipe objects registered.
Definition: recipeBase.cpp:94
void SetEnabled(const bool enabled)
Set the enabled status flag of the recipe object.
Definition: recipeBase.cpp:182
virtual ~RecipeBase()
Definition: recipeBase.cpp:131
static void CreateRecipeObj(const std::string &class_name, const std::string &proc_thread_name, const std::string &recipe_name, std::shared_ptr< RecipeBase > &new_obj)
Definition: recipeBase.cpp:31
void DisableRecipe()
Disable the recipe so that it will not be invoked.
Definition: recipeBase.cpp:188
bool GetInitialised() const
Return flag indicating if the recipe object has been initialised.
Definition: recipeBase.cpp:170
virtual void EnableUser()
Definition: recipeBase.cpp:243
bool GetEnabled() const
Return the enabled status of the recipe.
Definition: recipeBase.cpp:194
const std::string & GetProcThreadName() const
Get the name of the Processing Thread, hosting the Recipe instance.
Definition: recipeBase.cpp:219
const std::string & GetRecipeName() const
Get the name of the recipe.
Definition: recipeBase.cpp:140
virtual void InitialiseUser()
Initialise the recipe object. See "Initialise()".
Definition: recipeBase.cpp:175
static const std::string & GenId(const std::string &proc_thread_name, const std::string &recipe_name)
Generate the ID for this recipe from the Processing Thread and Recipe number.
Definition: recipeBase.cpp:14
static void GetRecipeObj(const std::string &class_name, const std::string &proc_thread_name, std::shared_ptr< RecipeBase > &recipe_obj, const bool initialise=true)
Definition: recipeBase.cpp:50
void Enable()
Enable the recipe so that it will be invoked by the Processing Thread to which it belongs.
Definition: recipeBase.cpp:231
ProcStatus GetStatus() const
Get status of the recipe object.
Definition: recipeBase.cpp:275
void Initialise()
Initalise the object. Invoked after creating a new object from the factory object.
Definition: recipeBase.cpp:145
virtual void CreateObjectUser(const std::string &proc_thread_name, const std::string &recipe_name, std::shared_ptr< RecipeBase > &new_object)
Instantiate a specific instance of the object.
Definition: recipeBase.cpp:224
void Disable()
Definition: recipeBase.cpp:248
void Process(DataFrame &frame)
Execute the processing of the given frame.
Definition: recipeBase.cpp:199
static bool HasRecipeObj(const std::string &class_name, const std::string &proc_thread_name, std::shared_ptr< RecipeBase > &recipe_obj)
Check if a Proc Recipe object is defined.
Definition: recipeBase.cpp:74
virtual void ProcessUser(DataFrame &frame)
User specific processing.
Definition: recipeBase.cpp:213
const std::string & GetRecipeId() const
Get the ID of the recipe.
Definition: recipeBase.cpp:135
static const std::map< std::string, std::shared_ptr< RecipeBase > > & GetRecipeFactoryObjs()
Statis method to retrieve references to the Recipe Factory Objects.
Definition: recipeBase.cpp:26
void SetStatus(const ProcStatus status)
Update status for a given Publisher Thread in the OLDB.
Definition: recipeBase.cpp:262
Definition: appBase.cpp:8
void PrepCplImage(const ccf::common::DataFrame &frame, cpl_image **image, ccf::DataTypes *target_data_type, uint32_t *target_buffer_size)
Definition: recipeBase.cpp:280
void ConvertImage(SRC_IM_TYPE *image_buffer, uint32_t pixels, TRG_IM_TYPE **target_buf, uint32_t *target_buf_size)
Definition: recipeBase.hpp:22
ProcStatus
Possible states for a Processing Recipe defined.
Definition: base.hpp:289
void AssertPtr(const void *ptr, const std::string &object, const std::string &location)
Check that pointer is not nullptr and raise rad::exception in case it is.
Definition: base.cpp:53
log4cplus::Logger & Logger()
Definition: base.cpp:9
DataTypes
Definition: dataType.hpp:46