AGX Dynamics  2.35.0.1
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()>;
127  using StepEventCallbackRef = agx::ref_ptr<StepEventCallback>;
128 
130  using Callbacks = agx::Vector<agx::Vector<Callback>>;
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 }
#define AGXPHYSICS_EXPORT
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:127
Derive from this class to implement a listener for simulation step events.
Helper to use lambdas as preCollide, pre, post and last step events.
virtual void addNotification() override
Add notification callback.
static void pre(FuncT callback, agxSDK::Simulation *simulation, Args &&... args)
Register a pre step event callback.
virtual ~StepEventCallback()
Reference counted object, protected destructor.
virtual void last(const agx::TimeStamp &t) override
Called after a step is taken in the simulation Implement this method in the derived class to get call...
static void post(FuncT callback, agxSDK::Simulation *simulation, Args &&... args)
Register a post step event callback.
static void preCollide(FuncT callback, agxSDK::Simulation *simulation, Args &&... args)
Register a pre-collide step event callback.
virtual void pre(const agx::TimeStamp &t) override
Called before a step is taken in the simulation Implement this method in the derived class to get cal...
virtual void post(const agx::TimeStamp &t) override
Called after a step is taken in the simulation Implement this method in the derived class to get call...
virtual void removeNotification() override
Remove notification callback - will unregister this from the static instances.
static void last(FuncT callback, agxSDK::Simulation *simulation, Args &&... args)
Register a last step event callback.
virtual void preCollide(const agx::TimeStamp &t) override
Called before collision detection is performed in the simulation Implement this method in the derived...
static void uninitialize(agxSDK::Simulation *simulation)
Removes internal step event listener from the simulation (if created).
Inheritance with partial specialization due to bug with ref_ptr containers.
Templated vector class.
Definition: agx/Vector.h:53
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:30
The agxUtil namespace contain classes and methods for utility functionality.
agx::Real TimeStamp
Definition: TimeStamp.h:26