diff pytouhou/vm/common.py @ 69:a142e57218a0

Refactor. Move VMs to pytouhou.vm.
author Thibaut Girka <thib@sitedethib.com>
date Sat, 27 Aug 2011 10:58:54 +0200
parents
children 69ec72b990a4
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/pytouhou/vm/common.py
@@ -0,0 +1,38 @@
+# -*- encoding: utf-8 -*-
+##
+## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published
+## by the Free Software Foundation; version 3 only.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+
+
+class MetaRegistry(type):
+    def __new__(mcs, name, bases, classdict):
+        instruction_handlers = {}
+        for item in classdict.itervalues():
+            try:
+                instruction_ids = item._instruction_ids
+            except AttributeError:
+                pass
+            else:
+                for id_ in instruction_ids:
+                    instruction_handlers[id_] = item
+        classdict['_handlers'] = instruction_handlers
+        return type.__new__(mcs, name, bases, classdict)
+
+
+
+def instruction(instruction_id):
+    def _decorator(func):
+        if not hasattr(func, '_instruction_ids'):
+            func._instruction_ids = set()
+        func._instruction_ids.add(instruction_id)
+        return func
+    return _decorator