1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
/*
* Copyright (C) 2018 Open Source Robotics Foundation
*
* Licensed 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef IGNITION_PLUGIN_ENABLEPLUGINFROMTHIS_HH_
#define IGNITION_PLUGIN_ENABLEPLUGINFROMTHIS_HH_
#include <memory>
#include <ignition/plugin/PluginPtr.hh>
namespace ignition
{
namespace plugin
{
// Forward declaration
class Loader;
/// \brief EnablePluginFromThis is an optional base class which can be
/// inherited by Plugin classes. When a Plugin class inherits it and that
/// Plugin class is instantiated using the Loader class, its instance will
/// be able to access the PluginPtr that manages its lifecycle. This
/// interface will also be available by calling
/// `instance->QueryInterface<EnablePluginFromThis>()`
///
/// \remark This class is analogous to std::enable_shared_from_this
class IGNITION_PLUGIN_VISIBLE EnablePluginFromThis
{
/// \brief Default constructor
public: EnablePluginFromThis();
/// \brief Get a copy of the PluginPtr that manages this object.
/// \return a copy of the PluginPtr that manages this object.
public: PluginPtr PluginFromThis();
/// \brief Get a const-qualified copy of the PluginPtr that manages this
/// object.
/// \return a const-qualified copy of the PluginPtr that manages this
/// object.
public: ConstPluginPtr PluginFromThis() const;
/// \brief Destructor
public: virtual ~EnablePluginFromThis();
/// \brief This is an abstract shared_ptr to the Plugin instance. This
/// shared_ptr will maintain a reference count on the shared library that
/// provides the plugin so that the shared library does not get unloaded
/// while this shared_ptr is still alive.
///
/// This function is meant for advanced use-cases, such as creating
/// Factory plugins, so we keep it protected. Only use this if you know
/// what you are doing.
///
/// \return shared_ptr to the Plugin instance.
protected: std::shared_ptr<void> PluginInstancePtrFromThis() const;
// Declare friendship so that the internal WeakPluginPtr can be set by
// the Loader class.
friend class Loader;
/// \brief This function is called by the Loader class whenever a plugin
/// containing this interface gets instantiated.
private: void PrivateSetPluginFromThis(const PluginPtr &_ptr);
private: class Implementation;
IGN_UTILS_WARN_IGNORE__DLL_INTERFACE_MISSING
private: std::unique_ptr<Implementation> pimpl;
IGN_UTILS_WARN_RESUME__DLL_INTERFACE_MISSING
};
}
}
#endif
|