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