2010-10-14 08:27:31 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-21 14:30:25 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2012-07-14 05:37:24 +02:00
|
|
|
#ifndef INCLUDED_COMPHELPER_MAKE_SHARED_FROM_UNO_HXX
|
2005-10-11 07:57:23 +00:00
|
|
|
#define INCLUDED_COMPHELPER_MAKE_SHARED_FROM_UNO_HXX
|
|
|
|
|
2015-09-14 09:21:33 +01:00
|
|
|
#include <memory>
|
2005-10-11 07:57:23 +00:00
|
|
|
|
|
|
|
namespace comphelper {
|
|
|
|
|
|
|
|
/// @internal
|
|
|
|
namespace detail {
|
|
|
|
/// @internal
|
2017-06-28 21:48:22 +02:00
|
|
|
template <typename T> struct ReleaseFunc {
|
2005-10-11 07:57:23 +00:00
|
|
|
void operator()( T * p ) const { p->release(); }
|
|
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
|
2015-09-14 09:21:33 +01:00
|
|
|
/** Makes a std::shared_ptr from a ref-counted UNO object pointer.
|
2005-10-11 07:57:23 +00:00
|
|
|
This makes sense if the object is used via UNO (implementing some X
|
|
|
|
interface) and also internally using its implementation class, e.g.
|
|
|
|
|
|
|
|
<pre>
|
2015-09-14 09:21:33 +01:00
|
|
|
std::shared_ptr<MyUnoImpl> const ptr(
|
2005-10-11 07:57:23 +00:00
|
|
|
comphelper::make_shared_from_UNO( new MyUnoImpl ) );
|
|
|
|
...
|
|
|
|
xUno->callingUno( uno::Reference<XSomeInterface>( ptr.get() ) );
|
|
|
|
...
|
|
|
|
takeSharedPtr( ptr );
|
|
|
|
...
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
@attention The shared_ptr operates on a separate reference counter, so
|
2015-09-14 09:21:33 +01:00
|
|
|
weak pointers (std::weak_ptr) are invalidated when the last
|
2005-10-11 07:57:23 +00:00
|
|
|
shared_ptr is destroyed, although the UNO object may still be
|
|
|
|
alive.
|
|
|
|
|
|
|
|
@param p object pointer
|
|
|
|
@return shared_ptr to object
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2015-09-14 09:21:33 +01:00
|
|
|
inline std::shared_ptr<T> make_shared_from_UNO( T * p )
|
2005-10-11 07:57:23 +00:00
|
|
|
{
|
|
|
|
p->acquire();
|
2015-09-14 09:21:33 +01:00
|
|
|
return std::shared_ptr<T>( p, detail::ReleaseFunc<T>() );
|
2005-10-11 07:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace comphelper
|
|
|
|
|
|
|
|
#endif // ! defined(INCLUDED_COMPHELPER_MAKE_SHARED_FROM_UNO_HXX)
|
|
|
|
|
2010-10-14 08:27:31 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|