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

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: 
1.1.1.6 ! root       23: // コンストラクタ
        !            24: Monitor::Monitor(uint id_, Object *obj_)
1.1       root       25: {
1.1.1.6 ! root       26:        id = id_;
1.1       root       27:        obj = obj_;
                     28: }
                     29: 
                     30: // デストラクタ
                     31: Monitor::~Monitor()
                     32: {
                     33: }
                     34: 
1.1.1.3   root       35: // ID を表示用文字列にして返す
                     36: /*static*/ const char *
1.1.1.5   root       37: Monitor::GetIdText(uint target_id)
1.1.1.3   root       38: {
                     39:        for (const auto& p : idtext) {
1.1.1.5   root       40:                uint id = p.first;
1.1.1.3   root       41:                const char *text = p.second;
                     42: 
                     43:                if (id == target_id) {
                     44:                        return text;
                     45:                }
                     46:        }
                     47:        return "unknownID?";
                     48: }
                     49: 
1.1       root       50: 
                     51: //
                     52: // モニターマネージャ
                     53: //
                     54: 
1.1.1.2   root       55: // グローバル参照用
                     56: MonitorManager *gMonitorManager;
                     57: 
                     58: // コンストラクタ
                     59: MonitorManager::MonitorManager()
                     60: {
                     61: }
                     62: 
                     63: // デストラクタ
                     64: MonitorManager::~MonitorManager()
                     65: {
                     66:        gMonitorManager = NULL;
                     67: }
                     68: 
1.1.1.6 ! root       69: // 登録(というか初期化というか確保というか)。
        !            70: // 成功すればモニターを返す。通常失敗しない。
        !            71: // 失敗すればいずれのケースも assert で終了する。
        !            72: Monitor *
        !            73: MonitorManager::Regist(uint id_, Object *parent_)
1.1       root       74: {
1.1.1.6 ! root       75:        assert(id_ < ID_SUBWIN_MAX);
1.1       root       76: 
1.1.1.6 ! root       77:        // 追加が遅すぎる。
        !            78:        assert(fixed == false);
1.1       root       79: 
1.1.1.6 ! root       80:        // 二重登録防止。
        !            81:        assertmsg((bool)monitors[id_] == false,
        !            82:                "%s already registered", Monitor::GetIdText(id_));
        !            83: 
        !            84:        // 生成。
        !            85:        monitors[id_].reset(new Monitor(id_, parent_));
        !            86:        assertmsg((bool)monitors[id_], "cannot create new monitor");
1.1       root       87: 
                     88:        // 名前が衝突していないことを確認。
                     89:        // どのケースの衝突も検出できるよう、追加後に全部列挙して..
                     90:        std::vector<std::string> names;
1.1.1.6 ! root       91:        for (const auto& uptr : monitors) {
        !            92:                if ((bool)uptr) {
        !            93:                        const Monitor *m = uptr.get();
        !            94:                        const std::vector<std::string>& aliases = GetAliases(m->GetId());
        !            95:                        names.insert(names.end(), aliases.begin(), aliases.end());
        !            96:                }
1.1       root       97:        }
                     98:        if (names.size() > 1) {
                     99:                // ソートして..
                    100:                std::sort(names.begin(), names.end());
                    101:                // 同じものが並んでいないか。
                    102:                for (int i = 0, sz = names.size() - 1; i < sz; i++) {
                    103:                        assertmsg(names[i] != names[i + 1],
                    104:                                "monitor alias \"%s\" duplicated", names[i].c_str());
                    105:                }
                    106:        }
1.1.1.6 ! root      107: 
        !           108:        return monitors[id_].get();
1.1       root      109: }
                    110: 
                    111: // mon がリストにあれば削除する。なければ何もしない。
                    112: void
                    113: MonitorManager::Unregist(Monitor *mon)
                    114: {
1.1.1.6 ! root      115:        uint id = mon->GetId();
        !           116:        monitors[id].reset();
1.1       root      117: }
                    118: 
                    119: 
                    120: // これ以降は追加禁止
                    121: void
                    122: MonitorManager::Fix()
                    123: {
                    124:        assert(fixed == false);
                    125: 
                    126:        fixed = true;
1.1.1.4   root      127: 
                    128:        // デバッグ用。
                    129:        if (0) {
1.1.1.5   root      130:                uint i = 0;
1.1.1.6 ! root      131:                for (const auto& uptr : monitors) {
        !           132:                        if ((bool)uptr) {
        !           133:                                const Monitor *m = uptr.get();
        !           134:                                auto sz = m->GetSize();
        !           135:                                printf("[%2u] %s\t%d,%d\n", i++, m->GetIdText(),
        !           136:                                        sz.width, sz.height);
        !           137:                        }
        !           138:                }
        !           139:        }
        !           140: }
        !           141: 
        !           142: // 指定された id を持つモニターを返す。
        !           143: // こっちは、なければ assert する。
        !           144: Monitor *
        !           145: MonitorManager::Get(uint id) const
        !           146: {
        !           147:        Monitor *mon = Find(id);
        !           148:        assertmsg(mon, "id=%u not found", id);
        !           149:        return mon;
        !           150: }
        !           151: 
        !           152: // 現在登録されているモニターの一覧を返す。
        !           153: const std::vector<Monitor *>
        !           154: MonitorManager::GetList() const
        !           155: {
        !           156:        std::vector<Monitor *> list;
        !           157: 
        !           158:        for (auto& uptr : monitors) {
        !           159:                if ((bool)uptr) {
        !           160:                        list.push_back(uptr.get());
1.1.1.4   root      161:                }
                    162:        }
1.1.1.6 ! root      163: 
        !           164:        return list;
1.1       root      165: }
                    166: 
                    167: // id リストから一覧表示用の文字列を生成して返す
                    168: /*static*/ std::string
1.1.1.5   root      169: MonitorManager::MakeListString(std::vector<uint> list)
1.1       root      170: {
                    171:        std::string str;
                    172: 
                    173:        // id をモニター情報内の1つ目の名前順にソートする
                    174:        std::sort(list.begin(), list.end(),
                    175:                [](const auto& a_id, const auto& b_id) {
1.1.1.2   root      176:                        const auto& a_alias = gMonitorManager->GetAliases(a_id);
                    177:                        const auto& b_alias = gMonitorManager->GetAliases(b_id);
1.1       root      178:                        const std::string a_name = a_alias[0];
                    179:                        const std::string b_name = b_alias[0];
                    180: 
                    181:                        return strcmp(a_name.c_str(), b_name.c_str()) < 0;
                    182:                }
                    183:        );
                    184: 
                    185:        for (const auto id : list) {
1.1.1.2   root      186:                const auto& aliases = gMonitorManager->GetAliases(id);
1.1       root      187:                assert(aliases.empty() == false);
                    188: 
                    189:                str += ' ';
                    190:                str += aliases[0];
                    191:                if (aliases.size() > 1) {
                    192:                        str += " (alias:";
                    193:                        for (int i = 1, sz = aliases.size(); i < sz; i++) {
                    194:                                str += ' ';
                    195:                                str += aliases[i];
                    196:                        }
                    197:                        str += ')';
                    198:                }
                    199:                str += '\n';
                    200:        }
                    201:        return str;
                    202: }

unix.superglobalmegacorp.com

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