3dtof / voxelsdk

VoxelSDK - an SDK supporting TI's 3D Time of Flight cameras
BSD 3-Clause "New" or "Revised" License
107 stars 71 forks source link

frame register and callback mechanism lacking ability to pass user data #43

Closed larrylisky closed 8 years ago

larrylisky commented 8 years ago

The current frame callback mechanism does not permit user to pass user data through arguments. This forces use of globals/share data. To better support structured C++ programming, please provide a (void*) user pointer as an argument in the call back function, and allow the callback registration to pass the user pointer.

hlprasu commented 8 years ago

It is certainly possible though not in the way of C style "void *" user data pointer.

The current callback mechanism uses C++ function model and not traditional C style function pointer. So the following are possible -

  1. You can use std::bind() to fix one of the arguments to desired value. Thus, the original callback function can have more arguments than those required for registerCallback().
  2. Use lambda functions with [&] syntax to refer to local variables within the function where the lambda callback has been defined. See SDK/Test/CameraSystemTest.cpp for example in which "lastTimeStamp" is a local variable which is outside and being referred to inside the callback.
  3. Create a class with operator() with arguments as required by the callback. Variables which want to manipulate outside the scope of the callback can be class members.

There are more things possible. See example code given in http://en.cppreference.com/w/cpp/utility/functional/function

"void *" is a C style of doing this which is outdated and requires explicit casting which is not recommended in C++.

An example code for Voxel SDK using std::bind():

include

include

using namespace Voxel;

void callback(DepthCamera &camera, const Frame &frame, FrameType callBackType, UserDataType &userData) { // Do whatever is needed.... }

int main() { . . . UserDataType userData;

using namespace std::placeholders;

auto callback1 = std::bind(callback, _1, _2, _3, std::ref(userData));

depthCamera->registerCallback(..., callback1); . . . }

Hope this solves the problem of passing user data.