Ignore:
Timestamp:
07/09/15 18:53:08 (10 years ago)
Author:
epyon
Message:
  • data_descriptor creators ( buggy )
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/interface/data_descriptor.hh

    r412 r413  
    284284                }
    285285
     286                template < typename Struct >
     287                static data_descriptor create()
     288                {
     289                        data_descriptor result;
     290                        result.initialize< Struct >();
     291                        return result;
     292                }
     293
    286294        protected:
    287295
     
    306314        struct raw_data_channel
    307315        {
    308                 raw_data_channel() : data( nullptr ), m_count( 0 ) {}
     316                raw_data_channel() : m_data( nullptr ), m_count( 0 ) {}
    309317                ~raw_data_channel()
    310318                {
    311                         if ( data != nullptr ) delete[] data;
     319                        if ( m_data != nullptr ) delete[] m_data;
    312320                }
    313321
     
    316324                uint32 element_count() const { return m_count; }
    317325                uint32 raw_size() const { return m_count * m_desc.element_size(); }
    318                 const uint8* raw_data() const { return data; }
     326                const uint8* raw_data() const { return m_data; }
     327
     328                // TODO: constexpr compile-time cast check?
     329                template < typename Struct >
     330                const Struct* data_cast() const
     331                {
     332                        return reinterpret_cast<const Struct*>( m_data );
     333                }
    319334
    320335                bool has_slot( slot vslot ) const { return m_desc.has_slot( vslot ); }
    321336
    322                 template < typename VTX >
     337                template < typename Struct >
    323338                static raw_data_channel* create( uint32 count = 0 )
    324339                {
    325340                        raw_data_channel* result = new raw_data_channel();
    326                         result->m_desc.initialize<VTX>();
     341                        result->m_desc.initialize<Struct>();
    327342                        result->m_count = count;
    328                         result->data = ( count > 0 ? ( new uint8[result->raw_size()] ) : nullptr );
     343                        result->m_data = ( count > 0 ? ( new uint8[result->raw_size()] ) : nullptr );
    329344                        return result;
    330345                }
     
    334349                        result->m_desc = desc;
    335350                        result->m_count = count;
    336                         result->data = ( count > 0 ? ( new uint8[result->raw_size()] ) : nullptr );
     351                        result->m_data = ( count > 0 ? ( new uint8[result->raw_size()] ) : nullptr );
    337352                        return result;
    338353                }
    339354
    340                 friend class mesh_creator;
    341                 friend class mesh_data_creator;
    342 
    343                 uint8*          data;
     355                template < typename Struct >
     356                friend class data_channel_creator;
     357                friend class raw_data_channel_creator;
     358
    344359        protected:
     360                uint8*          m_data;
    345361                data_descriptor m_desc;
    346362                uint32          m_count;
     
    348364        };
    349365
     366        class raw_data_channel_creator
     367        {
     368        public:
     369                raw_data_channel_creator( const data_descriptor& desc, uint32 size )
     370                {
     371                        m_channel = raw_data_channel::create( desc, size );
     372                        m_owned   = true;
     373                }
     374                explicit raw_data_channel_creator( raw_data_channel* channel )
     375                {
     376                        m_channel = channel;
     377                        m_owned   = false;
     378                }
     379
     380                uint32 element_size() const { return m_channel->element_size(); }
     381                uint32 size() const { return m_channel->element_count(); }
     382                uint32 raw_size() const { return m_channel->element_count() * m_channel->element_size(); }
     383                uint8* raw_data() { return m_channel->m_data; }
     384                const uint8* raw_data() const { return m_channel->m_data; }
     385                raw_data_channel* channel() { return m_channel; }
     386
     387                data_descriptor& descriptor() { return m_channel->m_desc; }
     388                const data_descriptor& descriptor() const { return m_channel->m_desc; }
     389
     390                raw_data_channel* release()
     391                {
     392                        raw_data_channel* result = m_channel;
     393                        m_channel = nullptr;
     394                        return result;
     395                }
     396
     397                ~raw_data_channel_creator()
     398                {
     399                        if ( m_owned && m_channel ) delete m_channel;
     400                }
     401        protected:
     402                raw_data_channel* m_channel;
     403                bool              m_owned;
     404        };
     405
     406        template < typename Struct >
     407        class data_channel_creator : public raw_data_channel_creator
     408        {
     409        public:
     410                explicit data_channel_creator( uint32 size )
     411                        : raw_data_channel_creator( data_descriptor::create< Struct >(), size ) {}
     412                // TODO - descriptor check
     413                explicit data_channel_creator( raw_data_channel* channel )
     414                        : raw_data_channel_creator( channel ) {}
     415               
     416                Struct* data() { return reinterpret_cast< Struct* >( m_channel->m_data ); }
     417                const Struct* data() const { return reinterpret_cast< const Struct* >( m_channel->m_data ); }
     418
     419                const Struct& operator []( uint32 i ) const
     420                {
     421                        NV_ASSERT( i < m_channel->m_count, "data_channel_creator indexing failure!" );
     422                        return reinterpret_cast< const Struct* >( m_channel->m_data )[i];
     423                }
     424
     425                Struct& operator []( uint32 i )
     426                {
     427                        NV_ASSERT( i < m_channel->m_count, "data_channel_creator indexing failure!" );
     428                        return reinterpret_cast<Struct*>( m_channel->m_data )[i];
     429                }
     430        };
    350431}
    351432
    352433#endif // NV_INTERFACE_DATA_DESCRIPTOR_HH
    353 
Note: See TracChangeset for help on using the changeset viewer.