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

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(func);
                     41:        assert(0 <= id_ && id_ < ID_SUBWIN_MAX);
                     42: 
                     43:        id = id_;
1.1.1.2 ! root       44:        assert(gMonitorManager);
        !            45:        gMonitorManager->Regist(this);
1.1       root       46: }
                     47: 
                     48: // このモニタが登録されていれば削除する
                     49: void
                     50: Monitor::Unregist()
                     51: {
1.1.1.2 ! root       52:        assert(gMonitorManager);
        !            53:        gMonitorManager->Unregist(this);
1.1       root       54: }
                     55: 
                     56: 
                     57: //
                     58: // モニターマネージャ
                     59: //
                     60: 
1.1.1.2 ! root       61: // グローバル参照用
        !            62: MonitorManager *gMonitorManager;
        !            63: 
        !            64: // コンストラクタ
        !            65: MonitorManager::MonitorManager()
        !            66: {
        !            67: }
        !            68: 
        !            69: // デストラクタ
        !            70: MonitorManager::~MonitorManager()
        !            71: {
        !            72:        gMonitorManager = NULL;
        !            73: }
        !            74: 
1.1       root       75: // 登録
                     76: void
                     77: MonitorManager::Regist(Monitor *mon)
                     78: {
                     79:        assert(fixed == false);
                     80: 
                     81:        // 二重登録防止
1.1.1.2 ! root       82:        assert(Find(mon->GetId()) == NULL);
1.1       root       83: 
                     84:        // 登録
                     85:        list.emplace_back(mon);
                     86: 
                     87:        // 名前が衝突していないことを確認。
                     88:        // どのケースの衝突も検出できるよう、追加後に全部列挙して..
                     89:        std::vector<std::string> names;
                     90:        for (const auto& m : list) {
                     91:                const std::vector<std::string>& aliases = GetAliases(m->GetId());
                     92:                names.insert(names.end(), aliases.begin(), aliases.end());
                     93:        }
                     94:        if (names.size() > 1) {
                     95:                // ソートして..
                     96:                std::sort(names.begin(), names.end());
                     97:                // 同じものが並んでいないか。
                     98:                for (int i = 0, sz = names.size() - 1; i < sz; i++) {
                     99:                        assertmsg(names[i] != names[i + 1],
                    100:                                "monitor alias \"%s\" duplicated", names[i].c_str());
                    101:                }
                    102:        }
                    103: }
                    104: 
                    105: // mon がリストにあれば削除する。なければ何もしない。
                    106: void
                    107: MonitorManager::Unregist(Monitor *mon)
                    108: {
                    109:        // 最初に見付かった一つを削除するだけでいい
                    110:        for (auto it = list.begin(); it != list.end(); ++it) {
                    111:                if (*it == mon) {
                    112:                        list.erase(it);
                    113:                        break;
                    114:                }
                    115:        }
                    116: }
                    117: 
                    118: 
                    119: // これ以降は追加禁止
                    120: void
                    121: MonitorManager::Fix()
                    122: {
                    123:        assert(fixed == false);
                    124: 
                    125:        fixed = true;
                    126: }
                    127: 
                    128: // id リストから一覧表示用の文字列を生成して返す
                    129: /*static*/ std::string
                    130: MonitorManager::MakeListString(std::vector<int> list)
                    131: {
                    132:        std::string str;
                    133: 
                    134:        // id をモニター情報内の1つ目の名前順にソートする
                    135:        std::sort(list.begin(), list.end(),
                    136:                [](const auto& a_id, const auto& b_id) {
1.1.1.2 ! root      137:                        const auto& a_alias = gMonitorManager->GetAliases(a_id);
        !           138:                        const auto& b_alias = gMonitorManager->GetAliases(b_id);
1.1       root      139:                        const std::string a_name = a_alias[0];
                    140:                        const std::string b_name = b_alias[0];
                    141: 
                    142:                        return strcmp(a_name.c_str(), b_name.c_str()) < 0;
                    143:                }
                    144:        );
                    145: 
                    146:        for (const auto id : list) {
1.1.1.2 ! root      147:                const auto& aliases = gMonitorManager->GetAliases(id);
1.1       root      148:                assert(aliases.empty() == false);
                    149: 
                    150:                str += ' ';
                    151:                str += aliases[0];
                    152:                if (aliases.size() > 1) {
                    153:                        str += " (alias:";
                    154:                        for (int i = 1, sz = aliases.size(); i < sz; i++) {
                    155:                                str += ' ';
                    156:                                str += aliases[i];
                    157:                        }
                    158:                        str += ')';
                    159:                }
                    160:                str += '\n';
                    161:        }
                    162:        return str;
                    163: }

unix.superglobalmegacorp.com

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