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_APPLICATION_PUBLIC_CPP_LAZY_INTERFACE_PTR_H_ | |
6 #define MOJO_APPLICATION_PUBLIC_CPP_LAZY_INTERFACE_PTR_H_ | |
7 | |
8 #include "mojo/application/public/cpp/connect.h" | |
9 #include "mojo/application/public/interfaces/service_provider.mojom.h" | |
10 | |
11 namespace mojo { | |
12 | |
13 // An InterfacePtr that will request an implementation from a specified | |
14 // ServiceProvider when it is first accessed with the get() method. | |
15 template <typename Interface> | |
16 class LazyInterfacePtr : public InterfacePtr<Interface> { | |
17 public: | |
18 LazyInterfacePtr() : service_provider_(nullptr) {} | |
19 | |
20 explicit LazyInterfacePtr(ServiceProvider* service_provider) | |
21 : service_provider_(service_provider) {} | |
22 | |
23 void set_service_provider(ServiceProvider* service_provider) { | |
24 if (service_provider != service_provider_) { | |
25 InterfacePtr<Interface>::reset(); | |
26 } | |
27 service_provider_ = service_provider; | |
28 } | |
29 | |
30 Interface* get() const { | |
31 if (!InterfacePtr<Interface>::get() && service_provider_) { | |
32 mojo::ConnectToService<Interface>( | |
33 service_provider_, const_cast<LazyInterfacePtr<Interface>*>(this)); | |
34 } | |
35 return InterfacePtr<Interface>::get(); | |
36 } | |
37 Interface* operator->() const { return get(); } | |
38 Interface& operator*() const { return *get(); } | |
39 | |
40 private: | |
41 ServiceProvider* service_provider_; | |
42 }; | |
43 | |
44 } // namespace mojo | |
45 | |
46 #endif // MOJO_APPLICATION_PUBLIC_CPP_LAZY_INTERFACE_PTR_H_ | |
OLD | NEW |