comparison pytouhou/vm/common.py @ 376:69ec72b990a4

Support more than one version of a vm.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 06 Aug 2012 23:10:09 +0200
parents a142e57218a0
children e15672733c93
comparison
equal deleted inserted replaced
375:8f2f3053906a 376:69ec72b990a4
15 15
16 class MetaRegistry(type): 16 class MetaRegistry(type):
17 def __new__(mcs, name, bases, classdict): 17 def __new__(mcs, name, bases, classdict):
18 instruction_handlers = {} 18 instruction_handlers = {}
19 for item in classdict.itervalues(): 19 for item in classdict.itervalues():
20 try: 20 if hasattr(item, '_instruction_ids'):
21 instruction_ids = item._instruction_ids 21 for version, instruction_ids in item._instruction_ids.iteritems():
22 except AttributeError: 22 for id_ in instruction_ids:
23 pass 23 instruction_handlers.setdefault(version, {})[id_] = item
24 else:
25 for id_ in instruction_ids:
26 instruction_handlers[id_] = item
27 classdict['_handlers'] = instruction_handlers 24 classdict['_handlers'] = instruction_handlers
28 return type.__new__(mcs, name, bases, classdict) 25 return type.__new__(mcs, name, bases, classdict)
29 26
30 27
31 28
32 def instruction(instruction_id): 29 def instruction(instruction_id, version=6):
33 def _decorator(func): 30 def _decorator(func):
34 if not hasattr(func, '_instruction_ids'): 31 if not hasattr(func, '_instruction_ids'):
35 func._instruction_ids = set() 32 func._instruction_ids = {}
36 func._instruction_ids.add(instruction_id) 33 func._instruction_ids.setdefault(version, set()).add(instruction_id)
37 return func 34 return func
38 return _decorator 35 return _decorator