| 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 | #ifndef ARB_STRING_H |
|---|
| 28 | #include <arb_string.h> |
|---|
| 29 | #endif |
|---|
| 30 | #ifndef AW_BASE_HXX |
|---|
| 31 | #include "aw_base.hxx" |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | #ifndef aw_assert |
|---|
| 36 | # define aw_assert(cond) arb_assert(cond) |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | class AW_scalar { |
|---|
| 40 | union { |
|---|
| 41 | char *s; |
|---|
| 42 | int32_t i; |
|---|
| 43 | float f; |
|---|
| 44 | GBDATA *p; |
|---|
| 45 | } value; |
|---|
| 46 | |
|---|
| 47 | enum { INT, FLOAT, STR, PNTR } type; |
|---|
| 48 | static const int SCALAR_TYPES = 4; |
|---|
| 49 | |
|---|
| 50 | static const char * const type_name[SCALAR_TYPES]; |
|---|
| 51 | static const AW_VARIABLE_TYPE vtype[SCALAR_TYPES]; |
|---|
| 52 | |
|---|
| 53 | public: |
|---|
| 54 | explicit AW_scalar(int32_t I) : type(INT) { value.i = I; } |
|---|
| 55 | explicit AW_scalar(float F) : type(FLOAT) { value.f = F; } |
|---|
| 56 | explicit AW_scalar(const char *S) : type(STR) { value.s = ARB_strdup(S); } |
|---|
| 57 | explicit AW_scalar(GBDATA *P) : type(PNTR) { value.p = P; } |
|---|
| 58 | explicit AW_scalar(class AW_awar *awar); |
|---|
| 59 | |
|---|
| 60 | AW_scalar(const AW_scalar& other) |
|---|
| 61 | : value(other.value), |
|---|
| 62 | type(other.type) |
|---|
| 63 | { |
|---|
| 64 | if (type == STR) value.s = ARB_strdup(value.s); |
|---|
| 65 | } |
|---|
| 66 | ~AW_scalar() { if (type == STR) { free(value.s); } } |
|---|
| 67 | |
|---|
| 68 | DECLARE_ASSIGNMENT_OPERATOR(AW_scalar); |
|---|
| 69 | |
|---|
| 70 | int32_t get_int() const { aw_assert(type == INT); return value.i; } |
|---|
| 71 | float get_float() const { aw_assert(type == FLOAT); return value.f; } |
|---|
| 72 | const char *get_string() const { aw_assert(type == STR); return value.s; } |
|---|
| 73 | GBDATA *get_pointer() const { aw_assert(type == PNTR); return value.p; } |
|---|
| 74 | |
|---|
| 75 | void set_int(int32_t I) { aw_assert(type == INT); value.i = I; } |
|---|
| 76 | void set_float(float F) { aw_assert(type == FLOAT); value.f = F; } |
|---|
| 77 | void set_string(const char *S) { aw_assert(type == STR); freedup(value.s, S); } |
|---|
| 78 | void set_pointer(GBDATA *P) { aw_assert(type == PNTR); value.p = P; } |
|---|
| 79 | |
|---|
| 80 | const char *get_type_name() const { return type_name[type]; } |
|---|
| 81 | AW_VARIABLE_TYPE matching_variable_type() const { return vtype[type]; } |
|---|
| 82 | bool matches_variable_type(AW_VARIABLE_TYPE atype) const { return matching_variable_type() == atype; } |
|---|
| 83 | |
|---|
| 84 | GB_ERROR write_to(class AW_awar *awar) const; |
|---|
| 85 | |
|---|
| 86 | bool operator == (const AW_scalar& other) const { |
|---|
| 87 | aw_assert(type == other.type); // type mismatch! |
|---|
| 88 | bool equal = false; |
|---|
| 89 | switch (type) { |
|---|
| 90 | case INT: equal = (value.i == other.value.i); break; |
|---|
| 91 | case FLOAT: equal = fabs(value.f-other.value.f)<0.000001; break; |
|---|
| 92 | case STR: equal = strcmp(value.s, other.value.s) == 0; break; |
|---|
| 93 | case PNTR: equal = (value.p == other.value.p); break; |
|---|
| 94 | } |
|---|
| 95 | return equal; |
|---|
| 96 | } |
|---|
| 97 | bool operator != (const AW_scalar& other) const { return !(*this == other); } |
|---|
| 98 | }; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | #else |
|---|
| 102 | #error aw_scalar.hxx included twice |
|---|
| 103 | #endif // AW_SCALAR_HXX |
|---|