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 <string> | 8 #include <string> |
9 | 9 |
10 #include "mojo/public/cpp/application/connect.h" | 10 #include "mojo/public/cpp/application/connect.h" |
11 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 11 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
12 | 12 |
13 namespace mojo { | 13 namespace mojo { |
14 | 14 |
15 template<typename Interface> | 15 template<typename Interface> |
16 class LazyInterfacePtr : InterfacePtr<Interface> { | 16 class LazyInterfacePtr : InterfacePtr<Interface> { |
17 public: | 17 public: |
18 LazyInterfacePtr() : service_provider_(NULL) {} | |
19 | |
18 LazyInterfacePtr(ServiceProvider* service_provider) | 20 LazyInterfacePtr(ServiceProvider* service_provider) |
19 : service_provider_(service_provider) { | 21 : service_provider_(service_provider) { |
20 } | 22 } |
21 | 23 |
24 void set_service_provider(ServiceProvider* service_provider) { | |
darin (slow to review)
2014/09/04 23:22:18
you probably want to make sure InterfacePtr<Interf
Aaron Boodman
2014/09/05 04:22:34
I decided to just reset the InterfaceProvider when
| |
25 DCHECK(!service_provider_); | |
26 service_provider_ = service_provider; | |
27 } | |
28 | |
22 Interface* get() const { | 29 Interface* get() const { |
23 if (!InterfacePtr<Interface>::get()) { | 30 if (!InterfacePtr<Interface>::get()) { |
24 mojo::ConnectToService<Interface>( | 31 mojo::ConnectToService<Interface>( |
25 service_provider_, | 32 service_provider_, |
26 const_cast<LazyInterfacePtr<Interface>*>(this)); | 33 const_cast<LazyInterfacePtr<Interface>*>(this)); |
27 } | 34 } |
28 return InterfacePtr<Interface>::get(); | 35 return InterfacePtr<Interface>::get(); |
29 } | 36 } |
30 Interface* operator->() const { return get(); } | 37 Interface* operator->() const { return get(); } |
31 Interface& operator*() const { return *get(); } | 38 Interface& operator*() const { return *get(); } |
32 | 39 |
33 private: | 40 private: |
34 ServiceProvider* service_provider_; | 41 ServiceProvider* service_provider_; |
35 }; | 42 }; |
36 | 43 |
37 } // namespace mojo | 44 } // namespace mojo |
38 | 45 |
39 #endif // MOJO_PUBLIC_CPP_APPLICATION_LAZY_INTERFACE_PTR_H_ | 46 #endif // MOJO_PUBLIC_CPP_APPLICATION_LAZY_INTERFACE_PTR_H_ |
OLD | NEW |