--- nono/lib/mainapp.cpp 2026/04/29 17:04:57 1.1.1.9 +++ nono/lib/mainapp.cpp 2026/04/29 17:05:15 1.1.1.14 @@ -4,16 +4,64 @@ // Licensed under nono-license.txt // +// +// CLI/GUI 共通のメイン部分 +// + #include "mainapp.h" +#include "config.h" +#include "hostnet.h" +#include "logger.h" +#include "monitor.h" #include "mystring.h" -#include "netdriver_selector.h" +#include "mythread.h" +#include "sram.h" +#include "vm_luna.h" +#include "vm_news.h" +#include "vm_x68k.h" +#include #include +#include #include #include +#if defined(__linux__) +#include +#endif // グローバルインスタンス MainApp gMainApp; +// コンストラクタ +MainApp::MainApp() +{ +} + +// デストラクタ +MainApp::~MainApp() +{ + // 逆順に解放 + + pVM.reset(); + + pConfig.reset(); + gConfig = NULL; + + pMonitorManager.reset(); + gMonitorManager = NULL; + +#if 0 // 確認用 + if (objects.empty() == false) { + printf("~MainApp: undead objects are:"); + for (const auto *obj : objects) { + printf(" %s", obj->GetIdStr()); + } + printf("\n"); + } +#endif + + logger.reset(); +} + // ヘルプメッセージ void MainApp::ShowHelp(bool all) const @@ -24,13 +72,13 @@ MainApp::ShowHelp(bool all) const printf("usage: %s []\n", getprogname()); p("-c vm directory or configuration file"); - p("-f fast mode"); + p("-f fast mode (same as '-V fast-mode=1')"); if (IsGUI()) - p("--fontsize fontsize in monitors {12,16} (default:12)"); + p("--fontsize fontsize (same as '-V monitor-fontsize=')"); p("-h show brief help message"); p("--help show all help message including for developers"); if (IsGUI()) - p("-s,--scale screen scale, default 1.0"); + p("-s mainview scale (same as '-V mainview-scale=')"); p("--show-config show configuration variables"); p("--show-hostnet show list of hostnet drivers"); p("-v show version"); @@ -39,14 +87,19 @@ MainApp::ShowHelp(bool all) const if (all) { printf("\n(options for developers)\n"); - p("-b [,] set breakpoint"); + p("-b [,][,]"); + p(" set breakpoint. is either 'main'(default) or 'xp'"); p("-B benchmark mode"); p("-C output log to console"); p("-d debugger prompt on startup"); - p("-D debugger on console"); + p("-D same as '-V debugger-driver=stdio'"); + p("-H human68k console emulation"); p("-L =[,..] set loglevel (-Lhelp displays names)"); + p("--load-only load host binary (a.out or ELF) but not execute"); p("-M [,..] monitors to display at startup (-Mhelp displays names)"); - p("-H human68k console emulation"); + p(" memdump[=[.]] fmt := B/W/L/M(MMU)/I(Disasm)/Z(XPDisasm)"); + p("-S MSX-DOS console emulation"); + p("--perf performance measure mode"); } } @@ -54,15 +107,22 @@ MainApp::ShowHelp(bool all) const // enum は getopt() の1文字のオプションと衝突しなければいいので適当に // 0x80 から始めておく。 enum { - OPT_fontsize = 0x80, + OPTstart = 0x80 - 1, + OPT_create_sram, + OPT_fontsize, + OPT_load_only, OPT_help, + OPT_perf, OPT_show_config, OPT_show_config_all, OPT_show_hostnet, }; static struct option longopts[] = { + { "create-sram", no_argument, NULL, OPT_create_sram }, { "fontsize", required_argument, NULL, OPT_fontsize }, + { "load-only", required_argument, NULL, OPT_load_only }, { "help", no_argument, NULL, OPT_help }, + { "perf", no_argument, NULL, OPT_perf }, { "show-config", no_argument, NULL, OPT_show_config }, // --show-config-all は開発用なのでヘルプには載せない { "show-config-all",no_argument, NULL, OPT_show_config_all }, @@ -79,120 +139,160 @@ static struct option longopts[] = { int MainApp::Init1(bool is_cli_, int ac, char *av[]) { + int rv; + is_cli = is_cli_; - if (!ParseOpt(ac, av)) { - return EXIT_FAILURE; +#if defined(__linux__) + // Linux ではケーパビリティが設定されていると coredump しないので + // 明示的に許可する必要がある + prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); +#endif + + rv = ParseOpt(ac, av); + if (rv != MainApp::PASS) { + return rv; } // ログ機構は引数処理後なるはや // CLI ならログは常に標準出力へ(も)出力。 // GUI なら -C で指定。 - gLogger.UseStdout(IsCLI() ? true : log_to_console); + logger.reset(new Logger()); + logger->UseStdout(IsCLI() ? true : log_to_console); - // 設定を作成。コンストラクタで初期値を用意 - gConfig.reset(new Config()); + // モニタマネージャ (VM 作成より前、もしかしたら Config より前?) + pMonitorManager.reset(new MonitorManager()); + gMonitorManager = pMonitorManager.get(); + + // 設定を作成。 + // c0. コンストラクタで初期値を用意。 + pConfig.reset(new Config()); + gConfig = pConfig.get(); + + // c1. ホームディレクトリに設定ファイルがあれば読み込んで設定を更新。 + struct passwd *passwd = getpwuid(getuid()); + // 見付からないことはないはずだが、一応。 + if (passwd != NULL) { + std::string filename = std::string(passwd->pw_dir) + "/.nono.cfg"; + ConfigFile dotfile(filename); + if (dotfile.Load()) { + if (gConfig->Update(dotfile) == false) { + return EXIT_FAILURE; + } + } + } - // 引数で指定されたディレクトリの設定ファイルがあれば読み込んで設定を更新 - ConfigFile file(vmfile); - if (file.Load() == false) { - return EXIT_FAILURE; + // c2. VM ディレクトリに設定ファイルがあれば読み込んで設定を更新。 + // vmfile が存在するかどうかはここで調べるまで分からない。 + ConfigFile cfgfile(vmfile); + if (cfgfile.Load()) { + if (gConfig->Update(cfgfile) == false) { + return EXIT_FAILURE; + } } - if (gConfig->Update(file) == false) { - return EXIT_FAILURE; + + // c3. 最後にコマンドライン引数で更新 + for (const auto& pair : config_options) { + if (gConfig->Update(pair.first, pair.second) == false) { + return EXIT_FAILURE; + } } - // 最後にコマンドライン引数で更新 - if (gConfig->Update(config_options) == false) { - return EXIT_FAILURE; + + // MSX-DOS モードならここでいくつかパラメータを強制的に変更。 + if (msxdos_mode) { + // LUNA-I 最小構成でいい + gConfig->Update("vmtype=luna", "-S"); + gConfig->Update("ram-size=16", "-S"); + gConfig->Update("luna-video-plane=1", "-S"); + // ホスト側には干渉しない。 + gConfig->Update("hostcom-driver=none", "-S"); + gConfig->Update("hostnet-driver=none", "-S"); } - // 1. VM 種別を決定 + // 0. VM 種別を決定 // VM 種別文字列から vmtype を決定 const ConfigItem& item = gConfig->Find("vmtype"); - std::string vmstr = string_tolower(item.AsString()); + vmstr = string_tolower(item.AsString()); if (vmstr == "x68030") { - vmtype = VMTYPE_X68030; + vmtype = VMType::X68030; } else if (vmstr == "luna") { - vmtype = VMTYPE_LUNA1; + vmtype = VMType::LUNA1; } else if (vmstr == "luna88k") { - vmtype = VMTYPE_LUNA88K; + vmtype = VMType::LUNA88K; + } else if (vmstr == "news") { + vmtype = VMType::NEWS; } else { if (item.GetFrom() == ConfigItem::FromInitial) { // 未指定の時 warnx("vmtype must be specified"); } else { // ユーザ由来の時 - item.Err("invalid vmtype"); + item.Err("Invalid vmtype"); } return EXIT_FAILURE; } - // 2. VM 作成 + // 0.5. VM が確定したところで SRAM 作成。 + if (create_sram) { + if (vmtype == VMType::X68030) { + return CreateSRAM(); + } else { + warnx("--create-sram is only for X68030 mode"); + return EXIT_FAILURE; + } + } + + // 1. VM 作成 switch (vmtype) { - case VMTYPE_X68030: - gVM.reset(new VM_X68030()); + case VMType::X68030: + pVM.reset(new VM_X68030()); + break; + + case VMType::LUNA1: + pVM.reset(new VM_LUNA1()); break; - case VMTYPE_LUNA1: - gVM.reset(new VM_LUNA1()); + case VMType::LUNA88K: + pVM.reset(new VM_LUNA88K()); break; - case VMTYPE_LUNA88K: - gVM.reset(new VM_LUNA88K()); + case VMType::NEWS: + pVM.reset(new VM_NEWS()); break; default: - __unreachable(); + PANIC("corrupted vmtype=%s", vmstr.c_str()); } + gVM = pVM.get(); - // 3. 動的なコンストラクション + // 2. 動的なコンストラクション if (!gVM->Create()) { - gVM.reset(); + pVM.reset(); return EXIT_FAILURE; } - // デバッガ (ここでもモニタを登録する) - debugger_create(); + // 3. ログの処理 // 設定内容を表示して終了 // (VM コンストラクタで変数の増減があるのでそれより後、 // Create() でも SCSI パラメータを減らすので、それより後) - gConfig->Fix(); + if (gConfig->Fix() == false) { + pVM.reset(); + return EXIT_SUCCESS; + } if (show_config) { gConfig->Show(show_config - 1); - gVM.reset(); + pVM.reset(); return EXIT_SUCCESS; } - // 4. ログレベルを設定。コンストラクト後すぐに行う。 + // ログレベルを設定。コンストラクト後すぐに行う。 // -L help もここで処理。 if (!ParseLogopt()) { - gVM.reset(); + pVM.reset(); return EXIT_FAILURE; } - // 5. ホストネットワークドライバの作成。 - // - // ホストネットワークドライバを作成する時にはログ表示が欲しいが、通常の - // フローではオブジェクトが出揃った後でログレベルを割り当てるので、 - // にわたま問題になってしまう。そこでこれだけ特別対応する。 - // 手順 3. のコンストラクションで EthernetDevice が NetDriverSelector を - // 作成し、手順 4. で通常通りにログレベルの割り当てを受ける。このログを - // 使ってここ(手順5)で実際の NetDriver を作成し、同時にログレベルも引き - // 継ぐことにする。 - if ((bool)gNetDriverSelector) { - // 実際の NetDriver を作成 - NetDriver *newdriver = gNetDriverSelector->InitDriver(); - if (newdriver == NULL) { - gVM.reset(); - return EXIT_FAILURE; - } - // 成功すればセット - gNetDriver.reset(newdriver); - - // NetDriverSelector はこれ以降まったく使わないがとりあえず放置。 - } - return PASS; } @@ -203,19 +303,21 @@ MainApp::Init1(bool is_cli_, int ac, cha bool MainApp::Init2() { - // VM 初期化。 - // これ以降はスレッドを開始したかもしれないので false を返す際には - // VM をデストラクトすること。 + // 5. VM 初期化。 if (!gVM->Init()) { - gVM.reset(); return false; } - // デバッガは VM オブジェクトリストに入っていない - debugger_init(); - // 起動時設定の適用 + // メインスレッド名を設定 + PTHREAD_SETNAME("Main"); + + // 6. スレッド開始 + if (!gVM->StartThread()) { + return false; + } + + // 7. 起動時設定の適用 if (!gVM->Apply()) { - gVM.reset(); return false; } @@ -224,7 +326,9 @@ MainApp::Init2() // コマンドライン引数を処理する。 // 知らない引数とかがあればこちらで usage を表示して false を返す。 -bool +// 戻り値は、MainApp::PASS なら実行を継続、 +// EXIT_SUCCESS/EXIT_FAILURE ならこのコードで終了。 +int MainApp::ParseOpt(int ac, char *av[]) { struct stat st; @@ -232,11 +336,9 @@ MainApp::ParseOpt(int ac, char *av[]) int b; int c; - fontsize = 12; - screen_scale = 1.0; cpath = "."; - while ((c = getopt_long(ac, av, "b:B:c:CdDfhHL:M:s:vV:X:", + while ((c = getopt_long(ac, av, "b:B:c:CdDfhHL:M:s:SvV:X:", longopts, NULL)) != -1) { switch (c) { case 'b': @@ -247,7 +349,7 @@ MainApp::ParseOpt(int ac, char *av[]) b = atoi(optarg); if (b < 2 || b > 6) { fprintf(stderr, "-B : 2..6\n"); - return false; + return EXIT_FAILURE; } benchmark_mode = b; break; @@ -269,11 +371,13 @@ MainApp::ParseOpt(int ac, char *av[]) break; case 'D': - debug_on_console = true; + // -D は -V debugger-driver=stdio と等価。 + config_options.emplace_back("debugger-driver=stdio", "-D"); break; case 'f': - fast_mode = true; + // -f は -V fast-mode=1 と等価。 + config_options.emplace_back("fast-mode=1", "-f"); break; case 'H': @@ -281,10 +385,7 @@ MainApp::ParseOpt(int ac, char *av[]) break; case 'L': - if (logopt[0] != '\0') { - strlcat(logopt, ",", sizeof(logopt)); - } - strlcat(logopt, optarg, sizeof(logopt)); + AddLogopt(optarg); break; case 'M': @@ -295,23 +396,20 @@ MainApp::ParseOpt(int ac, char *av[]) break; case 's': - if (IsGUI()) { - char *end; - errno = 0; - screen_scale = strtod(optarg, &end); - if (end == optarg || end[0] != '\0' || errno == ERANGE) { - warnx("-s: invalid argument"); - return false; - } - // 上限は適当 - if (screen_scale <= 0.0 || screen_scale >= 10.0) { - warnx("-s: invalid scale"); - return false; - } - } + { + auto line = string_format("mainview-scale=%s", optarg); + config_options.emplace_back(line, "-s"); + break; + } + + case 'S': + msxdos_mode = true; break; case 'X': + load_and_exec = true; + FALLTHROUGH; + case OPT_load_only: exec_file = optarg; for (int i = optind; i < ac; i++) { if (i != optind) { @@ -327,16 +425,46 @@ MainApp::ParseOpt(int ac, char *av[]) exit(0); case 'V': - config_options.push_back(optarg); + config_options.emplace_back(optarg, "-V"); + break; + + case OPT_create_sram: + create_sram = true; break; case OPT_fontsize: - fontsize = atoi(optarg); + { + auto line = string_format("monitor-fontsize=%s", optarg); + config_options.emplace_back(line, "--fontsize"); break; + } case OPT_help: ShowHelp(true); - return false; + return EXIT_SUCCESS; + + case OPT_perf: + // パフォーマンス確認用。 + // -Vprom-image=PROM.DAT はパスの問題があるので各自で追加指定 + // する必要がある。 + config_options.emplace_back(".rtc-force-fixed=1", "--perf"); + config_options.emplace_back("clock-sync=virtual", "--perf"); + config_options.emplace_back("ethernet-macaddr=02:00:00:00:00:01", + "--perf"); + config_options.emplace_back("hostcom-driver=none", "--perf"); + config_options.emplace_back("hostnet-driver=none", "--perf"); + config_options.emplace_back("luna-dipsw1=11110111", "--perf"); + config_options.emplace_back("spc0-id0-writeignore=1", "--perf"); + config_options.emplace_back("spc0-id1-writeignore=1", "--perf"); + config_options.emplace_back("spc0-id2-writeignore=1", "--perf"); + config_options.emplace_back("spc0-id3-writeignore=1", "--perf"); + config_options.emplace_back("spc0-id4-writeignore=1", "--perf"); + config_options.emplace_back("spc0-id5-writeignore=1", "--perf"); + config_options.emplace_back("spc0-id6-writeignore=1", "--perf"); + config_options.emplace_back("fast-mode=1", "--perf"); + // -C + log_to_console = true; + break; case OPT_show_config: show_config = 1; @@ -348,12 +476,15 @@ MainApp::ParseOpt(int ac, char *av[]) case OPT_show_hostnet: ShowHostnet(); - return false; + return EXIT_SUCCESS; case 'h': + ShowHelp(false); + return EXIT_SUCCESS; + default: ShowHelp(false); - return false; + return EXIT_FAILURE; } } @@ -365,7 +496,7 @@ MainApp::ParseOpt(int ac, char *av[]) // -c DIR/FILE => vmdir = DIR, vmfile = FILE if (stat(cpath, &st) < 0) { warn("stat %s", cpath); - return false; + return EXIT_FAILURE; } if (S_ISDIR(st.st_mode)) { vmdir = std::string(cpath); @@ -383,7 +514,7 @@ MainApp::ParseOpt(int ac, char *av[]) } } else { warnx("-c %s: path must be file or directory", cpath); - return false; + return EXIT_FAILURE; } // CLI 版で -Mhelp つけても黙って起動するのはさすがにどうかと思う。 @@ -393,23 +524,38 @@ MainApp::ParseOpt(int ac, char *av[]) if (IsCLI()) { if (monitor_opt == "help") { warnx("-Mhelp is not available on CLI"); - return false; + return EXIT_FAILURE; } if (!monitor_opt.empty()) { warnx("-M option is ignored on CLI"); - return true; + return EXIT_FAILURE; } } - // Human モードでは実行ファイル名が必要 + // Human モード、MSX-DOS モードでは実行ファイル名が必要 if (human_mode) { if (exec_file == NULL) { warnx("-H option needs -X"); - return false; + return EXIT_FAILURE; + } + } + if (msxdos_mode) { + if (exec_file == NULL) { + warnx("-S option needs -X"); + return EXIT_FAILURE; } } - return true; + // 実行ファイルは、ファイル名を間違えたくらいならここでエラーに出来る。 + if (exec_file) { + int r = access(exec_file, R_OK); + if (r != 0) { + warn("-X %s", exec_file); + return EXIT_FAILURE; + } + } + + return MainApp::PASS; } // バージョンを表示 @@ -421,6 +567,16 @@ MainApp::ShowVersion() const NONO_MAJOR_VER, NONO_MINOR_VER, NONO_PATCH_VER, NONO_DATE); } +// ログレベル指定文字列を logopt に追加する。 +void +MainApp::AddLogopt(const char *opt) +{ + if (logopt.empty() == false) { + logopt += ','; + } + logopt += opt; +} + // ログレベル指定文字列を処理する。 // str はログレベル指定文字列を ',' で連結した "foo=1,bar=2" 形式の文字列で、 // これを分解してそれぞれ担当するオブジェクトのログレベルにセットする。 @@ -431,7 +587,7 @@ MainApp::ParseLogopt() std::vector items; // 分解して.. - items = string_split(logopt, ','); + items = string_split(logopt.c_str(), ','); // "help" があればヘルプを表示して終了 for (const auto& item : items) { @@ -470,7 +626,7 @@ MainApp::SetLogopt(const std::vectorGetName().c_str(), o->loglevel); } } @@ -508,37 +664,78 @@ MainApp::SetLogopt1(const std::string& i return false; } + // 今の所、値域は -1 〜 9 ということにしておく。 + if (level < -1) { + level = -1; + } else if (level > 9) { + level = 9; + } + // "all" なら全部にセット if (name == "all") { - for (auto obj : gObjects) { + for (auto obj : gMainApp.GetObjects()) { if (obj->GetName().empty() == false) { - obj->loglevel = level; + obj->SetLogLevel(level); + } + } + return true; + } + // "fdd" なら "fdd*" 全部にする + if (name == "fdd") { + for (auto obj : gMainApp.GetObjects()) { + if (strncasecmp(obj->GetName().c_str(), "fdd", 3) == 0) { + obj->SetLogLevel(level); } } return true; } - std::vector found; // 候補オブジェクト - std::vector candidates; // 候補名 - - // 前方一致する名前(とそのオブジェクト)を列挙する - for (auto *obj : gObjects) { + // エイリアスリストを作る + using aliaslist_t = std::vector>; + aliaslist_t alias_list; + for (const auto& obj : gMainApp.GetObjects()) { const std::vector& aliases = obj->GetAliases(); for (const auto& a : aliases) { - // 完全一致したら即採用 - // (前方一致しつつ完全一致したらこの時点で採用して打ち切るため) - if (strcasecmp(a.c_str(), name.c_str()) == 0) { - obj->loglevel = level; - return true; - } + alias_list.emplace_back(a, obj); + } + } - // 前方一致したら覚えておく - if (strncasecmp(a.c_str(), name.c_str(), name.size()) == 0) { - candidates.emplace_back(a); - found.push_back(obj); + // エイリアスを完全一致のみのものと部分一致も許容するものに分ける + aliaslist_t exact_alias; + aliaslist_t partial_alias; + for (const auto& a0 : alias_list) { + bool exact = false; + for (const auto& a1 : alias_list) { + if (a0.first == a1.first) { + continue; + } + if (starts_with_ignorecase(a0.first, a1.first)) { + exact = true; + break; } } + if (exact) { + exact_alias.push_back(a0); + } else { + partial_alias.push_back(a0); + } + } + + // 完全一致をまず調べる + for (const auto& a : exact_alias) { + if (strcasecmp(a.first.c_str(), name.c_str()) == 0) { + a.second->SetLogLevel(level); + return true; + } + } + + aliaslist_t found; + for (const auto& a : partial_alias) { + // 前方一致したら覚えておく + if (starts_with_ignorecase(a.first, name)) { + found.push_back(a); + } } // 見付からない場合はエラー @@ -549,16 +746,15 @@ MainApp::SetLogopt1(const std::string& i // 1つだけなら確定 if (found.size() == 1) { - Object *obj = found[0]; - obj->loglevel = level; + found[0].second->SetLogLevel(level); return true; } // 複数あれば候補文字列を作成 *errmsg = string_format("Ambiguous logname \"%s\": candidates are", name.c_str()); - for (const auto& cand : candidates) { - *errmsg += string_format(" \"%s\"", cand.c_str()); + for (const auto& cand : found) { + *errmsg += string_format(" \"%s\"", cand.first.c_str()); } return false; } @@ -571,7 +767,7 @@ MainApp::GetLogNames() std::vector sortobj; // エイリアスを持つオブジェクトだけ抜き出す - for (const auto& obj : gObjects) { + for (const auto& obj : gMainApp.GetObjects()) { if (obj->GetAliases().empty() == false) { sortobj.emplace_back(obj); } @@ -579,10 +775,10 @@ MainApp::GetLogNames() // aliases の一語目で sortobj をソートする std::sort(sortobj.begin(), sortobj.end(), - [](const auto& a, const auto *b) { + [](const auto a, const auto b) { const auto& sa = a->GetAliases()[0]; const auto& sb = b->GetAliases()[0]; - return strcmp(sa.c_str(), sb.c_str()) < 0; + return sa < sb; } ); @@ -591,7 +787,7 @@ MainApp::GetLogNames() const auto& aliases = obj->GetAliases(); std::string str; - str += aliases[0]; + str = aliases[0]; if (aliases.size() > 1) { str += " (alias:"; for (int i = 1, sz = aliases.size(); i < sz; i++) { @@ -657,20 +853,119 @@ MainApp::SearchFile(const std::string& n return path; } -// 現在の VM が指定の feature を持っているか? +// 初回起動時用に SRAM.DAT を作成する。 +// 戻り値は EXIT_SUCCESS / EXIT_FAILURE。 +int +MainApp::CreateSRAM() +{ + char buf[16 * 1024]; + std::string filename; + autofd fd; + int r; + + filename = GetVMDir() + "SRAM.DAT"; + + // 存在確認のため一旦読み込み専用で開いてみる。 + // オープン出来たら何もしない。 + fd = open(filename.c_str(), O_RDONLY); + if (fd >= 0) { + warnx("%s: Already exists", filename.c_str()); + return EXIT_FAILURE; + } + + fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) { + warn("%s: open failed", filename.c_str()); + return EXIT_FAILURE; + } + + memset(&buf[0], 0, sizeof(buf)); + for (int i = 0; i < SRAMDevice::InitialData.size(); i++) { + buf[i] = SRAMDevice::InitialData[i]; + } + + r = write(fd, buf, sizeof(buf)); + if (r < 0) { + warn("%s: write failed", filename.c_str()); + return EXIT_FAILURE; + } + + fd.Close(); + warnx("created %s", filename.c_str()); + return EXIT_SUCCESS; +} + +// 現在の VM が指定のケーパビリティを持っているか? bool -MainApp::HasVMFeature(vmf_t feature) const +MainApp::Has(VMCap cap) const { - uint32 vmf = 1 << GetVMType(); - return (vmf & feature) != 0; + uint32 vmcap = 1U << (int)GetVMType(); + return (vmcap & (uint32)cap) != 0; } // コンパイルされている host netdriver の一覧を表示。 void MainApp::ShowHostnet() const { - auto list = NetDriverSelector::GetDrivers(); + auto list = HostNetDevice::GetDrivers(); for (const auto& name : list) { printf(" %s\n", name.c_str()); } } + +// オブジェクト登録 +void +MainApp::RegistObject(Object *obj) +{ + // ID が重複していないかチェック (NONE なら重複可) + auto id = obj->GetId(); + if (id != OBJ_NONE) { + if (FindObject(id)) { + PANIC("%s already exists", Object::GetIdStr(id)); + } + } + + obj->logger = gMainApp.GetLogger(); + + objects.push_back(obj); +} + +// オブジェクト削除 +void +MainApp::UnregistObject(Object *obj) +{ + // 最初に見付かった一つを削除するだけでいい + for (auto it = objects.begin(); it != objects.end(); ++it) { + if (*it == obj) { + objects.erase(it); + break; + } + } +} + +// 指定された id を持つオブジェクトを返す。なければ NULL を返す。 +Object * +MainApp::FindObject(int id) const +{ + // NONE はここで検索しない (SCSI デバイスなど、そっちで検索する) + if (id == OBJ_NONE) { + return NULL; + } + for (auto obj : objects) { + if (obj->GetId() == id) { + return obj; + } + } + return NULL; +} + +// 指定された id を持つオブジェクトを探す。なければ assert する。 +Object * +MainApp::GetObject(int id) const +{ + Object *obj = FindObject(id); + if (__predict_false(obj == NULL)) { + PANIC("objid=%s not found", Object::GetIdStr(id)); + } + return obj; +}