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

1.18      paf         1: /** @file
                      2:        Parser: @b int parser class.
                      3: 
1.53.14.1! paf         4:        Copyright (c) 2001-2005 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.53.14.1! paf         8: static const char * const IDENT_INT_C="$Date: 2004/02/11 15:33:12 $";
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:                }
1.50      paf        93:        r.write_no_lang(*new VInt(val));
1.25      parser     94: }
                     95: 
1.20      paf        96: // constructor
                     97: 
1.50      paf        98: MInt::MInt(): Methoded("int") {
1.9       paf        99:        // ^int.int[]
1.42      paf       100:        add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.9       paf       101: 
                    102:        // ^int.double[]
1.42      paf       103:        add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.9       paf       104: 
                    105:        // ^int.inc[] 
                    106:        // ^int.inc[offset]
1.20      paf       107:        add_native_method("inc", Method::CT_DYNAMIC, _inc, 0, 1);
1.13      paf       108:        // ^int.dec[] 
                    109:        // ^int.dec[offset]
1.20      paf       110:        add_native_method("dec", Method::CT_DYNAMIC, _dec, 0, 1);
1.13      paf       111:        // ^int.mul[k] 
1.20      paf       112:        add_native_method("mul", Method::CT_DYNAMIC, _mul, 1, 1);
1.13      paf       113:        // ^int.div[d]
1.20      paf       114:        add_native_method("div", Method::CT_DYNAMIC, _div, 1, 1);
1.13      paf       115:        // ^int.mod[offset]
1.20      paf       116:        add_native_method("mod", Method::CT_DYNAMIC, _mod, 1, 1);
1.13      paf       117: 
1.12      paf       118: 
1.18      paf       119:        // ^int.format{format}
1.20      paf       120:        add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.26      parser    121: 
1.32      parser    122:        // ^sql[query]
                    123:        // ^sql[query][$.limit(1) $.offset(2) $.default(0)]
1.30      parser    124:        add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.1       paf       125: }

E-mail: