Annotation of parser3/src/types/pa_vdouble.h, revision 1.62

1.11      paf         1: /** @file
1.16      paf         2:        Parser: @b double parser class decl.
1.11      paf         3: 
1.62    ! moko        4:        Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.32      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_VDOUBLE_H
                      9: #define PA_VDOUBLE_H
1.38      paf        10: 
1.62    ! moko       11: #define IDENT_PA_VDOUBLE_H "$Id: pa_vdouble.h,v 1.61 2015/04/06 22:27:27 moko Exp $"
1.43      paf        12: 
                     13: // includes
1.1       paf        14: 
1.19      paf        15: #include "classes.h"
1.9       paf        16: #include "pa_common.h"
1.5       paf        17: #include "pa_vstateless_object.h"
1.17      paf        18: 
1.53      misha      19: // defines
                     20: #define VDOUBLE_TYPE "double"
                     21: 
1.43      paf        22: // externs
                     23: 
                     24: extern Methoded* double_class;
1.1       paf        25: 
1.11      paf        26: /// value of type 'double'. implemented with @c double
1.43      paf        27: class VDouble: public VStateless_object {
1.1       paf        28: public: // Value
                     29: 
1.53      misha      30:        override const char* type() const { return VDOUBLE_TYPE; }
1.43      paf        31:        override VStateless_class *get_class() { return double_class; }
1.37      paf        32: 
1.48      paf        33:        /// VDouble: true
1.50      paf        34:        override bool is_evaluated_expr() const { return true; }
1.24      parser     35:        /// VDouble: clone
1.57      moko       36:        override Value& as_expr_result() { return *new VDouble(fdouble); }
1.1       paf        37: 
1.30      paf        38:        /** VDouble: fdouble 
                     39:        */
1.43      paf        40:        override const String* get_string() {
1.35      paf        41:                char local_buf[MAX_NUMBER];
1.60      moko       42:                size_t length=snprintf(local_buf, MAX_NUMBER, "%.15g", fdouble);
1.61      moko       43:                return new String(pa_strdup(local_buf, length));
1.1       paf        44:        }
1.11      paf        45:        /// VDouble: fdouble
1.43      paf        46:        override double as_double() const { return fdouble; }
1.21      parser     47:        /// VDouble: fdouble
1.47      paf        48:        override int as_int() const { return get_int(); }
1.11      paf        49:        /// VDouble: 0 or !0
1.43      paf        50:        override bool as_bool() const { return fdouble!=0; }
1.55      misha      51:        /// VInt: json-string
1.59      moko       52:        override const String* get_json_string(Json_options&) { return get_string(); }
1.1       paf        53: 
                     54: public: // usage
                     55: 
1.43      paf        56:        VDouble(double adouble): fdouble(adouble) {}
1.1       paf        57: 
1.47      paf        58:        int get_int() const { return (int)trunc(fdouble); }
                     59:        double get_double() const { return fdouble; }
                     60: 
1.1       paf        61:        void inc(double increment) { fdouble+=increment; }
1.58      moko       62:        
1.7       paf        63:        void mul(double k) { fdouble*=k; }
1.58      moko       64:        
                     65:        void div(double d) {
                     66:                if(d == 0)
                     67:                        throw Exception("number.zerodivision", 0, "Division by zero");
                     68:                fdouble/=d; 
                     69:        }
                     70:        
                     71:        void mod(int d) {
                     72:                if(d == 0)
                     73:                        throw Exception("number.zerodivision", 0, "Modulus by zero");
                     74:                fdouble=((int)fdouble)%d;
                     75:        }
1.47      paf        76: 
                     77: private:
                     78: 
1.1       paf        79:        double fdouble;
                     80: 
                     81: };
                     82: 
                     83: #endif

E-mail: