]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common: make C_ContextsBase container agnostic
authorPatrick Donnelly <pdonnell@redhat.com>
Tue, 24 Jul 2018 13:48:22 +0000 (06:48 -0700)
committerPatrick Donnelly <pdonnell@redhat.com>
Wed, 25 Jul 2018 23:03:11 +0000 (16:03 -0700)
Signed-off-by: Patrick Donnelly <pdonnell@redhat.com>
src/include/Context.h

index ec3173729ef2e486124f51fd343e646c038f1c63..a801578a598a1bc7e5ee602aa6d2d50f3aec5cfa 100644 (file)
@@ -196,11 +196,11 @@ struct C_Lock : public Context {
  * ContextType must be an ancestor class of ContextInstanceType, or the same class.
  * ContextInstanceType must be default-constructable.
  */
-template <class ContextType, class ContextInstanceType>
+template <class ContextType, class ContextInstanceType, class Container = std::list<ContextType *>>
 class C_ContextsBase : public ContextInstanceType {
 public:
   CephContext *cct;
-  std::list<ContextType*> contexts;
+  Container contexts;
 
   C_ContextsBase(CephContext *cct_)
     : cct(cct_)
@@ -214,23 +214,14 @@ public:
   void add(ContextType* c) {
     contexts.push_back(c);
   }
-  void take(std::list<ContextType*>& ls) {
-    contexts.splice(contexts.end(), ls);
-  }
-  bool sync_complete(int r) override {
-    auto p = contexts.begin();
-    while (p != contexts.end()) {
-      if ((*p)->sync_complete(r)) {
-       p = contexts.erase(p);
-      } else {
-       ++p;
-      }
-    }
-    if (contexts.empty()) {
-      delete this;
-      return true;
+  void take(Container& ls) {
+    Container c;
+    c.swap(ls);
+    if constexpr (std::is_same_v<Container, std::list<ContextType *>>) {
+      contexts.splice(contexts.end(), c);
+    } else {
+      contexts.insert(contexts.end(), c.begin(), c.end());
     }
-    return false;
   }
   void complete(int r) override {
     // Neuter any ContextInstanceType custom complete(), because although
@@ -242,7 +233,8 @@ public:
   }
   bool empty() { return contexts.empty(); }
 
-  static ContextType *list_to_context(list<ContextType *> &cs) {
+  template<class C>
+  static ContextType *list_to_context(C& cs) {
     if (cs.size() == 0) {
       return 0;
     } else if (cs.size() == 1) {