Merge pull request #4657 from mwichmann/feature/vars-defaulted
[scons.git] / SCons / Tool / MSCommon / MSVC / Dispatcher.py
blob31233a969de8604879ac736449e51a969fbbc3d3
1 # MIT License
3 # Copyright The SCons Foundation
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 """
25 Internal method dispatcher for Microsoft Visual C/C++.
27 MSVC modules can register their module (register_modulename) and individual
28 classes (register_class) with the method dispatcher during initialization. MSVC
29 modules tend to be registered immediately after the Dispatcher import near the
30 top of the file. Methods in the MSVC modules can be invoked indirectly without
31 having to hard-code the method calls effectively decoupling the upstream module
32 with the downstream modules:
34 The reset method dispatches calls to all registered objects with a reset method
35 and/or a _reset method. The reset methods are used to restore data structures
36 to their initial state for testing purposes. Typically, this involves clearing
37 cached values.
39 The verify method dispatches calls to all registered objects with a verify
40 method and/or a _verify method. The verify methods are used to check that
41 initialized data structures distributed across multiple modules are internally
42 consistent. An exception is raised when a verification constraint violation
43 is detected. Typically, this verifies that initialized dictionaries support
44 all of the requisite keys as new versions are added.
45 """
47 import sys
49 from ..common import (
50 debug,
53 _refs = []
56 def register_modulename(modname) -> None:
57 module = sys.modules[modname]
58 _refs.append(module)
61 def register_class(ref) -> None:
62 _refs.append(ref)
65 def reset() -> None:
66 debug('')
67 for ref in _refs:
68 for method in ['reset', '_reset']:
69 if not hasattr(ref, method) or not callable(getattr(ref, method, None)):
70 continue
71 debug('call %s.%s()', ref.__name__, method)
72 func = getattr(ref, method)
73 func()
76 def verify() -> None:
77 debug('')
78 for ref in _refs:
79 for method in ['verify', '_verify']:
80 if not hasattr(ref, method) or not callable(getattr(ref, method, None)):
81 continue
82 debug('call %s.%s()', ref.__name__, method)
83 func = getattr(ref, method)
84 func()