Skip to content

Commit

Permalink
Add script to generate sip code from headers
Browse files Browse the repository at this point in the history
Add more annotations

Add some references to upstream documentation

Add more annotations
  • Loading branch information
m-kuhn authored and 3nids committed Mar 30, 2017
1 parent 39cd81b commit ca008d7
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 41 deletions.
34 changes: 1 addition & 33 deletions scripts/sipdiff
Expand Up @@ -30,39 +30,7 @@ for file in $*; do

tempfile=$(${GP}mktemp ${f}XXXX --suffix=.h)

# Remove comments
if [[ "$REMOVE_COMMENTS" =~ YES ]]; then
${GP}sed 's/a/aA/g;s/__/aB/g;s/#/aC/g' "src/$d/$f.h" | cpp -E $arg - | ${GP}sed 's/aC/#/g;s/aB/__/g;s/aA/a/g' > $tempfile
else
cp "src/$d/$f.h" $tempfile
fi

# Remove override keyword
${GP}sed -i 's/ override;/;/g' $tempfile

# Remove preprocessor directives
${GP}sed -i '/^#/d' $tempfile

# Remove CORE_EXPORT etc
${GP}sed -i 's/ [A-Z]*_EXPORT//g' $tempfile

# Remove public keyword from inherited classes
${GP}sed -i 's/\(class.*:\) public\(.*\)/\1\2/g' $tempfile

# Remove Q_OBJECT,ENUMS,PROPERTY
${GP}sed -i -r '/^\s*Q_(OBJECT|ENUMS|PROPERTY).*?$/d' $tempfile

# Remove function definition in header
${GP}sed -i -r 's/^(\s*)?(virtual |static )?(inline )?(void|bool|int|double|Q\w+)(\*?)(\s+[^ ]*?\(.*?\)( const)?)\s*\{.*?\}$/\1\2\4\5\6;/g' $tempfile

# Remove nullptr
${GP}sed -i 's/nullptr/0/g' $tempfile

# Remove forward declarations
${GP}sed -i -r '/^\s*class Q\w+;$/d' $tempfile

# Remove Q_INVOKABLE
${GP}sed -i 's/Q_INVOKABLE //g' $tempfile
./scripts/sipify.pl "src/$d/$f.h" > $tempfile

vimdiff $tempfile python/$d/$f.sip

Expand Down
90 changes: 90 additions & 0 deletions scripts/sipify.pl
@@ -0,0 +1,90 @@
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;

my $headerfile = $ARGV[0];

open(my $header, "<", $headerfile) || die "Couldn't open '".$headerfile."' for reading because: ".$!;

my $GLOBAL = 1;
my $DOXYGEN_BLOCK = 2;

# TODO add contexts for
# "private: " -> skip
# "function bodies" -> skip
# "multiline function signatures"
# "enums" -> remove assigned values

my $context = $GLOBAL;
my $comment;

