Skip to content

Commit

Permalink
Add warning to QgsLayerTreeLayer::layer() dox
Browse files Browse the repository at this point in the history
Because the returned pointer must ALWAYS be tested before
being dereferenced
  • Loading branch information
nyalldawson committed Oct 26, 2018
1 parent 6944240 commit d1e2978
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions python/core/auto_generated/layertree/qgslayertreelayer.sip.in
Expand Up @@ -39,14 +39,32 @@ Constructor for QgsLayerTreeLayer using weak references to layer ID, ``name``, p
%End

QString layerId() const;
%Docstring
Returns the ID for the map layer associated with this node.

.. seealso:: :py:func:`layer`
%End

QgsMapLayer *layer() const;
%Docstring
Returns the map layer associated with this node.

.. warning::

This can be (and often is!) a None, e.g. in the case of a layer node representing a layer
which has not yet been fully loaded into a project, or a layer node representing a layer
with an invalid data source. The returned pointer must ALWAYS be checked to avoid dereferencing a None.

.. seealso:: :py:func:`layerId`
%End

virtual QString name() const;

%Docstring
Returns the layer's name.

.. seealso:: :py:func:`setName`

.. versionadded:: 3.0
%End

Expand All @@ -55,6 +73,8 @@ Returns the layer's name.
%Docstring
Sets the layer's name.

.. seealso:: :py:func:`name`

.. versionadded:: 3.0
%End

Expand Down
20 changes: 20 additions & 0 deletions src/core/layertree/qgslayertreelayer.h
Expand Up @@ -54,18 +54,38 @@ class CORE_EXPORT QgsLayerTreeLayer : public QgsLayerTreeNode
*/
explicit QgsLayerTreeLayer( const QString &layerId, const QString &name = QString(), const QString &source = QString(), const QString &provider = QString() );

/**
* Returns the ID for the map layer associated with this node.
*
* \see layer()
*/
QString layerId() const { return mRef.layerId; }

/**
* Returns the map layer associated with this node.
*
* \warning This can be (and often is!) a nullptr, e.g. in the case of a layer node representing a layer
* which has not yet been fully loaded into a project, or a layer node representing a layer
* with an invalid data source. The returned pointer must ALWAYS be checked to avoid dereferencing a nullptr.
*
* \see layerId()
*/
QgsMapLayer *layer() const { return mRef.get(); }

/**
* Returns the layer's name.
*
* \see setName()
*
* \since QGIS 3.0
*/
QString name() const override;

/**
* Sets the layer's name.
*
* \see name()
*
* \since QGIS 3.0
*/
void setName( const QString &n ) override;
Expand Down

0 comments on commit d1e2978

Please sign in to comment.