swift
command.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #ifndef SWIFT_SIM_XSWIFTBUS_COMMAND_H
5 #define SWIFT_SIM_XSWIFTBUS_COMMAND_H
6 
8 
9 #include <XPLMUtilities.h>
10 
11 #include <functional>
12 
13 namespace XSwiftBus
14 {
15 
19  class CCommand
20  {
21  public:
23  CCommand(const char *name, const char *description, std::function<void()> handler)
24  : m_handler(handler), m_command(XPLMCreateCommand(name, description))
25  {
26  XPLMRegisterCommandHandler(m_command, callback, false, static_cast<void *>(this));
27  }
28 
30  ~CCommand() { XPLMUnregisterCommandHandler(m_command, callback, false, static_cast<void *>(this)); }
31 
34  CCommand(const CCommand &) = delete;
35  CCommand &operator=(const CCommand &) = delete;
37 
38  private:
39  static int callback(XPLMCommandRef, XPLMCommandPhase phase, void *refcon)
40  {
41  if (phase == xplm_CommandBegin) { (static_cast<CCommand *>(refcon)->m_handler)(); }
42  return 1;
43  }
44 
45  std::function<void()> m_handler;
46  XPLMCommandRef m_command;
47  };
48 
49 } // namespace XSwiftBus
50 
51 #endif // SWIFT_SIM_XSWIFTBUS_COMMAND_H
Class-based interface to X-Plane's custom command API.
Definition: command.h:20
CCommand & operator=(const CCommand &)=delete
Not copyable.
CCommand(const CCommand &)=delete
Not copyable.
~CCommand()
Destructor.
Definition: command.h:30
CCommand(const char *name, const char *description, std::function< void()> handler)
Constructor.
Definition: command.h:23
Plugin loaded by X-Plane which publishes a DBus service.
Definition: command.h:14