Equalizer
1.2.1
|
00001 00002 /* Copyright (c) 2007-2012, Stefan Eilemann <eile@equalizergraphics.com> 00003 * 00004 * This library is free software; you can redistribute it and/or modify it under 00005 * the terms of the GNU Lesser General Public License version 2.1 as published 00006 * by the Free Software Foundation. 00007 * 00008 * This library is distributed in the hope that it will be useful, but WITHOUT 00009 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00010 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00011 * details. 00012 * 00013 * You should have received a copy of the GNU Lesser General Public License 00014 * along with this library; if not, write to the Free Software Foundation, Inc., 00015 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00016 */ 00017 00018 #ifndef COBASE_RNG_H 00019 #define COBASE_RNG_H 00020 00021 #include <co/base/debug.h> // for EQASSERT 00022 #include <co/base/init.h> // friend function 00023 #include <co/base/nonCopyable.h> 00024 #include <co/base/types.h> 00025 00026 #pragma warning (push) 00027 #pragma warning (disable: 4985) // inconsistent decl of ceil 00028 00029 #ifdef _WIN32 00030 # ifndef NOMINMAX 00031 # define NOMINMAX 00032 # endif 00033 # include <wtypes.h> 00034 # include <wincrypt.h> 00035 # pragma comment(lib, "advapi32.lib") 00036 #else 00037 # include <unistd.h> 00038 #endif 00039 00040 #include <fcntl.h> 00041 #include <limits> 00042 #include <stdio.h> 00043 #pragma warning (pop) 00044 00045 namespace co 00046 { 00047 namespace base 00048 { 00055 class RNG : public NonCopyable 00056 { 00057 public: 00059 RNG() { _init(); } 00060 00062 ~RNG() {} 00063 00065 void reseed() 00066 { 00067 #ifdef Darwin 00068 srandomdev(); 00069 #endif 00070 } 00071 00080 template< typename T > T get() 00081 { 00082 T value; 00083 #ifdef Linux 00084 EQASSERTINFO( _fd >= 0, "init() not called?" ); 00085 int read = ::read( _fd, &value, sizeof( T )); 00086 EQASSERTINFO( read == sizeof(T), 00087 read << " != " << sizeof( T ) << ": " << sysError ); 00088 if( read != sizeof( T )) 00089 { 00090 EQERROR << "random number generator not working" << std::endl; 00091 return 0; 00092 } 00093 00094 #elif defined (_WIN32) 00095 EQASSERTINFO( _provider, "init() not called?" ); 00096 if( !CryptGenRandom( _provider, sizeof( T ), (BYTE*)&value )) 00097 { 00098 EQASSERTINFO( false, "random number generator not working: " << 00099 sysError ); 00100 } 00101 #else // Darwin 00102 uint8_t* bytes = reinterpret_cast< uint8_t* >( &value ); 00103 for( size_t i=0; i<sizeof( T ); ++i ) 00104 bytes[i] = ( random() & 0xff ); 00105 #endif 00106 return value; 00107 } 00108 00109 private: 00110 #ifdef Linux 00111 static int _fd; 00112 #elif defined (_WIN32) 00113 static COBASE_API HCRYPTPROV _provider; 00114 #endif 00115 static COBASE_API bool _init(); 00116 static void _exit(); 00117 friend COBASE_API bool init( const int argc, char** argv ); 00118 }; 00119 00120 template<> inline float RNG::get() 00121 { 00122 const float max_limits = 00123 static_cast< float >( std::numeric_limits< uint32_t >::max( )); 00124 return ( get< uint32_t >() / max_limits); 00125 } 00126 00127 template<> inline double RNG::get() 00128 { 00129 const double max_limits = 00130 static_cast< double >( std::numeric_limits< uint64_t >::max( )); 00131 return ( get< uint64_t >() / max_limits); 00132 } 00133 00134 template<> inline bool RNG::get() 00135 { 00136 return ( get< uint32_t >() & 1 ); 00137 } 00138 } 00139 } 00140 #endif // COBASE_RNG_H