while(!eof $header) {
my $line = readline $header;

# Skip preprocessor stuff
if ($line =~ /^\s*#/) {
if ($line !~ /NO_SIPIFY/) {
next;
}
}
# Skip forward declarations
if ($line =~ m/^\s*class \w+;$/) {
next;
}
# Skip Q_OBJECT, Q_PROPERTY, Q_ENUM, Q_GADGET
if ($line =~ m/^\s*Q_(OBJECT|ENUMS|PROPERTY|GADGET).*?$/) {
next;
}

# Doxygen comment started
if ($DOXYGEN_BLOCK != $context) {
if ($line =~ m/\s*\/\*\*/) {
$context = $DOXYGEN_BLOCK;
$comment = "";
}
# class declaration started
if ($line =~ m/^(\s*class)\s*([A-Z]+_EXPORT)(\s+\w+)(\s*\:.*)?$/) {
$line = "$1$3";
# Inheritance
if ($4) {
my $m;
$m = $4;
$m =~ s/public //g;
$m =~ s/\s?private \w+,?//;
$line .= $m;
}
$line .= "\n{\n%TypeHeaderCode\n#include \"" . basename($headerfile) . "\"\n%End";
$line .= "\n%Docstring\n$comment\n%End\n";

my $skip;
# Skip opening curly bracket, we already added that above
$skip = readline $header;
$skip =~ m/^\s*{\s$/ || die "Unexpected content on line $line";
}

$line =~ s/SIP_FACTORY/\/Factory\//;
$line =~ s/SIP_SKIP/\/\//;
$line =~ s/SIP_OUT/\/Out\//g;
$line =~ s/SIP_INOUT/\/In,Out\//g;
$line =~ s/SIP_TRANSFER/\/Transfer\//g;
$line =~ s/SIP_PYNAME\((\w+)\)/\/PyName=$1\//;
$line =~ s/SIP_KEEPREFERENCE\((\w+)\)/\/KeepReference\//;
$line =~ s/SIP_TRANSFERTHIS\((\w+)\)/\/TransferThis\//;
$line =~ s/SIP_TRANSFERBACK\((\w+)\)/\/TransferBack\//;

$line =~ s/override;/;/g;
}

# In block comment
if ($DOXYGEN_BLOCK == $context) {
if ($line =~ m/\*\//) {
$context = $GLOBAL;
}
$line =~ m/\s*\*?(.*)/;
$comment .= "$1\n";
next;
}

print $line;
}
45 changes: 45 additions & 0 deletions src/core/qgis.h
Expand Up @@ -387,3 +387,48 @@ typedef unsigned long long qgssize;
#else
#define FALLTHROUGH
#endif

/**
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html?highlight=keepreference#function-annotation-Transfer
*
* Example QgsVectorLayer::setDiagramRenderer
*/
#define SIP_TRANSFER

/**
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html?highlight=keepreference#function-annotation-TransferBack
*/
#define SIP_TRANSFERBACK

/**
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html?highlight=keepreference#function-annotation-TransferThis
*/
#define SIP_TRANSFERTHIS

/**
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html?highlight=keepreference#argument-annotation-Out
*/
#define SIP_OUT

/**
* Combination of
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html?highlight=keepreference#argument-annotation-In
* and
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html?highlight=keepreference#argument-annotation-Out
*/
#define SIP_IN_OUT

/**
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html?highlight=keepreference#function-annotation-Factory
*/
#define SIP_FACTORY

/**
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html?highlight=keepreference#function-annotation-PyName
*/
#define SIP_PYNAME(name)

/**
* http://pyqt.sourceforge.net/Docs/sip4/annotations.html?highlight=keepreference#argument-annotation-KeepReference
*/
#define SIP_KEEPREFERENCE
16 changes: 8 additions & 8 deletions src/core/qgsvectorlayer.h
Expand Up @@ -657,7 +657,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
bool diagramsEnabled() const;

//! Sets diagram rendering object (takes ownership)
void setDiagramRenderer( QgsDiagramRenderer *r );
void setDiagramRenderer( QgsDiagramRenderer *r SIP_TRANSFER );
const QgsDiagramRenderer *diagramRenderer() const { return mDiagramRenderer; }

void setDiagramLayerSettings( const QgsDiagramLayerSettings &s );
Expand All @@ -675,7 +675,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* Set renderer which will be invoked to represent this layer.
* Ownership is transferred.
*/
void setRenderer( QgsFeatureRenderer *r );
void setRenderer( QgsFeatureRenderer *r SIP_TRANSFER );

//! Returns point, line or polygon
QgsWkbTypes::GeometryType geometryType() const;
Expand Down Expand Up @@ -911,7 +911,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* ring and item (first number is index 0), and feature
* to the given coordinates
*/
bool moveVertex( double x, double y, QgsFeatureId atFeatureId, int atVertex );
bool moveVertex( double x, double y, QgsFeatureId atFeatureId, int atVertex ) SIP_PYNAME( moveVertexV2 );

/** Moves the vertex at the given position number,
* ring and item (first number is index 0), and feature
Expand Down Expand Up @@ -958,7 +958,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* @note available in python as addCurvedRing
*/
// TODO QGIS 3.0 returns an enum instead of a magic constant
int addRing( QgsCurve *ring, QgsFeatureId *featureId = nullptr );
int addRing( QgsCurve *ring SIP_TRANSFER, QgsFeatureId *featureId = nullptr ) SIP_PYNAME( addCurvedRing );

/** Adds a new part polygon to a multipart feature
* @return
Expand Down Expand Up @@ -987,10 +987,10 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* @note available in python bindings as addPartV2
*/
// TODO QGIS 3.0 returns an enum instead of a magic constant
int addPart( const QgsPointSequence &ring );
int addPart( const QgsPointSequence &ring ) SIP_PYNAME( addPartV2 );

//! @note available in python as addCurvedPart
int addPart( QgsCurve *ring );
int addPart( QgsCurve *ring ) SIP_PYNAME( addCurvedPart );

/** Translates feature by dx, dy
* @param featureId id of the feature to translate
Expand Down Expand Up @@ -1080,7 +1080,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
int snapWithContext( const QgsPoint &startPoint,
double snappingTolerance,
QMultiMap < double, QgsSnappingResult > &snappingResults,
QMultiMap < double, QgsSnappingResult > &snappingResults SIP_OUT,
QgsSnapper::SnappingType snap_to );

//! Synchronises with changes in the datasource
Expand All @@ -1089,7 +1089,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
/** Return new instance of QgsMapLayerRenderer that will be used for rendering of given context
* @note added in 2.4
*/
virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) override;
virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) override SIP_FACTORY;

//! Return the extent of the layer
QgsRectangle extent() const override;
Expand Down

0 comments on commit ca008d7

Please sign in to comment.