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