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