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

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

E-mail: