Add make_shared emulation for std::tr1::shared_ptr; use make_shared

This commit is contained in:
John Hood
2014-06-15 00:59:55 -04:00
parent b41bad918d
commit 589d21bbf2
4 changed files with 49 additions and 9 deletions
+7 -3
View File
@@ -148,14 +148,16 @@ std::string Display::new_frame( bool initialized, const Framebuffer &last, const
/* Extend rows if we've gotten a resize and new is wider than old */ /* Extend rows if we've gotten a resize and new is wider than old */
if ( frame.last_frame.ds.get_width() < f.ds.get_width() ) { if ( frame.last_frame.ds.get_width() < f.ds.get_width() ) {
for ( Framebuffer::rows_type::iterator p = rows.begin(); p != rows.end(); p++ ) { for ( Framebuffer::rows_type::iterator p = rows.begin(); p != rows.end(); p++ ) {
*p = Framebuffer::row_pointer( new Row( **p ) ); *p = make_shared<Row>( **p );
(*p)->cells.resize( f.ds.get_width(), Cell( f.ds.get_background_rendition() ) ); (*p)->cells.resize( f.ds.get_width(), Cell( f.ds.get_background_rendition() ) );
} }
} }
/* Add rows if we've gotten a resize and new is taller than old */ /* Add rows if we've gotten a resize and new is taller than old */
if ( static_cast<int>( rows.size() ) < f.ds.get_height() ) { if ( static_cast<int>( rows.size() ) < f.ds.get_height() ) {
// get a proper blank row // get a proper blank row
blank_row = Framebuffer::row_pointer( new Row( f.ds.get_width(), 0 ) ); const size_t w = f.ds.get_width();
const color_type c = 0;
blank_row = make_shared<Row>( w, c );
rows.resize( f.ds.get_height(), blank_row ); rows.resize( f.ds.get_height(), blank_row );
} }
@@ -198,7 +200,9 @@ std::string Display::new_frame( bool initialized, const Framebuffer &last, const
if ( lines_scrolled ) { if ( lines_scrolled ) {
/* Now we need a proper blank row. */ /* Now we need a proper blank row. */
if ( blank_row.get() == NULL ) { if ( blank_row.get() == NULL ) {
blank_row = Framebuffer::row_pointer( new Row( f.ds.get_width(), 0 ) ); const size_t w = f.ds.get_width();
const color_type c = 0;
blank_row = make_shared<Row>( w, c );
} }
frame.update_rendition( initial_rendition(), true ); frame.update_rendition( initial_rendition(), true );
+6 -3
View File
@@ -84,10 +84,13 @@ DrawState::DrawState( int s_width, int s_height )
} }
Framebuffer::Framebuffer( int s_width, int s_height ) Framebuffer::Framebuffer( int s_width, int s_height )
: rows( s_height, row_pointer(new Row( s_width, 0 ))), icon_name(), window_title(), bell_count( 0 ), title_initialized( false ), ds( s_width, s_height ) : rows(), icon_name(), window_title(), bell_count( 0 ), title_initialized( false ), ds( s_width, s_height )
{ {
assert( s_height > 0 ); assert( s_height > 0 );
assert( s_width > 0 ); assert( s_width > 0 );
const size_t w = s_width;
const color_type c = 0;
rows = rows_type(s_height, row_pointer(make_shared<Row>( w, c )));
} }
Framebuffer::Framebuffer( const Framebuffer &other ) Framebuffer::Framebuffer( const Framebuffer &other )
@@ -334,7 +337,7 @@ void Framebuffer::delete_line( int row, int count )
rows.insert( start, count, newrow()); rows.insert( start, count, newrow());
} }
Row::Row( size_t s_width, color_type background_color ) Row::Row( const size_t s_width, const color_type background_color )
: cells( s_width, Cell( background_color ) ), wrap( false ), gen( get_gen() ) : cells( s_width, Cell( background_color ) ), wrap( false ), gen( get_gen() )
{} {}
@@ -424,7 +427,7 @@ void Framebuffer::resize( int s_width, int s_height )
for ( rows_type::iterator i = rows.begin(); for ( rows_type::iterator i = rows.begin();
i != rows.end() && *i != blankrow; i != rows.end() && *i != blankrow;
i++ ) { i++ ) {
*i = Framebuffer::row_pointer( new Row( **i ) ); *i = make_shared<Row>( **i );
(*i)->set_wrap( false ); (*i)->set_wrap( false );
(*i)->cells.resize( s_width, Cell( ds.get_background_rendition() ) ); (*i)->cells.resize( s_width, Cell( ds.get_background_rendition() ) );
} }
+9 -3
View File
@@ -48,6 +48,7 @@
namespace Terminal { namespace Terminal {
using shared::shared_ptr; using shared::shared_ptr;
using shared::make_shared;
typedef uint16_t color_type; typedef uint16_t color_type;
class Renditions { class Renditions {
@@ -156,7 +157,7 @@ namespace Terminal {
// in scrolling. // in scrolling.
uint64_t gen; uint64_t gen;
Row( size_t s_width, color_type background_color ); Row( const size_t s_width, const color_type background_color );
Row(); /* default constructor required by C++11 STL */ Row(); /* default constructor required by C++11 STL */
void insert_cell( int col, color_type background_color ); void insert_cell( int col, color_type background_color );
@@ -315,7 +316,12 @@ namespace Terminal {
unsigned int bell_count; unsigned int bell_count;
bool title_initialized; /* true if the window title has been set via an OSC */ bool title_initialized; /* true if the window title has been set via an OSC */
row_pointer newrow( void ) { return row_pointer( new Row( ds.get_width(), ds.get_background_rendition())); } row_pointer newrow( void )
{
const size_t w = ds.get_width();
const color_type c = ds.get_background_rendition();
return make_shared<Row>( w, c );
}
public: public:
Framebuffer( int s_width, int s_height ); Framebuffer( int s_width, int s_height );
@@ -349,7 +355,7 @@ namespace Terminal {
row_pointer &mutable_row = rows.at( row ); row_pointer &mutable_row = rows.at( row );
// If the row is shared, copy it. // If the row is shared, copy it.
if (!mutable_row.unique()) { if (!mutable_row.unique()) {
mutable_row = row_pointer( new Row( *mutable_row )); mutable_row = make_shared<Row>( *mutable_row );
} }
return mutable_row.get(); return mutable_row.get();
} }
+27
View File
@@ -42,9 +42,36 @@
namespace shared { namespace shared {
#ifdef HAVE_STD_SHARED_PTR #ifdef HAVE_STD_SHARED_PTR
using std::shared_ptr; using std::shared_ptr;
using std::make_shared;
#else #else
#ifdef HAVE_STD_TR1_SHARED_PTR #ifdef HAVE_STD_TR1_SHARED_PTR
using std::tr1::shared_ptr; using std::tr1::shared_ptr;
// make_shared emulation.
template<typename Tp, typename A1>
inline shared_ptr<Tp>
make_shared(const A1& a1)
{
return shared_ptr<Tp>(new Tp(a1));
}
template<typename Tp, typename A1, typename A2>
inline shared_ptr<Tp>
make_shared(const A1& a1, const A2& a2)
{
return shared_ptr<Tp>(new Tp(a1, a2));
}
template<typename Tp, typename A1, typename A2, typename A3>
inline shared_ptr<Tp>
make_shared(const A1& a1, const A2& a2, const A3& a3)
{
return shared_ptr<Tp>(new Tp(a1, a2, a3));
}
template<typename Tp, typename A1, typename A2, typename A3, typename A4>
inline shared_ptr<Tp>
make_shared(const A1& a1, const A2& a2, const A3& a3, const A4& a4)
{
return shared_ptr<Tp>(new Tp(a1, a2, a3, a4));
}
#else #else
#error Need a shared_ptr class. Try Boost::TR1. #error Need a shared_ptr class. Try Boost::TR1.
#endif #endif