1 | // ============================================================ // |
---|
2 | // // |
---|
3 | // File : aw_scalar.hxx // |
---|
4 | // Purpose : Scalar variables (similar to perl scalars) // |
---|
5 | // // |
---|
6 | // Coded by Ralf Westram (coder@reallysoft.de) in June 2011 // |
---|
7 | // Institute of Microbiology (Technical University Munich) // |
---|
8 | // http://www.arb-home.de/ // |
---|
9 | // // |
---|
10 | // ============================================================ // |
---|
11 | |
---|
12 | #ifndef AW_SCALAR_HXX |
---|
13 | #define AW_SCALAR_HXX |
---|
14 | |
---|
15 | #ifndef ARB_ASSERT_H |
---|
16 | #include <arb_assert.h> |
---|
17 | #endif |
---|
18 | #ifndef ARBTOOLS_H |
---|
19 | #include <arbtools.h> |
---|
20 | #endif |
---|
21 | #ifndef _GLIBCXX_CMATH |
---|
22 | #include <cmath> |
---|
23 | #endif |
---|
24 | #ifndef ARBDB_BASE_H |
---|
25 | #include <arbdb_base.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | |
---|
29 | #ifndef aw_assert |
---|
30 | # define aw_assert(cond) arb_assert(cond) |
---|
31 | #endif |
---|
32 | |
---|
33 | class AW_scalar { |
---|
34 | union { |
---|
35 | char *s; |
---|
36 | int32_t i; |
---|
37 | float f; |
---|
38 | GBDATA *p; |
---|
39 | } value; |
---|
40 | |
---|
41 | enum { INT, FLOAT, STR, PNTR } type; |
---|
42 | public: |
---|
43 | explicit AW_scalar(int32_t I) : type(INT) { value.i = I; } |
---|
44 | explicit AW_scalar(float F) : type(FLOAT) { value.f = F; } |
---|
45 | explicit AW_scalar(const char *S) : type(STR) { value.s = strdup(S); } |
---|
46 | explicit AW_scalar(GBDATA *P) : type(PNTR) { value.p = P; } |
---|
47 | explicit AW_scalar(class AW_awar *awar); |
---|
48 | |
---|
49 | explicit AW_scalar(const AW_scalar& other) |
---|
50 | : value(other.value), |
---|
51 | type(other.type) |
---|
52 | { |
---|
53 | if (type == STR) value.s = strdup(value.s); |
---|
54 | } |
---|
55 | ~AW_scalar() { if (type == STR) { free(value.s); } } |
---|
56 | |
---|
57 | DECLARE_ASSIGNMENT_OPERATOR(AW_scalar); |
---|
58 | |
---|
59 | int32_t get_int() const { aw_assert(type == INT); return value.i; } |
---|
60 | float get_float() const { aw_assert(type == FLOAT); return value.f; } |
---|
61 | const char *get_string() const { aw_assert(type == STR); return value.s; } |
---|
62 | GBDATA *get_pointer() const { aw_assert(type == PNTR); return value.p; } |
---|
63 | |
---|
64 | void set_int(int32_t I) { aw_assert(type == INT); value.i = I; } |
---|
65 | void set_float(float F) { aw_assert(type == FLOAT); value.f = F; } |
---|
66 | void set_string(const char *S) { aw_assert(type == STR); freedup(value.s, S); } |
---|
67 | void set_pointer(GBDATA *P) { aw_assert(type == PNTR); value.p = P; } |
---|
68 | |
---|
69 | GB_ERROR write_to(class AW_awar *awar); |
---|
70 | |
---|
71 | bool operator == (const AW_scalar& other) const { |
---|
72 | aw_assert(type == other.type); // type mismatch! |
---|
73 | bool equal = false; |
---|
74 | switch (type) { |
---|
75 | case INT: equal = (value.i == other.value.i); break; |
---|
76 | case FLOAT: equal = fabs(value.f-other.value.f)<0.000001; break; |
---|
77 | case STR: equal = strcmp(value.s, other.value.s) == 0; break; |
---|
78 | case PNTR: equal = (value.p == other.value.p); break; |
---|
79 | } |
---|
80 | return equal; |
---|
81 | } |
---|
82 | bool operator != (const AW_scalar& other) const { return !(*this == other); } |
---|
83 | }; |
---|
84 | |
---|
85 | |
---|
86 | #else |
---|
87 | #error aw_scalar.hxx included twice |
---|
88 | #endif // AW_SCALAR_HXX |
---|