rng.hpp

Go to the documentation of this file.
00001 /* $Id: rng.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2003 by David White <dave@whitevine.net>
00004    Copyright (C) 2005 - 2012 by Yann Dirson <ydirson@altern.org>
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 #ifndef RNG_HPP_INCLUDED
00017 #define RNG_HPP_INCLUDED
00018 
00019 #include <cstddef>
00020 
00021 #include "random.hpp"
00022 #include "simple_rng.hpp"
00023 
00024 #include <boost/function.hpp>
00025 
00026 namespace rand_rng
00027 {
00028 
00029 class rng
00030 {
00031 public:
00032     rng();
00033     /**
00034      * Get the next random number -- from the results if available, from the
00035      * generator otherwise. The two should match.
00036      */
00037     int get_random();
00038 
00039     /**
00040      * Get the next random number.
00041      * Do not check if the random number is consistent with local seed state
00042      * (evantually this should be never used).
00043      */
00044     int get_random_nocheck();
00045 
00046     const config* get_random_results();
00047     void set_random_results(const config& cfg);
00048 
00049     void set_seed(int seed);
00050 
00051 protected:
00052     int get_random_private(bool check);
00053     config* random();
00054     void set_random(config*);
00055 
00056 private:
00057     config* random_;
00058     size_t random_child_;
00059 
00060     simple_rng generator_;
00061 };
00062 
00063 /**
00064  * Set the random seed for the current random number generator, sets the seed
00065  * as valid nd calls the new seed callback if set
00066  */
00067 void set_seed(int seed);
00068 
00069 /**
00070  * Mark the RNG seed as invalid
00071  */
00072 void invalidate_seed();
00073 
00074 /**
00075  * Function to check whether the RNG has been updated with a new seed since
00076  * the las invalidate_seed() call
00077  */
00078 bool has_valid_seed();
00079 
00080 /**
00081  * Get the last seed the RNG was seeded with in set_seed
00082  */
00083 int get_last_seed();
00084 
00085 /**
00086  * Set the callback for a function that will be called on subsequent set_seed
00087  * calls.
00088  * @todo needs a reliable way of clearing the callback when things don't go as
00089  * normal (e.g. player quit the game while the callback is set)
00090  */
00091 void set_new_seed_callback(boost::function<void (int)> f);
00092 
00093 /** @return true if the seed callback is not NULL */
00094 bool has_new_seed_callback();
00095 
00096 /**
00097  * Clear the new seed callback
00098  */
00099 void clear_new_seed_callback();
00100 
00101 } // ends rand_rng namespace
00102 
00103 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:08 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs