From: Luca Heltai Date: Mon, 12 Jun 2017 10:48:33 +0000 (+0200) Subject: Make clone and crete return std::unique_ptr in Patterns. X-Git-Tag: v9.0.0-rc1~1511^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a645a7bde6c8bb54a1ff58f1bb670fd8d1e1e86d;p=dealii.git Make clone and crete return std::unique_ptr in Patterns. --- diff --git a/doc/news/changes/minor/20170612LucaHeltai b/doc/news/changes/minor/20170612LucaHeltai new file mode 100644 index 0000000000..bc264a3c02 --- /dev/null +++ b/doc/news/changes/minor/20170612LucaHeltai @@ -0,0 +1,5 @@ +Fixed: Patterns derived from PatternBase now return +std::unique_ptr in their clone() and +create() functions. +
+(Luca Heltai, 2017/06/12) diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index afcf791a43..9e35f26f48 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -107,7 +107,7 @@ namespace Patterns * Ownership of the objects returned by this function is passed to the * caller of this function. */ - virtual PatternBase *clone () const = 0; + virtual std::unique_ptr clone () const = 0; /** * Determine an estimate for the memory consumption (in bytes) of this @@ -130,7 +130,7 @@ namespace Patterns /** * Return pointer to the correct derived class based on description. */ - PatternBase *pattern_factory (const std::string &description); + std::unique_ptr pattern_factory (const std::string &description); /** * Test for the string being an integer. If bounds are given to the @@ -193,14 +193,14 @@ namespace Patterns * heap. Ownership of that object is transferred to the caller of this * function. */ - virtual PatternBase *clone () const; + virtual std::unique_ptr clone () const; /** * Creates new object if the start of description matches * description_init. Ownership of that object is transferred to the * caller of this function. */ - static Integer *create (const std::string &description); + static std::unique_ptr create (const std::string &description); private: /** @@ -285,7 +285,7 @@ namespace Patterns * heap. Ownership of that object is transferred to the caller of this * function. */ - virtual PatternBase *clone () const; + virtual std::unique_ptr clone () const; /** * Creates a new object on the heap using @p new if the given @@ -294,7 +294,7 @@ namespace Patterns * of the returned object is transferred to the caller of this function, * which should be freed using @p delete. */ - static Double *create (const std::string &description); + static std::unique_ptr create(const std::string &description); private: /** @@ -355,7 +355,7 @@ namespace Patterns * heap. Ownership of that object is transferred to the caller of this * function. */ - virtual PatternBase *clone () const; + virtual std::unique_ptr clone () const; /** * Determine an estimate for the memory consumption (in bytes) of this @@ -368,7 +368,7 @@ namespace Patterns * description_init. Ownership of that object is transferred to the * caller of this function. */ - static Selection *create (const std::string &description); + static std::unique_ptr create (const std::string &description); private: /** @@ -436,14 +436,14 @@ namespace Patterns * heap. Ownership of that object is transferred to the caller of this * function. */ - virtual PatternBase *clone () const; + virtual std::unique_ptr clone () const; /** * Creates new object if the start of description matches * description_init. Ownership of that object is transferred to the * caller of this function. */ - static List *create (const std::string &description); + static std::unique_ptr create (const std::string &description); /** * Determine an estimate for the memory consumption (in bytes) of this @@ -551,14 +551,14 @@ namespace Patterns * heap. Ownership of that object is transferred to the caller of this * function. */ - virtual PatternBase *clone () const; + virtual std::unique_ptr clone () const; /** * Creates new object if the start of description matches * description_init. Ownership of that object is transferred to the * caller of this function. */ - static Map *create (const std::string &description); + static std::unique_ptr create (const std::string &description); /** * Determine an estimate for the memory consumption (in bytes) of this @@ -645,14 +645,14 @@ namespace Patterns * heap. Ownership of that object is transferred to the caller of this * function. */ - virtual PatternBase *clone () const; + virtual std::unique_ptr clone () const; /** * Creates new object if the start of description matches * description_init. Ownership of that object is transferred to the * caller of this function. */ - static MultipleSelection *create (const std::string &description); + static std::unique_ptr create (const std::string &description); /** * Determine an estimate for the memory consumption (in bytes) of this @@ -709,14 +709,14 @@ namespace Patterns * heap. Ownership of that object is transferred to the caller of this * function. */ - virtual PatternBase *clone () const; + virtual std::unique_ptr clone () const; /** * Creates new object if the start of description matches * description_init. Ownership of that object is transferred to the * caller of this function. */ - static Bool *create (const std::string &description); + static std::unique_ptr create(const std::string &description); private: /** @@ -754,14 +754,14 @@ namespace Patterns * heap. Ownership of that object is transferred to the caller of this * function. */ - virtual PatternBase *clone () const; + virtual std::unique_ptr clone () const; /** * Creates new object if the start of description matches * description_init. Ownership of that object is transferred to the * caller of this function. */ - static Anything *create (const std::string &description); + static std::unique_ptr create(const std::string &description); private: /** @@ -827,7 +827,7 @@ namespace Patterns * heap. Ownership of that object is transferred to the caller of this * function. */ - virtual PatternBase *clone () const; + virtual std::unique_ptr clone () const; /** * file type flag @@ -839,7 +839,7 @@ namespace Patterns * description_init. Ownership of that object is transferred to the * caller of this function. */ - static FileName *create (const std::string &description); + static std::unique_ptr create (const std::string &description); private: /** @@ -886,14 +886,14 @@ namespace Patterns * heap. Ownership of that object is transferred to the caller of this * function. */ - virtual PatternBase *clone () const; + virtual std::unique_ptr clone () const; /** * Creates new object if the start of description matches * description_init. Ownership of that object is transferred to the * caller of this function. */ - static DirectoryName *create (const std::string &description); + static std::unique_ptr create(const std::string &description); private: /** diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index 9d2f8b9995..7e9db01526 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -74,9 +74,9 @@ namespace Patterns - PatternBase *pattern_factory (const std::string &description) + std::unique_ptr pattern_factory(const std::string &description) { - PatternBase *p; + std::unique_ptr p; p = Integer::create(description); if (p != nullptr) @@ -116,7 +116,7 @@ namespace Patterns Assert(false, ExcNotImplemented()); - return nullptr; + return p; } @@ -241,15 +241,14 @@ namespace Patterns - PatternBase * - Integer::clone () const + std::unique_ptr Integer::clone() const { - return new Integer(lower_bound, upper_bound); + return std::unique_ptr(new Integer(lower_bound, upper_bound)); } - Integer *Integer::create (const std::string &description) + std::unique_ptr Integer::create(const std::string &description) { if (description.compare(0, std::strlen(description_init), description_init) == 0) { @@ -263,20 +262,20 @@ namespace Patterns is.ignore(strlen(description_init) + strlen(" range ")); if (!(is >> lower_bound)) - return new Integer(); + return std::unique_ptr(new Integer()); is.ignore(strlen("...")); if (!(is >> upper_bound)) - return new Integer(); + return std::unique_ptr(new Integer()); - return new Integer(lower_bound, upper_bound); + return std::unique_ptr(new Integer(lower_bound, upper_bound)); } else - return new Integer(); + return std::unique_ptr(new Integer()); } else - return nullptr; + return std::unique_ptr(); } @@ -404,25 +403,24 @@ namespace Patterns } - PatternBase * - Double::clone () const + std::unique_ptr Double::clone() const { - return new Double(lower_bound, upper_bound); + return std::unique_ptr(new Double(lower_bound, upper_bound)); } - Double *Double::create (const std::string &description) + std::unique_ptr Double::create (const std::string &description) { const std::string description_init_str = description_init; if (description.compare(0, description_init_str.size(), description_init_str) != 0) - return nullptr; + return std::unique_ptr(); if (*description.rbegin() != ']') - return nullptr; + return std::unique_ptr(); std::string temp = description.substr(description_init_str.size()); if (temp == "]") - return new Double(1.0, -1.0); // return an invalid range + return std::unique_ptr(new Double(1.0, -1.0)); // return an invalid range if (temp.find("...") != std::string::npos) temp.replace(temp.find("..."), 3, " "); @@ -437,7 +435,7 @@ namespace Patterns { // parse lower bound and give up if not a double if (!(is >> lower_bound)) - return nullptr; + return std::unique_ptr(); } // ignore failure here and assume we got MAX_DOUBLE as upper bound: @@ -445,7 +443,7 @@ namespace Patterns if (is.fail()) upper_bound = max_double_value; - return new Double(lower_bound, upper_bound); + return std::unique_ptr(new Double(lower_bound, upper_bound)); } @@ -530,10 +528,9 @@ namespace Patterns - PatternBase * - Selection::clone () const + std::unique_ptr Selection::clone() const { - return new Selection(sequence); + return std::unique_ptr(new Selection(sequence)); } @@ -546,7 +543,7 @@ namespace Patterns - Selection *Selection::create (const std::string &description) + std::unique_ptr Selection::create(const std::string &description) { if (description.compare(0, std::strlen(description_init), description_init) == 0) { @@ -555,10 +552,10 @@ namespace Patterns sequence.erase(0, std::strlen(description_init) + 1); sequence.erase(sequence.length()-2, 2); - return new Selection(sequence); + return std::unique_ptr(new Selection(sequence)); } else - return nullptr; + return std::unique_ptr(); } @@ -686,10 +683,9 @@ namespace Patterns - PatternBase * - List::clone () const + std::unique_ptr List::clone() const { - return new List(*pattern, min_elements, max_elements, separator); + return std::unique_ptr(new List(*pattern, min_elements, max_elements, separator)); } @@ -703,7 +699,7 @@ namespace Patterns - List *List::create (const std::string &description) + std::unique_ptr List::create (const std::string &description) { if (description.compare(0, std::strlen(description_init), description_init) == 0) { @@ -719,11 +715,11 @@ namespace Patterns is.ignore(strlen(" of length ")); if (!(is >> min_elements)) - return new List(*base_pattern); + return std::unique_ptr(new List(*base_pattern)); is.ignore(strlen("...")); if (!(is >> max_elements)) - return new List(*base_pattern, min_elements); + return std::unique_ptr(new List(*base_pattern, min_elements)); is.ignore(strlen(" separated by <")); std::string separator; @@ -732,10 +728,10 @@ namespace Patterns else separator = ","; - return new List(*base_pattern, min_elements, max_elements, separator); + return std::unique_ptr(new List(*base_pattern, min_elements, max_elements, separator)); } else - return nullptr; + return std::unique_ptr(); } @@ -895,12 +891,11 @@ namespace Patterns - PatternBase * - Map::clone () const + std::unique_ptr Map::clone() const { - return new Map(*key_pattern, *value_pattern, - min_elements, max_elements, - separator); + return std::unique_ptr(new Map(*key_pattern, *value_pattern, + min_elements, max_elements, + separator)); } @@ -915,7 +910,7 @@ namespace Patterns - Map *Map::create (const std::string &description) + std::unique_ptr Map::create (const std::string &description) { if (description.compare(0, std::strlen(description_init), description_init) == 0) { @@ -939,11 +934,11 @@ namespace Patterns is.ignore(strlen(" of length ")); if (!(is >> min_elements)) - return new Map(*key_pattern, *value_pattern); + return std::unique_ptr(new Map(*key_pattern, *value_pattern)); is.ignore(strlen("...")); if (!(is >> max_elements)) - return new Map(*key_pattern, *value_pattern, min_elements); + return std::unique_ptr(new Map(*key_pattern, *value_pattern, min_elements)); is.ignore(strlen(" separated by <")); std::string separator; @@ -952,12 +947,12 @@ namespace Patterns else separator = ","; - return new Map(*key_pattern, *value_pattern, - min_elements, max_elements, - separator); + return std::unique_ptr(new Map(*key_pattern, *value_pattern, + min_elements, max_elements, + separator)); } else - return nullptr; + return std::unique_ptr(); } @@ -1077,10 +1072,9 @@ namespace Patterns - PatternBase * - MultipleSelection::clone () const + std::unique_ptr MultipleSelection::clone() const { - return new MultipleSelection(sequence); + return std::unique_ptr(new MultipleSelection(sequence)); } @@ -1093,7 +1087,7 @@ namespace Patterns - MultipleSelection *MultipleSelection::create (const std::string &description) + std::unique_ptr MultipleSelection::create (const std::string &description) { if (description.compare(0, std::strlen(description_init), description_init) == 0) { @@ -1102,10 +1096,10 @@ namespace Patterns sequence.erase(0, std::strlen(description_init) + 1); sequence.erase(sequence.length()-2, 2); - return new MultipleSelection(sequence); + return std::unique_ptr(new MultipleSelection(sequence)); } else - return nullptr; + return std::unique_ptr(); } @@ -1148,20 +1142,19 @@ namespace Patterns - PatternBase * - Bool::clone () const + std::unique_ptr Bool::clone() const { - return new Bool(); + return std::unique_ptr(new Bool()); } - Bool *Bool::create (const std::string &description) + std::unique_ptr Bool::create (const std::string &description) { if (description.compare(0, std::strlen(description_init), description_init) == 0) - return new Bool(); + return std::unique_ptr(new Bool()); else - return nullptr; + return std::unique_ptr(); } @@ -1209,20 +1202,19 @@ namespace Patterns - PatternBase * - Anything::clone () const + std::unique_ptr Anything::clone() const { - return new Anything(); + return std::unique_ptr(new Anything()); } - Anything *Anything::create (const std::string &description) + std::unique_ptr Anything::create (const std::string &description) { if (description.compare(0, std::strlen(description_init), description_init) == 0) - return new Anything(); + return std::unique_ptr(new Anything()); else - return nullptr; + return std::unique_ptr(); } @@ -1278,15 +1270,14 @@ namespace Patterns - PatternBase * - FileName::clone () const + std::unique_ptr FileName::clone() const { - return new FileName(file_type); + return std::unique_ptr(new FileName(file_type)); } - FileName *FileName::create (const std::string &description) + std::unique_ptr FileName::create (const std::string &description) { if (description.compare(0, std::strlen(description_init), description_init) == 0) { @@ -1303,10 +1294,10 @@ namespace Patterns else type = output; - return new FileName(type); + return std::unique_ptr(new FileName(type)); } else - return nullptr; + return std::unique_ptr(); } @@ -1353,20 +1344,19 @@ namespace Patterns - PatternBase * - DirectoryName::clone () const + std::unique_ptr DirectoryName::clone() const { - return new DirectoryName(); + return std::unique_ptr(new DirectoryName()); } - DirectoryName *DirectoryName::create (const std::string &description) + std::unique_ptr DirectoryName::create (const std::string &description) { if (description.compare(0, std::strlen(description_init), description_init) == 0) - return new DirectoryName(); + return std::unique_ptr(new DirectoryName()); else - return nullptr; + return std::unique_ptr(); } } // end namespace Patterns diff --git a/tests/base/patterns_01.cc b/tests/base/patterns_01.cc index d19d686625..e5c154ea30 100644 --- a/tests/base/patterns_01.cc +++ b/tests/base/patterns_01.cc @@ -34,12 +34,10 @@ int main() // description and verify that the // result is the same as what we // started out with - Patterns::Integer *pattern2 = Patterns::Integer::create (desc); + std::unique_ptr pattern2 = Patterns::Integer::create (desc); AssertThrow (pattern2 != nullptr, ExcInternalError()); AssertThrow (desc == pattern2->description(), ExcInternalError()); deallog << desc << std::endl; - - delete pattern2; }