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