/[svn]/linuxsampler/trunk/src/scriptvm/common.h
ViewVC logotype

Diff of /linuxsampler/trunk/src/scriptvm/common.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2593 by schoenebeck, Sun Jun 1 14:44:38 2014 UTC revision 2594 by schoenebeck, Thu Jun 5 00:16:25 2014 UTC
# Line 16  Line 16 
16    
17  #include "../common/global.h"  #include "../common/global.h"
18  #include <vector>  #include <vector>
19    #include <map>
20    #include <stddef.h> // offsetof()
21    
22  namespace LinuxSampler {  namespace LinuxSampler {
23            
# Line 85  namespace LinuxSampler { Line 87  namespace LinuxSampler {
87          virtual VMFnResult* exec(VMFnArgs* args) = 0;          virtual VMFnResult* exec(VMFnArgs* args) = 0;
88      };      };
89    
90        /**
91         * POD base of VMIntRelPtr and VMInt8RelPtr structures. Not intended to be
92         * used directly. Use VMIntRelPtr or VMInt8RelPtr instead.
93         */
94        struct VMRelPtr {
95            void** base; ///< Base pointer.
96            int offset;  ///< Offset (in bytes) to base pointer.
97        };
98    
99        /** @brief Pointer to built-in VM integer variable (of C/C++ type int).
100         *
101         * Used for defining built-in integer script variables.
102         *
103         * @b CAUTION: You may only use this class for pointing to C/C++ variables
104         * of type "int" (which on most systems is 32 bit in size). If the C/C++ int
105         * variable you want to reference is only 8 bit in size, then you @b must
106         * use VMInt8RelPtr instead!
107         *
108         * For efficiency reasons the actual native C/C++ int variable is referenced
109         * by two components here. The actual native int C/C++ variable in memory
110         * is dereferenced at VM run-time by taking the @c base pointer dereference
111         * and adding @c offset bytes. This has the advantage that for a large
112         * number of built-in int variables, only one (or few) base pointer need
113         * to be re-assigned before running a script, instead of updating each
114         * built-in variable each time before a script is executed.
115         *
116         * Refer to DECLARE_VMINT() for example code.
117         *
118         * @see VMInt8RelPtr, DECLARE_VMINT()
119         */
120        struct VMIntRelPtr : VMRelPtr {
121            VMIntRelPtr() {
122                base   = NULL;
123                offset = 0;
124            }
125            VMIntRelPtr(const VMRelPtr& data) {
126                base   = data.base;
127                offset = data.offset;
128            }
129            virtual int evalInt() { return *(int*)&(*(uint8_t**)base)[offset]; }
130            virtual void assign(int i) { *(int*)&(*(uint8_t**)base)[offset] = i; }
131        };
132    
133        /** @brief Pointer to built-in VM integer variable (of C/C++ type int8_t).
134         *
135         * Used for defining built-in integer script variables.
136         *
137         * @b CAUTION: You may only use this class for pointing to C/C++ variables
138         * of type "int8_t" (8 bit integer). If the C/C++ int variable you want to
139         * reference is an "int" type (which is 32 bit on most systems), then you
140         * @b must use VMIntRelPtr instead!
141         *
142         * For efficiency reasons the actual native C/C++ int variable is referenced
143         * by two components here. The actual native int C/C++ variable in memory
144         * is dereferenced at VM run-time by taking the @c base pointer dereference
145         * and adding @c offset bytes. This has the advantage that for a large
146         * number of built-in int variables, only one (or few) base pointer need
147         * to be re-assigned before running a script, instead of updating each
148         * built-in variable each time before a script is executed.
149         *
150         * Refer to DECLARE_VMINT() for example code.
151         *
152         * @see VMIntRelPtr, DECLARE_VMINT()
153         */
154        struct VMInt8RelPtr : VMIntRelPtr {
155            VMInt8RelPtr() : VMIntRelPtr() {}
156            VMInt8RelPtr(const VMRelPtr& data) : VMIntRelPtr(data) {}
157            virtual int evalInt() OVERRIDE {
158                return *(uint8_t*)&(*(uint8_t**)base)[offset];
159            }
160            virtual void assign(int i) OVERRIDE {
161                *(uint8_t*)&(*(uint8_t**)base)[offset] = i;
162            }
163        };
164    
165        /**
166         * Convenience macro for initializing VMIntRelPtr and VMInt8RelPtr
167         * structures. Example:
168         * @code
169         * struct Foo {
170         *   uint8_t a;
171         *   int b;
172         * };
173         *
174         * Foo foo1 = (Foo) { 1, 3000 };
175         * Foo foo2 = (Foo) { 2, 4000 };
176         *
177         * Foo* pFoo;
178         *
179         * VMInt8RelPtr var1 = DECLARE_VMINT(pFoo, class Foo, a);
180         * VMIntRelPtr  var2 = DECLARE_VMINT(pFoo, class Foo, b);
181         *
182         * pFoo = &foo1;
183         * printf("%d\n", var1->evalInt()); // will print 1
184         * printf("%d\n", var2->evalInt()); // will print 3000
185         *
186         * pFoo = &foo2;
187         * printf("%d\n", var1->evalInt()); // will print 2
188         * printf("%d\n", var2->evalInt()); // will print 4000
189         * @endcode
190         */
191        #define DECLARE_VMINT(basePtr, T_struct, T_member) ( \
192            (VMRelPtr) {                                     \
193                (void**) &basePtr,                           \
194                offsetof(T_struct, T_member)                 \
195            }                                                \
196        )                                                    \
197    
198        /** @brief Built-in VM 8 bit integer array variable.
199         *
200         * Used for defining built-in integer array script variables.
201         */
202        struct VMInt8Array {
203            int8_t* data;
204            int size;
205    
206            VMInt8Array() : data(NULL), size(0) {}
207        };
208    
209      class VMFunctionProvider {      class VMFunctionProvider {
210      public:      public:
211          virtual VMFunction* functionByName(const String& name) = 0;          virtual VMFunction* functionByName(const String& name) = 0;
212            virtual std::map<String,VMIntRelPtr*> builtInIntVariables() = 0;
213            virtual std::map<String,VMInt8Array*> builtInIntArrayVariables() = 0;
214            virtual std::map<String,int> builtInConstIntVariables() = 0;
215      };      };
216    
217        /** @brief Execution state of a virtual machine.
218         *
219         * An instance of this abstract base class represents exactly one execution
220         * state of a virtual machine. This encompasses most notably the VM
221         * execution stack, and VM polyphonic variables. You might see it as one
222         * virtual thread of the virtual machine.
223         *
224         * @see VMParserContext
225         */
226      class VMExecContext {      class VMExecContext {
227      public:      public:
228          virtual ~VMExecContext() {}          virtual ~VMExecContext() {}
# Line 132  namespace LinuxSampler { Line 265  namespace LinuxSampler {
265          return "invalid";          return "invalid";
266      }      }
267    
268        /** @brief Virtual machine representation of a script.
269         *
270         * An instance of this abstract base class represents a parsed script,
271         * translated into a virtual machine. You should first check if there were
272         * any parser errors. If there were any parser errors, you should refrain
273         * from executing the virtual machine. Otherwise if there were no parser
274         * errors (i.e. only warnings), then you might access one of the script's
275         * event handlers by i.e. calling eventHandlerByName() and pass the
276         * respective event handler to the ScriptVM class (or to one of its
277         * descendants) for execution.
278         *
279         * @see VMExecContext
280         */
281      class VMParserContext {      class VMParserContext {
282      public:      public:
283          virtual ~VMParserContext() {}          virtual ~VMParserContext() {}

Legend:
Removed from v.2593  
changed lines
  Added in v.2594

  ViewVC Help
Powered by ViewVC