Annotation of nono/lib/monitor.cpp, revision 1.1.1.3

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2021 nono project
                      4: // Licensed under nono-license.txt
                      5: //
                      6: 
1.1.1.2   root        7: //
                      8: // モニタ管理
                      9: //
                     10: 
1.1       root       11: #include "monitor.h"
                     12: #include <algorithm>
                     13: 
                     14: //
                     15: // モニター
                     16: //
                     17: 
                     18: // コンストラクタ
                     19: Monitor::Monitor()
                     20: {
                     21: }
                     22: 
                     23: // コンストラクタ (obj 指定あり)
                     24: Monitor::Monitor(Object *obj_)
                     25: {
                     26:        obj = obj_;
                     27: }
                     28: 
                     29: // デストラクタ
                     30: Monitor::~Monitor()
                     31: {
                     32:        Unregist();
                     33: }
                     34: 
                     35: // このモニタを登録する
                     36: void
                     37: Monitor::Regist(int id_)
                     38: {
                     39:        assert(obj);
                     40:        assert(0 <= id_ && id_ < ID_SUBWIN_MAX);
                     41: 
1.1.1.3 ! root       42:        // ポカ避けのため assert を入れたいが Bt45x は後から関数を決める方式
        !            43:        //assert(func);
        !            44: 
1.1       root       45:        id = id_;
1.1.1.2   root       46:        assert(gMonitorManager);
                     47:        gMonitorManager->Regist(this);
1.1       root       48: }
                     49: 
                     50: // このモニタが登録されていれば削除する
                     51: void
                     52: Monitor::Unregist()
                     53: {
1.1.1.2   root       54:        assert(gMonitorManager);
                     55:        gMonitorManager->Unregist(this);
1.1       root       56: }
                     57: 
1.1.1.3 ! root       58: // ID を表示用文字列にして返す
        !            59: /*static*/ const char *
        !            60: Monitor::GetIdText(int target_id)
        !            61: {
        !            62:        for (const auto& p : idtext) {
        !            63:                int id = p.first;
        !            64:                const char *text = p.second;
        !            65: 
        !            66:                if (id == target_id) {
        !            67:                        return text;
        !            68:                }
        !            69:        }
        !            70:        return "unknownID?";
        !            71: }
        !            72: 
1.1       root       73: 
                     74: //
                     75: // モニターマネージャ
                     76: //
                     77: 
1.1.1.2   root       78: // グローバル参照用
                     79: MonitorManager *gMonitorManager;
                     80: 
                     81: // コンストラクタ
                     82: MonitorManager::MonitorManager()
                     83: {
                     84: }
                     85: 
                     86: // デストラクタ
                     87: MonitorManager::~MonitorManager()
                     88: {
                     89:        gMonitorManager = NULL;
                     90: }
                     91: 
1.1       root       92: // 登録
                     93: void
                     94: MonitorManager::Regist(Monitor *mon)
                     95: {
                     96:        assert(fixed == false);
                     97: 
                     98:        // 二重登録防止
1.1.1.2   root       99:        assert(Find(mon->GetId()) == NULL);
1.1       root      100: 
                    101:        // 登録
                    102:        list.emplace_back(mon);
                    103: 
                    104:        // 名前が衝突していないことを確認。
                    105:        // どのケースの衝突も検出できるよう、追加後に全部列挙して..
                    106:        std::vector<std::string> names;
                    107:        for (const auto& m : list) {
                    108:                const std::vector<std::string>& aliases = GetAliases(m->GetId());
                    109:                names.insert(names.end(), aliases.begin(), aliases.end());
                    110:        }
                    111:        if (names.size() > 1) {
                    112:                // ソートして..
                    113:                std::sort(names.begin(), names.end());
                    114:                // 同じものが並んでいないか。
                    115:                for (int i = 0, sz = names.size() - 1; i < sz; i++) {
                    116:                        assertmsg(names[i] != names[i + 1],
                    117:                                "monitor alias \"%s\" duplicated", names[i].c_str());
                    118:                }
                    119:        }
                    120: }
                    121: 
                    122: // mon がリストにあれば削除する。なければ何もしない。
                    123: void
                    124: MonitorManager::Unregist(Monitor *mon)
                    125: {
                    126:        // 最初に見付かった一つを削除するだけでいい
                    127:        for (auto it = list.begin(); it != list.end(); ++it) {
                    128:                if (*it == mon) {
                    129:                        list.erase(it);
                    130:                        break;
                    131:                }
                    132:        }
                    133: }
                    134: 
                    135: 
                    136: // これ以降は追加禁止
                    137: void
                    138: MonitorManager::Fix()
                    139: {
                    140:        assert(fixed == false);
                    141: 
                    142:        fixed = true;
                    143: }
                    144: 
                    145: // id リストから一覧表示用の文字列を生成して返す
                    146: /*static*/ std::string
                    147: MonitorManager::MakeListString(std::vector<int> list)
                    148: {
                    149:        std::string str;
                    150: 
                    151:        // id をモニター情報内の1つ目の名前順にソートする
                    152:        std::sort(list.begin(), list.end(),
                    153:                [](const auto& a_id, const auto& b_id) {
1.1.1.2   root      154:                        const auto& a_alias = gMonitorManager->GetAliases(a_id);
                    155:                        const auto& b_alias = gMonitorManager->GetAliases(b_id);
1.1       root      156:                        const std::string a_name = a_alias[0];
                    157:                        const std::string b_name = b_alias[0];
                    158: 
                    159:                        return strcmp(a_name.c_str(), b_name.c_str()) < 0;
                    160:                }
                    161:        );
                    162: 
                    163:        for (const auto id : list) {
1.1.1.2   root      164:                const auto& aliases = gMonitorManager->GetAliases(id);
1.1       root      165:                assert(aliases.empty() == false);
                    166: 
                    167:                str += ' ';
                    168:                str += aliases[0];
                    169:                if (aliases.size() > 1) {
                    170:                        str += " (alias:";
                    171:                        for (int i = 1, sz = aliases.size(); i < sz; i++) {
                    172:                                str += ' ';
                    173:                                str += aliases[i];
                    174:                        }
                    175:                        str += ')';
                    176:                }
                    177:                str += '\n';
                    178:        }
                    179:        return str;
                    180: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.