Annotation of parser3/src/types/pa_vdouble.h, revision 1.63
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.63 ! moko 11: #define IDENT_PA_VDOUBLE_H "$Id: pa_vdouble.h,v 1.62 2015/10/26 01:22:01 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.30 paf 50: /** VDouble: fdouble
51: */
1.43 paf 52: override const String* get_string() {
1.35 paf 53: char local_buf[MAX_NUMBER];
1.60 moko 54: size_t length=snprintf(local_buf, MAX_NUMBER, "%.15g", fdouble);
1.61 moko 55: return new String(pa_strdup(local_buf, length));
1.1 paf 56: }
1.11 paf 57: /// VDouble: fdouble
1.43 paf 58: override double as_double() const { return fdouble; }
1.21 parser 59: /// VDouble: fdouble
1.47 paf 60: override int as_int() const { return get_int(); }
1.11 paf 61: /// VDouble: 0 or !0
1.43 paf 62: override bool as_bool() const { return fdouble!=0; }
1.55 misha 63: /// VInt: json-string
1.59 moko 64: override const String* get_json_string(Json_options&) { return get_string(); }
1.1 paf 65:
66: public: // usage
67:
1.63 ! moko 68: VDouble(double adouble): fdouble(adouble) {
! 69: if(!pa_finite(adouble))
! 70: throw Exception("number.format", 0, pa_isnan(adouble) ? "invalid number (double)" : "out of range (double)");
! 71: }
1.1 paf 72:
1.47 paf 73: int get_int() const { return (int)trunc(fdouble); }
74: double get_double() const { return fdouble; }
75:
1.1 paf 76: void inc(double increment) { fdouble+=increment; }
1.58 moko 77:
1.7 paf 78: void mul(double k) { fdouble*=k; }
1.58 moko 79:
80: void div(double d) {
81: if(d == 0)
82: throw Exception("number.zerodivision", 0, "Division by zero");
83: fdouble/=d;
84: }
85:
86: void mod(int d) {
87: if(d == 0)
88: throw Exception("number.zerodivision", 0, "Modulus by zero");
89: fdouble=((int)fdouble)%d;
90: }
1.47 paf 91:
92: private:
93:
1.1 paf 94: double fdouble;
95:
96: };
97:
98: #endif
E-mail: