SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
Extension.h
Go to the documentation of this file.
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
10 #ifndef smtk_common_Extension_h
11 #define smtk_common_Extension_h
12 
14 #include "smtk/CoreExports.h"
15 #include "smtk/SharedFromThis.h"
16 #include "smtk/common/CompilerInformation.h"
17 
18 #ifdef SMTK_MSVC
19 // Ignore symbol exposure warnings for STL classes.
20 #pragma warning(disable : 4251)
21 #endif
22 
23 #include <functional>
24 
55 #define smtkDeclareExtension(exportmacro, name, cls) \
56  void exportmacro smtk_##name##_extension_AutoInit_Construct() \
57  { \
58  smtk::common::Extension::registerExtension( \
59  #name, \
60  []() { return std::dynamic_pointer_cast<smtk::common::Extension>(cls::create()); }, \
61  /* Never register class-static extension constructor as one-shot */ false); \
62  } \
63  void exportmacro smtk_##name##_extension_AutoInit_Destruct() \
64  { \
65  smtk::common::Extension::unregisterExtension(#name); \
66  }
67 
68 namespace smtk
69 {
70 namespace common
71 {
72 
94 class SMTKCORE_EXPORT Extension : smtkEnableSharedPtr(Extension)
95 {
96 public:
98  virtual ~Extension();
99 
110  static bool registerExtension(
111  const std::string& name,
112  std::function<Extension::Ptr(void)> ctor,
113  bool oneShot = true);
114 
116  static bool unregisterExtension(const std::string& name);
117 
126  static void visitAll(
127  std::function<std::pair<bool, bool>(const std::string&, Extension::Ptr)> visitor);
128 
133  template<typename T>
134  static void visit(std::function<std::pair<bool, bool>(const std::string&, T)> visitor)
135  {
136  T result;
137  Extension::visitAll([&result, visitor](const std::string& name, Extension::Ptr entry) {
138  result = smtk::dynamic_pointer_cast<typename T::element_type>(entry);
139  if (!result)
140  {
141  return std::make_pair(false, false);
142  }
143  return visitor(name, result);
144  });
145  }
146 
148  static Extension::Ptr find(const std::string& name, bool removeOneShot = true);
149 
151  template<typename T>
152  static typename T::Ptr findAs(const std::string& name)
153  {
154  typename T::Ptr result = std::dynamic_pointer_cast<T>(Extension::find(name, false));
155  return result;
156  }
157 
158 protected:
159  Extension();
160 };
161 } // namespace common
162 } // namespace smtk
163 
164 #endif
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::common::Extension::find
static Extension::Ptr find(const std::string &name, bool removeOneShot=true)
Find an extension given a specific name.
Definition: Extension.cxx:85
smtkEnableSharedPtr
#define smtkEnableSharedPtr(...)
An abbreviation for enabling shared pointers.
Definition: SharedFromThis.h:154
smtk::common::Extension::visit
static void visit(std::function< std::pair< bool, bool >(const std::string &, T)> visitor)
Iterate over all the extensions that are subclasses of the given type.
Definition: Extension.h:134
smtk::common::Extension::visitAll
static void visitAll(std::function< std::pair< bool, bool >(const std::string &, Extension::Ptr)> visitor)
Call the given function on each registered extension.
Definition: Extension.cxx:61
smtk::common::Extension::findAs
static T::Ptr findAs(const std::string &name)
Find the first extension with a given name and type.
Definition: Extension.h:152
SharedFromThis.h
Macros for dealing with shared-pointer classes.
smtkTypeMacroBase
#define smtkTypeMacroBase(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:55
smtk::common::Extension
Allow extension of operator functionality in separate libraries.
Definition: Extension.h:94