From cc522f037f128aaf1d1ab0a0ce08f12deab90aac Mon Sep 17 00:00:00 2001 From: Daniel Arndt <daniel.arndt@iwr.uni-heidelberg.de> Date: Sat, 10 Feb 2018 15:18:07 +0100 Subject: [PATCH] Use make_unique/shared in base --- include/deal.II/base/mg_level_object.h | 2 +- include/deal.II/base/parallel.h | 4 +- include/deal.II/base/parameter_handler.h | 8 ++-- include/deal.II/base/polynomial.h | 4 +- include/deal.II/base/thread_management.h | 6 +-- source/base/logstream.cc | 2 +- source/base/parameter_handler.cc | 4 +- source/base/patterns.cc | 58 ++++++++++++------------ source/base/polynomial.cc | 38 ++++++++-------- source/base/quadrature.cc | 4 +- 10 files changed, 65 insertions(+), 65 deletions(-) diff --git a/include/deal.II/base/mg_level_object.h b/include/deal.II/base/mg_level_object.h index 68799ba890..feb0f95b01 100644 --- a/include/deal.II/base/mg_level_object.h +++ b/include/deal.II/base/mg_level_object.h @@ -215,7 +215,7 @@ MGLevelObject<Object>::resize (const unsigned int new_minlevel, minlevel = new_minlevel; for (unsigned int i=0; i<new_maxlevel-new_minlevel+1; ++i) - objects.push_back(std::shared_ptr<Object> (new Object)); + objects.push_back(std::make_shared<Object> ()); } diff --git a/include/deal.II/base/parallel.h b/include/deal.II/base/parallel.h index 75102edd49..ad3e37e36c 100644 --- a/include/deal.II/base/parallel.h +++ b/include/deal.II/base/parallel.h @@ -706,7 +706,7 @@ namespace parallel TBBPartitioner() #ifdef DEAL_II_WITH_THREADS : - my_partitioner(new tbb::affinity_partitioner()), + my_partitioner(std::make_shared<tbb::affinity_partitioner>()), in_use(false) #endif {} @@ -734,7 +734,7 @@ namespace parallel { dealii::Threads::Mutex::ScopedLock lock(mutex); if (in_use) - return std::shared_ptr<tbb::affinity_partitioner>(new tbb::affinity_partitioner()); + return std::make_shared<tbb::affinity_partitioner>(); in_use = true; return my_partitioner; diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index 2bd9204fb4..e5bc57cb97 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -1468,7 +1468,7 @@ private: * object. Every nodes in the property tree corresponding to a parameter * stores an index into this array. */ - std::vector<std::shared_ptr<const Patterns::PatternBase> > patterns; + std::vector<std::unique_ptr<const Patterns::PatternBase> > patterns; /** * A list of actions that are associated with parameters. These @@ -1618,7 +1618,7 @@ private: * * static void declare_parameters (ParameterHandler &prm); * private: - * std::shared_ptr<Problem> p; + * std::unique_ptr<Problem> p; * }; * * @@ -1627,7 +1627,7 @@ private: * * void HelperClass::create_new (const unsigned int run_no) * { - * p.reset(new Problem()); + * p = std_cxx14::make_unique<Problem>()); * } * * @@ -1991,7 +1991,7 @@ ParameterHandler::load (Archive &ar, const unsigned int) patterns.clear (); for (unsigned int j=0; j<descriptions.size(); ++j) - patterns.push_back (std::shared_ptr<const Patterns::PatternBase>(Patterns::pattern_factory(descriptions[j]))); + patterns.push_back (Patterns::pattern_factory(descriptions[j])); } diff --git a/include/deal.II/base/polynomial.h b/include/deal.II/base/polynomial.h index 6b949900b2..c78532899f 100644 --- a/include/deal.II/base/polynomial.h +++ b/include/deal.II/base/polynomial.h @@ -531,10 +531,10 @@ namespace Polynomials * polynomial, we keep one pointer to the list of coefficients; we do so * rather than keeping a vector of vectors in order to simplify * programming multithread-safe. In order to avoid memory leak, we use a - * shared_ptr in order to correctly free the memory of the vectors when + * unique_ptr in order to correctly free the memory of the vectors when * the global destructor is called. */ - static std::vector<std::shared_ptr<const std::vector<double> > > recursive_coefficients; + static std::vector<std::unique_ptr<const std::vector<double> > > recursive_coefficients; }; diff --git a/include/deal.II/base/thread_management.h b/include/deal.II/base/thread_management.h index 2cc1fccbc5..006f89fdbb 100644 --- a/include/deal.II/base/thread_management.h +++ b/include/deal.II/base/thread_management.h @@ -863,7 +863,7 @@ namespace Threads void start (const std::function<RT ()> &function) { thread_is_active = true; - ret_val.reset(new return_value<RT>()); + ret_val = std::make_shared<return_value<RT>>(); thread = std::thread (thread_entry_point, function, ret_val); } @@ -943,7 +943,7 @@ namespace Threads */ void start (const std::function<RT ()> &function) { - ret_val.reset(new return_value<RT>()); + ret_val = std::make_shared<return_value<RT>>(); call (function, *ret_val); } @@ -1629,7 +1629,7 @@ namespace Threads { // create a task descriptor and tell it to queue itself up with // the scheduling system - task_descriptor.reset (new internal::TaskDescriptor<RT>(function_object)); + task_descriptor = std::make_shared<internal::TaskDescriptor<RT>>(function_object); task_descriptor->queue_task (); } diff --git a/source/base/logstream.cc b/source/base/logstream.cc index 5040fde7e4..349b0461b1 100644 --- a/source/base/logstream.cc +++ b/source/base/logstream.cc @@ -253,7 +253,7 @@ LogStream::get_stream() // there can only be one access at a time if (outstreams.get().get() == nullptr) { - outstreams.get().reset (new std::ostringstream); + outstreams.get() = std::make_shared<std::ostringstream>(); outstreams.get()->setf(std::ios::showpoint | std::ios::left); } diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index 71c42099d2..506bffeae1 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -458,7 +458,7 @@ namespace read_xml_recursively (const boost::property_tree::ptree &source, const std::string ¤t_path, const char path_separator, - const std::vector<std::shared_ptr<const Patterns::PatternBase> > & + const std::vector<std::unique_ptr<const Patterns::PatternBase> > & patterns, boost::property_tree::ptree &destination) { @@ -586,7 +586,7 @@ void ParameterHandler::parse_input_from_xml (std::istream &in) void ParameterHandler::clear () { - entries.reset (new boost::property_tree::ptree()); + entries = std_cxx14::make_unique<boost::property_tree::ptree> (); } diff --git a/source/base/patterns.cc b/source/base/patterns.cc index 5dfe823970..e0241fef88 100644 --- a/source/base/patterns.cc +++ b/source/base/patterns.cc @@ -322,17 +322,17 @@ namespace Patterns is.ignore(strlen(description_init) + strlen(" range ")); if (!(is >> lower_bound)) - return std::unique_ptr<Integer>(new Integer()); + return std_cxx14::make_unique<Integer>(); is.ignore(strlen("...")); if (!(is >> upper_bound)) - return std::unique_ptr<Integer>(new Integer()); + return std_cxx14::make_unique<Integer>(); - return std::unique_ptr<Integer>(new Integer(lower_bound, upper_bound)); + return std_cxx14::make_unique<Integer>(lower_bound, upper_bound); } else - return std::unique_ptr<Integer>(new Integer()); + return std_cxx14::make_unique<Integer>(); } else return std::unique_ptr<Integer>(); @@ -481,7 +481,7 @@ namespace Patterns std::string temp = description.substr(description_init_str.size()); if (temp == "]") - return std::unique_ptr<Double>(new Double(1.0, -1.0)); // return an invalid range + return std_cxx14::make_unique<Double>(1.0, -1.0); // return an invalid range if (temp.find("...") != std::string::npos) temp.replace(temp.find("..."), 3, " "); @@ -504,7 +504,7 @@ namespace Patterns if (is.fail()) upper_bound = max_double_value; - return std::unique_ptr<Double>(new Double(lower_bound, upper_bound)); + return std_cxx14::make_unique<Double>(lower_bound, upper_bound); } @@ -613,7 +613,7 @@ namespace Patterns sequence.erase(0, std::strlen(description_init) + 1); sequence.erase(sequence.length()-2, 2); - return std::unique_ptr<Selection>(new Selection(sequence)); + return std_cxx14::make_unique<Selection>(sequence); } else return std::unique_ptr<Selection>(); @@ -753,7 +753,7 @@ namespace Patterns { if (description.compare(0, std::strlen(description_init), description_init) == 0) { - int min_elements, max_elements; + unsigned int min_elements = 0, max_elements = 0; std::istringstream is(description); is.ignore(strlen(description_init) + strlen(" of <")); @@ -761,24 +761,24 @@ namespace Patterns std::string str; std::getline(is, str, '>'); - std::shared_ptr<PatternBase> base_pattern (pattern_factory(str)); + std::unique_ptr<PatternBase> base_pattern (pattern_factory(str)); is.ignore(strlen(" of length ")); if (!(is >> min_elements)) - return std::unique_ptr<List>(new List(*base_pattern)); + return std_cxx14::make_unique<List>(*base_pattern); is.ignore(strlen("...")); if (!(is >> max_elements)) - return std::unique_ptr<List>(new List(*base_pattern, min_elements)); + return std_cxx14::make_unique<List>(*base_pattern, min_elements); is.ignore(strlen(" (inclusive) separated by <")); std::string separator; - if (is) + if (!is.eof()) std::getline(is, separator, '>'); else separator = ","; - return std::unique_ptr<List>(new List(*base_pattern, min_elements, max_elements, separator)); + return std_cxx14::make_unique<List>(*base_pattern, min_elements, max_elements, separator); } else return std::unique_ptr<List>(); @@ -937,7 +937,7 @@ namespace Patterns { if (description.compare(0, std::strlen(description_init), description_init) == 0) { - int min_elements, max_elements; + unsigned int min_elements = 0, max_elements = 0; std::istringstream is(description); is.ignore(strlen(description_init) + strlen(" of <")); @@ -952,27 +952,27 @@ namespace Patterns std::string value; std::getline(is, value, '>'); - std::shared_ptr<PatternBase> key_pattern (pattern_factory(key)); - std::shared_ptr<PatternBase> value_pattern (pattern_factory(value)); + std::unique_ptr<PatternBase> key_pattern (pattern_factory(key)); + std::unique_ptr<PatternBase> value_pattern (pattern_factory(value)); is.ignore(strlen(" of length ")); if (!(is >> min_elements)) - return std::unique_ptr<Map>(new Map(*key_pattern, *value_pattern)); + return std_cxx14::make_unique<Map>(*key_pattern, *value_pattern); is.ignore(strlen("...")); if (!(is >> max_elements)) - return std::unique_ptr<Map>(new Map(*key_pattern, *value_pattern, min_elements)); + return std_cxx14::make_unique<Map>(*key_pattern, *value_pattern, min_elements); is.ignore(strlen(" (inclusive) separated by <")); std::string separator; - if (is) + if (!is.eof()) std::getline(is, separator, '>'); else separator = ","; - return std::unique_ptr<Map>(new Map(*key_pattern, *value_pattern, - min_elements, max_elements, - separator, key_value_separator)); + return std_cxx14::make_unique<Map>(*key_pattern, *value_pattern, + min_elements, max_elements, + separator, key_value_separator); } else return std::unique_ptr<Map>(); @@ -1166,12 +1166,12 @@ namespace Patterns is.ignore(strlen(" separated by <")); std::string separator; - if (is) + if (!is.eof()) std::getline(is, separator, '>'); else separator = ":"; - return std::unique_ptr<Tuple>(new Tuple(patterns,separator)); + return std_cxx14::make_unique<Tuple>(patterns,separator); } else return std::unique_ptr<Tuple>(); @@ -1332,7 +1332,7 @@ namespace Patterns sequence.erase(0, std::strlen(description_init) + 1); sequence.erase(sequence.length()-2, 2); - return std::unique_ptr<MultipleSelection>(new MultipleSelection(sequence)); + return std_cxx14::make_unique<MultipleSelection>(sequence); } else return std::unique_ptr<MultipleSelection>(); @@ -1388,7 +1388,7 @@ namespace Patterns std::unique_ptr<Bool> Bool::create (const std::string &description) { if (description.compare(0, std::strlen(description_init), description_init) == 0) - return std::unique_ptr<Bool>(new Bool()); + return std_cxx14::make_unique<Bool>(); else return std::unique_ptr<Bool>(); } @@ -1444,7 +1444,7 @@ namespace Patterns std::unique_ptr<Anything> Anything::create (const std::string &description) { if (description.compare(0, std::strlen(description_init), description_init) == 0) - return std::unique_ptr<Anything>(new Anything()); + return std_cxx14::make_unique<Anything>(); else return std::unique_ptr<Anything>(); } @@ -1526,7 +1526,7 @@ namespace Patterns else type = output; - return std::unique_ptr<FileName>(new FileName(type)); + return std_cxx14::make_unique<FileName>(type); } else return std::unique_ptr<FileName>(); @@ -1582,7 +1582,7 @@ namespace Patterns std::unique_ptr<DirectoryName> DirectoryName::create (const std::string &description) { if (description.compare(0, std::strlen(description_init), description_init) == 0) - return std::unique_ptr<DirectoryName>(new DirectoryName()); + return std_cxx14::make_unique<DirectoryName>(); else return std::unique_ptr<DirectoryName>(); } diff --git a/source/base/polynomial.cc b/source/base/polynomial.cc index dfb805562f..c495c34d91 100644 --- a/source/base/polynomial.cc +++ b/source/base/polynomial.cc @@ -13,15 +13,15 @@ // // --------------------------------------------------------------------- -#include <deal.II/base/polynomial.h> -#include <deal.II/base/point.h> #include <deal.II/base/exceptions.h> -#include <deal.II/base/thread_management.h> +#include <deal.II/base/point.h> +#include <deal.II/base/polynomial.h> #include <deal.II/base/quadrature_lib.h> +#include <deal.II/base/std_cxx14/memory.h> +#include <deal.II/base/thread_management.h> #include <deal.II/lac/full_matrix.h> #include <deal.II/lac/lapack_full_matrix.h> - #include <cmath> #include <algorithm> #include <limits> @@ -373,11 +373,11 @@ namespace Polynomials // need to transform p into standard form as // well if necessary. copy the polynomial to // do this - std::shared_ptr<Polynomial<number> > q_data; + std::unique_ptr<Polynomial<number> > q_data; const Polynomial<number> *q = nullptr; if (p.in_lagrange_product_form == true) { - q_data.reset (new Polynomial<number>(p)); + q_data = std_cxx14::make_unique<Polynomial<number>>(p); q_data->transform_into_standard_form(); q = q_data.get(); } @@ -417,11 +417,11 @@ namespace Polynomials // need to transform p into standard form as // well if necessary. copy the polynomial to // do this - std::shared_ptr<Polynomial<number> > q_data; + std::unique_ptr<Polynomial<number> > q_data; const Polynomial<number> *q = nullptr; if (p.in_lagrange_product_form == true) { - q_data.reset (new Polynomial<number>(p)); + q_data = std_cxx14::make_unique<Polynomial<number>>(p); q_data->transform_into_standard_form(); q = q_data.get(); } @@ -453,11 +453,11 @@ namespace Polynomials // need to transform p into standard form as // well if necessary. copy the polynomial to // do this - std::shared_ptr<Polynomial<number> > q_data; + std::unique_ptr<Polynomial<number> > q_data; const Polynomial<number> *q = nullptr; if (p.in_lagrange_product_form == true) { - q_data.reset (new Polynomial<number>(p)); + q_data = std_cxx14::make_unique<Polynomial<number>>(p); q_data->transform_into_standard_form(); q = q_data.get(); } @@ -600,11 +600,11 @@ namespace Polynomials if (degree() == 0) return Monomial<number>(0, 0.); - std::shared_ptr<Polynomial<number> > q_data; + std::unique_ptr<Polynomial<number> > q_data; const Polynomial<number> *q = nullptr; if (in_lagrange_product_form == true) { - q_data.reset (new Polynomial<number>(*this)); + q_data = std_cxx14::make_unique<Polynomial<number>>(*this); q_data->transform_into_standard_form(); q = q_data.get(); } @@ -626,11 +626,11 @@ namespace Polynomials { // no simple form possible for Lagrange // polynomial on product form - std::shared_ptr<Polynomial<number> > q_data; + std::unique_ptr<Polynomial<number> > q_data; const Polynomial<number> *q = nullptr; if (in_lagrange_product_form == true) { - q_data.reset (new Polynomial<number>(*this)); + q_data = std_cxx14::make_unique<Polynomial<number>>(*this); q_data->transform_into_standard_form(); q = q_data.get(); } @@ -976,7 +976,7 @@ namespace Polynomials // Reserve space for polynomials up to degree 19. Should be sufficient // for the start. - std::vector<std::shared_ptr<const std::vector<double> > > + std::vector<std::unique_ptr<const std::vector<double> > > Hierarchical::recursive_coefficients(20); @@ -1053,9 +1053,9 @@ namespace Polynomials // now make these arrays // const recursive_coefficients[0] = - std::shared_ptr<const std::vector<double> >(c0); + std::unique_ptr<const std::vector<double> >(c0); recursive_coefficients[1] = - std::shared_ptr<const std::vector<double> >(c1); + std::unique_ptr<const std::vector<double> >(c1); } else if (k==2) { @@ -1072,7 +1072,7 @@ namespace Polynomials (*c2)[2] = 4.*a; recursive_coefficients[2] = - std::shared_ptr<const std::vector<double> >(c2); + std::unique_ptr<const std::vector<double> >(c2); } else { @@ -1116,7 +1116,7 @@ namespace Polynomials // const pointer in the // coefficients array recursive_coefficients[k] = - std::shared_ptr<const std::vector<double> >(ck); + std::unique_ptr<const std::vector<double> >(ck); }; }; } diff --git a/source/base/quadrature.cc b/source/base/quadrature.cc index acef4b5dfe..475e946be9 100644 --- a/source/base/quadrature.cc +++ b/source/base/quadrature.cc @@ -164,7 +164,7 @@ Quadrature<dim>::Quadrature (const SubQuadrature &q1, if (is_tensor_product_flag) { - tensor_basis.reset(new std::array<Quadrature<1>, dim>()); + tensor_basis = std_cxx14::make_unique<std::array<Quadrature<1>, dim>> (); for (unsigned int i=0; i<dim-1; ++i) (*tensor_basis)[i] = q1.get_tensor_basis()[i]; (*tensor_basis)[dim-1] = q2; @@ -266,7 +266,7 @@ Subscriptor(), ++k; } - tensor_basis.reset (new std::array<Quadrature<1>, dim>()); + tensor_basis = std_cxx14::make_unique<std::array<Quadrature<1>, dim>> (); for (unsigned int i=0; i<dim; ++i) (*tensor_basis)[i] = q; } -- 2.39.5