📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| 161行目: | 161行目: | ||
<u>まず、D-Busヘルパー実行ファイルを作成する。</u><br> | <u>まず、D-Busヘルパー実行ファイルを作成する。</u><br> | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// 例: | // 例: MochiuHelper.hファイル | ||
#ifndef | #ifndef MOCHIUHELPER_H | ||
#define | #define MOCHIUHELPER_H | ||
#include <sdbus-c++/sdbus-c++.h> | #include <sdbus-c++/sdbus-c++.h> | ||
#include <iostream> | #include <iostream> | ||
#include "MochiuAdapter.h" // 生成されたアダプタヘッダをインクルード | |||
#include " | // アダプタ名を明示的に指定しない場合は、以下に示すようなクラス継承の記述でも可能 | ||
// class MochiuHelper : public org::example::mochiu::method_adaptor | |||
class MochiuHelper : public MochiuAdaptor | class MochiuHelper : public MochiuAdaptor | ||
{ | { | ||
public: | public: | ||
MochiuHelper(sdbus::IConnection &connection, | MochiuHelper(sdbus::IConnection& connection, std::string objectPath) : MochiuAdaptor(connection, std::move(objectPath)) | ||
{} | { | ||
} | |||
// func1メソッドの実装 | // func1メソッドの実装 | ||
int func1(const | int func1(const int32_t& arg1, const std::string& arg2) override | ||
{ | { | ||
std::cout << "func1 called with | 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; | |||
} | |||
int main() | |||
{ | { | ||
try { | |||
// システムバスを接続する場合 | |||
auto connection = sdbus::createSessionBusConnection(); | |||
// セッションバスを接続する場合 | |||
//auto connection = sdbus::createSessionBusConnection(); | |||
// D-Busサービス名を指定 | |||
connection->requestName("org.example.mochiu"); | |||
{ | // D-Busオブジェクトの作成 | ||
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 // | 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 \ | ||
MochiuAdaptor | |||
<br> | <br> | ||
<u>最後に、生成されたソースコードを上記のヘルパー実行ファイルのソースコードに統合する。</u> | <u>最後に、生成されたソースコードを上記のヘルパー実行ファイルのソースコードに統合する。</u> | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// 例: | // 例: MochiuHelper.hファイル | ||
#include " | #include "MochiuAdapter.h" // 生成されたアダプタヘッダをインクルード | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br> | <br> | ||