]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Use std::string in SmartPointer and Subscriptor interface
authorDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Tue, 9 Apr 2019 02:26:59 +0000 (04:26 +0200)
committerDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Thu, 11 Apr 2019 13:28:10 +0000 (15:28 +0200)
doc/news/changes/minor/20190408Arndt [new file with mode: 0644]
include/deal.II/base/smartpointer.h
include/deal.II/base/subscriptor.h
source/base/subscriptor.cc
tests/base/unsubscribe_subscriptor.debug.output
tests/base/unsubscribe_subscriptor.debug.output.clang-6
tests/base/unsubscribe_subscriptor.debug.output.intel

diff --git a/doc/news/changes/minor/20190408Arndt b/doc/news/changes/minor/20190408Arndt
new file mode 100644 (file)
index 0000000..eaf8cc5
--- /dev/null
@@ -0,0 +1,4 @@
+Changed: SmartPointer and Subscriptor use a std::string
+instead of a const char * as identifier.
+<br>
+(Daniel Arndt, 2019/04/08)
index 82066b872a94666d5cb3da5a679ef3e1df5b64a5..a27fce37afa3f8b3ad4cf60da6620df286186adc 100644 (file)
@@ -95,7 +95,7 @@ public:
    * The <tt>id</tt> 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<T, P>::SmartPointer(T *t)
 
 
 template <typename T, typename P>
-inline SmartPointer<T, P>::SmartPointer(T *t, const char *id)
+inline SmartPointer<T, P>::SmartPointer(T *t, const std::string &id)
   : t(t)
   , id(id)
   , pointed_to_object_is_alive(false)
index aae53ff17d1783befcb04060824c2f7863f28eb8..cd07834741d21ba96f8f11fd51e678e60060ee46 100644 (file)
@@ -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 ConstCharStar = const char *>
-  typename std::enable_if<
-    std::is_same<ConstCharStar, const char *>::value>::type
-  subscribe(std::atomic<bool> *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<bool> *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<bool> *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<unsigned int> 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<const char *, unsigned int, MapCompare> counter_map;
+  mutable std::map<std::string, unsigned int> 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<const char *>(std::atomic<bool> *const validity,
-                                     const char *             id) const;
 DEAL_II_NAMESPACE_CLOSE
 
 #endif
index 669f71b9092b8b03336b6dc26a7a89efff46b1f8..3aa98f2d5f7e6a8be27f5babd22155d62d5f8357 100644 (file)
@@ -126,10 +126,9 @@ Subscriptor::operator=(Subscriptor &&s) noexcept
 
 
 
-template <>
 void
-Subscriptor::subscribe<const char *>(std::atomic<bool> *const validity,
-                                     const char *             id) const
+Subscriptor::subscribe(std::atomic<bool> *const validity,
+                       const std::string &      id) const
 {
   std::lock_guard<std::mutex> lock(mutex);
 
@@ -137,7 +136,7 @@ Subscriptor::subscribe<const char *>(std::atomic<bool> *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<const char *>(std::atomic<bool> *const validity,
 
 void
 Subscriptor::unsubscribe(std::atomic<bool> *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));
index e0f38759735b0cbfffd7aa4d547a7d3fc3e26788..03bd8c6462bf34c6813add691744ba81d62721cb 100644 (file)
@@ -3,7 +3,7 @@ DEAL::Exception: ExcNoSubscriber(object_info->name(), name)
 DEAL::
 --------------------------------------------------------
 An error occurred in file <subscriptor.cc> in function
-    void dealii::Subscriptor::unsubscribe(std::atomic<bool>*, const char*) const
+    void dealii::Subscriptor::unsubscribe(std::atomic<bool>*, 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 <subscriptor.cc> in function
-    void dealii::Subscriptor::unsubscribe(std::atomic<bool>*, const char*) const
+    void dealii::Subscriptor::unsubscribe(std::atomic<bool>*, const string&) const
 The violated condition was: 
     validity_ptr_it != validity_pointers.end()
 Additional information: 
index 5c2e16e2dafb284636d1b32aa7aa6c9438ee2331..41313921d2040a437511c8748e21ec41b2d50210 100644 (file)
@@ -3,7 +3,7 @@ DEAL::Exception: ExcNoSubscriber(object_info->name(), name)
 DEAL::
 --------------------------------------------------------
 An error occurred in file <subscriptor.cc> in function
-    void dealii::Subscriptor::unsubscribe(std::atomic<bool> *const, const char *) const
+    void dealii::Subscriptor::unsubscribe(std::atomic<bool> *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 <subscriptor.cc> in function
-    void dealii::Subscriptor::unsubscribe(std::atomic<bool> *const, const char *) const
+    void dealii::Subscriptor::unsubscribe(std::atomic<bool> *const, const std::string &) const
 The violated condition was: 
     validity_ptr_it != validity_pointers.end()
 Additional information: 
index 76846bd70c1164cc7c5971fbf63032da0dceed9e..4817452f841bf92bc4703dd73669d4e84d5c1cf2 100644 (file)
@@ -3,7 +3,7 @@ DEAL::Exception: ExcNoSubscriber(object_info->name(), name)
 DEAL::
 --------------------------------------------------------
 An error occurred in file <subscriptor.cc> in function
-    void dealii::Subscriptor::unsubscribe(std::atomic<bool> *, const char *) const
+    void dealii::Subscriptor::unsubscribe(std::atomic<bool> *, const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> &) 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 <subscriptor.cc> in function
-    void dealii::Subscriptor::unsubscribe(std::atomic<bool> *, const char *) const
+    void dealii::Subscriptor::unsubscribe(std::atomic<bool> *, const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> &) const
 The violated condition was: 
     validity_ptr_it != validity_pointers.end()
 Additional information: 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.