From f8dc647a0f66d5e20a23670f57c6e0faabd81531 Mon Sep 17 00:00:00 2001 From: Roland Schulz Date: Fri, 17 Oct 2014 02:29:15 -0400 Subject: [PATCH] Create scoped_cptr to replace scoped_ptr_sfree Compareed to the previous scoped_ptr_sfree, scoped_cptr has a get method, a bool-conversion, and allows to specify a custom deleter. If just a guard is required then the scoped_guard_sfree typedef can be used which doesn't require to specify a type. Change-Id: I81cb50c4cf34e2d5a93e9c4b0043afcbc5feb535 --- cmake/gmxTestCXX11.cmake | 1 + src/gromacs/selection/parser.cpp | 40 ++++++++--------- src/gromacs/selection/parser.y | 40 ++++++++--------- .../trajectoryanalysis/modules/sasa.cpp | 4 +- .../trajectoryanalysis/modules/select.cpp | 8 ++-- .../{scoped_ptr_sfree.h => scoped_cptr.h} | 44 +++++++++++++------ 6 files changed, 77 insertions(+), 60 deletions(-) rename src/gromacs/utility/{scoped_ptr_sfree.h => scoped_cptr.h} (70%) diff --git a/cmake/gmxTestCXX11.cmake b/cmake/gmxTestCXX11.cmake index eaa8888e48..b20f86fc4c 100644 --- a/cmake/gmxTestCXX11.cmake +++ b/cmake/gmxTestCXX11.cmake @@ -59,6 +59,7 @@ struct VarList { typedef VarList VarListTail; typedef std::pair ListType; }; +class a { explicit operator bool() {return true;} }; int main() { typedef std::unique_ptr intPointer; intPointer p(new int(10)); diff --git a/src/gromacs/selection/parser.cpp b/src/gromacs/selection/parser.cpp index b0beaa649c..536ee2a5d7 100644 --- a/src/gromacs/selection/parser.cpp +++ b/src/gromacs/selection/parser.cpp @@ -94,11 +94,11 @@ /* Line 371 of yacc.c */ #line 56 "parser.y" -#include "gromacs/utility/scoped_ptr_sfree.h" +#include "gromacs/utility/scoped_cptr.h" #include "parser_internal.h" -using gmx::scoped_ptr_sfree; +using gmx::scoped_guard_sfree; using gmx::SelectionParserValue; using gmx::SelectionParserValueList; using gmx::SelectionParserValueListPointer; @@ -1975,7 +1975,7 @@ yyreduce: #line 252 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree nameGuard((yyvsp[(1) - (1)].str)); + scoped_guard_sfree nameGuard((yyvsp[(1) - (1)].str)); SelectionTreeElementPointer s = _gmx_sel_init_group_by_name((yyvsp[(1) - (1)].str), scanner); SelectionTreeElementPointer p @@ -2001,7 +2001,7 @@ yyreduce: #line 270 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree nameGuard((yyvsp[(1) - (2)].str)); + scoped_guard_sfree nameGuard((yyvsp[(1) - (2)].str)); set((yyval.sel), _gmx_sel_init_selection((yyvsp[(1) - (2)].str), get((yyvsp[(2) - (2)].sel)), scanner)); END_ACTION; } @@ -2012,7 +2012,7 @@ yyreduce: #line 277 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree nameGuard((yyvsp[(1) - (3)].str)); + scoped_guard_sfree nameGuard((yyvsp[(1) - (3)].str)); set((yyval.sel), _gmx_sel_assign_variable((yyvsp[(1) - (3)].str), get((yyvsp[(3) - (3)].sel)), scanner)); END_ACTION; } @@ -2023,7 +2023,7 @@ yyreduce: #line 284 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree nameGuard((yyvsp[(1) - (3)].str)); + scoped_guard_sfree nameGuard((yyvsp[(1) - (3)].str)); set((yyval.sel), _gmx_sel_assign_variable((yyvsp[(1) - (3)].str), get((yyvsp[(3) - (3)].sel)), scanner)); END_ACTION; } @@ -2034,7 +2034,7 @@ yyreduce: #line 291 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree nameGuard((yyvsp[(1) - (3)].str)); + scoped_guard_sfree nameGuard((yyvsp[(1) - (3)].str)); set((yyval.sel), _gmx_sel_assign_variable((yyvsp[(1) - (3)].str), get((yyvsp[(3) - (3)].sel)), scanner)); END_ACTION; } @@ -2178,7 +2178,7 @@ yyreduce: #line 383 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree opGuard((yyvsp[(2) - (3)].str)); + scoped_guard_sfree opGuard((yyvsp[(2) - (3)].str)); set((yyval.sel), _gmx_sel_init_comparison(get((yyvsp[(1) - (3)].sel)), get((yyvsp[(3) - (3)].sel)), (yyvsp[(2) - (3)].str), scanner)); CHECK_SEL((yyval.sel)); END_ACTION; @@ -2190,7 +2190,7 @@ yyreduce: #line 394 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree nameGuard((yyvsp[(2) - (2)].str)); + scoped_guard_sfree nameGuard((yyvsp[(2) - (2)].str)); set((yyval.sel), _gmx_sel_init_group_by_name((yyvsp[(2) - (2)].str), scanner)); END_ACTION; } @@ -2241,7 +2241,7 @@ yyreduce: #line 422 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard((yyvsp[(1) - (2)].str)); + scoped_guard_sfree posmodGuard((yyvsp[(1) - (2)].str)); set((yyval.sel), _gmx_sel_init_keyword((yyvsp[(2) - (2)].meth), SelectionParserValueListPointer(), (yyvsp[(1) - (2)].str), scanner)); CHECK_SEL((yyval.sel)); END_ACTION; @@ -2253,7 +2253,7 @@ yyreduce: #line 430 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard((yyvsp[(1) - (3)].str)); + scoped_guard_sfree posmodGuard((yyvsp[(1) - (3)].str)); set((yyval.sel), _gmx_sel_init_keyword_strmatch((yyvsp[(2) - (3)].meth), gmx::eStringMatchType_Auto, get((yyvsp[(3) - (3)].vlist)), (yyvsp[(1) - (3)].str), scanner)); CHECK_SEL((yyval.sel)); END_ACTION; @@ -2265,7 +2265,7 @@ yyreduce: #line 438 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard((yyvsp[(1) - (4)].str)); + scoped_guard_sfree posmodGuard((yyvsp[(1) - (4)].str)); set((yyval.sel), _gmx_sel_init_keyword_strmatch((yyvsp[(2) - (4)].meth), (yyvsp[(3) - (4)].smt), get((yyvsp[(4) - (4)].vlist)), (yyvsp[(1) - (4)].str), scanner)); CHECK_SEL((yyval.sel)); END_ACTION; @@ -2277,7 +2277,7 @@ yyreduce: #line 446 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard((yyvsp[(1) - (3)].str)); + scoped_guard_sfree posmodGuard((yyvsp[(1) - (3)].str)); set((yyval.sel), _gmx_sel_init_keyword((yyvsp[(2) - (3)].meth), get((yyvsp[(3) - (3)].vlist)), (yyvsp[(1) - (3)].str), scanner)); CHECK_SEL((yyval.sel)); END_ACTION; @@ -2289,7 +2289,7 @@ yyreduce: #line 457 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard((yyvsp[(1) - (3)].str)); + scoped_guard_sfree posmodGuard((yyvsp[(1) - (3)].str)); set((yyval.sel), _gmx_sel_init_method((yyvsp[(2) - (3)].meth), get((yyvsp[(3) - (3)].plist)), (yyvsp[(1) - (3)].str), scanner)); CHECK_SEL((yyval.sel)); END_ACTION; @@ -2331,7 +2331,7 @@ yyreduce: #line 497 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard((yyvsp[(1) - (2)].str)); + scoped_guard_sfree posmodGuard((yyvsp[(1) - (2)].str)); set((yyval.sel), _gmx_sel_init_keyword((yyvsp[(2) - (2)].meth), SelectionParserValueListPointer(), (yyvsp[(1) - (2)].str), scanner)); CHECK_SEL((yyval.sel)); END_ACTION; @@ -2343,7 +2343,7 @@ yyreduce: #line 505 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard((yyvsp[(1) - (3)].str)); + scoped_guard_sfree posmodGuard((yyvsp[(1) - (3)].str)); set((yyval.sel), _gmx_sel_init_method((yyvsp[(2) - (3)].meth), get((yyvsp[(3) - (3)].plist)), (yyvsp[(1) - (3)].str), scanner)); CHECK_SEL((yyval.sel)); END_ACTION; @@ -2436,7 +2436,7 @@ yyreduce: #line 570 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard((yyvsp[(1) - (2)].str)); + scoped_guard_sfree posmodGuard((yyvsp[(1) - (2)].str)); set((yyval.sel), _gmx_sel_init_keyword((yyvsp[(2) - (2)].meth), SelectionParserValueListPointer(), (yyvsp[(1) - (2)].str), scanner)); CHECK_SEL((yyval.sel)); END_ACTION; @@ -2475,7 +2475,7 @@ yyreduce: #line 608 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree keywordGuard((yyvsp[(1) - (3)].str)); + scoped_guard_sfree keywordGuard((yyvsp[(1) - (3)].str)); set((yyval.sel), _gmx_sel_init_position(get((yyvsp[(3) - (3)].sel)), (yyvsp[(1) - (3)].str), scanner)); CHECK_SEL((yyval.sel)); END_ACTION; @@ -2551,7 +2551,7 @@ yyreduce: #line 675 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree nameGuard((yyvsp[(1) - (2)].str)); + scoped_guard_sfree nameGuard((yyvsp[(1) - (2)].str)); set((yyval.param), SelectionParserParameter::create((yyvsp[(1) - (2)].str), get((yyvsp[(2) - (2)].vlist)))); END_ACTION; } @@ -2720,7 +2720,7 @@ yyreduce: #line 783 "parser.y" { BEGIN_ACTION; - scoped_ptr_sfree stringGuard((yyvsp[(1) - (1)].str)); + scoped_guard_sfree stringGuard((yyvsp[(1) - (1)].str)); set((yyval.val), SelectionParserValue::createString((yyvsp[(1) - (1)].str))); END_ACTION; } diff --git a/src/gromacs/selection/parser.y b/src/gromacs/selection/parser.y index c90c582998..bbd080f8fa 100644 --- a/src/gromacs/selection/parser.y +++ b/src/gromacs/selection/parser.y @@ -54,11 +54,11 @@ #include "gmxpre.h" } %{ -#include "gromacs/utility/scoped_ptr_sfree.h" +#include "gromacs/utility/scoped_cptr.h" #include "parser_internal.h" -using gmx::scoped_ptr_sfree; +using gmx::scoped_guard_sfree; using gmx::SelectionParserValue; using gmx::SelectionParserValueList; using gmx::SelectionParserValueListPointer; @@ -251,7 +251,7 @@ cmd_plain: /* empty */ | string { BEGIN_ACTION; - scoped_ptr_sfree nameGuard($1); + scoped_guard_sfree nameGuard($1); SelectionTreeElementPointer s = _gmx_sel_init_group_by_name($1, scanner); SelectionTreeElementPointer p @@ -269,28 +269,28 @@ cmd_plain: /* empty */ | string selection { BEGIN_ACTION; - scoped_ptr_sfree nameGuard($1); + scoped_guard_sfree nameGuard($1); set($$, _gmx_sel_init_selection($1, get($2), scanner)); END_ACTION; } | IDENTIFIER '=' sel_expr { BEGIN_ACTION; - scoped_ptr_sfree nameGuard($1); + scoped_guard_sfree nameGuard($1); set($$, _gmx_sel_assign_variable($1, get($3), scanner)); END_ACTION; } | IDENTIFIER '=' num_expr { BEGIN_ACTION; - scoped_ptr_sfree nameGuard($1); + scoped_guard_sfree nameGuard($1); set($$, _gmx_sel_assign_variable($1, get($3), scanner)); END_ACTION; } | IDENTIFIER '=' pos_expr { BEGIN_ACTION; - scoped_ptr_sfree nameGuard($1); + scoped_guard_sfree nameGuard($1); set($$, _gmx_sel_assign_variable($1, get($3), scanner)); END_ACTION; } @@ -382,7 +382,7 @@ sel_expr: NOT sel_expr sel_expr: num_expr CMP_OP num_expr { BEGIN_ACTION; - scoped_ptr_sfree opGuard($2); + scoped_guard_sfree opGuard($2); set($$, _gmx_sel_init_comparison(get($1), get($3), $2, scanner)); CHECK_SEL($$); END_ACTION; @@ -393,7 +393,7 @@ sel_expr: num_expr CMP_OP num_expr sel_expr: GROUP string { BEGIN_ACTION; - scoped_ptr_sfree nameGuard($2); + scoped_guard_sfree nameGuard($2); set($$, _gmx_sel_init_group_by_name($2, scanner)); END_ACTION; } @@ -421,7 +421,7 @@ str_match_type: sel_expr: pos_mod KEYWORD_GROUP { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard($1); + scoped_guard_sfree posmodGuard($1); set($$, _gmx_sel_init_keyword($2, SelectionParserValueListPointer(), $1, scanner)); CHECK_SEL($$); END_ACTION; @@ -429,7 +429,7 @@ sel_expr: pos_mod KEYWORD_GROUP | pos_mod KEYWORD_STR basic_value_list { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard($1); + scoped_guard_sfree posmodGuard($1); set($$, _gmx_sel_init_keyword_strmatch($2, gmx::eStringMatchType_Auto, get($3), $1, scanner)); CHECK_SEL($$); END_ACTION; @@ -437,7 +437,7 @@ sel_expr: pos_mod KEYWORD_GROUP | pos_mod KEYWORD_STR str_match_type basic_value_list { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard($1); + scoped_guard_sfree posmodGuard($1); set($$, _gmx_sel_init_keyword_strmatch($2, $3, get($4), $1, scanner)); CHECK_SEL($$); END_ACTION; @@ -445,7 +445,7 @@ sel_expr: pos_mod KEYWORD_GROUP | pos_mod KEYWORD_NUMERIC basic_value_list { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard($1); + scoped_guard_sfree posmodGuard($1); set($$, _gmx_sel_init_keyword($2, get($3), $1, scanner)); CHECK_SEL($$); END_ACTION; @@ -456,7 +456,7 @@ sel_expr: pos_mod KEYWORD_GROUP sel_expr: pos_mod METHOD_GROUP method_params { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard($1); + scoped_guard_sfree posmodGuard($1); set($$, _gmx_sel_init_method($2, get($3), $1, scanner)); CHECK_SEL($$); END_ACTION; @@ -496,7 +496,7 @@ num_expr: TOK_INT num_expr: pos_mod KEYWORD_NUMERIC %prec NUM_REDUCT { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard($1); + scoped_guard_sfree posmodGuard($1); set($$, _gmx_sel_init_keyword($2, SelectionParserValueListPointer(), $1, scanner)); CHECK_SEL($$); END_ACTION; @@ -504,7 +504,7 @@ num_expr: pos_mod KEYWORD_NUMERIC %prec NUM_REDUCT | pos_mod METHOD_NUMERIC method_params { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard($1); + scoped_guard_sfree posmodGuard($1); set($$, _gmx_sel_init_method($2, get($3), $1, scanner)); CHECK_SEL($$); END_ACTION; @@ -569,7 +569,7 @@ str_expr: string | pos_mod KEYWORD_STR { BEGIN_ACTION; - scoped_ptr_sfree posmodGuard($1); + scoped_guard_sfree posmodGuard($1); set($$, _gmx_sel_init_keyword($2, SelectionParserValueListPointer(), $1, scanner)); CHECK_SEL($$); END_ACTION; @@ -607,7 +607,7 @@ pos_expr: METHOD_POS method_params pos_expr: KEYWORD_POS OF sel_expr %prec PARAM_REDUCT { BEGIN_ACTION; - scoped_ptr_sfree keywordGuard($1); + scoped_guard_sfree keywordGuard($1); set($$, _gmx_sel_init_position(get($3), $1, scanner)); CHECK_SEL($$); END_ACTION; @@ -674,7 +674,7 @@ method_param: PARAM value_list { BEGIN_ACTION; - scoped_ptr_sfree nameGuard($1); + scoped_guard_sfree nameGuard($1); set($$, SelectionParserParameter::create($1, get($2))); END_ACTION; } @@ -782,7 +782,7 @@ basic_value_item: | string %prec PARAM_REDUCT { BEGIN_ACTION; - scoped_ptr_sfree stringGuard($1); + scoped_guard_sfree stringGuard($1); set($$, SelectionParserValue::createString($1)); END_ACTION; } diff --git a/src/gromacs/trajectoryanalysis/modules/sasa.cpp b/src/gromacs/trajectoryanalysis/modules/sasa.cpp index 6fec3377ab..3e5f2543d8 100644 --- a/src/gromacs/trajectoryanalysis/modules/sasa.cpp +++ b/src/gromacs/trajectoryanalysis/modules/sasa.cpp @@ -71,7 +71,7 @@ #include "gromacs/trajectoryanalysis/analysissettings.h" #include "gromacs/utility/exceptions.h" #include "gromacs/utility/futil.h" -#include "gromacs/utility/scoped_ptr_sfree.h" +#include "gromacs/utility/scoped_cptr.h" #include "gromacs/utility/smalloc.h" #include "gromacs/utility/stringutil.h" @@ -942,7 +942,7 @@ Sasa::analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc, } sfree(area); } - scoped_ptr_sfree dotsGuard(surfacedots); + scoped_guard_sfree dotsGuard(surfacedots); if (retval != 0) { GMX_THROW(InternalError("nsc_dclm_pbc failed")); diff --git a/src/gromacs/trajectoryanalysis/modules/select.cpp b/src/gromacs/trajectoryanalysis/modules/select.cpp index 909c70d6af..443c4a129c 100644 --- a/src/gromacs/trajectoryanalysis/modules/select.cpp +++ b/src/gromacs/trajectoryanalysis/modules/select.cpp @@ -70,7 +70,7 @@ #include "gromacs/utility/arrayref.h" #include "gromacs/utility/exceptions.h" #include "gromacs/utility/gmxassert.h" -#include "gromacs/utility/scoped_ptr_sfree.h" +#include "gromacs/utility/scoped_cptr.h" #include "gromacs/utility/smalloc.h" #include "gromacs/utility/stringutil.h" @@ -652,11 +652,11 @@ Select::writeOutput() { GMX_RELEASE_ASSERT(top_->hasTopology(), "Topology should have been loaded or an error given earlier"); - t_atoms atoms; + t_atoms atoms; atoms = top_->topology()->atoms; - t_pdbinfo *pdbinfo; + t_pdbinfo *pdbinfo; snew(pdbinfo, atoms.nr); - scoped_ptr_sfree pdbinfoGuard(pdbinfo); + scoped_guard_sfree pdbinfoGuard(pdbinfo); if (atoms.pdbinfo != NULL) { std::memcpy(pdbinfo, atoms.pdbinfo, atoms.nr*sizeof(*pdbinfo)); diff --git a/src/gromacs/utility/scoped_ptr_sfree.h b/src/gromacs/utility/scoped_cptr.h similarity index 70% rename from src/gromacs/utility/scoped_ptr_sfree.h rename to src/gromacs/utility/scoped_cptr.h index 7078a80aeb..f4b9ada399 100644 --- a/src/gromacs/utility/scoped_ptr_sfree.h +++ b/src/gromacs/utility/scoped_cptr.h @@ -34,7 +34,7 @@ */ /*! \libinternal \file * \brief - * Declares gmx::scoped_ptr_sfree. + * Declares gmx::scoped_cptr and gmx::scoped_guard_sfree. * * \author Teemu Murtola * \inlibraryapi @@ -43,30 +43,36 @@ #ifndef GMX_UTILITY_SCOPED_PTR_SFREE_H #define GMX_UTILITY_SCOPED_PTR_SFREE_H +#include "config.h" + #include "gromacs/utility/common.h" #include "gromacs/utility/smalloc.h" namespace gmx { +//! sfree wrapper to be used as scoped_cptr deleter +template +inline void sfree_wrapper(T *p) +{ + sfree(p); +} + /*! \libinternal \brief - * Stripped-down version of scoped_ptr that uses sfree(). + * Stripped-down version of scoped_ptr that uses sfree() or custom deleter. * - * Currently only implements constructor from a pointer value and destructor; - * other operations can be added if they become necessary. + * Currently only implements some operations; other operations can be added + * if they become necessary. * - * This class provides a very basic guard/smart pointer for C pointers. - * Currently, boost::shared_ptr is used in a few locations that require more - * flexibility, but is not suitable for all cases either. A scoped_ptr with - * deleter support would be a general enough implementation for all uses. - * C++11 unique_ptr has this, but for non-C++11 support we need something else. + * This class provides a basic guard/smart pointer for C pointers. * * Methods in this class do not throw. * * \inlibraryapi * \ingroup module_utility */ -class scoped_ptr_sfree +template +class scoped_cptr { public: /*! \brief @@ -74,16 +80,26 @@ class scoped_ptr_sfree * * \param[in] ptr Pointer to use for initialization. */ - explicit scoped_ptr_sfree(void *ptr) : ptr_(ptr) {} + explicit scoped_cptr(T *ptr) : ptr_(ptr) {} //! Frees the pointer passed to the constructor. - ~scoped_ptr_sfree() { sfree(ptr_); } + ~scoped_cptr() { D(ptr_); } + //! Returns the stored pointer. + T * get() const { return ptr_; } + //! Check for non-null pointer in boolean context. +#ifdef GMX_CXX11 + explicit +#endif + operator bool () const { return ptr_ != 0; } private: - void *ptr_; + T *ptr_; - GMX_DISALLOW_COPY_AND_ASSIGN(scoped_ptr_sfree); + GMX_DISALLOW_COPY_AND_ASSIGN(scoped_cptr); }; +//! Simple guard which calls sfree. See scoped_cptr for details. +typedef scoped_cptr scoped_guard_sfree; + } // namespace gmx #endif -- 2.22.0