Changeset 438


Ignore:
Timestamp:
07/23/15 17:29:49 (10 years ago)
Author:
epyon
Message:
  • massive amount of std::string removal
  • removed slurp, use filesystem::slurp instead
  • lua_values const_string support
  • several bugfixes
  • program_manager and shader loading without std::string/std::stream
Location:
trunk
Files:
25 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/core/random.hh

    r397 r438  
    1818        {
    1919        public:
    20                 typedef unsigned int result_type; // std::mt19937::result_type
     20                typedef unsigned int result_type; // mt19937::result_type
    2121                typedef unsigned int seed_type;
    2222
     
    174174                }
    175175        private:
    176                 // temporary solution until we get rid of std::random
     176                // temporary solution until we get rid of ::random
    177177                char m_data[16 * 1024];
    178178        };
  • trunk/nv/core/time.hh

    r395 r438  
    3434
    3535        /**
    36          * Get millisecond count based on std::clock
     36         * Get millisecond count based on ::clock
    3737         */
    3838        uint32 get_cpu_ms();
    3939
    4040        /**
    41          * Get microsecond count based on std::clock
     41         * Get microsecond count based on ::clock
    4242         */
    4343        uint64 get_cpu_us();
  • trunk/nv/core/types.hh

    r431 r438  
    3333        };
    3434
    35         template<>
    36         struct is_container < std::string >
    37         {
    38                 static const bool value = false;
    39         };
     35//      template<>
     36//      struct is_container < string_view >
     37//      {
     38//              static const bool value = false;
     39//      };
    4040
    4141        struct type_entry;
  • trunk/nv/engine/program_manager.hh

    r399 r438  
    2828        protected:
    2929                virtual resource_id load_resource( lua::table_guard& table );
    30                 void load_source( lua::table_guard& table, std::string& out, const std::string& append );
     30                string_buffer load_source( lua::table_guard& table, const string_view& append );
    3131                virtual void release( program p );
    3232        private:
    3333                context* m_context;
    34                 std::string   m_vertex_head;
    35                 std::string   m_fragment_head;
     34                const_string m_shader_head;
    3635        };
    3736
  • trunk/nv/gfx/texture_font.hh

    r399 r438  
    4343        {
    4444                public:
    45                         texture_font( texture_atlas* atlas, const char * filename, float size );
     45                        texture_font( texture_atlas* atlas, const string_view& filename, float size );
    4646                        const texture_glyph* get_glyph( uint16 charcode ) const;
    4747                        bool load_glyphs( string_view codes );
     
    5454
    5555                        texture_atlas* m_atlas; //!< Atlas Image object for this font.
    56                         std::string m_filename; //!< Name of the file.
     56                        string128 m_filename;//!< Filename
    5757                        float m_size;           //!< Font size.
    5858                        float m_height;         //!< Height of the font. (x-height?)
  • trunk/nv/gl/gl_device.hh

    r406 r438  
    5656                virtual int get_attribute_location( program p, const std::string& name, bool fatal = true ) const;
    5757                virtual void prepare_program( program p );
    58                 virtual const std::string& get_shader_header() const { return m_shader_header; }
     58                virtual string_view get_shader_header() const { return m_shader_header; }
    5959                virtual ~gl_device();
    6060        protected:
    6161                uniform_base* get_uniform( program p, const std::string& name, bool fatal = true ) const;
    6262
    63        
    6463        private:
    6564                bool compile( gl_program_info* p, string_view vertex_program, string_view fragment_program );
     
    6867                void load_attributes( gl_program_info* p );
    6968                void load_uniforms( gl_program_info* p );
    70                 std::string m_shader_header;
     69                string_buffer m_shader_header;
    7170                handle_store< gl_texture_info, texture >         m_textures;
    7271                handle_store< gl_buffer_info,  buffer >          m_buffers;
  • trunk/nv/gl/gl_enum.hh

    r395 r438  
    4545        unsigned int datatype_to_gl_enum( datatype type );
    4646        datatype gl_enum_to_datatype( unsigned int gl_enum );
    47         std::string datatype_to_glsl_type( datatype type );
     47        string_view datatype_to_glsl_type( datatype type );
    4848
    4949} // namespace nv
  • trunk/nv/interface/device.hh

    r412 r438  
    182182                virtual const texture_info* get_texture_info( texture ) const = 0;
    183183                virtual const buffer_info* get_buffer_info( buffer ) const = 0;
    184                 virtual const std::string& get_shader_header() const = 0;
     184                virtual string_view get_shader_header() const = 0;
    185185
    186186                virtual texture create_texture( image_data* data, sampler asampler )
  • trunk/nv/io/c_file_system.hh

    r422 r438  
    2525                c_file_system();
    2626                virtual ~c_file_system();
    27                 virtual bool exists( const char* fpath );
    28                 virtual stream* open( const char* fpath, const char* fmode = "rb" );
     27                virtual const_string slurp( const string_view& path );
     28                virtual bool exists( const string_view& );
     29                virtual stream* open( const string_view&, const string_view& = "rb" );
    2930        };
    3031
  • trunk/nv/io/c_stream.hh

    r422 r438  
    2626        protected:
    2727                c_stream();
     28                // TODO: const char* is prone to deletion - don't store it, or store differently!
    2829                c_stream( void* pfile, const char* filename );
    2930        public:
  • trunk/nv/lua/lua_values.hh

    r406 r438  
    167167
    168168                template <>
     169                struct pass_traits < const_string >
     170                {
     171                        static void push( lua_State *L, const const_string& s ) { detail::push_string_view( L, s ); }
     172                        static const_string to( lua_State *L, int index ) { return const_string( detail::to_string_view( L, index ) ); }
     173                        static const_string to( lua_State *L, int index, const_string def ) { return const_string( detail::to_string_view( L, index, def ) ); }
     174                };
     175
     176                template <>
    169177                struct pass_traits<ref>
    170178                {
  • trunk/nv/stl/container/contiguous_storage.hh

    r435 r438  
    8383                                reallocate( 0, false );
    8484                                m_data = other.m_data;
     85                                other.m_data = nullptr;
    8586                        }
    8687                        return *this;
  • trunk/nv/stl/container/growing_storage.hh

    r437 r438  
    112112                inline growing_storage& operator=( growing_storage&& other )
    113113                {
    114                         Storage::reallocate( 0, false );
    115                         m_size = other.m_size;
    116                         Storage::operator=( nv::move( other ) );
    117                         other.m_size = size_impl_type();
     114                        if ( this != &other )
     115                        {
     116                                m_size = other.m_size;
     117                                Storage::operator=( nv::move( other ) );
     118                                other.m_size = size_impl_type();
     119                        }
    118120                        return *this;
    119121                }
     
    179181                void insert( iterator position, const value_type& value )
    180182                {
    181                         iterator iend = Storage::data() + m_size;
     183                        ptrdiff_t offset = position - Storage::data();
    182184                        if ( try_grow( 1 ) )
    183185                        {
    184                                 raw_alias_copy( position, iend, position + 1 );
    185                                 copy_construct_object( position, value );
     186                                iterator iposition = Storage::data() + offset;
     187                                iterator iend = Storage::data() + m_size - 1;
     188                                raw_alias_copy( iposition, iend, iposition + 1 );
     189                                copy_construct_object( iposition, value );
    186190                        }
    187191                }
     
    192196                        // TODO: distance can't be called on destructive iterators - check
    193197                        //   and use pushback if needed?
    194                         iterator iend = Storage::data() + m_size;
     198                        ptrdiff_t offset = position - Storage::data();
    195199                        size_type d = distance( first, last );
    196200                        if ( try_grow( d ) )
    197201                        {
    198                                 raw_alias_copy( position, iend, position + d );
    199                                 InitializePolicy::copy( first, last, position );
     202                                iterator iposition = Storage::data() + offset;
     203                                iterator iend = Storage::data() + m_size - d;
     204                                raw_alias_copy( iposition, iend, iposition + d );
     205                                InitializePolicy::copy( first, last, iposition );
    200206                        }
    201207                }
  • trunk/nv/stl/file_system.hh

    r422 r438  
    1414
    1515#include <nv/common.hh>
     16#include <nv/stl/string.hh>
    1617#include <nv/stl/stream.hh>
    1718
     
    2526        public:
    2627                virtual ~file_system() {}
    27                 virtual bool exists( const char* fpath ) = 0;
    28                 virtual stream* open( const char* fpath, const char* fmode = "rb" ) = 0;
     28                virtual const_string slurp( const string_view& path ) = 0;
     29                virtual bool exists( const string_view& path ) = 0;
     30                virtual stream* open( const string_view& path, const string_view& mode = "rb" ) = 0;
     31
    2932        };
    3033
  • trunk/nv/stl/string.hh

    r435 r438  
    2929#include <nv/stl/string/short_string.hh>
    3030#include <string>
    31 #include <sstream>
    3231#include <nv/stl/type_traits/primary.hh>
    3332#include <nv/stl/memory.hh>
     
    4746                inline H operator()( const std::string& value ) const { return get( value ); }
    4847        };
    49 
    50 
    51         /**
    52         * Simple function for slurping a file into a string.
    53         */
    54         std::string slurp( const std::string& filename );
    5548
    5649        namespace detail
  • trunk/nv/stl/string/const_string.hh

    r433 r438  
    4141                        initialize( s, N - 1 );
    4242                }
     43                // TODO : implement
     44                //              inline string_buffer_base( const string_twine& twine );
     45
    4346                ~const_string()
    4447                {
     
    7477                {
    7578                        char* new_data = new char[s + 1];
    76                         nvmemcpy( new_data, p, s );
     79                        if (p) nvmemcpy( new_data, p, s );
    7780                        new_data[s] = 0;
    7881                        assign( new_data, s );
  • trunk/nv/stl/string/short_string.hh

    r435 r438  
    1010#include <nv/stl/string/common.hh>
    1111#include <nv/stl/string/string_base.hh>
     12#include <nv/stl/string/string_twine.hh>
    1213#include <nv/stl/container/random_access.hh>
    1314#include <nv/stl/container/contiguous_storage.hh>
     
    3233                }
    3334
     35
     36                template < typename Base >
     37                inline explicit string_buffer_base( const string_base< Base >& s ) : this_type( s.data(), s.size() ) {}
    3438                inline explicit string_buffer_base( const string_view& s ) : this_type( s.data(), s.size() ) {}
    35                 template < typename S >
    36                 inline string_buffer_base( const string_base<S>& rhs ) : this_type( rhs.data(), rhs.size() ) {}
     39
     40                inline string_buffer_base( const string_twine& twine )
     41                {
     42                        append( twine );
     43                }
    3744                inline string_buffer_base( string_buffer_base&& copy ) = default;
    3845                inline string_buffer_base& operator=( string_buffer_base&& other ) = default;
    39                 inline string_buffer_base& operator=( const string_view& other )
    40                 {
    41                         clear();
    42                         append( other );
    43                 }
    4446
    4547                size_t append( const char* data, size_t sz )
     
    4951                        raw_copy_n( data, amount, this->begin() + pos );
    5052                        return amount;
     53                }
     54
     55                size_t append( const string_twine& twine )
     56                {
     57                        size_t pos = size();
     58                        size_t dump_size = twine.dump_size();
     59                        expand_by( dump_size );
     60                        twine.dump( array_ref< char >( data() + pos, size() - pos ) );
     61                        return size() - pos;
    5162                }
    5263
     
    111122                void reserve( size_type new_capacity )
    112123                {
    113                         Storage::reserve( new_capacity + 1, true );
     124                        Storage::reserve( new_capacity + 1 );
    114125                        this->data()[this->size()] = 0;
    115126                }
     
    117128                void resize( size_type new_size )
    118129                {
    119                         Storage::reserve( new_size + 1, true );
     130                        Storage::reserve( new_size + 1 );
    120131                        Storage::resize( min( new_size, capacity() - 1 ) );
    121132                        this->data()[this->size()] = 0;
  • trunk/src/core/library.cc

    r437 r438  
    66
    77#include "nv/core/library.hh"
    8 #include <string>
    98
    109#if NV_PLATFORM == NV_WINDOWS
     
    7473    NV_LOG_NOTICE( "library \"", m_name, "\" : loading..." );
    7574
    76         std::string name( m_name.data(), m_name.length() );
    77         std::string ext( NV_LIB_EXT );
     75        string128 name( m_name );
     76        string_view ext( NV_LIB_EXT );
    7877
    79         if ( name.length() < ext.length() || name.substr( name.length() - ext.length(), ext.length() ) != ext )
     78        if ( name.length() < ext.length() || !name.ends_with( ext ) )
    8079    {
    81         name.append( ext.data(), ext.length() );
     80        name.append( ext );
    8281    }
    8382
    84     m_handle = NV_LIB_OPEN( name.c_str() );
     83    m_handle = NV_LIB_OPEN( name.data() );
    8584
    8685    if ( m_handle == NULL )
    8786    {
    88                 NV_LOG_NOTICE( "library \"", m_name, "\" : failed to open!" );
     87                NV_LOG_NOTICE( "library \"", name, "\" : failed to open!" );
    8988                return false;
    9089    }
  • trunk/src/engine/program_manager.cc

    r433 r438  
    99#include "nv/core/logging.hh"
    1010#include "nv/lua/lua_nova.hh"
    11 
     11#include "nv/io/c_file_system.hh"
    1212
    1313nv::program_manager::program_manager( context* a_context ) : m_context( a_context )
    1414{
    15         m_vertex_head   = a_context->get_device()->get_shader_header();
    16         m_fragment_head = a_context->get_device()->get_shader_header();
     15        m_shader_head = a_context->get_device()->get_shader_header();
    1716}
    1817
     
    2019{
    2120        NV_LOG_DEBUG( table.get_string("id") );
    22         std::string vsource;
    23         std::string fsource;
    24         std::string csource;
     21        string_buffer vsource;
     22        string_buffer fsource;
     23        string_buffer header( m_shader_head );
    2524        if ( table.is_table("common") )
    2625        {
    2726                lua::table_guard common( table, "common" );
    28                 load_source( common, csource, "" );
     27                header.append( "\n" + load_source( common, "" ) + "\n" );
    2928        }
    3029        {
    3130                lua::table_guard vtable( table, "vertex" );
    32                 load_source( vtable, vsource, m_vertex_head+"\n"+csource+"\n");
     31                vsource = load_source( vtable, header );
    3332        }
    3433        {
    3534                lua::table_guard ftable( table, "fragment" );
    36                 load_source( ftable, fsource, m_fragment_head+"\n"+csource+"\n" );
     35                fsource = load_source( ftable, header );
    3736        }
    3837
    39         nv::program program = m_context->get_device()->create_program( string_view( vsource.c_str(), vsource.size() ), string_view( fsource.c_str(), fsource.size() ) );
     38        nv::program program = m_context->get_device()->create_program( vsource, fsource );
    4039        return add( program );
    4140}
     
    4645}
    4746
    48 void nv::program_manager::load_source( lua::table_guard& table, std::string& out, const std::string& append )
     47nv::string_buffer nv::program_manager::load_source( lua::table_guard& table, const string_view& append )
    4948{
    50         out = append;
     49        c_file_system fs;
     50        string_buffer out( append );
    5151        if ( table.is_string( "files" ) )
    5252        {
    53                 out += nv::slurp( table.get_std_string( "files" ) );
     53                out.append( fs.slurp( table.get_string( "files" ) ) );
    5454        }
    5555        else if ( table.is_table( "files" ) )
     
    5959                for ( uint32 i = 1; i <= count; ++i )
    6060                {
    61                         std::string include( inctable.get<std::string,uint32>(i) );
    62                         if ( i == count ) out += "#line 1\n";
    63                         out += nv::slurp( include );
     61                        const_string include( inctable.get<const_string,uint32>(i) );
     62                        if ( i == count ) out.append( "#line 1\n" );
     63                        out.append( fs.slurp( include ) );
    6464                }
    6565        }
     
    6767        if ( table.is_string( "file" ) )
    6868        {
    69                 out += "#line 1\n" + nv::slurp( table.get_std_string( "file" ) );
     69                const_string data = fs.slurp( table.get_string( "file" ) );
     70                out.append( "#line 1\n" + data );
    7071        }
    7172
    7273        if ( table.is_string( "source" ) )
    7374        {
    74                 out += table.get_std_string( "source" );
     75                out.append( table.get_string( "source" ) );
    7576        }
     77        return out;
    7678}
  • trunk/src/gfx/texture_font.cc

    r405 r438  
    2929}
    3030
    31 texture_font::texture_font( texture_atlas* atlas, const char * filename, float size )
    32         : m_atlas( atlas ), m_filename(filename), m_size( size ),
     31texture_font::texture_font( texture_atlas* atlas, const string_view& filename, float size )
     32        : m_atlas( atlas ), m_filename( filename ), m_size( size ),
    3333        m_height(0), m_linegap(0), m_ascender(0), m_descender(0),
    3434        m_hinting( true ), m_filtering( true ), m_rlibrary( nullptr ), m_rface( nullptr )
     
    5353        NV_CHECK_FREETYPE_ERROR( error, "error on FT_Init_FreeType, code - ", error );
    5454
    55         error = FT_New_Face( reinterpret_cast<FT_Library>(m_rlibrary), filename, 0, reinterpret_cast<FT_Face*>(&m_rface) );
     55        error = FT_New_Face( reinterpret_cast<FT_Library>(m_rlibrary), filename.data(), 0, reinterpret_cast<FT_Face*>(&m_rface) );
    5656        NV_CHECK_FREETYPE_ERROR( error, "error on FT_New_Face, code - ", error );
    5757
  • trunk/src/gl/gl_device.cc

    r433 r438  
    1717gl_device::gl_device()
    1818{
    19         m_shader_header  = "#version 120\n";
     19        m_shader_header.append( "#version 120\n" );
    2020        for ( auto& i : get_uniform_factory() )
    21                 m_shader_header += "uniform "+datatype_to_glsl_type( i.second->get_datatype() )+" "+ std::string( i.first.data(), i.first.size() ) +";\n";
     21                m_shader_header.append( "uniform "+datatype_to_glsl_type( i.second->get_datatype() )+" "+ i.first +";\n" );
    2222        for ( auto& i : get_link_uniform_factory() )
    23                 m_shader_header += "uniform sampler2D "+std::string( i.first.data(), i.first.size() ) +";\n";
     23                m_shader_header.append( "uniform sampler2D "+i.first +";\n" );
    2424}
    2525
  • trunk/src/gl/gl_enum.cc

    r395 r438  
    354354}
    355355
    356 std::string nv::datatype_to_glsl_type( datatype type )
     356string_view nv::datatype_to_glsl_type( datatype type )
    357357{
    358358        switch( type )
  • trunk/src/io/c_file_system.cc

    r395 r438  
    1111using namespace nv;
    1212
     13
    1314c_file_system::c_file_system()
    1415{
     
    2122}
    2223
    23 bool c_file_system::exists( const char* fpath )
     24bool c_file_system::exists( const string_view& fpath )
    2425{
    25         FILE* file = ::fopen( fpath, "rb" );
     26        FILE* file = ::fopen( fpath.data(), "rb" );
    2627        if ( !file )
    2728        {
     
    3233}
    3334
    34 stream* c_file_system::open( const char* fpath, const char* fmode /*= "rb" */ )
     35stream* c_file_system::open( const string_view& fpath, const string_view& fmode /*= "rb" */ )
    3536{
    36         NV_ASSERT( fpath != nullptr && fmode != nullptr, "Bad parameters passed to open" );
    37         FILE* file = ::fopen( fpath, fmode );
     37        NV_ASSERT( !fpath.empty() && !fmode.empty(), "Bad parameters passed to open" );
     38        FILE* file = ::fopen( fpath.data(), fmode.data() );
    3839        if ( !file )
    3940        {
    4041                return nullptr;
    4142        }
    42         return new c_stream( file, fpath );
     43        return new c_stream( file, fpath.data() );
    4344}
     45
     46nv::const_string c_file_system::slurp( const string_view& path )
     47{
     48        stream* fstream = open( path, "rb" );
     49        if ( !fstream ) return const_string();
     50        uint32 size = fstream->size();
     51        const_string result( nullptr, size );
     52        fstream->read( const_cast<char*>( result.data() ), size, 1 );
     53        delete fstream;
     54        return result;
     55}
  • trunk/src/lua/lua_raw.cc

    r437 r438  
    3030        {
    3131                size_t l = nv::uint64_to_buffer( buffer, nv::uint64( lua_touserdata( L, idx ) ) );
    32                 return nv::string_view( buffer.data(), buffer.size() );
     32                return nv::string_view( buffer.data(), l );
    3333        }
    3434        else if ( type == LUA_TNUMBER )
    3535        {
    3636                size_t l = nv::f64_to_buffer( buffer, lua_tonumber( L, idx ) );
    37                 return nv::string_view( buffer.data(), buffer.size() );
     37                return nv::string_view( buffer.data(), l );
    3838        }
    3939        return "UNKNOWN!";
  • trunk/src/stl/string.cc

    r435 r438  
    1212#include <cstdlib>
    1313#include <fstream> // for slurp only
     14#include <sstream> // for slurp only
    1415
    1516using namespace nv;
    1617
    1718//static const double s_power_10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
    18 
    19 std::string nv::slurp( const std::string& filename )
    20 {
    21         std::ifstream input( filename );
    22         //              if ( !input.is_open() ) NV_THROW( std::runtime_error, "File "+filename+" not found!");
    23         std::stringstream sstr;
    24         while ( input >> sstr.rdbuf() );
    25         return sstr.str();
    26 }
    2719
    2820static inline void string_reverse( char* begin, char* end )
Note: See TracChangeset for help on using the changeset viewer.