|
|
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.8 ! root 69: // モニタを登録。
! 70: Monitor *
! 71: MonitorManager::Regist(uint id_, Object *parent_)
! 72: {
! 73: return RegistCommon(id_, parent_, NULL);
! 74: }
! 75:
! 76: // 呼び出し側で作成したモニタを登録。
! 77: Monitor *
! 78: MonitorManager::Regist(Monitor *monitor_)
! 79: {
! 80: return RegistCommon(monitor_->GetId(), monitor_->obj, monitor_);
! 81: }
! 82:
! 83: // id_, parent_ のモニタを登録。
! 84: // monitor_ は通常 NULL を指定しておけばこちらで標準モニタを作成する。
! 85: // 呼び出し元で派生クラスなどを生成していればそれを指定。
1.1.1.6 root 86: // 成功すればモニターを返す。通常失敗しない。
87: // 失敗すればいずれのケースも assert で終了する。
88: Monitor *
1.1.1.8 ! root 89: MonitorManager::RegistCommon(uint id_, Object *parent_, Monitor *monitor_)
1.1 root 90: {
1.1.1.6 root 91: assert(id_ < ID_SUBWIN_MAX);
1.1 root 92:
1.1.1.6 root 93: // 追加が遅すぎる。
94: assert(fixed == false);
1.1 root 95:
1.1.1.6 root 96: // 二重登録防止。
97: assertmsg((bool)monitors[id_] == false,
98: "%s already registered", Monitor::GetIdText(id_));
99:
1.1.1.8 ! root 100: // 登録、もしくはここで生成。
! 101: if (monitor_) {
! 102: monitors[id_].reset(monitor_);
! 103: } else {
! 104: monitors[id_].reset(new Monitor(id_, parent_));
! 105: assertmsg((bool)monitors[id_], "cannot create new monitor");
! 106: }
1.1 root 107:
108: // 名前が衝突していないことを確認。
109: // どのケースの衝突も検出できるよう、追加後に全部列挙して..
110: std::vector<std::string> names;
1.1.1.6 root 111: for (const auto& uptr : monitors) {
112: if ((bool)uptr) {
113: const Monitor *m = uptr.get();
114: const std::vector<std::string>& aliases = GetAliases(m->GetId());
115: names.insert(names.end(), aliases.begin(), aliases.end());
116: }
1.1 root 117: }
118: if (names.size() > 1) {
119: // ソートして..
120: std::sort(names.begin(), names.end());
121: // 同じものが並んでいないか。
122: for (int i = 0, sz = names.size() - 1; i < sz; i++) {
123: assertmsg(names[i] != names[i + 1],
124: "monitor alias \"%s\" duplicated", names[i].c_str());
125: }
126: }
1.1.1.6 root 127:
128: return monitors[id_].get();
1.1 root 129: }
130:
131: // mon がリストにあれば削除する。なければ何もしない。
132: void
133: MonitorManager::Unregist(Monitor *mon)
134: {
1.1.1.6 root 135: uint id = mon->GetId();
136: monitors[id].reset();
1.1 root 137: }
138:
139:
140: // これ以降は追加禁止
141: void
142: MonitorManager::Fix()
143: {
144: assert(fixed == false);
145:
146: fixed = true;
1.1.1.4 root 147:
148: // デバッグ用。
149: if (0) {
1.1.1.5 root 150: uint i = 0;
1.1.1.6 root 151: for (const auto& uptr : monitors) {
152: if ((bool)uptr) {
153: const Monitor *m = uptr.get();
154: auto sz = m->GetSize();
155: printf("[%2u] %s\t%d,%d\n", i++, m->GetIdText(),
156: sz.width, sz.height);
157: }
158: }
159: }
160: }
161:
162: // 指定された id を持つモニターを返す。
163: // こっちは、なければ assert する。
164: Monitor *
165: MonitorManager::Get(uint id) const
166: {
167: Monitor *mon = Find(id);
168: assertmsg(mon, "id=%u not found", id);
169: return mon;
170: }
171:
172: // 現在登録されているモニターの一覧を返す。
173: const std::vector<Monitor *>
174: MonitorManager::GetList() const
175: {
176: std::vector<Monitor *> list;
177:
178: for (auto& uptr : monitors) {
179: if ((bool)uptr) {
180: list.push_back(uptr.get());
1.1.1.4 root 181: }
182: }
1.1.1.6 root 183:
184: return list;
1.1 root 185: }
186:
1.1.1.7 root 187: // id リストから一覧表示用の文字列リストを生成して返す
188: std::vector<std::string>
189: MonitorManager::MakeListString(std::vector<uint> list) const
1.1 root 190: {
1.1.1.7 root 191: std::vector<std::string> rv;
1.1 root 192:
193: // id をモニター情報内の1つ目の名前順にソートする
194: std::sort(list.begin(), list.end(),
1.1.1.7 root 195: [&](const auto& a_id, const auto& b_id) {
196: const auto& a_alias = GetAliases(a_id);
197: const auto& b_alias = GetAliases(b_id);
1.1 root 198: const std::string a_name = a_alias[0];
199: const std::string b_name = b_alias[0];
200:
201: return strcmp(a_name.c_str(), b_name.c_str()) < 0;
202: }
203: );
204:
205: for (const auto id : list) {
1.1.1.7 root 206: const auto& aliases = GetAliases(id);
1.1 root 207: assert(aliases.empty() == false);
208:
1.1.1.7 root 209: std::string str = aliases[0];
1.1 root 210: if (aliases.size() > 1) {
211: str += " (alias:";
212: for (int i = 1, sz = aliases.size(); i < sz; i++) {
213: str += ' ';
214: str += aliases[i];
215: }
216: str += ')';
217: }
1.1.1.7 root 218: rv.push_back(str);
1.1 root 219: }
1.1.1.7 root 220: return rv;
1.1 root 221: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.