1 |
/* |
2 |
* Copyright (c) 2014-2015 Christian Schoenebeck |
3 |
* |
4 |
* http://www.linuxsampler.org |
5 |
* |
6 |
* This file is part of LinuxSampler and released under the same terms. |
7 |
* See README file for details. |
8 |
*/ |
9 |
|
10 |
#ifndef LS_COREVMFUNCTIONS_H |
11 |
#define LS_COREVMFUNCTIONS_H |
12 |
|
13 |
#include "../common/global.h" |
14 |
#include "common.h" |
15 |
|
16 |
namespace LinuxSampler { |
17 |
|
18 |
class ScriptVM; |
19 |
|
20 |
/////////////////////////////////////////////////////////////////////////// |
21 |
// convenience base classes for built-in script functions ... |
22 |
|
23 |
/** |
24 |
* An instance of this class is returned by built-in function implementations |
25 |
* which do not return a function return value. |
26 |
*/ |
27 |
class VMEmptyResult : public VMFnResult, public VMExpr { |
28 |
public: |
29 |
StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call |
30 |
|
31 |
VMEmptyResult() : flags(STMT_SUCCESS) {} |
32 |
ExprType_t exprType() const { return EMPTY_EXPR; } |
33 |
VMExpr* resultValue() { return this; } |
34 |
StmtFlags_t resultFlags() { return flags; } |
35 |
}; |
36 |
|
37 |
/** |
38 |
* An instance of this class is returned by built-in function implementations |
39 |
* which return an integer value as function return value. |
40 |
*/ |
41 |
class VMIntResult : public VMFnResult, public VMIntExpr { |
42 |
public: |
43 |
StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call |
44 |
int value; ///< result value of the function call |
45 |
|
46 |
VMIntResult() : flags(STMT_SUCCESS) {} |
47 |
int evalInt() { return value; } |
48 |
VMExpr* resultValue() { return this; } |
49 |
StmtFlags_t resultFlags() { return flags; } |
50 |
}; |
51 |
|
52 |
/** |
53 |
* An instance of this class is returned by built-in function implementations |
54 |
* which return a string value as function return value. |
55 |
*/ |
56 |
class VMStringResult : public VMFnResult, public VMStringExpr { |
57 |
public: |
58 |
StmtFlags_t flags; ///< general completion status (i.e. success or failure) of the function call |
59 |
String value; ///< result value of the function call |
60 |
|
61 |
VMStringResult() : flags(STMT_SUCCESS) {} |
62 |
String evalStr() { return value; } |
63 |
VMExpr* resultValue() { return this; } |
64 |
StmtFlags_t resultFlags() { return flags; } |
65 |
}; |
66 |
|
67 |
/** |
68 |
* Abstract base class for built-in script functions which do not return any |
69 |
* function return value (void). |
70 |
*/ |
71 |
class VMEmptyResultFunction : public VMFunction { |
72 |
protected: |
73 |
ExprType_t returnType() { return EMPTY_EXPR; } |
74 |
VMFnResult* errorResult(); |
75 |
VMFnResult* successResult(); |
76 |
protected: |
77 |
VMEmptyResult result; |
78 |
}; |
79 |
|
80 |
/** |
81 |
* Abstract base class for built-in script functions which return an integer |
82 |
* (scalar) as their function return value. |
83 |
*/ |
84 |
class VMIntResultFunction : public VMFunction { |
85 |
protected: |
86 |
ExprType_t returnType() { return INT_EXPR; } |
87 |
VMFnResult* errorResult(int i = 0); |
88 |
VMFnResult* successResult(int i = 0); |
89 |
protected: |
90 |
VMIntResult result; |
91 |
}; |
92 |
|
93 |
/** |
94 |
* Abstract base class for built-in script functions which return a string as |
95 |
* their function return value. |
96 |
*/ |
97 |
class VMStringResultFunction : public VMFunction { |
98 |
protected: |
99 |
ExprType_t returnType() { return STRING_EXPR; } |
100 |
VMFnResult* errorResult(const String& s = ""); |
101 |
VMFnResult* successResult(const String& s = ""); |
102 |
protected: |
103 |
VMStringResult result; |
104 |
}; |
105 |
|
106 |
|
107 |
/////////////////////////////////////////////////////////////////////////// |
108 |
// implementations of core built-in script functions ... |
109 |
|
110 |
/** |
111 |
* Implements the built-in message() script function. |
112 |
*/ |
113 |
class CoreVMFunction_message : public VMEmptyResultFunction { |
114 |
public: |
115 |
int minRequiredArgs() const { return 1; } |
116 |
int maxAllowedArgs() const { return 1; } |
117 |
bool acceptsArgType(int iArg, ExprType_t type) const; |
118 |
ExprType_t argType(int iArg) const { return STRING_EXPR; } |
119 |
VMFnResult* exec(VMFnArgs* args); |
120 |
}; |
121 |
|
122 |
/** |
123 |
* Implements the built-in exit() script function. |
124 |
*/ |
125 |
class CoreVMFunction_exit : public VMEmptyResultFunction { |
126 |
public: |
127 |
int minRequiredArgs() const { return 0; } |
128 |
int maxAllowedArgs() const { return 0; } |
129 |
bool acceptsArgType(int iArg, ExprType_t type) const { return false; } |
130 |
ExprType_t argType(int iArg) const { return INT_EXPR; /*whatever*/ } |
131 |
VMFnResult* exec(VMFnArgs* args); |
132 |
}; |
133 |
|
134 |
/** |
135 |
* Implements the built-in wait() script function. |
136 |
*/ |
137 |
class CoreVMFunction_wait : public VMEmptyResultFunction { |
138 |
public: |
139 |
CoreVMFunction_wait(ScriptVM* vm) : vm(vm) {} |
140 |
int minRequiredArgs() const { return 1; } |
141 |
int maxAllowedArgs() const { return 1; } |
142 |
bool acceptsArgType(int iArg, ExprType_t type) const { return type == INT_EXPR; } |
143 |
ExprType_t argType(int iArg) const { return INT_EXPR; } |
144 |
VMFnResult* exec(VMFnArgs* args); |
145 |
protected: |
146 |
ScriptVM* vm; |
147 |
}; |
148 |
|
149 |
/** |
150 |
* Implements the built-in abs() script function. |
151 |
*/ |
152 |
class CoreVMFunction_abs : public VMIntResultFunction { |
153 |
public: |
154 |
int minRequiredArgs() const { return 1; } |
155 |
int maxAllowedArgs() const { return 1; } |
156 |
bool acceptsArgType(int iArg, ExprType_t type) const; |
157 |
ExprType_t argType(int iArg) const { return INT_EXPR; } |
158 |
VMFnResult* exec(VMFnArgs* args); |
159 |
}; |
160 |
|
161 |
/** |
162 |
* Implements the built-in random() script function. |
163 |
*/ |
164 |
class CoreVMFunction_random : public VMIntResultFunction { |
165 |
public: |
166 |
int minRequiredArgs() const { return 2; } |
167 |
int maxAllowedArgs() const { return 2; } |
168 |
bool acceptsArgType(int iArg, ExprType_t type) const; |
169 |
ExprType_t argType(int iArg) const { return INT_EXPR; } |
170 |
VMFnResult* exec(VMFnArgs* args); |
171 |
}; |
172 |
|
173 |
/** |
174 |
* Implements the built-in num_elements() script function. |
175 |
*/ |
176 |
class CoreVMFunction_num_elements : public VMIntResultFunction { |
177 |
public: |
178 |
int minRequiredArgs() const { return 1; } |
179 |
int maxAllowedArgs() const { return 1; } |
180 |
bool acceptsArgType(int iArg, ExprType_t type) const; |
181 |
ExprType_t argType(int iArg) const { return INT_ARR_EXPR; } |
182 |
VMFnResult* exec(VMFnArgs* args); |
183 |
}; |
184 |
|
185 |
} // namespace LinuxSampler |
186 |
|
187 |
#endif // LS_COREVMFUNCTIONS_H |