1 // This is brl/bpro/bprb/bprb_func_process.h 2 #ifndef bprb_func_process_h_ 3 #define bprb_func_process_h_ 4 //------------------------------------------------------------------------------ 5 //: 6 // \file 7 // \brief The bmdl process class 8 // 9 // A specialized process class for bpro library. This method receives 10 // a separate function pointer for init, execute and finish method to 11 // execute 12 // 13 // \author 14 // Gamze D. Tunali 15 // 16 // \verbatim 17 // Modifications: 18 // Gamze D. Tunali November 20, 2008 Initial version. 19 // \endverbatim 20 //------------------------------------------------------------------------------ 21 #include <iostream> 22 #include <bprb/bprb_process_ext.h> 23 #ifdef _MSC_VER 24 # include <vcl_msvc_warnings.h> 25 #endif 26 27 class bprb_func_process: public bprb_process_ext 28 { 29 public: 30 bprb_func_process() = default; 31 bprb_func_process(bool (* fpt)(bprb_func_process &),const char * name)32 bprb_func_process(bool(*fpt)(bprb_func_process&), const char* name) 33 : fpt_(fpt), fpt_cons_(nullptr), fpt_init_(nullptr), fpt_finish_(nullptr), name_(name) 34 {} 35 bprb_func_process(bool (* fpt)(bprb_func_process &),const char * name,bool (* cons)(bprb_func_process &),bool (* init)(bprb_func_process &),bool (* finish)(bprb_func_process &))36 bprb_func_process(bool(*fpt)(bprb_func_process&), const char* name, 37 bool(*cons)(bprb_func_process&), 38 bool(*init)(bprb_func_process&), 39 bool(*finish)(bprb_func_process&)) 40 : fpt_(fpt), fpt_cons_(cons), fpt_init_(init), 41 fpt_finish_(finish), name_(name) 42 { if (fpt_cons_) fpt_cons_(*this); } 43 44 ~bprb_func_process() override = default; 45 clone()46 bprb_func_process* clone() const override { return new bprb_func_process(fpt_, name_.c_str(),fpt_cons_, fpt_init_, fpt_finish_); } 47 set_init_func(bool (* fpt)(bprb_func_process &))48 void set_init_func(bool(*fpt)(bprb_func_process&)) { fpt_init_ = fpt; } 49 set_finish_func(bool (* fpt)(bprb_func_process &))50 void set_finish_func(bool(*fpt)(bprb_func_process&)) { fpt_finish_ = fpt; } 51 name()52 std::string name() const override { return name_; } 53 54 template <class T> get_input(unsigned i)55 T get_input(unsigned i) 56 { 57 if (input_types_.size()>i) { 58 if (!input_data_[i]) { 59 std::cerr << "ERROR: input_data_[" << i << "] == NULL" << std::endl; 60 return 0; 61 } 62 if (!(input_data_[i]->is_a()==input_types_[i])) { 63 std::cerr << "Input: [" << i << "] has wrong INPUT TYPE! \n" << "Should be: " << input_types_[i] << " is: " <<input_data_[i]->is_a() << "\n"; 64 return 0; 65 } 66 } 67 brdb_value_t<T>* input = static_cast<brdb_value_t<T>* >(input_data_.at(i).ptr()); 68 T val = input->value(); 69 return val; 70 } 71 72 template <class T> set_output_val(unsigned int i,T data)73 void set_output_val(unsigned int i, T data) 74 { 75 brdb_value_sptr output = new brdb_value_t<T>(data); 76 set_output(i, output); 77 } 78 79 //: Execute the process execute()80 bool execute() override { return fpt_(*this); } 81 82 //: Perform any initialization required by the process init()83 bool init() override { if (fpt_init_) return fpt_init_(*this); else return false; } 84 85 //: Perform any clean up or final computation finish()86 bool finish() override { if (fpt_finish_) return fpt_finish_(*this); else return false; } 87 88 private: 89 bool (*fpt_)(bprb_func_process&); // pointer to execute method 90 bool (*fpt_cons_)(bprb_func_process&); // pointer to cons method (like constructor) 91 bool (*fpt_init_)(bprb_func_process&); // pointer to init method 92 bool (*fpt_finish_)(bprb_func_process&); // pointer to finish method 93 std::string name_; 94 }; 95 96 #endif // bprb_func_process_h_ 97