From 69a1d048aa702a5ca320f9de22a6b859960b07be Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Tue, 9 Apr 2019 04:26:59 +0200 Subject: [PATCH] Use std::string in SmartPointer and Subscriptor interface --- doc/news/changes/minor/20190408Arndt | 4 ++ include/deal.II/base/smartpointer.h | 6 +-- include/deal.II/base/subscriptor.h | 38 ++----------------- source/base/subscriptor.cc | 11 +++--- .../base/unsubscribe_subscriptor.debug.output | 4 +- ...subscribe_subscriptor.debug.output.clang-6 | 4 +- ...unsubscribe_subscriptor.debug.output.intel | 4 +- 7 files changed, 22 insertions(+), 49 deletions(-) create mode 100644 doc/news/changes/minor/20190408Arndt diff --git a/doc/news/changes/minor/20190408Arndt b/doc/news/changes/minor/20190408Arndt new file mode 100644 index 0000000000..eaf8cc5e3f --- /dev/null +++ b/doc/news/changes/minor/20190408Arndt @@ -0,0 +1,4 @@ +Changed: SmartPointer and Subscriptor use a std::string +instead of a const char * as identifier. +
+(Daniel Arndt, 2019/04/08) diff --git a/include/deal.II/base/smartpointer.h b/include/deal.II/base/smartpointer.h index 82066b872a..a27fce37af 100644 --- a/include/deal.II/base/smartpointer.h +++ b/include/deal.II/base/smartpointer.h @@ -95,7 +95,7 @@ public: * The id is used in the call to Subscriptor::subscribe(id) and by * ~SmartPointer() in the call to Subscriptor::unsubscribe(). */ - SmartPointer(T *t, const char *id); + SmartPointer(T *t, const std::string &id); /** * Constructor taking a normal pointer. If possible, i.e. if the pointer is @@ -200,7 +200,7 @@ private: /** * The identification for the subscriptor. */ - const char *const id; + const std::string id; /** * The Smartpointer is invalidated when the object pointed to is destroyed @@ -235,7 +235,7 @@ inline SmartPointer::SmartPointer(T *t) template -inline SmartPointer::SmartPointer(T *t, const char *id) +inline SmartPointer::SmartPointer(T *t, const std::string &id) : t(t) , id(id) , pointed_to_object_is_alive(false) diff --git a/include/deal.II/base/subscriptor.h b/include/deal.II/base/subscriptor.h index aae53ff17d..cd07834741 100644 --- a/include/deal.II/base/subscriptor.h +++ b/include/deal.II/base/subscriptor.h @@ -105,23 +105,11 @@ public: /** * Subscribes a user of the object by storing the pointer @p validity. The - * subscriber may be identified by text supplied as @p identifier. The latter - * variable must not be a temporary and its type must decay to - * const char *. In particular, calling this function with a rvalue reference - * is not allowed. - */ - template - typename std::enable_if< - std::is_same::value>::type - subscribe(std::atomic *const validity, - ConstCharStar identifier = nullptr) const; - - /** - * Calling subscribe() with a rvalue reference as identifier is not allowed. + * subscriber may be identified by text supplied as @p identifier. */ void subscribe(std::atomic *const validity, - const char *&& identifier) const = delete; + const std::string & identifier = "") const; /** * Unsubscribes a user from the object. @@ -131,7 +119,7 @@ public: */ void unsubscribe(std::atomic *const validity, - const char * identifier = nullptr) const; + const std::string & identifier = "") const; /** * Return the present number of subscriptions to this object. This allows to @@ -222,24 +210,11 @@ private: */ mutable std::atomic counter; - /* - * Functor struct used for key comparison in #counter_map. - * Not the memory location but the actual C-string content is compared. - */ - struct MapCompare - { - bool - operator()(const char *lhs, const char *rhs) const - { - return std::strcmp(lhs, rhs) > 0; - } - }; - /** * In this map, we count subscriptions for each different identification * string supplied to subscribe(). */ - mutable std::map counter_map; + mutable std::map counter_map; /** * The data type used in #counter_map. @@ -340,11 +315,6 @@ Subscriptor::list_subscribers(StreamType &stream) const << it.first << '\"' << std::endl; } -// forward declare template specialization -template <> -void -Subscriptor::subscribe(std::atomic *const validity, - const char * id) const; DEAL_II_NAMESPACE_CLOSE #endif diff --git a/source/base/subscriptor.cc b/source/base/subscriptor.cc index 669f71b909..3aa98f2d5f 100644 --- a/source/base/subscriptor.cc +++ b/source/base/subscriptor.cc @@ -126,10 +126,9 @@ Subscriptor::operator=(Subscriptor &&s) noexcept -template <> void -Subscriptor::subscribe(std::atomic *const validity, - const char * id) const +Subscriptor::subscribe(std::atomic *const validity, + const std::string & id) const { std::lock_guard lock(mutex); @@ -137,7 +136,7 @@ Subscriptor::subscribe(std::atomic *const validity, object_info = &typeid(*this); ++counter; - const char *const name = (id != nullptr) ? id : unknown_subscriber; + const std::string name = (id != "") ? id : unknown_subscriber; map_iterator it = counter_map.find(name); if (it == counter_map.end()) @@ -153,9 +152,9 @@ Subscriptor::subscribe(std::atomic *const validity, void Subscriptor::unsubscribe(std::atomic *const validity, - const char * id) const + const std::string & id) const { - const char *name = (id != nullptr) ? id : unknown_subscriber; + const std::string name = (id != "") ? id : unknown_subscriber; if (counter == 0) { AssertNothrow(counter > 0, ExcNoSubscriber(object_info->name(), name)); diff --git a/tests/base/unsubscribe_subscriptor.debug.output b/tests/base/unsubscribe_subscriptor.debug.output index e0f3875973..03bd8c6462 100644 --- a/tests/base/unsubscribe_subscriptor.debug.output +++ b/tests/base/unsubscribe_subscriptor.debug.output @@ -3,7 +3,7 @@ DEAL::Exception: ExcNoSubscriber(object_info->name(), name) DEAL:: -------------------------------------------------------- An error occurred in file in function - void dealii::Subscriptor::unsubscribe(std::atomic*, const char*) const + void dealii::Subscriptor::unsubscribe(std::atomic*, const string&) const The violated condition was: it != counter_map.end() Additional information: @@ -14,7 +14,7 @@ DEAL::Exception: ExcMessage( "This Subscriptor object does not know anything abo DEAL:: -------------------------------------------------------- An error occurred in file in function - void dealii::Subscriptor::unsubscribe(std::atomic*, const char*) const + void dealii::Subscriptor::unsubscribe(std::atomic*, const string&) const The violated condition was: validity_ptr_it != validity_pointers.end() Additional information: diff --git a/tests/base/unsubscribe_subscriptor.debug.output.clang-6 b/tests/base/unsubscribe_subscriptor.debug.output.clang-6 index 5c2e16e2da..41313921d2 100644 --- a/tests/base/unsubscribe_subscriptor.debug.output.clang-6 +++ b/tests/base/unsubscribe_subscriptor.debug.output.clang-6 @@ -3,7 +3,7 @@ DEAL::Exception: ExcNoSubscriber(object_info->name(), name) DEAL:: -------------------------------------------------------- An error occurred in file in function - void dealii::Subscriptor::unsubscribe(std::atomic *const, const char *) const + void dealii::Subscriptor::unsubscribe(std::atomic *const, const std::string &) const The violated condition was: it != counter_map.end() Additional information: @@ -14,7 +14,7 @@ DEAL::Exception: ExcMessage( "This Subscriptor object does not know anything abo DEAL:: -------------------------------------------------------- An error occurred in file in function - void dealii::Subscriptor::unsubscribe(std::atomic *const, const char *) const + void dealii::Subscriptor::unsubscribe(std::atomic *const, const std::string &) const The violated condition was: validity_ptr_it != validity_pointers.end() Additional information: diff --git a/tests/base/unsubscribe_subscriptor.debug.output.intel b/tests/base/unsubscribe_subscriptor.debug.output.intel index 76846bd70c..4817452f84 100644 --- a/tests/base/unsubscribe_subscriptor.debug.output.intel +++ b/tests/base/unsubscribe_subscriptor.debug.output.intel @@ -3,7 +3,7 @@ DEAL::Exception: ExcNoSubscriber(object_info->name(), name) DEAL:: -------------------------------------------------------- An error occurred in file in function - void dealii::Subscriptor::unsubscribe(std::atomic *, const char *) const + void dealii::Subscriptor::unsubscribe(std::atomic *, const std::__cxx11::basic_string, std::allocator> &) const The violated condition was: it != counter_map.end() Additional information: @@ -14,7 +14,7 @@ DEAL::Exception: ExcMessage( "This Subscriptor object does not know anything abo DEAL:: -------------------------------------------------------- An error occurred in file in function - void dealii::Subscriptor::unsubscribe(std::atomic *, const char *) const + void dealii::Subscriptor::unsubscribe(std::atomic *, const std::__cxx11::basic_string, std::allocator> &) const The violated condition was: validity_ptr_it != validity_pointers.end() Additional information: -- 2.39.5