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

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.64    ! moko       11: #define IDENT_PA_VDOUBLE_H "$Id: pa_vdouble.h,v 1.63 2015/11/16 18:27:15 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
1.63      moko       20: 
1.53      misha      21: #define VDOUBLE_TYPE "double"
                     22: 
1.63      moko       23: // double validation defines
                     24: 
                     25: #if _MSC_VER
                     26: #include <float.h>
                     27: #define pa_isnan(d) _isnan(d)
                     28: #define pa_finite(d) _finite(d)
                     29: #else
                     30: #define pa_isnan(d) isnan(d)
                     31: #define pa_finite(d) finite(d)
                     32: #endif
                     33: 
1.43      paf        34: // externs
                     35: 
                     36: extern Methoded* double_class;
1.1       paf        37: 
1.11      paf        38: /// value of type 'double'. implemented with @c double
1.43      paf        39: class VDouble: public VStateless_object {
1.1       paf        40: public: // Value
                     41: 
1.53      misha      42:        override const char* type() const { return VDOUBLE_TYPE; }
1.43      paf        43:        override VStateless_class *get_class() { return double_class; }
1.37      paf        44: 
1.48      paf        45:        /// VDouble: true
1.50      paf        46:        override bool is_evaluated_expr() const { return true; }
1.24      parser     47:        /// VDouble: clone
1.57      moko       48:        override Value& as_expr_result() { return *new VDouble(fdouble); }
1.1       paf        49: 
1.64    ! moko       50:        /// VDouble: fdouble
1.43      paf        51:        override const String* get_string() {
1.35      paf        52:                char local_buf[MAX_NUMBER];
1.60      moko       53:                size_t length=snprintf(local_buf, MAX_NUMBER, "%.15g", fdouble);
1.61      moko       54:                return new String(pa_strdup(local_buf, length));
1.1       paf        55:        }
1.11      paf        56:        /// VDouble: fdouble
1.43      paf        57:        override double as_double() const { return fdouble; }
1.21      parser     58:        /// VDouble: fdouble
1.47      paf        59:        override int as_int() const { return get_int(); }
1.11      paf        60:        /// VDouble: 0 or !0
1.43      paf        61:        override bool as_bool() const { return fdouble!=0; }
1.55      misha      62:        /// VInt: json-string
1.59      moko       63:        override const String* get_json_string(Json_options&) { return get_string(); }
1.1       paf        64: 
                     65: public: // usage
                     66: 
1.63      moko       67:        VDouble(double adouble): fdouble(adouble) {
                     68:                if(!pa_finite(adouble))
                     69:                        throw Exception("number.format", 0, pa_isnan(adouble) ? "invalid number (double)" : "out of range (double)");
                     70:        }
1.1       paf        71: 
1.47      paf        72:        int get_int() const { return (int)trunc(fdouble); }
                     73:        double get_double() const { return fdouble; }
                     74: 
1.1       paf        75:        void inc(double increment) { fdouble+=increment; }
1.58      moko       76:        
1.7       paf        77:        void mul(double k) { fdouble*=k; }
1.58      moko       78:        
                     79:        void div(double d) {
                     80:                if(d == 0)
                     81:                        throw Exception("number.zerodivision", 0, "Division by zero");
                     82:                fdouble/=d; 
                     83:        }
                     84:        
                     85:        void mod(int d) {
                     86:                if(d == 0)
                     87:                        throw Exception("number.zerodivision", 0, "Modulus by zero");
                     88:                fdouble=((int)fdouble)%d;
                     89:        }
1.47      paf        90: 
                     91: private:
                     92: 
1.1       paf        93:        double fdouble;
                     94: 
                     95: };
                     96: 
                     97: #endif

E-mail: