00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef VARIABLE_H_INCLUDED
00017 #define VARIABLE_H_INCLUDED
00018
00019 #include "config.hpp"
00020 #include "tstring.hpp"
00021
00022 #include <vector>
00023 #include <set>
00024 #include <string>
00025
00026 class game_state;
00027 class unit_map;
00028
00029
00030
00031
00032
00033
00034 class vconfig
00035 {
00036 public:
00037 vconfig();
00038 vconfig(const vconfig& v);
00039 vconfig(const config* cfg, const config* cache_key=NULL);
00040 ~vconfig();
00041
00042 vconfig& operator=(const vconfig cfg);
00043 vconfig& operator=(const config* cfg);
00044
00045 bool null() const { return cfg_ == NULL; }
00046 const config& get_config() const { return *cfg_; }
00047 const config get_parsed_config() const;
00048
00049 typedef std::vector<vconfig> child_list;
00050 child_list get_children(const std::string& key) const;
00051 vconfig child(const std::string& key) const;
00052 bool has_child(const std::string& key) const;
00053
00054 const t_string expand(const std::string&) const;
00055 const t_string operator[](const std::string& key) const { return expand(key); }
00056 const t_string& get_attribute(const std::string& key) const { return (*cfg_)[key]; }
00057 bool has_attribute(const std::string& key) const { return cfg_->has_attribute(key); }
00058 bool empty() const { return (null() || cfg_->empty()); }
00059
00060 struct all_children_iterator {
00061 typedef std::pair<const std::string, const vconfig> value_type;
00062 typedef std::forward_iterator_tag iterator_category;
00063 typedef int difference_type;
00064 typedef std::auto_ptr<value_type> pointer;
00065 typedef value_type& reference;
00066 typedef config::all_children_iterator Itor;
00067 explicit all_children_iterator(Itor i=Itor());
00068
00069 all_children_iterator& operator++();
00070 all_children_iterator operator++(int);
00071
00072 value_type operator*() const;
00073 pointer operator->() const;
00074
00075 const std::string get_key() const;
00076 size_t get_index() const;
00077 const vconfig get_child() const;
00078
00079 bool operator==(all_children_iterator i) const;
00080 bool operator!=(all_children_iterator i) const;
00081
00082 private:
00083 Itor i_;
00084 unsigned inner_index_;
00085 unsigned index_offset_;
00086 };
00087
00088
00089 all_children_iterator ordered_begin() const;
00090 all_children_iterator ordered_end() const;
00091
00092 private:
00093 const config* cfg_;
00094 const config* cache_key_;
00095 mutable std::set<std::string> recursion_;
00096 };
00097
00098 namespace variable
00099 {
00100
00101
00102
00103
00104
00105 class manager
00106 {
00107 public:
00108 manager(game_state* repository);
00109 ~manager();
00110 };
00111
00112 }
00113
00114
00115
00116 class scoped_wml_variable
00117 {
00118 public:
00119 scoped_wml_variable(const std::string& var_name);
00120 virtual ~scoped_wml_variable();
00121 const std::string& name() const { return var_name_; }
00122 virtual void activate() = 0;
00123 void store(const config& var_value);
00124 bool activated() const { return activated_; }
00125 private:
00126 config previous_val_;
00127 const std::string var_name_;
00128 bool activated_;
00129 };
00130
00131 class scoped_xy_unit : public scoped_wml_variable
00132 {
00133 public:
00134 scoped_xy_unit(const std::string& var_name, const int x, const int y, const unit_map& umap)
00135 : scoped_wml_variable(var_name), x_(x), y_(y), umap_(umap) {}
00136 void activate();
00137 private:
00138 const int x_, y_;
00139 const unit_map& umap_;
00140 };
00141
00142 class scoped_recall_unit : public scoped_wml_variable
00143 {
00144 public:
00145 scoped_recall_unit(const std::string& var_name, const std::string& player,
00146 unsigned int recall_index) : scoped_wml_variable(var_name), player_(player),
00147 recall_index_(recall_index) {}
00148 void activate();
00149 private:
00150 const std::string player_;
00151 unsigned int recall_index_;
00152 };
00153
00154
00155 struct variable_info
00156 {
00157 typedef std::pair<std::vector<config*>::iterator, std::vector<config*>::iterator> array_range;
00158 public:
00159
00160
00161
00162
00163 enum TYPE { TYPE_SCALAR,
00164 TYPE_ARRAY,
00165 TYPE_CONTAINER,
00166 TYPE_UNSPECIFIED };
00167
00168 variable_info(const std::string& varname, bool force_valid=true,
00169 TYPE validation_type=TYPE_UNSPECIFIED);
00170
00171 TYPE vartype;
00172 bool is_valid;
00173 std::string key;
00174 bool explicit_index;
00175 size_t index;
00176 config *vars;
00177
00178
00179
00180
00181
00182 t_string& as_scalar();
00183 config& as_container();
00184 array_range as_array();
00185 };
00186
00187 #endif