Skip to content

Commit aa2d3bc

Browse files
committed
Feature #8725: Replace '*foo=bar' by 'memcpy'
1 parent 4f8e7e0 commit aa2d3bc

File tree

3 files changed

+93
-73
lines changed

3 files changed

+93
-73
lines changed

src/core/qgsfeaturerequest.cpp

+81-61
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ inline static float calculateViewPixelTolerance( const QgsRectangle& boundingRec
232232
double mapUnitsFactor = 1;
233233

234234
// Calculate one aprox factor of the size of the BBOX from the source CoordinateSystem to the target CoordinateSystem.
235-
if (ct && !((QgsCoordinateTransform*)ct)->isShortCircuited())
235+
if ( ct && !((QgsCoordinateTransform*)ct)->isShortCircuited() )
236236
{
237237
QgsRectangle sourceRect = boundingRect;
238238
QgsRectangle targetRect = ct->transform(sourceRect);
@@ -259,7 +259,7 @@ inline static QgsRectangle calculateBoundingBox( const QVector<QPointF>& points
259259
double xmax = -std::numeric_limits<double>::max();
260260
double ymax = -std::numeric_limits<double>::max();
261261

262-
for (int i = 0, numPoints = points.size(); i < numPoints; ++i)
262+
for ( int i = 0, numPoints = points.size(); i < numPoints; ++i )
263263
{
264264
x = points[i].x();
265265
y = points[i].y();
@@ -285,10 +285,10 @@ inline static QgsRectangle calculateBoundingBox( QGis::WkbType wkbType, unsigned
285285
int sizeOfDoubleX = sizeof(double);
286286
int sizeOfDoubleY = QGis::wkbDimensions(wkbType)==3 /*hasZValue*/ ? 2*sizeof(double) : sizeof(double);
287287

288-
for (size_t i = 0; i < numPoints; ++i)
288+
for ( size_t i = 0; i < numPoints; ++i )
289289
{
290-
x = *(( double * ) wkb ); wkb += sizeOfDoubleX;
291-
y = *(( double * ) wkb ); wkb += sizeOfDoubleY;
290+
memcpy( &x, wkb, sizeof( double ) ); wkb += sizeOfDoubleX;
291+
memcpy( &y, wkb, sizeof( double ) ); wkb += sizeOfDoubleY;
292292

293293
if (xmin>x) xmin = x;
294294
if (ymin>y) ymin = y;
@@ -310,7 +310,7 @@ inline static bool generalizeGeometry( QGis::WkbType wkbType, unsigned char* sou
310310
int sizeOfDoubleY = QGis::wkbDimensions(wkbType)==3 /*hasZValue*/ ? 2*sizeof(double) : sizeof(double);
311311

312312
// Skip the unnecesary generalization because of is a very single geometry
313-
size_t minimumSize = (geometryType==QGis::WKBLineString ? 4 + 2*(sizeOfDoubleX+sizeOfDoubleY) : 8 + 5*(sizeOfDoubleX+sizeOfDoubleY) );
313+
size_t minimumSize = ( geometryType==QGis::WKBLineString ? 4 + 2*(sizeOfDoubleX+sizeOfDoubleY) : 8 + 5*(sizeOfDoubleX+sizeOfDoubleY) );
314314
if ( writeHeader ) minimumSize += 5;
315315
if ( sourceWkbSize <= minimumSize )
316316
{
@@ -327,40 +327,48 @@ inline static bool generalizeGeometry( QGis::WkbType wkbType, unsigned char* sou
327327
if ( writeHeader )
328328
{
329329
char byteOrder = QgsApplication::endian(); // byteOrder
330-
*targetWkb = byteOrder;
330+
memcpy( targetWkb, &byteOrder, 1 );
331331
targetWkb += 1;
332332

333-
*((int*)targetWkb) = geometryType; // type
333+
memcpy( targetWkb, &geometryType, 4 ); // type
334334
targetWkb += 4;
335335

336-
if (geometryType==QGis::WKBPolygon) { *((int*)targetWkb) = 1; targetWkb += 4; } // numRings
336+
if ( geometryType == QGis::WKBPolygon ) // numRings
337+
{
338+
int numRings = 1;
339+
memcpy( targetWkb, &numRings, 4 );
340+
targetWkb += 4;
341+
}
337342
}
338343

339344
// Write the generalized geometry
340-
if (geometryType==QGis::WKBLineString)
345+
if ( geometryType == QGis::WKBLineString )
341346
{
342-
*((int*)targetWkb) = 2; // numPoints;
343-
targetWkb += 4;
344-
345-
double* ptr = (double*)targetWkb;
346-
targetWkb += 32;
347+
int numPoints = 2;
348+
memcpy( targetWkb, &numPoints, 4 ); // numPoints;
349+
targetWkb += 4;
347350

348-
*ptr = x1; ptr++; *ptr = y1; ptr++;
349-
*ptr = x2; ptr++; *ptr = y2; ptr++;
351+
memcpy( targetWkb, &x1, sizeof( double ) ); targetWkb += sizeof( double );
352+
memcpy( targetWkb, &y1, sizeof( double ) ); targetWkb += sizeof( double );
353+
memcpy( targetWkb, &x2, sizeof( double ) ); targetWkb += sizeof( double );
354+
memcpy( targetWkb, &y2, sizeof( double ) ); targetWkb += sizeof( double );
350355
}
351356
else
352357
{
353-
*((int*)targetWkb) = 5; // numPoints;
358+
int numPoints = 5;
359+
memcpy( targetWkb, &numPoints, 4 ); // numPoints;
354360
targetWkb += 4;
355361

356-
double* ptr = (double*)targetWkb;
357-
targetWkb += 80;
358-
359-
*ptr = x1; ptr++; *ptr = y1; ptr++;
360-
*ptr = x2; ptr++; *ptr = y1; ptr++;
361-
*ptr = x2; ptr++; *ptr = y2; ptr++;
362-
*ptr = x1; ptr++; *ptr = y2; ptr++;
363-
*ptr = x1; ptr++; *ptr = y1; ptr++;
362+
memcpy( targetWkb, &x1, sizeof( double ) ); targetWkb += sizeof( double );
363+
memcpy( targetWkb, &y1, sizeof( double ) ); targetWkb += sizeof( double );
364+
memcpy( targetWkb, &x2, sizeof( double ) ); targetWkb += sizeof( double );
365+
memcpy( targetWkb, &y1, sizeof( double ) ); targetWkb += sizeof( double );
366+
memcpy( targetWkb, &x2, sizeof( double ) ); targetWkb += sizeof( double );
367+
memcpy( targetWkb, &y2, sizeof( double ) ); targetWkb += sizeof( double );
368+
memcpy( targetWkb, &x1, sizeof( double ) ); targetWkb += sizeof( double );
369+
memcpy( targetWkb, &y2, sizeof( double ) ); targetWkb += sizeof( double );
370+
memcpy( targetWkb, &x1, sizeof( double ) ); targetWkb += sizeof( double );
371+
memcpy( targetWkb, &y1, sizeof( double ) ); targetWkb += sizeof( double );
364372
}
365373
targetWkbSize += targetWkb - wkb2;
366374
targetWkb = wkb2;
@@ -384,11 +392,14 @@ inline static bool simplifyWkbGeometry( QGis::WkbType wkbType, unsigned char* so
384392
// Write the main header of the geometry
385393
if ( writeHeader )
386394
{
387-
*targetWkb = *sourceWkb; // byteOrder
395+
memcpy( targetWkb, sourceWkb, 1 ); // byteOrder
388396
sourceWkb += 1;
389397
targetWkb += 1;
390398

391-
*((int*)targetWkb) = QGis::flatType( (QGis::WkbType) *((int*)sourceWkb) ); // type
399+
int geometryType;
400+
memcpy( &geometryType, sourceWkb, 4 );
401+
int flatType = QGis::flatType( (QGis::WkbType)geometryType );
402+
memcpy( targetWkb, &flatType, 4 ); // type
392403
sourceWkb += 4;
393404
targetWkb += 4;
394405

@@ -400,35 +411,36 @@ inline static bool simplifyWkbGeometry( QGis::WkbType wkbType, unsigned char* so
400411
unsigned int flatType = QGis::flatType( wkbType );
401412

402413
// Write the geometry
403-
if (flatType==QGis::WKBLineString || isaLinearRing)
414+
if ( flatType == QGis::WKBLineString || isaLinearRing )
404415
{
405416
double x,y, lastX=0,lastY=0;
406417

407418
int sizeOfDoubleX = sizeof(double);
408419
int sizeOfDoubleY = QGis::wkbDimensions(wkbType)==3 /*hasZValue*/ ? 2*sizeof(double) : sizeof(double);
409420

410-
int numPoints = *((int*)sourceWkb);
421+
int numPoints;
422+
memcpy( &numPoints, sourceWkb, 4);
411423
sourceWkb += 4;
412424
if (numPoints <= (isaLinearRing ? 5 : 2)) canbeGeneralizable = false;
413425

414426
int numTargetPoints = 0;
415-
*((int*)targetWkb) = numTargetPoints;
427+
memcpy( targetWkb, &numTargetPoints, 4 );
416428
targetWkb += 4;
417429
targetWkbSize += 4;
418430

419431
double* ptr = (double*)targetWkb;
420432
map2pixelTol *= map2pixelTol; //-> Use mappixelTol for 'LengthSquare' calculations.
421433

422434
// Process each vertex...
423-
for (int i = 0, numPoints_i = (isaLinearRing ? numPoints-1 : numPoints); i < numPoints_i; ++i)
435+
for ( int i = 0, numPoints_i = (isaLinearRing ? numPoints-1 : numPoints); i < numPoints_i; ++i )
424436
{
425-
x = *((double*)sourceWkb); sourceWkb += sizeOfDoubleX;
426-
y = *((double*)sourceWkb); sourceWkb += sizeOfDoubleY;
437+
memcpy( &x, sourceWkb, sizeof( double ) ); sourceWkb += sizeOfDoubleX;
438+
memcpy( &y, sourceWkb, sizeof( double ) ); sourceWkb += sizeOfDoubleY;
427439

428440
if ( i==0 || !canbeGeneralizable || calculateLengthSquared2D(x,y,lastX,lastY)>map2pixelTol )
429441
{
430-
*ptr = lastX = x; ptr++;
431-
*ptr = lastY = y; ptr++;
442+
memcpy( ptr, &x, sizeof( double ) ); lastX = x; ptr++;
443+
memcpy( ptr, &y, sizeof( double ) ); lastY = y; ptr++;
432444
numTargetPoints++;
433445
}
434446
}
@@ -437,29 +449,33 @@ inline static bool simplifyWkbGeometry( QGis::WkbType wkbType, unsigned char* so
437449
// Fix the topology of the geometry
438450
if ( isaLinearRing )
439451
{
440-
*ptr = x = *((double*)(targetWkb+0)); ptr++;
441-
*ptr = y = *((double*)(targetWkb+8)); ptr++;
452+
memcpy( &x, targetWkb+0, sizeof( double ) );
453+
memcpy( &y, targetWkb+8, sizeof( double ) );
454+
memcpy( ptr, &x, sizeof( double ) ); ptr++;
455+
memcpy( ptr, &y, sizeof( double ) ); ptr++;
442456
numTargetPoints++;
443457
}
444458
targetWkbSize += numTargetPoints * 16;
445459
targetWkb = wkb2;
446460

447-
*((int*)targetWkb) = numTargetPoints;
461+
memcpy( targetWkb, &numTargetPoints, 4 );
448462
result = numPoints!=numTargetPoints;
449463
}
450464
else
451-
if (flatType==QGis::WKBPolygon)
465+
if ( flatType == QGis::WKBPolygon )
452466
{
453-
int numRings = *((int*)sourceWkb);
467+
int numRings;
468+
memcpy( &numRings, sourceWkb, 4 );
454469
sourceWkb += 4;
455470

456-
*((int*)targetWkb) = numRings;
471+
memcpy( targetWkb, &numRings, 4 );
457472
targetWkb += 4;
458473
targetWkbSize += 4;
459474

460-
for (int i = 0; i < numRings; ++i)
475+
for ( int i = 0; i < numRings; ++i )
461476
{
462-
int numPoints_i = *((int*)sourceWkb);
477+
int numPoints_i;
478+
memcpy( &numPoints_i, sourceWkb, 4 );
463479
QgsRectangle envelope_i = numRings==1 ? envelope : calculateBoundingBox( wkbType, sourceWkb+4, numPoints_i );
464480

465481
size_t sourceWkbSize_i = 4 + numPoints_i * (hasZValue ? 3 : 2) * sizeof(double);
@@ -473,39 +489,43 @@ inline static bool simplifyWkbGeometry( QGis::WkbType wkbType, unsigned char* so
473489
}
474490
}
475491
else
476-
if (flatType==QGis::WKBMultiLineString || flatType==QGis::WKBMultiPolygon)
492+
if ( flatType == QGis::WKBMultiLineString || flatType == QGis::WKBMultiPolygon )
477493
{
478-
int numGeoms = *((int*)sourceWkb);
494+
int numGeoms;
495+
memcpy( &numGeoms, sourceWkb, 4 );
479496
sourceWkb += 4;
480497
wkb1 += 4;
481498

482-
*((int*)targetWkb) = numGeoms;
499+
memcpy( targetWkb, &numGeoms, 4 );
483500
targetWkb += 4;
484501
targetWkbSize += 4;
485502

486-
for (int i = 0; i < numGeoms; ++i)
503+
for ( int i = 0; i < numGeoms; ++i )
487504
{
488505
size_t sourceWkbSize_i = 0;
489506
size_t targetWkbSize_i = 0;
490507

491508
// ... calculate the wkb-size of the current child complex geometry
492-
if (flatType==QGis::WKBMultiLineString)
509+
if ( flatType == QGis::WKBMultiLineString )
493510
{
494-
int numPoints_i = *((int*)(wkb1+5));
511+
int numPoints_i;
512+
memcpy( &numPoints_i, wkb1+5, 4 );
495513
int wkbSize_i = 4 + numPoints_i * (hasZValue ? 3 : 2) * sizeof(double);
496514

497515
sourceWkbSize_i += 5 + wkbSize_i;
498516
wkb1 += 5 + wkbSize_i;
499517
}
500518
else
501519
{
502-
int numPrings_i = *((int*)(wkb1+5));
520+
int numPrings_i;
521+
memcpy( &numPrings_i, wkb1+5, 4 );
503522
sourceWkbSize_i = 9;
504523
wkb1 += 9;
505524

506525
for (int j = 0; j < numPrings_i; ++j)
507526
{
508-
int numPoints_i = *((int*)(wkb1));
527+
int numPoints_i;
528+
memcpy( &numPoints_i, wkb1, 4);
509529
int wkbSize_i = 4 + numPoints_i * (hasZValue ? 3 : 2) * sizeof(double);
510530

511531
sourceWkbSize_i += wkbSize_i;
@@ -556,7 +576,7 @@ bool QgsFeatureRequest::simplifyGeometry( QgsGeometry* geometry, const QgsCoordi
556576

557577
// Check whether the geometry can be simplified using the map2pixel context
558578
QGis::GeometryType geometryType = geometry->type();
559-
if (!(geometryType==QGis::Line || geometryType==QGis::Polygon)) return false;
579+
if ( !(geometryType==QGis::Line || geometryType==QGis::Polygon) ) return false;
560580

561581
QgsRectangle envelope = geometry->boundingBox();
562582
QGis::WkbType wkbType = geometry->wkbType();
@@ -580,7 +600,7 @@ bool QgsFeatureRequest::simplifyGeometry( QgsGeometry* geometry, const QgsCoordi
580600
bool QgsFeatureRequest::simplifyGeometry( QGis::GeometryType geometryType, const QgsRectangle& envelope, double* xptr, int xStride, double* yptr, int yStride, int pointCount, int& pointSimplifiedCount, const QgsCoordinateTransform* coordinateTransform, const QgsMapToPixel* mtp, float mapToPixelTol )
581601
{
582602
pointSimplifiedCount = pointCount;
583-
if (geometryType==QGis::Point || geometryType==QGis::UnknownGeometry) return false;
603+
if ( geometryType == QGis::Point || geometryType == QGis::UnknownGeometry ) return false;
584604
pointSimplifiedCount = 0;
585605

586606
double map2pixelTol = mapToPixelTol * calculateViewPixelTolerance( envelope, coordinateTransform, mtp );
@@ -594,20 +614,20 @@ bool QgsFeatureRequest::simplifyGeometry( QGis::GeometryType geometryType, const
594614

595615
for ( int i = 0, numPoints = geometryType==QGis::Polygon ? pointCount-1 : pointCount; i < numPoints; ++i )
596616
{
597-
x = *((double*)xsourcePtr); xsourcePtr += xStride;
598-
y = *((double*)ysourcePtr); ysourcePtr += yStride;
617+
memcpy( &x, xsourcePtr, sizeof( double ) ); xsourcePtr += xStride;
618+
memcpy( &y, ysourcePtr, sizeof( double ) ); ysourcePtr += yStride;
599619

600620
if ( i==0 || calculateLengthSquared2D(x,y,lastX,lastY)>map2pixelTol )
601621
{
602-
*((double*)xtargetPtr) = lastX = x; xtargetPtr += xStride;
603-
*((double*)ytargetPtr) = lastY = y; ytargetPtr += yStride;
622+
memcpy( xtargetPtr, &x, sizeof( double ) ); lastX = x; xtargetPtr += xStride;
623+
memcpy( ytargetPtr, &y, sizeof( double ) ); lastY = y; ytargetPtr += yStride;
604624
pointSimplifiedCount++;
605625
}
606626
}
607-
if ( geometryType==QGis::Polygon )
627+
if ( geometryType == QGis::Polygon )
608628
{
609-
*((double*)xtargetPtr) = *xptr;
610-
*((double*)ytargetPtr) = *yptr;
629+
memcpy( xtargetPtr, xptr, sizeof( double ) );
630+
memcpy( ytargetPtr, yptr, sizeof( double ) );
611631
pointSimplifiedCount++;
612632
}
613633
return pointSimplifiedCount!=pointCount;

src/core/symbology-ng/qgsrendererv2.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ const unsigned char* QgsFeatureRendererV2::_getLineString( QPolygonF& pts, QgsRe
9090
QPointF* ptr = pts.data();
9191
for ( unsigned int i = 0; i < nPoints; ++i, ++ptr )
9292
{
93-
x = *(( double * ) wkb ); wkb += sizeOfDoubleX;
94-
y = *(( double * ) wkb ); wkb += sizeOfDoubleY;
93+
memcpy( &x, wkb, sizeof( double ) ); wkb += sizeOfDoubleX;
94+
memcpy( &y, wkb, sizeof( double ) ); wkb += sizeOfDoubleY;
9595

9696
*ptr = QPointF( x, y );
9797
}
@@ -149,8 +149,8 @@ const unsigned char* QgsFeatureRendererV2::_getPolygon( QPolygonF& pts, QList<QP
149149
QPointF* ptr = poly.data();
150150
for ( unsigned int jdx = 0; jdx < nPoints; ++jdx, ++ptr )
151151
{
152-
x = *(( double * ) wkb ); wkb += sizeOfDoubleX;
153-
y = *(( double * ) wkb ); wkb += sizeOfDoubleY;
152+
memcpy( &x, wkb, sizeof( double ) ); wkb += sizeOfDoubleX;
153+
memcpy( &y, wkb, sizeof( double ) ); wkb += sizeOfDoubleY;
154154

155155
*ptr = QPointF( x, y );
156156
}

src/providers/ogr/qgsogrfeatureiterator.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,15 @@ bool QgsOgrSimplifiedFeatureIterator::simplifyOgrGeometry( const QgsFeatureReque
343343
OGRwkbGeometryType wkbGeometryType = wkbFlatten( geometry->getGeometryType() );
344344

345345
// Simplify the geometry rewriting temporally its WKB-stream for saving calloc's.
346-
if (wkbGeometryType==wkbLineString)
346+
if ( wkbGeometryType == wkbLineString )
347347
{
348348
OGRLineString* lineString = (OGRLineString*)geometry;
349349

350350
int numPoints = lineString->getNumPoints();
351351
if ( (isaLinearRing && numPoints<=5) || (!isaLinearRing && numPoints<=2) ) return false;
352352

353353
OGREnvelope env;
354-
geometry->getEnvelope(&env );
354+
geometry->getEnvelope( &env );
355355
QgsRectangle envelope( env.MinX, env.MinY, env.MaxX, env.MaxY );
356356

357357
// Can replace the geometry by its BBOX ?
@@ -398,32 +398,32 @@ bool QgsOgrSimplifiedFeatureIterator::simplifyOgrGeometry( const QgsFeatureReque
398398

399399
if ( request.simplifyGeometry( geometryType, envelope, xptr, 16, yptr, 16, numPoints, numSimplifiedPoints ) )
400400
{
401-
lineString->setPoints(numSimplifiedPoints, points);
401+
lineString->setPoints( numSimplifiedPoints, points );
402402
lineString->flattenTo2D();
403403
}
404404
return numSimplifiedPoints!=numPoints;
405405
}
406406
}
407407
else
408-
if (wkbGeometryType==wkbPolygon)
408+
if ( wkbGeometryType == wkbPolygon )
409409
{
410410
OGRPolygon* polygon = (OGRPolygon*)geometry;
411411
bool result = simplifyOgrGeometry( request, polygon->getExteriorRing(), true );
412412

413-
for (int i = 0, numInteriorRings = polygon->getNumInteriorRings(); i < numInteriorRings; ++i)
413+
for ( int i = 0, numInteriorRings = polygon->getNumInteriorRings(); i < numInteriorRings; ++i )
414414
{
415415
result |= simplifyOgrGeometry( request, polygon->getInteriorRing(i), true );
416416
}
417417
if ( result ) polygon->flattenTo2D();
418418
return result;
419419
}
420420
else
421-
if (wkbGeometryType==wkbMultiLineString || wkbGeometryType==wkbMultiPolygon)
421+
if ( wkbGeometryType == wkbMultiLineString || wkbGeometryType == wkbMultiPolygon )
422422
{
423423
OGRGeometryCollection* collection = (OGRGeometryCollection*)geometry;
424424
bool result = false;
425425

426-
for (int i = 0, numGeometries = collection->getNumGeometries(); i < numGeometries; ++i)
426+
for ( int i = 0, numGeometries = collection->getNumGeometries(); i < numGeometries; ++i )
427427
{
428428
result |= simplifyOgrGeometry( request, collection->getGeometryRef(i), wkbGeometryType==wkbMultiPolygon );
429429
}
@@ -440,7 +440,7 @@ void QgsOgrSimplifiedFeatureIterator::notifyReadedFeature( OGRFeatureH fet, OGRG
440440
{
441441
OGRwkbGeometryType wkbType = QgsOgrProvider::ogrWkbSingleFlatten( OGR_G_GetGeometryType(geom) );
442442

443-
if (wkbType==wkbLineString || wkbType==wkbPolygon)
443+
if ( wkbType == wkbLineString || wkbType == wkbPolygon )
444444
{
445445
simplifyOgrGeometry( mRequest, (OGRGeometry*)geom, wkbType==wkbPolygon );
446446
}

0 commit comments

Comments
 (0)