AGXDynamics  2.35.0.2
StepEventCallback.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
20 
21 namespace agxUtil
22 {
44  {
45  public:
52  static void uninitialize( agxSDK::Simulation* simulation );
53 
60  template<typename FuncT, typename... Args>
61  static void preCollide( FuncT callback, agxSDK::Simulation* simulation, Args&&... args );
62 
69  template<typename FuncT, typename... Args>
70  static void pre( FuncT callback, agxSDK::Simulation* simulation, Args&&... args );
71 
78  template<typename FuncT, typename... Args>
79  static void post( FuncT callback, agxSDK::Simulation* simulation, Args&&... args );
80 
87  template<typename FuncT, typename... Args>
88  static void last( FuncT callback, agxSDK::Simulation* simulation, Args&&... args );
89 
90  protected:
94  virtual ~StepEventCallback();
95 
99  virtual void addNotification() override;
100 
104  virtual void removeNotification() override;
105 
106  virtual void preCollide( const agx::TimeStamp& t ) override;
107  virtual void pre( const agx::TimeStamp& t ) override;
108  virtual void post( const agx::TimeStamp& t ) override;
109  virtual void last( const agx::TimeStamp& t ) override;
110 
111  private:
112  enum CallbackType
113  {
114  PRE_COLLIDE_TYPE,
115  PRE_TYPE,
116  POST_TYPE,
117  LAST_TYPE,
118  NUM_CALLBACK_TYPES
119  };
120 
126  using Callback = std::function<void()>;
128 
131 
132  private:
139  static void registerCallback( Callback callback, CallbackType callbackType, agxSDK::Simulation* simulation );
140 
141  private:
147 
151  void executeCallbacks( const agx::TimeStamp& t, CallbackType callbackType );
152 
153  private:
154  static InstanceType s_instances;
155  Callbacks m_callbacks;
156  };
157 
158  template<typename FuncT, typename... Args>
159  void StepEventCallback::preCollide( FuncT callback, agxSDK::Simulation* simulation, Args&&... args )
160  {
161  if ( simulation == nullptr )
162  return;
163 
164  registerCallback( std::bind( callback, simulation, std::forward<Args>( args )... ), PRE_COLLIDE_TYPE, simulation );
165  }
166 
167  template<typename FuncT, typename... Args>
168  void StepEventCallback::pre( FuncT callback, agxSDK::Simulation* simulation, Args&&... args )
169  {
170  if ( simulation == nullptr )
171  return;
172 
173  registerCallback( std::bind( callback, simulation, std::forward<Args>( args )... ), PRE_TYPE, simulation );
174  }
175 
176  template<typename FuncT, typename... Args>
177  void StepEventCallback::post( FuncT callback, agxSDK::Simulation* simulation, Args&&... args )
178  {
179  if ( simulation == nullptr )
180  return;
181 
182  registerCallback( std::bind( callback, simulation, std::forward<Args>( args )... ), POST_TYPE, simulation );
183  }
184 
185  template<typename FuncT, typename... Args>
186  void StepEventCallback::last( FuncT callback, agxSDK::Simulation* simulation, Args&&... args )
187  {
188  if ( simulation == nullptr )
189  return;
190 
191  registerCallback( std::bind( callback, simulation, std::forward<Args>( args )... ), LAST_TYPE, simulation );
192  }
193 }
agxUtil::StepEventCallback::last
static void last(FuncT callback, agxSDK::Simulation *simulation, Args &&... args)
Register a last step event callback.
Definition: StepEventCallback.h:186
agxSDK::StepEventListener
Derive from this class to implement a listener for simulation step events.
Definition: StepEventListener.h:37
agx::Vector< agx::Vector< Callback > >
agxUtil::StepEventCallback::pre
static void pre(FuncT callback, agxSDK::Simulation *simulation, Args &&... args)
Register a pre step event callback.
Definition: StepEventCallback.h:168
agxUtil::StepEventCallback::post
static void post(FuncT callback, agxSDK::Simulation *simulation, Args &&... args)
Register a post step event callback.
Definition: StepEventCallback.h:177
StepEventListener.h
AGXPHYSICS_EXPORT
#define AGXPHYSICS_EXPORT
agx::ref_ptr
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
agxSDK::Simulation
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
agx::LinearProbingHashTable
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: LinearProbingHashTable.h:871
agxUtil::StepEventCallback::preCollide
static void preCollide(FuncT callback, agxSDK::Simulation *simulation, Args &&... args)
Register a pre-collide step event callback.
Definition: StepEventCallback.h:159
agx::TimeStamp
agx::Real TimeStamp
Definition: TimeStamp.h:26
agxUtil::StepEventCallback
Helper to use lambdas as preCollide, pre, post and last step events.
Definition: StepEventCallback.h:43
agxUtil
The agxUtil namespace contain classes and methods for utility functionality.
Definition: CylindricalJoint.h:22