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_APPLICATION_LAZY_INTERFACE_PTR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_APPLICATION_LAZY_INTERFACE_PTR_H_ |
6 #define MOJO_PUBLIC_CPP_APPLICATION_LAZY_INTERFACE_PTR_H_ | 6 #define MOJO_PUBLIC_CPP_APPLICATION_LAZY_INTERFACE_PTR_H_ |
7 | 7 |
8 #include "mojo/public/cpp/application/connect.h" | 8 #include "mojo/public/cpp/application/connect.h" |
9 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 9 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
10 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 | 12 |
13 template <typename Interface> | 13 template <typename Interface> |
14 class LazyInterfacePtr : public InterfacePtr<Interface> { | 14 class LazyInterfacePtr : public InterfacePtr<Interface> { |
15 public: | 15 public: |
16 LazyInterfacePtr() : service_provider_(nullptr) {} | 16 LazyInterfacePtr() : service_provider_(nullptr) {} |
17 | 17 |
18 LazyInterfacePtr(ServiceProvider* service_provider) | 18 LazyInterfacePtr(ServiceProvider* service_provider) |
19 : service_provider_(service_provider) {} | 19 : service_provider_(service_provider) {} |
20 | 20 |
21 void set_service_provider(ServiceProvider* service_provider) { | 21 void set_service_provider(ServiceProvider* service_provider) { |
22 if (service_provider != service_provider_) { | 22 if (service_provider != service_provider_) { |
23 InterfacePtr<Interface>::reset(); | 23 InterfacePtr<Interface>::reset(); |
24 } | 24 } |
25 service_provider_ = service_provider; | 25 service_provider_ = service_provider; |
26 } | 26 } |
27 | 27 |
28 Interface* get() const { | 28 Interface* get() const { |
29 if (!InterfacePtr<Interface>::get()) { | 29 if (!InterfacePtr<Interface>::get() && service_provider_) { |
30 mojo::ConnectToService<Interface>( | 30 mojo::ConnectToService<Interface>( |
31 service_provider_, const_cast<LazyInterfacePtr<Interface>*>(this)); | 31 service_provider_, const_cast<LazyInterfacePtr<Interface>*>(this)); |
32 } | 32 } |
33 return InterfacePtr<Interface>::get(); | 33 return InterfacePtr<Interface>::get(); |
34 } | 34 } |
35 Interface* operator->() const { return get(); } | 35 Interface* operator->() const { return get(); } |
36 Interface& operator*() const { return *get(); } | 36 Interface& operator*() const { return *get(); } |
37 | 37 |
38 private: | 38 private: |
39 ServiceProvider* service_provider_; | 39 ServiceProvider* service_provider_; |
40 }; | 40 }; |
41 | 41 |
42 } // namespace mojo | 42 } // namespace mojo |
43 | 43 |
44 #endif // MOJO_PUBLIC_CPP_APPLICATION_LAZY_INTERFACE_PTR_H_ | 44 #endif // MOJO_PUBLIC_CPP_APPLICATION_LAZY_INTERFACE_PTR_H_ |
OLD | NEW |