Annotation of nono/wx/wxuimessage.cpp, revision 1.1.1.5

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: // UI スレッドへのメッセージ機構 (GUI 側)
                      9: //
                     10: 
1.1       root       11: #include "wxuimessage.h"
1.1.1.5 ! root       12: #include <array>
        !            13: #include <mutex>
1.1       root       14: 
                     15: // イベントタイプの定義
                     16: wxDEFINE_EVENT(NONO_EVT_UIMESSAGE, wxCommandEvent);
                     17: 
1.1.1.5 ! root       18: static std::array<std::vector<wxEvtHandler *>, UIMessage::ID_MAX> table;
        !            19: static std::mutex mtx;
1.1       root       20: 
                     21: // UIMessage を wxWidgets のイベントハンドラに接続する
                     22: /*static*/ void
1.1.1.5 ! root       23: WXUIMessage::Connect(UIMessage::ID id, wxEvtHandler *win,
        !            24:        wxObjectEventFunction function)
1.1       root       25: {
1.1.1.5 ! root       26:        std::lock_guard<std::mutex> lock(mtx);
        !            27:        win->Connect(id, NONO_EVT_UIMESSAGE, function);
        !            28:        table[id].push_back(win);
1.1       root       29: }
                     30: 
                     31: // イベントハンドラの接続を解除する
                     32: /*static*/ void
1.1.1.5 ! root       33: WXUIMessage::Disconnect(UIMessage::ID id, wxEvtHandler *win,
        !            34:        wxObjectEventFunction function)
1.1       root       35: {
1.1.1.5 ! root       36:        std::lock_guard<std::mutex> lock(mtx);
        !            37:        win->Disconnect(NONO_EVT_UIMESSAGE, function);
        !            38:        for (auto it = table[id].begin(); it != table[id].end(); ++it) {
        !            39:                if (*it == win) {
        !            40:                        table[id].erase(it);
1.1.1.3   root       41:                        break;
                     42:                }
1.1       root       43:        }
                     44: }
                     45: 
1.1.1.5 ! root       46: // UIMessage::Post() で呼ばれるコールバック関数。
1.1       root       47: /*static*/ void
1.1.1.5 ! root       48: WXUIMessage::Process(uint id, int arg)
1.1       root       49: {
1.1.1.5 ! root       50:        if (id >= UIMessage::ID_MAX) {
        !            51:                PANIC("Invalid UIMessage %u", id);
        !            52:        }
        !            53:        std::lock_guard<std::mutex> lock(mtx);
        !            54:        for (auto win : table[id]) {
        !            55:                wxCommandEvent ev(NONO_EVT_UIMESSAGE, id);
        !            56:                ev.SetInt(arg);
        !            57:                wxPostEvent(win, ev);
1.1       root       58:        }
                     59: }
1.1.1.2   root       60: 
                     61: // すべてのハンドラが解除されているかチェックする (開発用)
                     62: /*static*/ void
                     63: WXUIMessage::AssertAllDisconnected()
                     64: {
1.1.1.5 ! root       65:        for (int i = 0; i < table.size(); i++) {
        !            66:                if (table[i].empty() == false) {
1.1.1.2   root       67:                        warnx("UIMessage ID=%d not disconnected", i);
                     68:                }
                     69:        }
                     70: }

unix.superglobalmegacorp.com

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