ai/composite/engine.hpp

Go to the documentation of this file.
00001 
00002 /* $Id: engine.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00003 /*
00004    Copyright (C) 2009 - 2012 by Yurii Chernyi <terraninfo@terraninfo.net>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (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 
00017 /**
00018  * AI Support engine - creating specific ai components from config
00019  * @file
00020  */
00021 
00022 #ifndef AI_COMPOSITE_ENGINE_HPP_INCLUDED
00023 #define AI_COMPOSITE_ENGINE_HPP_INCLUDED
00024 
00025 #include "component.hpp"
00026 #include "../contexts.hpp"
00027 
00028 #include <algorithm>
00029 #include <iterator>
00030 
00031 //============================================================================
00032 
00033 namespace ai {
00034 
00035 class rca_context;
00036 class ai_context;
00037 class component;
00038 
00039 class engine : public component {
00040 public:
00041     engine( readonly_context &context, const config &cfg );
00042 
00043 
00044     virtual ~engine();
00045 
00046 
00047     static void parse_aspect_from_config( readonly_context &context, const config &cfg, const std::string &id, std::back_insert_iterator<std::vector< aspect_ptr > > b );
00048 
00049 
00050     static void parse_goal_from_config( readonly_context &context, const config &cfg, std::back_insert_iterator<std::vector< goal_ptr > > b );
00051 
00052 
00053     static void parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr > > b );
00054 
00055 
00056     static void parse_engine_from_config( readonly_context &context, const config &cfg, std::back_insert_iterator<std::vector< engine_ptr > > b );
00057 
00058 
00059     static void parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b );
00060 
00061 
00062     //do not override that method in subclasses which cannot create aspects
00063     virtual void do_parse_aspect_from_config( const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr> > b );
00064 
00065 
00066     //do not override that method in subclasses which cannot create candidate_actions
00067     virtual void do_parse_candidate_action_from_config( rca_context &context, const config &cfg, std::back_insert_iterator<std::vector< candidate_action_ptr > > b );
00068 
00069     //do not override that method in subclasses which cannot create goals
00070     virtual void do_parse_goal_from_config( const config &cfg, std::back_insert_iterator<std::vector< goal_ptr > > b );
00071 
00072     //do not override that method in subclasses which cannot create engines
00073     virtual void do_parse_engine_from_config( const config &cfg, std::back_insert_iterator<std::vector< engine_ptr > > b );
00074 
00075 
00076     //do not override that method in subclasses which cannot create stages
00077     virtual void do_parse_stage_from_config( ai_context &context, const config &cfg, std::back_insert_iterator<std::vector< stage_ptr > > b );
00078 
00079     //do not override that method in subclasses which cannot evaluate formulas
00080     virtual std::string evaluate(const std::string& str);
00081 
00082     readonly_context& get_readonly_context();
00083 
00084     /**
00085      * set ai context (which is not available during early initialization)
00086      */
00087     virtual void set_ai_context(ai_context_ptr context);
00088 
00089     virtual ai_context_ptr get_ai_context();
00090     /**
00091      * serialize
00092      */
00093     virtual config to_config() const;
00094 
00095 
00096     virtual std::string get_id() const
00097     { return id_; }
00098 
00099     virtual std::string get_engine() const
00100     { return engine_; }
00101 
00102     virtual std::string get_name() const
00103     { return name_; }
00104 
00105 protected:
00106     readonly_context &ai_;
00107     ai_context_ptr ai_context_;
00108 
00109     /** name of the engine which has created this engine*/
00110     std::string engine_;
00111     std::string id_;
00112     std::string name_;
00113 };
00114 
00115 
00116 class engine_factory;
00117 
00118 class engine_factory{
00119 public:
00120     typedef boost::shared_ptr< engine_factory > factory_ptr;
00121     typedef std::map<std::string, factory_ptr> factory_map;
00122     typedef std::pair<const std::string, factory_ptr> factory_map_pair;
00123 
00124     static factory_map& get_list() {
00125         static factory_map *engine_factories;
00126         if (engine_factories==NULL) {
00127             engine_factories = new factory_map;
00128         }
00129         return *engine_factories;
00130     }
00131 
00132     virtual engine_ptr get_new_instance( readonly_context &ai, const config &cfg ) = 0;
00133     virtual engine_ptr get_new_instance( readonly_context &ai, const std::string& name ) = 0;
00134 
00135     engine_factory( const std::string &name )
00136     {
00137         factory_ptr ptr_to_this(this);
00138         get_list().insert(make_pair(name,ptr_to_this));
00139     }
00140 
00141     virtual ~engine_factory() {}
00142 };
00143 
00144 
00145 template<class ENGINE>
00146 class register_engine_factory : public engine_factory {
00147 public:
00148     register_engine_factory( const std::string &name )
00149         : engine_factory( name )
00150     {
00151     }
00152 
00153     virtual engine_ptr get_new_instance( readonly_context &ai, const config &cfg ){
00154         return engine_ptr(new ENGINE(ai,cfg));
00155     }
00156 
00157     virtual engine_ptr get_new_instance( readonly_context &ai, const std::string& name ){
00158         config cfg;
00159         cfg["name"] = name;
00160         cfg["engine"] = "cpp";
00161         return engine_ptr(new ENGINE(ai,cfg));
00162     }
00163 };
00164 
00165 } //end of namespace ai
00166 
00167 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:28 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs