codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
// 模擬OS by ミングスレの名無し #include <string> #include <vector> #include <queue> #include <map> // ------------------------------------------------------------------- // 定義系 ------------------------------------------------------------ // とりあえずメッセージ enum WM_MSG { WM_UNDEFINED, WM_CREATE, WM_PAINT, WM_FOO, WM_DESTOROY }; // ハンドル系 typedef unsigned int HANDLE; typedef HANDLE HWND; // ウィンドプロシジャ typedef bool (*WndProc)(HWND, WM_MSG, int); // なんかウィンドウ typedef struct { std::string name; WndProc proc; } Window; // メッセージ構造体 typedef struct _tMsg { HWND hWnd; WM_MSG msg; int param; _tMsg(): msg(WM_UNDEFINED), param(0) {} _tMsg(HWND h, WM_MSG m, int i): hWnd(h), msg(m), param(i) {} } MSG; // メッセージのキュー typedef std::queue<MSG> MsgQue; // ウィンドウとメッセージキューのセット typedef std::pair<Window, MsgQue> WinSet; // ウィンドウの列挙 typedef std::vector<WinSet> EnumWin; // ------------------------------------------------------------------- // OSの中 ------------------------------------------------------------ EnumWin ew; HANDLE RegisterWindow(const char *name, WndProc proc) { Window wm = {std::string(name), proc}; ew.push_back(WinSet(wm, MsgQue())); return (HANDLE)ew.size()-1; } bool GetMessage(HANDLE hWnd, MSG &msg) { if( ew[hWnd].second.size()<1 ) return false; MSG m = ew[hWnd].second.front(); msg.hWnd = hWnd; msg.msg = m.msg; msg.param = m.param; ew[hWnd].second.pop(); return true; } bool DispatchMessage(MSG &msg) { return ew[msg.hWnd].first.proc(msg.hWnd, msg.msg, msg.param); } void os_no_sigoto(HWND hWnd) { // 本当はここはリアルタイム ew[hWnd].second.push(MSG(hWnd, WM_CREATE, 1)); ew[hWnd].second.push(MSG(hWnd, WM_FOO, 1)); ew[hWnd].second.push(MSG(hWnd, WM_PAINT, 1)); ew[hWnd].second.push(MSG(hWnd, WM_DESTOROY, 1)); } // ------------------------------------------------------------------- // ------------------------------------------------------------------- // ------------------------------------------------------------------- // このOSで動かす何らかのアプリケーションコード ---------------------- // として、仮想言語環境を準備する ------------------------------------ // ------------------------------------------------------------------- class Form; bool def_proc(HWND, WM_MSG, int); // 秘密のマップ static std::map<HWND, Form*> mapWin; // 隠蔽クラスちゃん class Form { friend bool def_proc(HWND, WM_MSG, int); private: typedef bool (*Listener)(WM_MSG, int); std::map<WM_MSG, Listener> ml; bool doProc(HWND hWnd, WM_MSG msg, int param) { return ml.count(msg)>0? ml[msg](msg, param): true; } public: HWND hWnd; Form(const char *name) { hWnd = RegisterWindow(name, def_proc); mapWin[hWnd] = this; } void addListener(WM_MSG msg, Listener func) { ml[msg] = func; } }; // 隠されたウィンドプロシジャ bool def_proc(HWND hWnd, WM_MSG msg, int param) { return mapWin[hWnd]->doProc(hWnd, msg, param); } // ------------------------------------------------------------------- // ------------------------------------------------------------------- // ------------------------------------------------------------------- // この仮想環境で動かす何らかのアプリケーションコード ---------------- // ------------------------------------------------------------------- bool onCreate(WM_MSG msg, int param) { printf("生まれた!\n"); return true; } bool onFoo(WM_MSG msg, int param) { printf("ほほう!\n"); return true; } bool onPaint(WM_MSG msg, int param) { printf("描画かね!\n"); return true; } bool onDestoroy(WM_MSG msg, int param) { printf("さよなら!\n"); return true; } // WinMainな奴 int main() { Form form1("名前2"); form1.addListener(WM_CREATE, onCreate); form1.addListener(WM_FOO, onFoo); form1.addListener(WM_PAINT, onPaint); form1.addListener(WM_DESTOROY, onDestoroy); os_no_sigoto(form1.hWnd); // ここは無視 MSG msg; while(GetMessage(form1.hWnd, msg)) // ウィンドウにメッセージがあるか尋ねて { if(!DispatchMessage(msg)) break; // あればディスパッチ } return 0; }
Private
[
?
]
Run code
Submit