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