Sierra Toolkit  Version of the Day
UserPlugin.hpp
Go to the documentation of this file.
1 #ifndef STK_UTIL_DIAG_UserPlugin_h
2 #define STK_UTIL_DIAG_UserPlugin_h
3 
4 #include <memory>
5 #include <map>
6 #include <vector>
7 #include <string>
8 #include <typeinfo>
9 
10 #include <stk_util/diag/StringUtil.hpp>
11 #include <stk_util/util/Fortran.hpp>
12 #include <stk_util/diag/Writer_fwd.hpp>
13 
38 namespace sierra {
39 namespace Plugin {
40 
41 std::string derived_id_name(int derived_id);
42 
55 class Registry
56 {
57 public:
58 
63  typedef std::pair<const std::type_info *, std::string> NamePair;
64 
65 // /**
66 // * @brief Class <b>hash_nocase</b> implements a hash, case insensitive NamePair
67 // * hash functor.
68 // *
69 // */
70 // struct hash_nocase
71 // {
72 // size_t operator()(const NamePair &n) const {
73 // return sierra::hash_string_nocase(n.second.c_str());
74 // }
75 // };
76 
77 // /**
78 // * @brief Class <b>hash_nocase</b> implements a hash, case insensitive compare
79 // * equal NamePair functor.
80 // *
81 // */
82 // struct equal_nocase : public std::binary_function<NamePair, NamePair, bool>
83 // {
84 // bool operator()(const NamePair &lhs, const NamePair &rhs) const {
85 // sierra::equal_nocase<NamePair::second_type> second_equal_nocase;
86 
87 // return *lhs.first == *rhs.first && second_equal_nocase(lhs.second, rhs.second);
88 // }
89 // };
90 
96  struct less_nocase : public std::binary_function<NamePair, NamePair, bool>
97  {
98  bool operator()(const NamePair &lhs, const NamePair &rhs) const {
100 
101 #ifdef SIERRA_TYPE_INFO_BEFORE_EQUALITY_BUG
102  return (lhs.first->before(*rhs.first) && *lhs.first != *rhs.first)
103  || (*lhs.first == *rhs.first && second_less_nocase(lhs.second, rhs.second));
104 #else
105  return lhs.first->before(*rhs.first)
106  || (*lhs.first == *rhs.first && second_less_nocase(lhs.second, rhs.second));
107 #endif
108  }
109  };
110 
115  typedef std::map<NamePair, void *, less_nocase> RegistryMap;
116 
117  static RegistryMap &getRegistryMap();
118 
124  {}
125 
133  explicit Registry(const NamePair &name_pair) {
134  registerIt(name_pair, this);
135  }
136 
142  virtual ~Registry()
143  {}
144 
151  static Registry &rootInstance();
152 
175  static void registerDL(const char *so_path, const char *function_name = 0);
176 
177  template <typename T>
178  static T getsym(const char *sym);
179 
198  void registerIt(const NamePair &name_pair, void *func_ptr);
199 
214  void *getPluginPtr(const NamePair &name_pair) const;
215 
230  void *getFunctionPtr(const NamePair &name_pair) const;
231 
242  Registry *getFactoryPtr(const NamePair &name) const;
243 
254  void *getFuncPtr(const NamePair &name_pair) const;
255 
266  std::vector<std::string> getDerivedNames(const std::type_info &type) const;
267 
282  template<class T>
283  static T &create(const std::string &derived_name) {
284  return static_cast<T &>(*Registry::rootInstance().getFactoryPtr(std::make_pair(&typeid(T), derived_name)));
285  }
286 
295  std::ostream &verbose_print(std::ostream &os) const;
296 
306 };
307 
308 inline stk_classic::diag::Writer &operator<<(stk_classic::diag::Writer &dout, const Registry &registry) {
309  return registry.verbose_print(dout);
310 }
311 
312 
330 template <class Creator, typename S = Creator *(*)()>
332 {
333 public:
334  typedef S Signature;
335 
336 private:
337  UserPlugin();
338  UserPlugin(const UserPlugin&);
339  UserPlugin &operator=(const UserPlugin&);
340 
341 public:
349  static UserPlugin &instance() {
351  }
352 
368  static void registerCreator(const std::string &derived_name, Signature function) {
369  Registry::rootInstance().registerIt(std::make_pair(&typeid(Signature), derived_name), (void *) function);
370  }
371 
386  static Signature create(const std::string &derived_name) {
387  Signature creator_function = (Signature) Registry::rootInstance().getPluginPtr(std::make_pair(&typeid(Signature), derived_name));
388 
389  return (*creator_function);
390  }
391 
406  static Signature create(int derived_id) {
407  return create(derived_id_name(derived_id));
408  }
409 
419  static bool exists(const std::string &derived_name) {
420  return Registry::rootInstance().getFuncPtr(std::make_pair(&typeid(Signature), derived_name)) != NULL;
421  }
422 
423  static std::vector<std::string> getDerivedNames() {
425  }
426 
435  template <class DerivedClass>
436  class Register
437  {
438  public:
439  typedef DerivedClass XDerivedClass;
440 
450  explicit Register(const std::string &derived_name)
451  : m_function(DerivedClass::createInstance)
452  {
453  UserPlugin<Creator, Signature>::instance().registerCreator(derived_name, m_function);
454  }
455 
465  Register(const std::string &derived_name, Signature create_instance)
466  : m_function(create_instance)
467  {
468  UserPlugin<Creator, Signature>::instance().registerCreator(derived_name, m_function);
469  }
470 
479  explicit Register(int derived_id)
480  : m_function(XDerivedClass::createInstance)
481  {
482  UserPlugin<Creator, Signature>::instance().registerCreator(derived_id_name(derived_id), m_function);
483  }
484 
493  Register(int derived_id, Signature create_instance)
494  : m_function(create_instance)
495  {
496  UserPlugin<Creator, Signature>::instance().registerCreator(derived_id_name(derived_id), m_function);
497  }
498 
499  private:
500  Signature m_function;
501  };
502 };
503 
504 
519 template <class S>
521 {
522 private:
523  UserSubroutine();
525  UserSubroutine &operator=(const UserSubroutine&);
526 
527 public:
528  typedef S Signature;
529 
537  inline static UserSubroutine &instance() {
539  }
540 
556  inline static void registerFunction(const std::string &function_name, Signature *function) {
557  Registry::rootInstance().registerIt(std::make_pair(&typeid(Signature), function_name), (void *) function);
558  }
559 
573  static Signature *execute(const std::string &function_name) {
574  Signature *user_function = (Signature *) Registry::rootInstance().getFunctionPtr(std::make_pair(&typeid(Signature), function_name));
575 
576  return (*user_function);
577  }
578 
592  static Signature *getFunction(const std::string &function_name) {
593  Signature *user_function = (Signature *) Registry::rootInstance().getFunctionPtr(std::make_pair(&typeid(Signature), function_name));
594 
595  return user_function;
596  }
597 
608  static bool exists(const std::string &derived_name) {
609  return Registry::rootInstance().getFuncPtr(std::make_pair(&typeid(Signature), derived_name)) != NULL;
610  }
611 
617  class Register
618  {
619  public:
628  Register(const std::string &function_name, Signature *function)
629  : m_function(function)
630  {
631  UserSubroutine<Signature>::instance().registerFunction(function_name, *m_function);
632  }
633 
634  private:
635  Signature * m_function;
636  };
637 };
638 
639 template <>
640 void *Registry::getsym<void *>(const char *sym);
641 
642 template <typename T>
643 inline T Registry::getsym(const char *sym) {
644  return static_cast<T>(getsym<void *>(sym));
645 }
646 
647 } // namespace Plugin
648 } // namespace sierra
649 
650 typedef std::type_info *type_info_func();
651 
662 extern "C" {
663  void SIERRA_FORTRAN(register_user_subroutine)(
664  type_info_func type_id,
665  void * user_subroutine,
666  const char * name,
667  int name_length );
668 }
669 
670 
683 #define FORTRAN_USER_SUBROUTINE(NAME, USER_SUB) extern "C" const std::type_info * SIERRA_FORTRAN(NAME)() {return &typeid(USER_SUB::Signature);}
684 
685 #endif // STK_UTIL_DIAG_UserPlugin_h
Class template Register registers the createInstance() function with the derived_name on object creat...
Definition: UserPlugin.hpp:436
Registry(const NamePair &name_pair)
Creates a new Registry instance and registers it, and more importantly the derived class factory with...
Definition: UserPlugin.hpp:133
void SIERRA_FORTRAN() register_user_subroutine(type_info_func type_id, void *user_subroutine, const char *name, int name_length)
FORTRAN compatible user subprogram registration routine.
std::ostream & dout()
Diagnostic output stream.
Definition: OutputLog.cpp:674
S Signature
Subroutine call signature.
Definition: UserPlugin.hpp:528
Definition: Env.cpp:53
static Signature create(int derived_id)
Member function create returns the createInstance() function associated with the specified base class...
Definition: UserPlugin.hpp:406
std::pair< const std::type_info *, std::string > NamePair
Typedef NamePair is the derived class key.
Definition: UserPlugin.hpp:63
static void registerDL(const char *so_path, const char *function_name=0)
Member function registerDL opens a dynamic library and optionally executes a "C" registration functio...
Definition: UserPlugin.cpp:151
static bool exists(const std::string &derived_name)
Member function exists returns true if user function specified by derived_name exists.
Definition: UserPlugin.hpp:608
std::ostream & verbose_print(std::ostream &os) const
Member function dump dumps the registry.
Definition: UserPlugin.cpp:212
S Signature
Creator signature.
Definition: UserPlugin.hpp:334
static bool exists(const std::string &derived_name)
Member function exists returns true if class of the type specified by derived_name exists in BaseClas...
Definition: UserPlugin.hpp:419
static UserPlugin & instance()
Member function instance returns the instance of the registry, cast as a UserPlugin registry...
Definition: UserPlugin.hpp:349
Register(const std::string &derived_name, Signature create_instance)
Creates a new Register instance. Upon creation, the DerivedClass::createInstance() instance creation ...
Definition: UserPlugin.hpp:465
static Signature create(const std::string &derived_name)
Member function create returns the createInstance() function associated with the specified base class...
Definition: UserPlugin.hpp:386
Register(int derived_id)
Creates a new Register instance. Upon creation, the DerivedClass::createInstance() instance creation ...
Definition: UserPlugin.hpp:479
Class less_nocase implements a case insensitive compare functor.
Definition: StringUtil.hpp:480
static void registerFunction(const std::string &function_name, Signature *function)
Member function registerFunction registers the user function&#39;s name with the specified user function ...
Definition: UserPlugin.hpp:556
void registerIt(const NamePair &name_pair, void *func_ptr)
Member function registerIt registers a name pair with a void pointer.
Definition: UserPlugin.cpp:54
Register(const std::string &function_name, Signature *function)
Creates a new Register instance. Upon creation, the func_ptr() function is registered with the functi...
Definition: UserPlugin.hpp:628
void * getPluginPtr(const NamePair &name_pair) const
Member function getPluginPtr find the function with the name pair specified.
Definition: UserPlugin.cpp:75
static T & create(const std::string &derived_name)
Definition: UserPlugin.hpp:283
static void registerCreator(const std::string &derived_name, Signature function)
Member function registerCreator registers the base class name and specified derived class name with t...
Definition: UserPlugin.hpp:368
Register(const std::string &derived_name)
Creates a new Register instance. Upon creation, the DerivedClass::createInstance() instance creation ...
Definition: UserPlugin.hpp:450
Class Writer implements a runtime selectable diagnostic output writer to aid in the development and d...
Definition: Writer.hpp:49
static UserSubroutine & instance()
Member function instance returns the instance of the registry, cast as a UserSubroutine registry...
Definition: UserPlugin.hpp:537
std::vector< std::string > getDerivedNames(const std::type_info &type) const
Member function getDerivedNames returns names assocaited with the function pointers of the specified ...
Definition: UserPlugin.cpp:135
Class hash_nocase implements a hash, case insensitive NamePair hash functor.
Definition: UserPlugin.hpp:96
void * getFuncPtr(const NamePair &name_pair) const
Member function getFuncPtr returns the function pointer with the specfied <it>name_pair.
Definition: UserPlugin.cpp:126
static Signature * getFunction(const std::string &function_name)
Member function execute returns the user function function pointer associated with the specified sign...
Definition: UserPlugin.hpp:592
Class template Register registers the user function function pointer with the function_name on object...
Definition: UserPlugin.hpp:617
static Signature * execute(const std::string &function_name)
Member function execute returns the user function function associated with the specified signature an...
Definition: UserPlugin.hpp:573
Register(int derived_id, Signature create_instance)
Creates a new Register instance. Upon creation, the DerivedClass::createInstance() instance creation ...
Definition: UserPlugin.hpp:493
Registry * getFactoryPtr(const NamePair &name) const
Member function getFuncPtr returns the function pointer with the specfied <it>name_pair.
Definition: UserPlugin.cpp:110
std::map< NamePair, void *, less_nocase > RegistryMap
Typedef RegistryMap is the registration map.
Definition: UserPlugin.hpp:115
static Registry & rootInstance()
Member function rootInstance creates the singleton.
Definition: UserPlugin.cpp:44
void * getFunctionPtr(const NamePair &name_pair) const
Member function getFunctionPtr find the function with the name pair specified.
Definition: UserPlugin.cpp:93