completes int_generator policies

This commit is contained in:
Peter Thorson
2013-03-02 20:47:01 -06:00
parent ea4e66e14b
commit b373a388e7
2 changed files with 37 additions and 4 deletions

View File

@@ -28,5 +28,31 @@
#ifndef WEBSOCKETPP_RANDOM_NONE_HPP
#define WEBSOCKETPP_RANDOM_NONE_HPP
namespace websocketpp {
namespace random {
namespace none {
/// Thread safe stub "random" integer generator.
/**
* This template class provides a random integer stub. The interface mimics the
* WebSocket++ RNG generator classes but the generater function always returns
* zero. This can be used to stub out the RNG for unit and performance testing.
*
* Call operator() to generate the next number
*/
template <typename int_type>
class int_generator {
public:
int_generator() {}
/// advances the engine's state and returns the generated value
int_type operator()() {
return 0;
}
};
} // namespace none
} // namespace random
} // namespace websocketpp
#endif //WEBSOCKETPP_RANDOM_NONE_HPP

View File

@@ -32,6 +32,7 @@
namespace websocketpp {
namespace random {
namespace random_device {
/// Thread safe non-deterministic random integer generator.
/**
@@ -50,22 +51,28 @@ namespace random {
template <typename int_type, typename concurrency>
class int_generator {
public:
typedef typename concurrency::scoped_lock_type scoped_lock_type;
typedef typename concurrency::mutex_type mutex_type;
/// constructor
//mac TODO: figure out if signed types present a range problem
random_device() : m_dis() {}
int_generator() : m_dis() {}
/// advances the engine's state and returns the generated value
int_type operator()() {
concurrency::scoped_lock_type(m_lock);
scoped_lock_type guard(m_lock);
return m_dis(m_rng);
}
private:
lib::random_device m_rng;
lib::uniform_int_distribution<int_type> m_dis;
concurrency::mutex_type m_lock;
mutex_type m_lock;
};
} // namespace random_device
} // namespace random
} // namespace websocketpp