Equalizer 1.0

rng.h

00001 
00002 /* Copyright (c) 2007-2011, 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 functions
00023 #include <co/base/nonCopyable.h>
00024 #include <co/base/types.h>
00025 
00026 #ifdef _WIN32
00027 #  ifndef NOMINMAX
00028 #    define NOMINMAX
00029 #  endif
00030 #  include <wtypes.h>
00031 #  include <WinCrypt.h>
00032 #  pragma comment(lib, "advapi32.lib")
00033 #endif
00034 
00035 #include <fcntl.h>
00036 #include <limits>
00037 #include <stdio.h>
00038 
00039 namespace co
00040 {
00041 namespace base
00042 {
00049     class RNG : public NonCopyable
00050     {
00051     public:
00053         RNG() { reseed(); }
00054 
00056         ~RNG() {}
00057 
00059         void reseed()
00060         {
00061 #ifdef Darwin
00062             srandomdev();
00063 #endif
00064         }
00065 
00074         template< typename T > T get()
00075         {
00076             T value;
00077 #ifdef Linux
00078             EQASSERTINFO( _fd >= 0, "init() not called?" );
00079             int read = ::read( _fd, &value, sizeof( T ));
00080             EQASSERTINFO( read == sizeof(T),
00081                           read << " != " << sizeof( T ) << ": " << sysError );
00082             if( read != sizeof( T ))
00083             {
00084                 EQERROR << "random number generator not working" << std::endl;
00085                 return 0;
00086             }
00087 
00088 #elif defined (_WIN32)
00089             EQASSERTINFO( _provider, "init() not called?" );
00090             if( !CryptGenRandom( _provider, sizeof( T ), (BYTE*)&value ))
00091             {
00092                 EQASSERTINFO( false, "random number generator not working: " <<
00093                                      sysError );
00094             }
00095 #else // Darwin
00096             uint8_t* bytes = reinterpret_cast< uint8_t* >( &value );
00097             for( size_t i=0; i<sizeof( T ); ++i )
00098                 bytes[i] = ( random() & 0xff );
00099 #endif
00100             return value;
00101         }
00102 
00103     private:
00104 #ifdef Linux
00105         static int _fd;
00106 #elif defined (_WIN32)
00107         static COBASE_API HCRYPTPROV _provider;
00108 #endif
00109         static bool _init();
00110         static bool _exit();
00111         friend COBASE_API bool init( const int argc, char** argv );
00112         friend COBASE_API bool exit();
00113     };
00114 
00115     template<> inline float RNG::get()
00116     {
00117         const float max_limits =
00118             static_cast< float >( std::numeric_limits< uint32_t >::max( ));
00119         return ( get< uint32_t >() / max_limits);
00120     }
00121     
00122     template<> inline double RNG::get()
00123     {
00124         const double max_limits =
00125             static_cast< double >( std::numeric_limits< uint64_t >::max( ));
00126         return ( get< uint64_t >() / max_limits);
00127     }
00128     
00129     template<> inline bool RNG::get()
00130     {
00131         return ( get< uint32_t >() & 1 );
00132     }
00133 }
00134 }
00135 #endif  // COBASE_RNG_H
Generated on Sun May 8 2011 19:11:07 for Equalizer 1.0 by  doxygen 1.7.3