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

1.11      paf         1: /** @file
1.16      paf         2:        Parser: @b double parser class decl.
1.11      paf         3: 
1.56    ! moko        4:        Copyright (c) 2001-2012 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.56    ! moko       11: #define IDENT_PA_VDOUBLE_H "$Id: 2010-09-16 23:33:52 $"
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.43      paf        36:        override Value& as_expr_result(bool ) { 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.47      paf        42:                size_t length=snprintf(local_buf, MAX_NUMBER, has_frac()? "%g": "%.0f", fdouble);
1.54      misha      43:                return new String(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
                     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.7       paf        62:        void mul(double k) { fdouble*=k; }
                     63:        void div(double d) { fdouble/=d; }
                     64:        void mod(int d) { fdouble=((int)fdouble)%d; }
1.47      paf        65: 
                     66: private:
                     67: 
                     68:        bool has_frac() {
1.52      misha      69:                return fabs(fdouble-trunc(fdouble))>1e-100;
1.47      paf        70:        }
1.1       paf        71: 
                     72: private:
                     73: 
                     74:        double fdouble;
                     75: 
                     76: };
                     77: 
                     78: #endif

E-mail: