lazy_interface_ptr.h revision 116680a4aac90f2aa7413d9095a592090648e557
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/service_provider/service_provider.mojom.h" 12 13namespace mojo { 14 15template<typename Interface> 16class LazyInterfacePtr : InterfacePtr<Interface> { 17 public: 18 LazyInterfacePtr(ServiceProvider* service_provider) 19 : service_provider_(service_provider) { 20 } 21 22 Interface* get() const { 23 if (!InterfacePtr<Interface>::get()) { 24 mojo::ConnectToService<Interface>( 25 service_provider_, 26 const_cast<LazyInterfacePtr<Interface>*>(this)); 27 } 28 return InterfacePtr<Interface>::get(); 29 } 30 Interface* operator->() const { return get(); } 31 Interface& operator*() const { return *get(); } 32 33 private: 34 ServiceProvider* service_provider_; 35}; 36 37} // namespace mojo 38 39#endif // MOJO_PUBLIC_CPP_APPLICATION_LAZY_INTERFACE_PTR_H_ 40