variable.hpp

Go to the documentation of this file.
00001 /* $Id: variable.hpp 26694 2008-05-18 13:59:35Z mordante $ */
00002 /*
00003    Copyright (C) 2003 by David White <dave@whitevine.net>
00004    Copyright (C) 2005 - 2008 by Philippe Plantier <ayin@anathas.org>
00005 
00006    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License version 2
00010    or at your option any later version.
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY.
00013 
00014    See the COPYING file for more details.
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  * A variable-expanding proxy for the config class. This class roughly behaves
00032  * as a constant config object, but automatically expands variables.
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; /** < Synonym for operator[] */
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     /** In-order iteration over all children. */
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  * Used to for the functions in variable.cpp to locate the current global
00103  * variable repository
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 /** Information on a WML variable. */
00155 struct variable_info
00156 {
00157 typedef std::pair<std::vector<config*>::iterator, std::vector<config*>::iterator> array_range;
00158 public:
00159     /**
00160      * TYPE: the correct variable type should be decided by the user of the info structure
00161      * Note: an Array can also be considered a Container, since index 0 will be used by default
00162      */
00163     enum TYPE { TYPE_SCALAR,    //a Scalar variable resolves to a t_string attribute of *vars
00164                 TYPE_ARRAY,     //an Array variable is a series of Containers
00165                 TYPE_CONTAINER, //a Container is a specific index of an Array (contains Scalars)
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; //default is TYPE_UNSPECIFIED
00172     bool is_valid;
00173     std::string key; //the name of the internal attribute or child
00174     bool explicit_index; //true if query ended in [...] specifier
00175     size_t index; //the index of the child
00176     config *vars; //the containing node in game_state::variables
00177 
00178     /**
00179      * Results: after deciding the desired type, these methods can retrieve the result
00180      * Note: first you should force_valid or check is_valid, otherwise these may fail
00181      */
00182     t_string& as_scalar();
00183     config& as_container();
00184     array_range as_array(); //range may be empty
00185 };
00186 
00187 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs