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

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;
1.1.1.4 ! root      143: 
        !           144:        // デバッグ用。
        !           145:        if (0) {
        !           146:                int i = 0;
        !           147:                for (const auto& m : list) {
        !           148:                        auto sz = m->GetSize();
        !           149:                        printf("[%2d] %s\t%d,%d\n", i++, m->GetIdText(),
        !           150:                                sz.width, sz.height);
        !           151:                }
        !           152:        }
1.1       root      153: }
                    154: 
                    155: // id リストから一覧表示用の文字列を生成して返す
                    156: /*static*/ std::string
                    157: MonitorManager::MakeListString(std::vector<int> list)
                    158: {
                    159:        std::string str;
                    160: 
                    161:        // id をモニター情報内の1つ目の名前順にソートする
                    162:        std::sort(list.begin(), list.end(),
                    163:                [](const auto& a_id, const auto& b_id) {
1.1.1.2   root      164:                        const auto& a_alias = gMonitorManager->GetAliases(a_id);
                    165:                        const auto& b_alias = gMonitorManager->GetAliases(b_id);
1.1       root      166:                        const std::string a_name = a_alias[0];
                    167:                        const std::string b_name = b_alias[0];
                    168: 
                    169:                        return strcmp(a_name.c_str(), b_name.c_str()) < 0;
                    170:                }
                    171:        );
                    172: 
                    173:        for (const auto id : list) {
1.1.1.2   root      174:                const auto& aliases = gMonitorManager->GetAliases(id);
1.1       root      175:                assert(aliases.empty() == false);
                    176: 
                    177:                str += ' ';
                    178:                str += aliases[0];
                    179:                if (aliases.size() > 1) {
                    180:                        str += " (alias:";
                    181:                        for (int i = 1, sz = aliases.size(); i < sz; i++) {
                    182:                                str += ' ';
                    183:                                str += aliases[i];
                    184:                        }
                    185:                        str += ')';
                    186:                }
                    187:                str += '\n';
                    188:        }
                    189:        return str;
                    190: }

unix.superglobalmegacorp.com

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