Mercurial > ~dholland > hg > ag > index.cgi
comparison tests/agcl/ffcalc/number.h @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 #ifndef NUMBER_H | |
2 #define NUMBER_H | |
3 | |
4 #include <stdio.h> | |
5 #include <assert.h> | |
6 | |
7 class Number { | |
8 double value; | |
9 int valid; | |
10 int *useCount; | |
11 int serialNumber; | |
12 static int nCreated; | |
13 static char buf[]; | |
14 void logCreation() {*useCount = 1; printf("Create: %d\n", serialNumber); fflush(stdout); | |
15 } | |
16 public: | |
17 Number() : valid(0), useCount(new int), serialNumber(nCreated++) {logCreation();} | |
18 Number(double d) : value(d), valid(1), useCount(new int), serialNumber(nCreated++) {logCreation();} | |
19 Number(const Number &n) | |
20 : value(n.value), | |
21 valid(n.valid), | |
22 useCount(n.useCount), | |
23 serialNumber(n.serialNumber) | |
24 { | |
25 (*useCount)++; | |
26 printf("Copy constructor: %s\n", n.asString()); | |
27 printf("Copy constructor: %s\n", asString()); | |
28 fflush(stdout); | |
29 } | |
30 ~Number() { | |
31 assert(*useCount > 0); | |
32 if (--(*useCount) == 0) printf("Delete %d\n", serialNumber); | |
33 else printf("Destructor: %s\n", asString()); | |
34 fflush(stdout); | |
35 } | |
36 char *asString() const { | |
37 char *vs = valid ? "valid" : "invalid"; | |
38 sprintf(buf, "%X: sn%d(%s) = %G, *%X = %d", this, serialNumber, vs, value, useCount, *useCount); | |
39 return buf; | |
40 } | |
41 Number &operator = (const Number &n) { | |
42 assert(*useCount > 0); | |
43 if (--(*useCount) == 0) printf("Delete %d\n", serialNumber); | |
44 fflush(stdout); | |
45 useCount = n.useCount; | |
46 value = n.value; | |
47 valid = n.valid; | |
48 serialNumber = n.serialNumber; | |
49 (*useCount)++; | |
50 return *this; | |
51 } | |
52 Number &operator += (const Number &n) {valid |= n.valid; value += n.value; return *this;} | |
53 Number &operator -= (const Number &n) {valid |= n.valid; value -= n.value; return *this;} | |
54 Number &operator *= (const Number &n) {valid |= n.valid; value *= n.value; return *this;} | |
55 Number &operator /= (const Number &n) {valid |= n.valid; if(valid) value /= n.value; return *this;} | |
56 Number &operator -() {value = -value; return *this;} | |
57 operator char * () { | |
58 if (valid) { | |
59 sprintf(buf, "%G", value); | |
60 printf("operator char * = %g\n", value); | |
61 return buf; | |
62 } | |
63 else return "Invalid"; | |
64 } | |
65 }; | |
66 | |
67 #endif |