| 1 | // ================================================================= // |
|---|
| 2 | // // |
|---|
| 3 | // File : aw_awar.hxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in September 2010 // |
|---|
| 7 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 8 | // http://www.arb-home.de/ // |
|---|
| 9 | // // |
|---|
| 10 | // ================================================================= // |
|---|
| 11 | |
|---|
| 12 | #ifndef AW_AWAR_HXX |
|---|
| 13 | #define AW_AWAR_HXX |
|---|
| 14 | |
|---|
| 15 | #ifndef AW_BASE_HXX |
|---|
| 16 | #include "aw_base.hxx" |
|---|
| 17 | #endif |
|---|
| 18 | #ifndef CB_H |
|---|
| 19 | #include <cb.h> |
|---|
| 20 | #endif |
|---|
| 21 | #ifndef ARBDB_BASE_H |
|---|
| 22 | #include <arbdb_base.h> |
|---|
| 23 | #endif |
|---|
| 24 | #ifndef ARB_ASSERT_H |
|---|
| 25 | #include <arb_assert.h> |
|---|
| 26 | #endif |
|---|
| 27 | #ifndef ATTRIBUTES_H |
|---|
| 28 | #include <attributes.h> |
|---|
| 29 | #endif |
|---|
| 30 | #ifndef ARBTOOLS_H |
|---|
| 31 | #include <arbtools.h> |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | // -------------- |
|---|
| 35 | // AWARS |
|---|
| 36 | |
|---|
| 37 | class AW_root_cblist; |
|---|
| 38 | struct AW_var_target; |
|---|
| 39 | struct AW_widget_refresh_cb; |
|---|
| 40 | |
|---|
| 41 | enum AW_widget_type { |
|---|
| 42 | AW_WIDGET_INPUT_FIELD, |
|---|
| 43 | AW_WIDGET_TEXT_FIELD, |
|---|
| 44 | AW_WIDGET_LABEL_FIELD, |
|---|
| 45 | AW_WIDGET_CHOICE_MENU, |
|---|
| 46 | AW_WIDGET_TOGGLE_FIELD, |
|---|
| 47 | AW_WIDGET_SELECTION_LIST, |
|---|
| 48 | AW_WIDGET_TOGGLE, |
|---|
| 49 | AW_WIDGET_SCALER, |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | class AW_awar : virtual Noncopyable { |
|---|
| 54 | struct { |
|---|
| 55 | struct { |
|---|
| 56 | float min; |
|---|
| 57 | float max; |
|---|
| 58 | } f; |
|---|
| 59 | const char *srt; |
|---|
| 60 | } pp; |
|---|
| 61 | |
|---|
| 62 | AW_root_cblist *callback_list; |
|---|
| 63 | AW_var_target *target_list; |
|---|
| 64 | AW_widget_refresh_cb *refresh_list; |
|---|
| 65 | |
|---|
| 66 | union { |
|---|
| 67 | char *s; |
|---|
| 68 | float f; |
|---|
| 69 | long l; |
|---|
| 70 | GBDATA *p; |
|---|
| 71 | } default_value; |
|---|
| 72 | |
|---|
| 73 | bool in_tmp_branch; |
|---|
| 74 | |
|---|
| 75 | static bool allowed_to_run_callbacks; |
|---|
| 76 | |
|---|
| 77 | double callback_time_sum; // in seconds |
|---|
| 78 | int callback_time_count; // number of callbacks traced in callback_time_sum |
|---|
| 79 | |
|---|
| 80 | #if defined(DEBUG) |
|---|
| 81 | bool is_global; |
|---|
| 82 | #endif // DEBUG |
|---|
| 83 | |
|---|
| 84 | void remove_all_callbacks(); |
|---|
| 85 | void remove_all_target_vars(); |
|---|
| 86 | |
|---|
| 87 | void assert_var_type(AW_VARIABLE_TYPE target_var_type); |
|---|
| 88 | |
|---|
| 89 | bool has_managed_tmp_state() const { return !in_tmp_branch && gb_origin; } |
|---|
| 90 | |
|---|
| 91 | void update_tmp_state_during_change(); |
|---|
| 92 | |
|---|
| 93 | public: |
|---|
| 94 | // read only |
|---|
| 95 | class AW_root *root; |
|---|
| 96 | |
|---|
| 97 | GBDATA *gb_var; // if unmapped, points to same DB elem as 'gb_origin' |
|---|
| 98 | GBDATA *gb_origin; // this is set ONCE on creation of awar |
|---|
| 99 | |
|---|
| 100 | // read only |
|---|
| 101 | |
|---|
| 102 | AW_VARIABLE_TYPE variable_type; // type of the awar |
|---|
| 103 | char *awar_name; // name of the awar |
|---|
| 104 | |
|---|
| 105 | #if defined(ASSERTION_USED) |
|---|
| 106 | static bool deny_read; // true -> make awar reads fail |
|---|
| 107 | static bool deny_write; // true -> make awar writes fail |
|---|
| 108 | #endif |
|---|
| 109 | |
|---|
| 110 | void unlink(); // unconditionally unlink from DB |
|---|
| 111 | |
|---|
| 112 | bool unlink_from_DB(GBDATA *gb_main); |
|---|
| 113 | |
|---|
| 114 | void run_callbacks(); |
|---|
| 115 | double mean_callback_time() const { |
|---|
| 116 | if (callback_time_sum>0) { |
|---|
| 117 | return callback_time_sum / callback_time_count; |
|---|
| 118 | } |
|---|
| 119 | return 0.0; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | void update_target(AW_var_target*pntr); |
|---|
| 123 | void update_targets(); |
|---|
| 124 | |
|---|
| 125 | AW_awar(AW_VARIABLE_TYPE var_type, const char *var_name, const char *var_value, float var_float_value, AW_default default_file, AW_root *root); |
|---|
| 126 | ~AW_awar(); |
|---|
| 127 | |
|---|
| 128 | void tie_widget(AW_CL cd1, Widget widget, AW_widget_type type, AW_window *aww); |
|---|
| 129 | void untie_all_widgets(); |
|---|
| 130 | |
|---|
| 131 | AW_awar *add_callback(const RootCallback& cb); |
|---|
| 132 | AW_awar *add_callback(RootCallbackSimple f) { return add_callback(makeRootCallback(f)); } |
|---|
| 133 | |
|---|
| 134 | AW_awar *remove_callback(const RootCallback& cb); |
|---|
| 135 | AW_awar *remove_callback(RootCallbackSimple f) { return remove_callback(makeRootCallback(f)); } |
|---|
| 136 | |
|---|
| 137 | AW_awar *add_target_var(char **ppchr); |
|---|
| 138 | AW_awar *add_target_var(long *pint); |
|---|
| 139 | AW_awar *add_target_var(float *pfloat); |
|---|
| 140 | void update(); // awar has changed |
|---|
| 141 | |
|---|
| 142 | AW_awar *set_minmax(float min, float max); // min<max !!! |
|---|
| 143 | float get_min() const; |
|---|
| 144 | float get_max() const; |
|---|
| 145 | AW_awar *set_min(float min) { return set_minmax(min, get_max()); } |
|---|
| 146 | AW_awar *set_max(float max) { return set_minmax(get_min(), max); } |
|---|
| 147 | AW_awar *set_srt(const char *srt); |
|---|
| 148 | |
|---|
| 149 | AW_awar *map(const char *awarn); |
|---|
| 150 | AW_awar *map(AW_default dest); // map to new address |
|---|
| 151 | AW_awar *map(AW_awar *dest); // map to new address |
|---|
| 152 | AW_awar *unmap(); // map to original address |
|---|
| 153 | bool is_mapped() const { return gb_var != gb_origin; } |
|---|
| 154 | |
|---|
| 155 | #if defined(ASSERTION_USED) |
|---|
| 156 | bool is_valid() const { return correlated(gb_var, gb_origin); } // both or none NULp |
|---|
| 157 | #endif // ASSERTION_USED |
|---|
| 158 | |
|---|
| 159 | void get(char **p_string) { freeset(*p_string, read_string()); } // deletes existing targets !!! |
|---|
| 160 | void get(long *p_int) { *p_int = (long)read_int(); } |
|---|
| 161 | void get(float *p_float) { *p_float = read_float(); } |
|---|
| 162 | |
|---|
| 163 | AW_VARIABLE_TYPE get_type() const; |
|---|
| 164 | |
|---|
| 165 | char *read_string() const; |
|---|
| 166 | const char *read_char_pntr() const; |
|---|
| 167 | char *read_as_string() const; |
|---|
| 168 | long read_int() const; |
|---|
| 169 | float read_float() const; |
|---|
| 170 | GBDATA *read_pointer() const; |
|---|
| 171 | |
|---|
| 172 | GB_ERROR write_string(const char *aw_string); |
|---|
| 173 | GB_ERROR write_as_string(const char *aw_string); |
|---|
| 174 | GB_ERROR write_int(long aw_int); |
|---|
| 175 | GB_ERROR write_float(float aw_float); |
|---|
| 176 | GB_ERROR write_pointer(GBDATA *aw_pointer); |
|---|
| 177 | |
|---|
| 178 | GB_ERROR write_as(char *aw_value) { return write_as_string(aw_value); }; |
|---|
| 179 | |
|---|
| 180 | // same as write_-versions above, but always touches the database field |
|---|
| 181 | GB_ERROR rewrite_string(const char *aw_string); |
|---|
| 182 | GB_ERROR rewrite_as_string(const char *aw_string); |
|---|
| 183 | GB_ERROR rewrite_int(long aw_int); |
|---|
| 184 | GB_ERROR rewrite_float(float aw_float); |
|---|
| 185 | GB_ERROR rewrite_pointer(GBDATA *aw_pointer); |
|---|
| 186 | |
|---|
| 187 | GB_ERROR rewrite_as(char *aw_value) { return rewrite_as_string(aw_value); }; |
|---|
| 188 | |
|---|
| 189 | GB_ERROR toggle_toggle(); // switches between 1/0 |
|---|
| 190 | void touch(); |
|---|
| 191 | |
|---|
| 192 | GB_ERROR reset_to_default(); |
|---|
| 193 | |
|---|
| 194 | GB_ERROR make_global() __ATTR__USERESULT; // should be used by ARB_init_global_awars only |
|---|
| 195 | void set_temp_if_is_default(GBDATA *gb_db); |
|---|
| 196 | }; |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | #else |
|---|
| 201 | #error aw_awar.hxx included twice |
|---|
| 202 | #endif // AW_AWAR_HXX |
|---|
| 203 | |
|---|