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