| OLD | NEW | 
|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ | 
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ | 
| 7 | 7 | 
| 8 #include <utility> | 8 #include <utility> | 
| 9 #include <vector> | 9 #include <vector> | 
| 10 | 10 | 
| 11 #include "base/macros.h" | 11 #include "base/macros.h" | 
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" | 
|  | 13 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" | 
| 13 #include "mojo/public/cpp/bindings/interface_ptr.h" | 14 #include "mojo/public/cpp/bindings/interface_ptr.h" | 
| 14 | 15 | 
| 15 namespace mojo { | 16 namespace mojo { | 
|  | 17 namespace internal { | 
| 16 | 18 | 
| 17 template <typename Interface> | 19 template <typename Interface, template <typename> class Ptr> | 
| 18 class InterfacePtrSet { | 20 class PtrSet { | 
| 19  public: | 21  public: | 
| 20   InterfacePtrSet() {} | 22   PtrSet() {} | 
| 21   ~InterfacePtrSet() { CloseAll(); } | 23   ~PtrSet() { CloseAll(); } | 
| 22 | 24 | 
| 23   void AddInterfacePtr(InterfacePtr<Interface> ptr) { | 25   void AddPtr(Ptr<Interface> ptr) { | 
| 24     auto weak_interface_ptr = new Element(std::move(ptr)); | 26     auto weak_interface_ptr = new Element(std::move(ptr)); | 
| 25     ptrs_.push_back(weak_interface_ptr->GetWeakPtr()); | 27     ptrs_.push_back(weak_interface_ptr->GetWeakPtr()); | 
| 26     ClearNullInterfacePtrs(); | 28     ClearNullPtrs(); | 
| 27   } | 29   } | 
| 28 | 30 | 
| 29   template <typename FunctionType> | 31   template <typename FunctionType> | 
| 30   void ForAllPtrs(FunctionType function) { | 32   void ForAllPtrs(FunctionType function) { | 
| 31     for (const auto& it : ptrs_) { | 33     for (const auto& it : ptrs_) { | 
| 32       if (it) | 34       if (it) | 
| 33         function(it->get()); | 35         function(it->get()); | 
| 34     } | 36     } | 
| 35     ClearNullInterfacePtrs(); | 37     ClearNullPtrs(); | 
| 36   } | 38   } | 
| 37 | 39 | 
| 38   void CloseAll() { | 40   void CloseAll() { | 
| 39     for (const auto& it : ptrs_) { | 41     for (const auto& it : ptrs_) { | 
| 40       if (it) | 42       if (it) | 
| 41         it->Close(); | 43         it->Close(); | 
| 42     } | 44     } | 
| 43     ptrs_.clear(); | 45     ptrs_.clear(); | 
| 44   } | 46   } | 
| 45 | 47 | 
| 46  private: | 48  private: | 
| 47   class Element { | 49   class Element { | 
| 48    public: | 50    public: | 
| 49     explicit Element(InterfacePtr<Interface> ptr) | 51     explicit Element(Ptr<Interface> ptr) | 
| 50         : ptr_(std::move(ptr)), weak_ptr_factory_(this) { | 52         : ptr_(std::move(ptr)), weak_ptr_factory_(this) { | 
| 51       ptr_.set_connection_error_handler([this]() { delete this; }); | 53       ptr_.set_connection_error_handler([this]() { delete this; }); | 
| 52     } | 54     } | 
| 53     ~Element() {} | 55     ~Element() {} | 
| 54 | 56 | 
| 55     void Close() { ptr_.reset(); } | 57     void Close() { ptr_.reset(); } | 
| 56 | 58 | 
| 57     Interface* get() { return ptr_.get(); } | 59     Interface* get() { return ptr_.get(); } | 
| 58 | 60 | 
| 59     base::WeakPtr<Element> GetWeakPtr() { | 61     base::WeakPtr<Element> GetWeakPtr() { | 
| 60       return weak_ptr_factory_.GetWeakPtr(); | 62       return weak_ptr_factory_.GetWeakPtr(); | 
| 61     } | 63     } | 
| 62 | 64 | 
| 63    private: | 65    private: | 
| 64     InterfacePtr<Interface> ptr_; | 66     Ptr<Interface> ptr_; | 
| 65     base::WeakPtrFactory<Element> weak_ptr_factory_; | 67     base::WeakPtrFactory<Element> weak_ptr_factory_; | 
| 66 | 68 | 
| 67     DISALLOW_COPY_AND_ASSIGN(Element); | 69     DISALLOW_COPY_AND_ASSIGN(Element); | 
| 68   }; | 70   }; | 
| 69 | 71 | 
| 70   void ClearNullInterfacePtrs() { | 72   void ClearNullPtrs() { | 
| 71     ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), | 73     ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), | 
| 72                                [](const base::WeakPtr<Element>& p) { | 74                                [](const base::WeakPtr<Element>& p) { | 
| 73                                  return p.get() == nullptr; | 75                                  return p.get() == nullptr; | 
| 74                                }), | 76                                }), | 
| 75                 ptrs_.end()); | 77                 ptrs_.end()); | 
| 76   } | 78   } | 
| 77 | 79 | 
| 78   std::vector<base::WeakPtr<Element>> ptrs_; | 80   std::vector<base::WeakPtr<Element>> ptrs_; | 
| 79 }; | 81 }; | 
| 80 | 82 | 
|  | 83 }  // namespace internal | 
|  | 84 | 
|  | 85 template <typename Interface> | 
|  | 86 using InterfacePtrSet = internal::PtrSet<Interface, InterfacePtr>; | 
|  | 87 | 
|  | 88 template <typename Interface> | 
|  | 89 using AssociatedInterfacePtrSet = | 
|  | 90     internal::PtrSet<Interface, AssociatedInterfacePtr>; | 
|  | 91 | 
| 81 }  // namespace mojo | 92 }  // namespace mojo | 
| 82 | 93 | 
| 83 #endif  // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ | 94 #endif  // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ | 
| OLD | NEW | 
|---|