From 3c95cde33cd59f99fcc90313220dfc539acdef7e Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Fri, 25 Sep 2015 00:48:51 +0100 Subject: [PATCH] refs #271 C++ API wrapper for the X-Plane Plugin SDK's C API for custom commands. --- src/xbus/command.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/xbus/command.h diff --git a/src/xbus/command.h b/src/xbus/command.h new file mode 100644 index 000000000..956d0ddf5 --- /dev/null +++ b/src/xbus/command.h @@ -0,0 +1,53 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#ifndef BLACKSIM_XBUS_COMMAND_H +#define BLACKSIM_XBUS_COMMAND_H + +//! \file + +#include +#include + +namespace XBus +{ + + /*! + * Class-based interface to X-Plane's custom command API. + */ + class CCommand + { + public: + //! Constructor. + CCommand(const char *name, const char *description, std::function handler) : m_handler(handler) + { + m_command = XPLMCreateCommand(name, description); + XPLMRegisterCommandHandler(m_command, callback, false, static_cast(this)); + } + + //! Destructor. + ~CCommand() + { + XPLMUnregisterCommandHandler(m_command, callback, false, static_cast(this)); + } + + private: + static int callback(XPLMCommandRef, XPLMCommandPhase phase, void *refcon) + { + if (phase == xplm_CommandBegin) { (static_cast(refcon)->m_handler)(); } + return 1; + } + + std::function m_handler; + XPLMCommandRef m_command; + }; + +} + +#endif