Machine
VMachine is an implementation of a very simple virtual machine.
The virtual machine can maintain only state, but it can implement a limitless complexity of state transition. The machine is essentially a "Moore Machine"; a state variable with an input encoder, and an output decoder. The machine is preconfigured from graphical application which allows the user to build a state transition table. It would typically created by the GUI configuration application and stored to the resource block of a new application. To use it, the new application would extract it from resources as a VMachine and use it inside a function dedicated to that machine.
Machines are very nearly as fast as the huge switch statements they are designed to replace. There is a small performance hit. The reason they make sense is that one can build extremely intricate state machines. Nested state machines become easily manageable, when implemented this way, saving huge amounts of development time. It is easier to implement this particular set of problems this way because a very big interdependent switch statement is simply asking for trouble.
If, for example, one wishes to make a single small change to the function of a switch based state machine, in some circumstances one may be forced to change all of the switch clauses in the statement in some tiny way. It is easy to accidentally miss 10% of the necessary changes which then become bugs. This mistake is easy to make if the switch statement approaches perhaps 30 states/clauses. The actual statement might be 6-800 lines long. The same change in the machine editor is a single mouse click on the state transition table. There is much less room to make mistakes, but no limitation on what can be described.
The machine typically "plugs in" to a function designed to allow it to perform useful actions. In a parser this might take the form of reading a character, making a decision based on the current state, and entering a new state. An output flag might capture the current character position. The actual reading of the character, and capture/storage of the character position is performed in a while loop under instruction from the machine. The machine only looks at what is happening, makes decisions, and signals operations. This "read - decide - modify" action occurs in a single function call, to the machine, somewhere in the loop.
Information is passed to/from the machine, in an input and output DWORD. The GUI configuration app, is capable of producing header files which describe anonymous structs. These in turn partition the i/o DWORD's into bitfields. Where flags represent named quantities, the configuration app will produce enums which allow the bit fields to be tested against textual descriptive quantities defined in the configuration app.
Typically the machine would set a flag to initiate it's own exit from the while loop. |