ARB
awt_config_manager.hxx
Go to the documentation of this file.
1 // ==================================================================== //
2 // //
3 // File : awt_config_manager.hxx //
4 // Purpose : general interface to store/restore //
5 // a set of related awars //
6 // //
7 // //
8 // Coded by Ralf Westram (coder@reallysoft.de) in January 2002 //
9 // Copyright Department of Microbiology (Technical University Munich) //
10 // //
11 // Visit our web site at: http://www.arb-home.de/ //
12 // //
13 // //
14 // ==================================================================== //
15 
16 #ifndef AWT_CONFIG_MANAGER_HXX
17 #define AWT_CONFIG_MANAGER_HXX
18 
19 #ifndef CONFIGMAPPING_H
20 #include <ConfigMapping.h>
21 #endif
22 #ifndef AW_BASE_HXX
23 #include <aw_base.hxx>
24 #endif
25 #ifndef CB_H
26 #include <cb.h>
27 #endif
28 
29 #define awt_assert(cond) arb_assert(cond)
30 
31 struct ConfigMapping;
33 
35  const char *awar_name;
36  const char *config_name; // Note: this key defines the write-order during 'restore'; see also AWT_config_manager.cxx@write_to_awars
37 };
38 
40  const char *name; // of predefined config (has to begin with '*')
41  const char *description;
42  const char *config; // config string (as generated by AWT_config::config_string)
43 };
44 
45 // -------------------
46 // AWT_config
47 
48 class AWT_config : virtual Noncopyable {
49  // stores one specific configuration (key->value pairs)
50  //
51  // this class allows to modify the config_string before calling AWT_config_definition::write().
52  // This is e.g. necessary if some config-entries change and you want to support
53  // automatic conversion from old format to new format.
54 
55  ConfigMapping *mapping;
56  GB_ERROR parse_error; // set by AWT_config(const char *)
57 
58  void init_from_awars(const ConfigMapping& cfgname2awar);
59 
60 public:
61  AWT_config(const char *cfgStr);
62  AWT_config(const ConfigMapping& cfgname2awar); // internal use (reads current awar values)
63  AWT_config(const AWT_config_definition *cfg_def); // internal use (reads current awar values)
64  ~AWT_config();
65 
66  GB_ERROR parseError() const { return parse_error; }
67 
68  // props + modifiers
69  bool has_entry(const char *entry) const {
70  // returns true if mapping contains 'entry'
71  awt_assert(!parse_error);
72  return mapping->has_entry(entry);
73  }
74  const char *get_entry(const char *entry) const {
75  // returns value of 'entry'
76  awt_assert(!parse_error);
77  return mapping->get_entry(entry);
78  }
79  void set_entry(const char *entry, const char *value) {
80  // sets a (new) entry to value
81  awt_assert(!parse_error);
82  mapping->set_entry(entry, value);
83  }
84  void delete_entry(const char *entry) {
85  // deletes an existing 'entry'
86  awt_assert(!parse_error);
87  mapping->delete_entry(entry);
88  }
89 
90 
91  // result
92  char *config_string() const { // @@@ change result type to string?
93  // return current state as config string
94  awt_assert(!parse_error);
95  return ARB_strdup(mapping->config_string().c_str());
96  }
97  void get_entries(class ConstStrArray& to_array);
98 
99  void write_to_awars(const ConfigMapping& cfgname2awar, bool warn) const; // internal use (write config into awars)
100 };
101 
102 // ------------------------------
103 // AWT_config_definition
104 
106  ConfigMapping *config_mapping; // defines config-name -> awar-name relation
107 
108 public:
110  AWT_config_definition(AWT_config_mapping_def *mapping_definition); // simple definition
112 
113  void add(const char *awar_name, const char *config_name);
114  void add(const char *awar_name, const char *config_name, int counter);
115  void add(const AWT_config_mapping_def *mapping_definition);
116 
117  char *read() const; // awars -> config string (heap copy)
118  void write(const char *cfgStr) const; // config string -> awars (use to restore a saved configuration)
119  void reset() const; // reset awars to defaults
120 
121  const ConfigMapping& get_mapping() const { awt_assert(config_mapping); return *config_mapping; }
122 };
123 
124 // ----------------------------------------
125 // callbacks from config manager :
126 
127 DECLARE_CBTYPE_FVV_AND_BUILDERS(ConfigSetupCallback, void, AWT_config_definition&); // defines makeConfigSetupCallback
128 DECLARE_CBTYPE_VV_AND_BUILDERS (StoreConfigCallback, char*); // defines makeStoreConfigCallback
129 DECLARE_CBTYPE_FVV_AND_BUILDERS(RestoreConfigCallback, void, const char *); // defines makeRestoreConfigCallback
130 
131 // ----------------------------------
132 // the config manager itself
133 // adds button at cursor position when called (from a window generator function)
134 
135 void AWT_insert_config_manager(AW_window *aww, AW_default default_file_, const char *id, const StoreConfigCallback& store, const RestoreConfigCallback& load_or_reset, const char *macro_id = NULp, const AWT_predefined_config *predef = NULp);
136 void AWT_insert_config_manager(AW_window *aww, AW_default default_file_, const char *id, ConfigSetupCallback setup_cb, const char *macro_id = NULp, const AWT_predefined_config *predef = NULp);
137 void AWT_insert_config_manager(AW_window *aww, AW_default default_file_, const char *id, const AWT_config_mapping_def *mapping, const char *macro_id = NULp, const AWT_predefined_config *predef = NULp);
138 
139 // -------------------------------
140 // modify stored configs
141 
142 typedef char *(*ConfigModifyCallback)(const char *key, const char *value, AW_CL cl_user);
143 void AWT_modify_managed_configs(AW_default default_file_, const char *id, ConfigModifyCallback mod_cb, AW_CL cl_user);
144 
145 #else
146 #error awt_config_manager.hxx included twice
147 #endif // AWT_CONFIG_MANAGER_HXX
148 
void delete_entry(const char *entry)
void add(const char *awar_name, const char *config_name)
bool has_entry(const char *entry) const
void delete_entry(const char *entry)
Definition: ConfigMapping.h:49
void write_to_awars(const ConfigMapping &cfgname2awar, bool warn) const
char * ARB_strdup(const char *str)
Definition: arb_string.h:27
char *(* ConfigModifyCallback)(const char *key, const char *value, AW_CL cl_user)
GB_ERROR parseError() const
bool has_entry(const char *entry) const
Definition: ConfigMapping.h:35
std::string config_string() const
Definition: ConfigMapping.h:55
void AWT_insert_config_manager(AW_window *aww, AW_default default_file_, const char *id, const StoreConfigCallback &store, const RestoreConfigCallback &load_or_reset, const char *macro_id=NULp, const AWT_predefined_config *predef=NULp)
void AWT_modify_managed_configs(AW_default default_file_, const char *id, ConfigModifyCallback mod_cb, AW_CL cl_user)
DECLARE_CBTYPE_FVV_AND_BUILDERS(ConfigSetupCallback, void, AWT_config_definition &)
AWT_config(const char *cfgStr)
#define awt_assert(cond)
void set_entry(const std::string &entry, const std::string &value)
Definition: ConfigMapping.h:44
long AW_CL
Definition: cb.h:21
void get_entries(class ConstStrArray &to_array)
void set_entry(const char *entry, const char *value)
const char * get_entry(const char *entry) const
Definition: ConfigMapping.h:39
const ConfigMapping & get_mapping() const
#define NULp
Definition: cxxforward.h:97
const char * get_entry(const char *entry) const
DECLARE_CBTYPE_VV_AND_BUILDERS(StoreConfigCallback, char *)
char * config_string() const
void write(const char *cfgStr) const