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

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

unix.superglobalmegacorp.com

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