📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

161行目: 161行目:
<u>まず、D-Busヘルパー実行ファイルを作成する。</u><br>
<u>まず、D-Busヘルパー実行ファイルを作成する。</u><br>
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
  // 例: mochiu_helper.hファイル
  // 例: MochiuHelper.hファイル
   
   
  #ifndef MOCHIU_HELPER_H
  #ifndef MOCHIUHELPER_H
  #define MOCHIU_HELPER_H
  #define MOCHIUHELPER_H
   
   
  #include <sdbus-c++/sdbus-c++.h>
  #include <sdbus-c++/sdbus-c++.h>
  #include <iostream>
  #include <iostream>
#include <cstring>
  #include "MochiuAdapter.h"  // 生成されたアダプタヘッダをインクルード
#include <memory>
  #include "mochiu_adaptor.h"  // 生成されたプロキシヘッダをインクルード
// アダプタ名を明示的に指定しない場合は、以下に示すようなクラス継承の記述でも可能
 
// class MochiuHelper : public org::example::mochiu::method_adaptor
  class MochiuHelper : public MochiuAdaptor
  class MochiuHelper : public MochiuAdaptor
  {
  {
  public:
  public:
     MochiuHelper(sdbus::IConnection &connection, const std::string &objectPath) : MochiuAdaptor(connection, objectPath)
     MochiuHelper(sdbus::IConnection& connection, std::string objectPath) : MochiuAdaptor(connection, std::move(objectPath))
     {}
     {
    }
   
   
     // func1メソッドの実装
     // func1メソッドの実装
     int func1(const int &arg1, const std::string &arg2) override
     int func1(const int32_t& arg1, const std::string& arg2) override
     {
     {
       std::cout << "func1 called with arg1: " << arg1 << " and arg2: " << arg2 << std::endl;
       std::cout << "func1 called with args: " << arg1 << ", " << arg2 << std::endl;
       return arg1 * 2;
       return arg1 * 2; // 例: 入力値の2倍を返す
     }
     }
  };
  };
</syntaxhighlight>
<br>
<syntaxhighlight lang="c++">
// 例: MochiuHelper.cppファイル
#include <sdbus-c++/sdbus-c++.h>
#include <iostream>
#include <future>
#include <chrono>
#include "MochiuHelper.h"
// イベントループを実行し、60秒後に終了するための関数
void runEventLoopWithTimeout(std::shared_ptr<sdbus::IConnection> connection)
{
    // 60秒後に終了するための非同期タスクを作成
    auto future = std::async(std::launch::async, []{
      std::this_thread::sleep_for(std::chrono::seconds(60));
    });
    // イベントループを開始
    while (future.wait_for(std::chrono::seconds(0)) == std::future_status::timeout) {
      connection->processPendingRequest();
    }
    std::cout << "60 seconds timeout reached. Exiting..." << std::endl;
}
   
   
  class MochiuService
  int main()
  {
  {
private:
    try {
    std::unique_ptr<sdbus::IConnection> m_connection;
      // システムバスを接続する場合
    std::unique_ptr<MochiuHelper> m_helper;
      auto connection = sdbus::createSessionBusConnection();
      // セッションバスを接続する場合
      //auto connection = sdbus::createSessionBusConnection();
   
   
public:
      // D-Busサービス名を指定
    MochiuService() : m_connection(sdbus::createSessionBusConnection("org.example.mochiu")),
      connection->requestName("org.example.mochiu");
                      m_helper(std::make_unique<MochiuHelper>(*m_connection, "/org/example/mochiu"))
 
     {
      // D-Busオブジェクトの作成
        m_connection->enterEventLoop();
      MochiuHelper mochiuHelper(*connection, "/org/example/mochiu");
      // タイムアウト付きでイベントループを実行
      runEventLoopWithTimeout(connection);
     }
    catch (const sdbus::Error& e) {
      std::cerr << "D-Bus error: " << e.what() << std::endl;
      return -1;
    }
    catch (const std::exception& e) {
      std::cerr << "Error: " << e.what() << std::endl;
      return -1;
     }
     }
};
   
   
  #endif // MOCHIU_HELPER_H
    return 0;
}
  #endif // MOCHIUHELPER_H
  </syntaxhighlight>
  </syntaxhighlight>
<br>
<br>
213行目: 256行目:
  # 例 :
  # 例 :
  sdbus-c++-xml2cpp --adaptor-only org.example.mochiu.xml \
  sdbus-c++-xml2cpp --adaptor-only org.example.mochiu.xml \
                    mochiu_adaptor
                  MochiuAdaptor
<br>
<br>
<u>最後に、生成されたソースコードを上記のヘルパー実行ファイルのソースコードに統合する。</u>
<u>最後に、生成されたソースコードを上記のヘルパー実行ファイルのソースコードに統合する。</u>
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
  // 例: mochiu_header.hファイル
  // 例: MochiuHelper.hファイル
   
   
  #include "mochiu_adaptor.h"  // 生成されたプロキシヘッダをインクルード
  #include "MochiuAdapter.h"  // 生成されたアダプタヘッダをインクルード
  </syntaxhighlight>
  </syntaxhighlight>
<br>
<br>