1ccd49701a54a2c14c25870c31cc4b90277d3f48
[framework/web/wrt-plugins-tizen.git] / src / Common / ArgumentValidator.h
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #ifndef _ARGUMENT_VALIDATOR_H_
19 #define _ARGUMENT_VALIDATOR_H_
20
21 #include <string>
22 #include <vector>
23 #include <map>
24 #include <JavaScriptCore/JavaScript.h>
25 #include <ctime>
26
27 namespace DeviceAPI {
28 namespace Common{
29
30 class ArgumentValidator{
31 public:
32     ArgumentValidator(JSContextRef ctx, int argc, const JSValueRef* argv);
33     ~ArgumentValidator();
34
35     double toNumber(int index, bool nullable = false, double defaultvalue = 0);
36     long toLong(int index, bool nullable = false, long defaultvalue = 0);
37     unsigned long toULong(int index, bool nullable = false, long defaultvalue = 0);
38     long long toLongLong(int index, bool nullable = false, long long defaultvalue = 0);
39     unsigned long long toULongLong(int index, bool nullable = false, long long defaultvalue = 0);
40     double toDouble(int index, bool nullable = false, double defaultvalue = 0);
41     std::string toString(int index, bool nullable = false, const std::string& defaultvalue = "");
42     bool toBool(int index, bool nullable = false, bool defaultvalue = false);
43     time_t toTimeT(int index, bool nullable = false, time_t defaultvalue = 0);
44     JSObjectRef toObject(int index, bool nullable = false);
45     JSObjectRef toObject(int index, JSClassRef info, bool nullable = false);
46     JSObjectRef toFunction(int index, bool nullable = false);
47     JSObjectRef toArrayObject(int index, bool nullable = false);
48     JSValueRef toJSValueRef(int index, bool nullable = false);
49
50     std::vector<std::string> toStringVector(int index, bool nullable = false);
51     std::vector<long> toLongVector(int index, bool nullable = false);
52     std::vector<double> toDoubleVector(int index, bool nullable = false);
53     std::vector<time_t> toTimeTVector(int index, bool nullable = false);
54     std::vector<bool> toBoolVector(int index, bool nullable = false);
55     std::vector<JSValueRef> toJSValueRefVector(int index, bool nullable= false);
56
57     std::map<std::string, std::string> toStringMap(int index, bool nullable = false);
58
59 private:
60     JSValueRef getArgument(int index, bool nullable);
61     JSContextRef mContext;
62     int mArgc;
63     const JSValueRef* mArgv;
64
65 };
66
67 }
68 }
69
70 #endif //_ARGUMENT_VALIDATOR_H_
71
72