// // The MIT License (MIT) // // Copyright (c) 2022 Livox. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. // #ifndef LIVOX_COMMAND_CALLBACK_H #define LIVOX_COMMAND_CALLBACK_H #include #include #include "livox_lidar_api.h" namespace livox { namespace lidar { class CommandCallback { public: virtual ~CommandCallback() {} virtual void operator()(livox_status status,uint32_t handle, void *data) = 0; }; template class MemberFunctionCallback : public CommandCallback { public: typedef void (T::*MemFn)(livox_status status, uint32_t handle, ResponseType *data); MemberFunctionCallback(T *cls, MemFn func) : this_(cls), func_(func) {} void operator()(livox_status status, uint32_t handle, void *data) { if (this_) { (this_->*func_)(status, handle, static_cast(data)); } } private: T *this_; MemFn func_; }; template class MemberFunctionCallback : public CommandCallback { public: typedef void (T::*MemFn)(livox_status status, uint32_t handle, uint8_t response); MemberFunctionCallback(T *cls, MemFn func) : this_(cls), func_(func) {} void operator()(livox_status status, uint32_t handle, void *data) { if (this_) { if (data == NULL) { (this_->*func_)(status, handle, 0); } else { (this_->*func_)(status, handle, static_cast(reinterpret_cast(data))); } } } private: T *this_; MemFn func_; }; template std::shared_ptr MakeCommandCallback(T *cls, typename MemberFunctionCallback::MemFn func) { std::shared_ptr cb(new MemberFunctionCallback(cls, func)); return cb; } template class FunctionStatusCallback : public CommandCallback { public: typedef void (*Fn)(livox_status status, uint32_t handle, T *data, void *client_data); public: FunctionStatusCallback(Fn func, void *client_data) : func_(func), client_data_(client_data) {} void operator()(livox_status status, uint32_t handle, void *data) { if (func_) { (*func_)(status, handle, static_cast(data), client_data_); } } private: Fn func_; void *client_data_; }; template <> class FunctionStatusCallback : public CommandCallback { public: typedef void (*Fn)(livox_status status, uint32_t handle, uint8_t data, void *client_data); public: FunctionStatusCallback(Fn func, void *client_data) : func_(func), client_data_(client_data) {} void operator()(livox_status status, uint32_t handle, void *data) { if (func_) { if (data == NULL) { (*func_)(status, handle, 0, client_data_); } else { (*func_)(status, handle, *(uint8_t *)data, client_data_); } } } private: Fn func_; void *client_data_; }; template std::shared_ptr MakeCommandCallback(typename FunctionStatusCallback::Fn func, void *client_data) { std::shared_ptr cb(new FunctionStatusCallback(func, client_data)); return cb; } template class MessageCallback : public CommandCallback { public: typedef void (*Fn)(livox_status status, uint32_t handle, T *data); public: MessageCallback(const Fn &func) : func_(func) {} void operator()(livox_status status, uint32_t handle, void *data) { if (func_) { (*func_)(status, handle, static_cast(data)); } } private: Fn func_; }; template class BoostFunctionMessageCallback : public CommandCallback { public: typedef std::function Fn; public: BoostFunctionMessageCallback(const Fn &func) : func_(func) {} void operator()(livox_status status, uint32_t handle, void *data) { if (func_) { func_(status, handle, static_cast(data)); } } private: Fn func_; }; template std::shared_ptr MakeMessageCallback(typename MessageCallback::Fn func) { std::shared_ptr cb(new MessageCallback(func)); return cb; } template std::shared_ptr MakeMessageCallback(typename BoostFunctionMessageCallback::Fn func) { std::shared_ptr cb(new BoostFunctionMessageCallback(func)); return cb; } template class MemberMessageCallback : public CommandCallback { public: typedef void (T::*MemFn)(livox_status status, uint32_t handle, ResponseType *data); MemberMessageCallback(T *cls, MemFn func) : this_(cls), func_(func) {} void operator()(livox_status status,uint32_t handle, void *data) { if (this_) { (this_->*func_)(status, handle, (ResponseType *)data); } } private: T *this_; MemFn func_; }; template std::shared_ptr MakeMemberMessageCallback( T *_this, typename MemberMessageCallback::MemFn func) { std::shared_ptr cb(new MemberMessageCallback(_this, func)); return cb; } template class BoostFunctionCallback : public CommandCallback { public: typedef std::function Fn; BoostFunctionCallback(const Fn &func) : func_(func) {} void operator()(livox_status status,uint32_t handle, void *data) { if (func_) { if (data == NULL) { func_(status, handle, NULL); } else { func_(status, handle, (ResponseType *)data); } } } private: Fn func_; }; template <> class BoostFunctionCallback : public CommandCallback { public: typedef std::function Fn; BoostFunctionCallback(const Fn &func) : func_(func) {} void operator()(livox_status status,uint32_t handle, void *data) { if (func_) { if (data == NULL) { func_(status, handle, 0); } else { func_(status, handle, static_cast(reinterpret_cast(data))); } } } private: Fn func_; }; template std::shared_ptr MakeCommandCallback(const typename BoostFunctionCallback::Fn &func) { std::shared_ptr cb(new BoostFunctionCallback(func)); return cb; } } // namespace lidar } // namespace livox #endif // LIVOX_COMMAND_CALLBACK_H