ifw-ccf  2.0.0
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  CCFINFO(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 
147  // /// Set the enabled status flag of the recipe object.
148  // void SetEnabled(const bool enabled);
149 
150  // /// Disable the recipe so that it will not be invoked.
151  // void DisableRecipe();
152 
154  void Enable();
155 
158  virtual void EnableUser();
159 
162  void Disable();
163 
166  virtual void DisableUser();
167 
169  bool GetEnabled() const;
170 
172  virtual void ProcessUser(DataFrame& frame);
173 
175  virtual void CreateObjectUser(const std::string& proc_thread_name,
176  const std::string& recipe_name,
177  std::shared_ptr<RecipeBase>& new_object);
178 
180  void SetStatus(const ProcStatus status);
181 
183  ProcStatus GetStatus() const;
184 
185  // TODO: Add:
186  // void ResetDbStatus() const;
187  // void UpdateDbStatus() const;
188  // std::string ToString() const;
189  // void Dismantle();
190  // virtual void DismantleUser();
191 
192  // TODO: Add + invoke when a setup command is received.
193  // void HandleSetup();
194  // void HandleSetupUser();
195 
196  protected:
197 
199  void SetEnabled(const bool enabled);
200 
202  void DisableRecipe();
203 
204  private:
205  static std::map<std::string, std::shared_ptr<RecipeBase>> s_recipe_factory_objects;
206  static std::map<std::string, std::shared_ptr<RecipeBase>> s_recipe_objects;
207  static bool s_cpl_initialised;
208 
209  bool m_initialised;
210  bool m_enabled;
211  double m_delay;
212 
213  std::string m_recipe_id;
214  std::string m_recipe_name;
215  std::string m_proc_thread_name;
216 
217  ProcStatus m_recipe_status;
218 
219  std::string m_db_base_name;
220  };
221 
222 }
223 
224 #endif // CCF_COMMON_RECIPE_BASE_HPP_H_
ccf::Base
Class to be used as parent all CCF classes.
Definition: base.hpp:152
ccf::common::RecipeBase::GenId
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
ccf::common::RecipeBase::GetInitialised
bool GetInitialised() const
Return flag indicating if the recipe object has been initialised.
Definition: recipeBase.cpp:213
CCFTHROW
#define CCFTHROW(msg)
Definition: base.hpp:568
ccf::common::RecipeBase::SetStatus
void SetStatus(const ProcStatus status)
Update status for a given Publisher Thread in the OLDB.
Definition: recipeBase.cpp:305
CCFINFO
#define CCFINFO(logger, msg)
INFO log macro. Includes the location ("CCFLOC") in the log message.
Definition: base.hpp:552
ccf::common::RecipeBase::Process
void Process(DataFrame &frame)
Execute the processing of the given frame.
Definition: recipeBase.cpp:242
db.hpp
ccf::AssertPtr
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
ccf::common::RecipeBase::GetRecipeObj
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:71
ccf::common::RecipeBase::AddRecipeFactoryObj
static void AddRecipeFactoryObj(TYPE &recipe_factory_obj)
Static method to register a Data Publisher factory object in the internal registry.
Definition: recipeBase.hpp:56
ccf::common::RecipeBase::CreateObjectUser
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:267
ccf::common::RecipeBase::GetRecipeName
const std::string & GetRecipeName() const
Get the name of the recipe.
Definition: recipeBase.cpp:188
ccf::common::RecipeBase::GetRecipeFactoryObjs
static const std::map< std::string, std::shared_ptr< RecipeBase > > & GetRecipeFactoryObjs()
Statis method to retrieve references to the Recipe Factory Objects.
Definition: recipeBase.cpp:47
ccf::common::RecipeBase::Disable
void Disable()
Definition: recipeBase.cpp:291
ccf::common::RecipeBase::Initialise
void Initialise()
Initalise the object. Invoked after creating a new object from the factory object.
Definition: recipeBase.cpp:193
setup.hpp
ccf::common::RecipeBase::RecipeBase
RecipeBase()
Definition: recipeBase.cpp:166
ccf::common::RecipeBase::CreateRecipeObj
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:52
CCFLOC
#define CCFLOC
Macro generating a location identifier: "<file>:<line>:<function>:<thread>".
Definition: base.hpp:457
ccf::common::RecipeBase::HasRecipeObj
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:95
ccf::common::RecipeBase::DisableRecipe
void DisableRecipe()
Disable the recipe so that it will not be invoked.
Definition: recipeBase.cpp:231
ccf::ProcStatus
ProcStatus
Possible states for a Processing Recipe defined.
Definition: base.hpp:303
ccf::common::RecipeBase::EnableUser
virtual void EnableUser()
Definition: recipeBase.cpp:286
ccf::common::RecipeBase::GetDbPath
const std::string & GetDbPath() const
Generate DB path for this recipe.
Definition: recipeBase.cpp:208
ccf::common::RecipeBase::~RecipeBase
virtual ~RecipeBase()
Definition: recipeBase.cpp:179
ccf::common::RecipeBase::GetRecipeObjs
static const std::map< std::string, std::shared_ptr< RecipeBase > > & GetRecipeObjs()
Get reference to all Processing Recipe objects registered.
Definition: recipeBase.cpp:115
dataFrame.hpp
ccf::common::RecipeBase::DisableUser
virtual void DisableUser()
Definition: recipeBase.cpp:300
ccf::common::PrepCplImage
void PrepCplImage(const ccf::common::DataFrame &frame, cpl_image **image, ccf::DataTypes *target_data_type, uint32_t *target_buffer_size)
Definition: recipeBase.cpp:323
base.hpp
ccf::common
Definition: appBase.cpp:8
ccf::common::RecipeBase::ProcessUser
virtual void ProcessUser(DataFrame &frame)
User specific processing.
Definition: recipeBase.cpp:256
ccf::common::ConvertImage
void ConvertImage(SRC_IM_TYPE *image_buffer, uint32_t pixels, TRG_IM_TYPE **target_buf, uint32_t *target_buf_size)
Definition: recipeBase.hpp:22
ccf::common::RecipeBase::GetRecipeId
const std::string & GetRecipeId() const
Get the ID of the recipe.
Definition: recipeBase.cpp:183
ccf::common::RecipeBase::GetStatus
ProcStatus GetStatus() const
Get status of the recipe object.
Definition: recipeBase.cpp:318
ccf::common::RecipeBase
Processing Recipe base class. All recipes shall be derived from this class.
Definition: recipeBase.hpp:47
ccf::common::RecipeBase::InitialiseUser
virtual void InitialiseUser()
Initialise the recipe object. See "Initialise()".
Definition: recipeBase.cpp:218
ccf::common::RecipeBase::GetProcThreadName
const std::string & GetProcThreadName() const
Get the name of the Processing Thread, hosting the Recipe instance.
Definition: recipeBase.cpp:262
ccf::common::RecipeBase::GetEnabled
bool GetEnabled() const
Return the enabled status of the recipe.
Definition: recipeBase.cpp:237
ccf::DataTypes
DataTypes
Definition: dataType.hpp:46
ccf::common::DataFrame
Frame class used to store the data and metadata for one frames received from the camera.
Definition: dataFrame.hpp:17
ccf::Logger
log4cplus::Logger & Logger()
Definition: base.cpp:9
ccf::common::RecipeBase::Enable
void Enable()
Enable the recipe so that it will be invoked by the Processing Thread to which it belongs.
Definition: recipeBase.cpp:274
ccf::common::RecipeBase::SetEnabled
void SetEnabled(const bool enabled)
Set the enabled status flag of the recipe object.
Definition: recipeBase.cpp:225