Annotation of parser3/src/classes/int.C, revision 1.58
1.18 paf 1: /** @file
2: Parser: @b int parser class.
3:
1.58 ! misha 4: Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
1.40 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.45 paf 6: */
1.18 paf 7:
1.58 ! misha 8: static const char * const IDENT_INT_C="$Date: 2007/04/23 10:30:09 $";
1.1 paf 9:
1.22 paf 10: #include "classes.h"
1.50 paf 11: #include "pa_vmethod_frame.h"
12:
1.1 paf 13: #include "pa_request.h"
14: #include "pa_vdouble.h"
15: #include "pa_vint.h"
1.56 misha 16: #include "pa_vbool.h"
1.1 paf 17:
1.20 paf 18: // externs
1.1 paf 19:
1.50 paf 20: void _string_format(Request& r, MethodParams&);
1.20 paf 21:
22: // class
23:
1.50 paf 24: class MInt: public Methoded {
1.20 paf 25: public:
1.50 paf 26: MInt();
1.23 paf 27: public: // Methoded
1.25 parser 28: bool used_directly() { return true; }
1.20 paf 29: };
1.1 paf 30:
1.50 paf 31: // global variable
32:
33: DECLARE_CLASS_VAR(int, new MInt, 0);
34:
1.1 paf 35: // methods
36:
1.50 paf 37: static void _int(Request& r, MethodParams& params) {
1.56 misha 38: // just checking (default) syntax validity, never really using it here, just for string.int compatibility
1.50 paf 39: if(params.count()>0)
1.55 paf 40: params.as_int(0, "default must be int", r);
1.42 paf 41:
1.50 paf 42: VInt& vint=GET_SELF(r, VInt);
43: r.write_no_lang(*new VInt(vint.get_int()));
1.1 paf 44: }
45:
1.50 paf 46: static void _double(Request& r, MethodParams& params) {
1.56 misha 47: // just checking (default) syntax validity, never really using it here, just for string.double compatibility
1.50 paf 48: if(params.count()>0)
1.55 paf 49: params.as_double(0, "default must be double", r);
1.42 paf 50:
1.50 paf 51: VInt& vint=GET_SELF(r, VInt);
52: r.write_no_lang(*new VDouble(vint.as_double()));
1.1 paf 53: }
54:
1.56 misha 55: static void _bool(Request& r, MethodParams& params) {
56: // just checking (default) syntax validity, never really using it here, just for string.bool compatibility
57: if(params.count()>0)
58: params.as_bool(0, "default must be bool", r);
59:
60: VInt& vint=GET_SELF(r, VInt);
1.58 ! misha 61: r.write_no_lang(VBool::get(vint.as_bool()));
1.56 misha 62: }
63:
1.13 paf 64: typedef void (*vint_op_func_ptr)(VInt& vint, double param);
65:
66: static void __inc(VInt& vint, double param) { vint.inc((int)param); }
67: static void __dec(VInt& vint, double param) { vint.inc((int)-param); }
68: static void __mul(VInt& vint, double param) { vint.mul(param); }
69: static void __div(VInt& vint, double param) { vint.div(param); }
70: static void __mod(VInt& vint, double param) { vint.mod((int)param); }
71:
1.50 paf 72: static void vint_op(Request& r, MethodParams& params,
1.13 paf 73: vint_op_func_ptr func) {
1.50 paf 74: VInt& vint=GET_SELF(r, VInt);
75: double param=params.count()?params.as_double(0, "param must be numerical", r):1;
76: (*func)(vint, param);
1.1 paf 77: }
78:
1.50 paf 79: static void _inc(Request& r, MethodParams& params) { vint_op(r, params, &__inc); }
80: static void _dec(Request& r, MethodParams& params) { vint_op(r, params, &__dec); }
81: static void _mul(Request& r, MethodParams& params) { vint_op(r, params, &__mul); }
82: static void _div(Request& r, MethodParams& params) { vint_op(r, params, &__div); }
83: static void _mod(Request& r, MethodParams& params) { vint_op(r, params, &__mod); }
1.15 paf 84:
1.26 parser 85: // from string.C
1.25 parser 86: extern
1.50 paf 87: const String* sql_result_string(Request& r, MethodParams& params,
88: HashStringValue*& options, Value*& default_code);
89: static void _sql(Request& r, MethodParams& params) {
1.31 parser 90: int val;
1.50 paf 91: HashStringValue* options;
92: Value* default_code=0;
93: if(const String* string=sql_result_string(r, params, options, default_code))
1.31 parser 94: val=string->as_int();
95: else
1.36 parser 96: if(default_code)
1.43 paf 97: val=r.process_to_value(*default_code).as_int();
1.36 parser 98: else {
1.57 misha 99: throw Exception(PARSER_RUNTIME,
1.50 paf 100: 0,
1.36 parser 101: "produced no result, but no default option specified");
1.31 parser 102: }
1.50 paf 103: r.write_no_lang(*new VInt(val));
1.25 parser 104: }
105:
1.20 paf 106: // constructor
107:
1.50 paf 108: MInt::MInt(): Methoded("int") {
1.9 paf 109: // ^int.int[]
1.42 paf 110: add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.9 paf 111:
112: // ^int.double[]
1.42 paf 113: add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.9 paf 114:
1.56 misha 115: // ^double.bool[]
116: add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1);
117:
1.9 paf 118: // ^int.inc[]
119: // ^int.inc[offset]
1.20 paf 120: add_native_method("inc", Method::CT_DYNAMIC, _inc, 0, 1);
1.13 paf 121: // ^int.dec[]
122: // ^int.dec[offset]
1.20 paf 123: add_native_method("dec", Method::CT_DYNAMIC, _dec, 0, 1);
1.13 paf 124: // ^int.mul[k]
1.20 paf 125: add_native_method("mul", Method::CT_DYNAMIC, _mul, 1, 1);
1.13 paf 126: // ^int.div[d]
1.20 paf 127: add_native_method("div", Method::CT_DYNAMIC, _div, 1, 1);
1.13 paf 128: // ^int.mod[offset]
1.20 paf 129: add_native_method("mod", Method::CT_DYNAMIC, _mod, 1, 1);
1.13 paf 130:
1.12 paf 131:
1.18 paf 132: // ^int.format{format}
1.20 paf 133: add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.26 parser 134:
1.32 parser 135: // ^sql[query]
136: // ^sql[query][$.limit(1) $.offset(2) $.default(0)]
1.30 parser 137: add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.1 paf 138: }
E-mail: