Changeset 310


Ignore:
Timestamp:
08/14/14 19:00:26 (11 years ago)
Author:
epyon
Message:
  • range - added bit enumeration iterator
  • flags - added flag enumeration iterator
  • wx - switched to more performant update mechanism
  • gl_window - no need to call load_gl_no_context twice
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/flags.hh

    r197 r310  
    1616#include <nv/common.hh>
    1717#include <nv/type_traits.hh>
    18 #include <bitset>
     18#include <nv/array.hh>
    1919
    2020namespace nv
    2121{
    22         template< typename T, typename IS_ENUM >
    23         struct base_underlying_type_helper
    24         {
    25                 typedef T type;
    26         };
    27 
    28         template< typename T >
    29         struct base_underlying_type_helper< T, std::true_type >
    30         {
    31                 typedef typename nv::underlying_type<T>::type type;
    32         };
    33 
    34 
    35         template< typename T >
    36         struct base_underlying_type
    37         {
    38                 typedef typename base_underlying_type_helper< T, typename std::is_enum<T>::type >::type type;
    39         };
    40 
    4122        template < uint32 SIZE, typename T = uint32 >
    4223        class flags
    4324        {
    4425        public:
    45 
    4626                class reference
    4727                {
     
    6747
    6848                private:
    69                         reference() : m_flags( nullptr ), m_index( 0 ) {}
     49                        reference() : m_flags( nullptr ), m_index( index_type(0) ) {}
    7050
    7151                        reference( flags<SIZE,T>* a_flags, index_type a_index )
     
    7959                };
    8060
    81                 typedef uint8  data_type;
    82                 typedef bool   value_type;
    83                 typedef T      index_type;
     61                class enumerator
     62                {
     63                        friend class flags<SIZE,T>;
     64                public:
     65                        typedef T              index_type;
     66                        typedef T              value_type;
     67                        typedef T*             iterator;
     68                        typedef const T*       const_iterator;
     69                        typedef T&             reference;
     70                        typedef const T&       const_reference;
     71                        typedef std::size_t    size_type;
     72                        typedef std::ptrdiff_t difference_type;
     73
     74                        T operator* () const { return m_index; }
     75                        T const* operator-> () const { return &m_index; }
     76                        bool operator== ( const enumerator& rhs ) const
     77                        {
     78                                return ( m_index == rhs.m_index ) && ( m_flags == rhs.m_flags );
     79                        }
     80                        bool operator!= ( const enumerator& rhs ) const
     81                        {
     82                                return !( *this == rhs );
     83                        }
     84                       
     85                        enumerator& operator++ ()
     86                        {
     87                                next();
     88                                return *this;
     89                        }
     90                        enumerator operator++ (int)
     91                        {
     92                                auto result = *this;
     93                                ++*this;
     94                                return result;
     95                        }
     96                protected:
     97                        enumerator() : m_flags( nullptr ), m_index( index_type(0) ) {}
     98
     99                        enumerator( const flags<SIZE,T>* a_flags, index_type a_index )
     100                                : m_flags( a_flags ), m_index( a_index )
     101                        {       
     102                                if ( a_flags && !a_flags->test( a_index ) ) next();
     103                        }
     104
     105                        void next()
     106                        {
     107                                if ( m_flags )
     108                                do
     109                                {
     110                                        if ( raw_index_type(m_index) >= SIZE ) { m_flags = nullptr; m_index = index_type(0); return; }
     111                                        m_index = index_type((raw_index_type)m_index + 1);
     112                                } while ( !m_flags->test( m_index ) );
     113                        }
     114
     115                        const flags<SIZE,T>* m_flags;
     116                        index_type           m_index;
     117                };
     118
     119                typedef uint8     data_type;
     120                typedef T         index_type;
    84121                typedef uint32 size_type;
    85122                typedef typename base_underlying_type<T>::type raw_index_type;
     
    89126                static const size_type data_size      = (SIZE+data_type_size-1) / data_type_size;
    90127
     128                enumerator begin()  const { return enumerator( this, index_type(0) ); }
     129                enumerator cbegin() const { return enumerator( this, index_type(0) ); }
     130
     131                enumerator end()  const { return enumerator(); }
     132                enumerator cend() const { return enumerator(); }
     133
    91134                flags()
    92135                {
     
    123166                }
    124167
    125                 void set( index_type i, value_type value )
     168                void set( index_type i, bool value )
    126169                {
    127170                        raw_index_type idx = static_cast< raw_index_type >( i ) / data_type_size;
  • trunk/nv/lib/wx.hh

    r295 r310  
    3232                explicit gl_canvas( wxWindow *parent );
    3333                virtual void OnUpdate() = 0;
    34                 virtual void Stop() { m_update_timer->Stop(); }
    35                 virtual void Start() { m_update_timer->Start(10); }
     34//              virtual void Stop() { m_update_timer->Stop(); }
     35//              virtual void Start() { m_update_timer->Start(20); }
     36                virtual void Stop()
     37                {
     38                        Disconnect( wxEVT_IDLE, wxIdleEventHandler(gl_canvas::OnIdle) );
     39                        //m_update_timer->Stop();
     40                }
     41                virtual void Start()
     42                {
     43                        Connect( wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(gl_canvas::OnIdle) );
     44                        //m_update_timer->Start(20);
     45                }
     46                void OnIdle(wxIdleEvent& evt)
     47                {
     48                        if(m_render)
     49                        {
     50                                OnUpdate();
     51                                evt.RequestMore(); // render continuously, not only once on idle
     52                        }
     53                }
    3654                ~gl_canvas();
    3755        protected:
     
    4260                nv::window*  m_window;
    4361                wxTimer*     m_update_timer;
     62                bool         m_render;
    4463                wxDECLARE_EVENT_TABLE();
    4564        };
     
    6887                m_window = m_device->adopt_window( GetHWND(), dc.GetHDC() );
    6988                m_context = m_window->get_context();
    70                 m_update_timer = new gl_update_timer( this );
    7189        }
    7290
    7391        inline gl_canvas::~gl_canvas()
    7492        {
    75                 delete m_update_timer;
    7693                delete m_window;
    7794                delete m_device;
  • trunk/nv/range.hh

    r255 r310  
    8989                };
    9090
     91                template < typename T >
     92                class bits_iterator_base : public forward_iterator_base< T >
     93                {
     94                public:
     95                        typedef typename base_underlying_type<T>::type base_type;
     96                        static const T invalid = T( 0 );
     97
     98                        bits_iterator_base( T value, T current ) : forward_iterator_base( current ), m_full( value )
     99                        {
     100                                if( !( (base_type)value & (base_type)current ) )
     101                                        next();
     102                        }
     103                        bits_iterator_base& operator++ ()
     104                        {
     105                                next();
     106                                return *this;
     107                        }
     108                        bits_iterator_base operator++ (int)
     109                        {
     110                                auto result = *this;
     111                                ++*this;
     112                                return result;
     113                        }
     114                private:
     115                        void next()
     116                        {
     117                                do
     118                                {
     119                                        if ( m_value == invalid || m_full <= m_value ) { m_value = invalid; m_full = invalid; break; }
     120                                        m_value = T((base_type)m_value << 1);
     121                                } while ( !( (base_type)m_full & (base_type)m_value ) );
     122                        }
     123
     124                        T m_full;
     125                };
     126
     127
    91128        } // namespace detail
    92129
     
    134171
    135172        template < typename T >
     173        class bits_iterator_provider
     174        {
     175        public:
     176                typedef typename base_underlying_type<T>::type base_type;
     177                typedef detail::bits_iterator_base<T> iterator;
     178                static const T invalid = T( 0 );
     179
     180                bits_iterator_provider( T value ) : m_value( value ) {}
     181                iterator begin() { return iterator( m_value, T(1) ); }
     182                iterator end() { return iterator( m_value, invalid ); }
     183
     184        protected:
     185                T m_value;
     186        };
     187
     188        template < typename T >
    136189        range_iterator_provider<T> range( T begin, T end )
    137190        {
     
    163216        }
    164217
     218        template < typename T >
     219        bits_iterator_provider<T> bits( T value )
     220        {
     221                return bits_iterator_provider<T>( value );
     222        }
     223
     224
    165225}
    166226
  • trunk/nv/type_traits.hh

    r253 r310  
    207207                ((TYPE*)object)->TYPE::~TYPE();
    208208        }
     209
     210        template< typename T, typename IS_ENUM >
     211        struct base_underlying_type_helper
     212        {
     213                typedef T type;
     214        };
     215
     216        template< typename T >
     217        struct base_underlying_type_helper< T, std::true_type >
     218        {
     219                typedef typename nv::underlying_type<T>::type type;
     220        };
     221
     222
     223        template< typename T >
     224        struct base_underlying_type
     225        {
     226                typedef typename base_underlying_type_helper< T, typename std::is_enum<T>::type >::type type;
     227        };
     228
     229
    209230}
    210231
  • trunk/src/gl/gl_window.cc

    r304 r310  
    350350#if NV_PLATFORM == NV_WINDOWS
    351351#if NV_SDL_VERSION == NV_SDL_20
    352         nv::load_gl_no_context();
    353352        m_context = new native_gl_context( m_device, dc );
    354353        SDL_Window* window = SDL_CreateWindowFrom( handle );
Note: See TracChangeset for help on using the changeset viewer.