Annotation of parser3/src/classes/int.C, revision 1.61

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.61    ! moko       16: volatile const char * IDENT_INT_C="$Id: int.C,v 1.60 2012-03-16 09:24:07 moko Exp $" IDENT_PA_VINT_H;
1.60      moko       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.61    ! moko       84: extern const String* sql_result_string(Request& r, MethodParams& params, Value*& default_code);
        !            85: 
1.50      paf        86: static void _sql(Request& r, MethodParams& params) {
1.31      parser     87:        int val;
1.50      paf        88:        Value* default_code=0;
1.61    ! moko       89:        if(const String* string=sql_result_string(r, params, default_code))
1.31      parser     90:                val=string->as_int();
                     91:        else
1.36      parser     92:                if(default_code)
1.43      paf        93:                        val=r.process_to_value(*default_code).as_int();
1.36      parser     94:                else {
1.57      misha      95:                        throw Exception(PARSER_RUNTIME,
1.50      paf        96:                                0,
1.36      parser     97:                                "produced no result, but no default option specified");
1.31      parser     98:                }
1.50      paf        99:        r.write_no_lang(*new VInt(val));
1.25      parser    100: }
                    101: 
1.20      paf       102: // constructor
                    103: 
1.50      paf       104: MInt::MInt(): Methoded("int") {
1.9       paf       105:        // ^int.int[]
1.42      paf       106:        add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.9       paf       107: 
                    108:        // ^int.double[]
1.42      paf       109:        add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.9       paf       110: 
1.56      misha     111:        // ^double.bool[]
                    112:        add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1);
                    113: 
1.9       paf       114:        // ^int.inc[] 
                    115:        // ^int.inc[offset]
1.20      paf       116:        add_native_method("inc", Method::CT_DYNAMIC, _inc, 0, 1);
1.13      paf       117:        // ^int.dec[] 
                    118:        // ^int.dec[offset]
1.20      paf       119:        add_native_method("dec", Method::CT_DYNAMIC, _dec, 0, 1);
1.13      paf       120:        // ^int.mul[k] 
1.20      paf       121:        add_native_method("mul", Method::CT_DYNAMIC, _mul, 1, 1);
1.13      paf       122:        // ^int.div[d]
1.20      paf       123:        add_native_method("div", Method::CT_DYNAMIC, _div, 1, 1);
1.13      paf       124:        // ^int.mod[offset]
1.20      paf       125:        add_native_method("mod", Method::CT_DYNAMIC, _mod, 1, 1);
1.13      paf       126: 
1.12      paf       127: 
1.18      paf       128:        // ^int.format{format}
1.20      paf       129:        add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.26      parser    130: 
1.32      parser    131:        // ^sql[query]
                    132:        // ^sql[query][$.limit(1) $.offset(2) $.default(0)]
1.30      parser    133:        add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.1       paf       134: }

E-mail: