Skip to content

Commit 200ae38

Browse files
committedOct 27, 2013
Feature #8725: Replace '*foo=bar' by 'memcpy'
1 parent c9fccbd commit 200ae38

File tree

3 files changed

+93
-73
lines changed

3 files changed

+93
-73
lines changed
 

‎src/core/qgsfeaturerequest.cpp

Lines changed: 81 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ inline static float calculateViewPixelTolerance( const QgsRectangle& boundingRec
225225
double mapUnitsFactor = 1;
226226

227227
// Calculate one aprox factor of the size of the BBOX from the source CoordinateSystem to the target CoordinateSystem.
228-
if (ct && !((QgsCoordinateTransform*)ct)->isShortCircuited())
228+
if ( ct && !((QgsCoordinateTransform*)ct)->isShortCircuited() )
229229
{
230230
QgsRectangle sourceRect = boundingRect;
231231
QgsRectangle targetRect = ct->transform(sourceRect);
@@ -252,7 +252,7 @@ inline static QgsRectangle calculateBoundingBox( const QVector<QPointF>& points
252252
double xmax = -std::numeric_limits<double>::max();
253253
double ymax = -std::numeric_limits<double>::max();
254254

255-
for (int i = 0, numPoints = points.size(); i < numPoints; ++i)
255+
for ( int i = 0, numPoints = points.size(); i < numPoints; ++i )
256256
{
257257
x = points[i].x();
258258
y = points[i].y();
@@ -278,10 +278,10 @@ inline static QgsRectangle calculateBoundingBox( QGis::WkbType wkbType, unsigned
278278
int sizeOfDoubleX = sizeof(double);
279279
int sizeOfDoubleY = QGis::wkbDimensions(wkbType)==3 /*hasZValue*/ ? 2*sizeof(double) : sizeof(double);
280280

281-
for (size_t i = 0; i < numPoints; ++i)
281+
for ( size_t i = 0; i < numPoints; ++i )
282282
{
283-
x = *(( double * ) wkb ); wkb += sizeOfDoubleX;
284-
y = *(( double * ) wkb ); wkb += sizeOfDoubleY;
283+
memcpy( &x, wkb, sizeof( double ) ); wkb += sizeOfDoubleX;
284+
memcpy( &y, wkb, sizeof( double ) ); wkb += sizeOfDoubleY;
285285

286286
if (xmin>x) xmin = x;
287287
if (ymin>y) ymin = y;
@@ -303,7 +303,7 @@ inline static bool generalizeGeometry( QGis::WkbType wkbType, unsigned char* sou
303303
int sizeOfDoubleY = QGis::wkbDimensions(wkbType)==3 /*hasZValue*/ ? 2*sizeof(double) : sizeof(double);
304304

305305
// Skip the unnecesary generalization because of is a very single geometry
306-
size_t minimumSize = (geometryType==QGis::WKBLineString ? 4 + 2*(sizeOfDoubleX+sizeOfDoubleY) : 8 + 5*(sizeOfDoubleX+sizeOfDoubleY) );
306+
size_t minimumSize = ( geometryType==QGis::WKBLineString ? 4 + 2*(sizeOfDoubleX+sizeOfDoubleY) : 8 + 5*(sizeOfDoubleX+sizeOfDoubleY) );
307307
if ( writeHeader ) minimumSize += 5;
308308
if ( sourceWkbSize <= minimumSize )
309309
{
@@ -320,40 +320,48 @@ inline static bool generalizeGeometry( QGis::WkbType wkbType, unsigned char* sou
320320
if ( writeHeader )
321321
{
322322
char byteOrder = QgsApplication::endian(); // byteOrder
323-
*targetWkb = byteOrder;
323+
memcpy( targetWkb, &byteOrder, 1 );
324324
targetWkb += 1;
325325

326-
*((int*)targetWkb) = geometryType; // type
326+
memcpy( targetWkb, &geometryType, 4 ); // type
327327
targetWkb += 4;
328328

329-
if (geometryType==QGis::WKBPolygon) { *((int*)targetWkb) = 1; targetWkb += 4; } // numRings
329+
if ( geometryType == QGis::WKBPolygon ) // numRings
330+
{
331+
int numRings = 1;
332+
memcpy( targetWkb, &numRings, 4 );
333+
targetWkb += 4;
334+
}
330335
}
331336

332337
// Write the generalized geometry
333-
if (geometryType==QGis::WKBLineString)
338+
if ( geometryType == QGis::WKBLineString )
334339
{
335-
*((int*)targetWkb) = 2; // numPoints;
336-
targetWkb += 4;
337-
338-
double* ptr = (double*)targetWkb;
339-
targetWkb += 32;
340+
int numPoints = 2;
341+
memcpy( targetWkb, &numPoints, 4 ); // numPoints;
342+
targetWkb += 4;
340343

341-
*ptr = x1; ptr++; *ptr = y1; ptr++;
342-
*ptr = x2; ptr++; *ptr = y2; ptr++;
344+
memcpy( targetWkb, &x1, sizeof( double ) ); targetWkb += sizeof( double );
345+
memcpy( targetWkb, &y1, sizeof( double ) ); targetWkb += sizeof( double );
346+
memcpy( targetWkb, &x2, sizeof( double ) ); targetWkb += sizeof( double );
347+
memcpy( targetWkb, &y2, sizeof( double ) ); targetWkb += sizeof( double );
343348
}
344349
else
345350
{
346-
*((int*)targetWkb) = 5; // numPoints;
351+
int numPoints = 5;
352+
memcpy( targetWkb, &numPoints, 4 ); // numPoints;
347353
targetWkb += 4;
348354

349-
double* ptr = (double*)targetWkb;
350-
targetWkb += 80;
351-
352-
*ptr = x1; ptr++; *ptr = y1; ptr++;
353-
*ptr = x2; ptr++; *ptr = y1; ptr++;
354-
*ptr = x2; ptr++; *ptr = y2; ptr++;
355-
*ptr = x1; ptr++; *ptr = y2; ptr++;
356-
*ptr = x1; ptr++; *ptr = y1; ptr++;
355+
memcpy( targetWkb, &x1, sizeof( double ) ); targetWkb += sizeof( double );
356+
memcpy( targetWkb, &y1, sizeof( double ) ); targetWkb += sizeof( double );
357+
memcpy( targetWkb, &x2, sizeof( double ) ); targetWkb += sizeof( double );
358+
memcpy( targetWkb, &y1, sizeof( double ) ); targetWkb += sizeof( double );
359+
memcpy( targetWkb, &x2, sizeof( double ) ); targetWkb += sizeof( double );
360+
memcpy( targetWkb, &y2, sizeof( double ) ); targetWkb += sizeof( double );
361+
memcpy( targetWkb, &x1, sizeof( double ) ); targetWkb += sizeof( double );
362+
memcpy( targetWkb, &y2, sizeof( double ) ); targetWkb += sizeof( double );
363+
memcpy( targetWkb, &x1, sizeof( double ) ); targetWkb += sizeof( double );
364+
memcpy( targetWkb, &y1, sizeof( double ) ); targetWkb += sizeof( double );
357365
}
358366
targetWkbSize += targetWkb - wkb2;
359367
targetWkb = wkb2;
@@ -377,11 +385,14 @@ inline static bool simplifyWkbGeometry( QGis::WkbType wkbType, unsigned char* so
377385
// Write the main header of the geometry
378386
if ( writeHeader )
379387
{
380-
*targetWkb = *sourceWkb; // byteOrder
388+
memcpy( targetWkb, sourceWkb, 1 ); // byteOrder
381389
sourceWkb += 1;
382390
targetWkb += 1;
383391

384-
*((int*)targetWkb) = QGis::flatType( (QGis::WkbType) *((int*)sourceWkb) ); // type
392+
int geometryType;
393+
memcpy( &geometryType, sourceWkb, 4 );
394+
int flatType = QGis::flatType( (QGis::WkbType)geometryType );
395+
memcpy( targetWkb, &flatType, 4 ); // type
385396
sourceWkb += 4;
386397
targetWkb += 4;
387398

@@ -393,35 +404,36 @@ inline static bool simplifyWkbGeometry( QGis::WkbType wkbType, unsigned char* so
393404
unsigned int flatType = QGis::flatType( wkbType );
394405

395406
// Write the geometry
396-
if (flatType==QGis::WKBLineString || isaLinearRing)
407+
if ( flatType == QGis::WKBLineString || isaLinearRing )
397408
{
398409
double x,y, lastX=0,lastY=0;
399410

400411
int sizeOfDoubleX = sizeof(double);
401412
int sizeOfDoubleY = QGis::wkbDimensions(wkbType)==3 /*hasZValue*/ ? 2*sizeof(double) : sizeof(double);
402413

403-
int numPoints = *((int*)sourceWkb);
414+
int numPoints;
415+
memcpy( &numPoints, sourceWkb, 4);
404416
sourceWkb += 4;
405417
if (numPoints <= (isaLinearRing ? 5 : 2)) canbeGeneralizable = false;
406418

407419
int numTargetPoints = 0;
408-
*((int*)targetWkb) = numTargetPoints;
420+
memcpy( targetWkb, &numTargetPoints, 4 );
409421
targetWkb += 4;
410422
targetWkbSize += 4;
411423

412424
double* ptr = (double*)targetWkb;
413425
map2pixelTol *= map2pixelTol; //-> Use mappixelTol for 'LengthSquare' calculations.
414426

415427
// Process each vertex...
416-
for (int i = 0, numPoints_i = (isaLinearRing ? numPoints-1 : numPoints); i < numPoints_i; ++i)
428+
for ( int i = 0, numPoints_i = (isaLinearRing ? numPoints-1 : numPoints); i < numPoints_i; ++i )
417429
{
418-
x = *((double*)sourceWkb); sourceWkb += sizeOfDoubleX;
419-
y = *((double*)sourceWkb); sourceWkb += sizeOfDoubleY;
430+
memcpy( &x, sourceWkb, sizeof( double ) ); sourceWkb += sizeOfDoubleX;
431+
memcpy( &y, sourceWkb, sizeof( double ) ); sourceWkb += sizeOfDoubleY;
420432

421433
if ( i==0 || !canbeGeneralizable || calculateLengthSquared2D(x,y,lastX,lastY)>map2pixelTol )
422434
{
423-
*ptr = lastX = x; ptr++;
424-
*ptr = lastY = y; ptr++;
435+
memcpy( ptr, &x, sizeof( double ) ); lastX = x; ptr++;
436+
memcpy( ptr, &y, sizeof( double ) ); lastY = y; ptr++;
425437
numTargetPoints++;
426438
}
427439
}
@@ -430,29 +442,33 @@ inline static bool simplifyWkbGeometry( QGis::WkbType wkbType, unsigned char* so
430442
// Fix the topology of the geometry
431443
if ( isaLinearRing )
432444
{
433-
*ptr = x = *((double*)(targetWkb+0)); ptr++;
434-
*ptr = y = *((double*)(targetWkb+8)); ptr++;
445+
memcpy( &x, targetWkb+0, sizeof( double ) );
446+
memcpy( &y, targetWkb+8, sizeof( double ) );
447+
memcpy( ptr, &x, sizeof( double ) ); ptr++;
448+
memcpy( ptr, &y, sizeof( double ) ); ptr++;
435449
numTargetPoints++;
436450
}
437451
targetWkbSize += numTargetPoints * 16;
438452
targetWkb = wkb2;
439453

440-
*((int*)targetWkb) = numTargetPoints;
454+
memcpy( targetWkb, &numTargetPoints, 4 );
441455
result = numPoints!=numTargetPoints;
442456
}
443457
else
444-
if (flatType==QGis::WKBPolygon)
458+
if ( flatType == QGis::WKBPolygon )
445459
{
446-
int numRings = *((int*)sourceWkb);
460+
int numRings;
461+
memcpy( &numRings, sourceWkb, 4 );
447462
sourceWkb += 4;
448463

449-
*((int*)targetWkb) = numRings;
464+
memcpy( targetWkb, &numRings, 4 );
450465
targetWkb += 4;
451466
targetWkbSize += 4;
452467

453-
for (int i = 0; i < numRings; ++i)
468+
for ( int i = 0; i < numRings; ++i )
454469
{
455-
int numPoints_i = *((int*)sourceWkb);
470+
int numPoints_i;
471+
memcpy( &numPoints_i, sourceWkb, 4 );
456472
QgsRectangle envelope_i = numRings==1 ? envelope : calculateBoundingBox( wkbType, sourceWkb+4, numPoints_i );
457473

458474
size_t sourceWkbSize_i = 4 + numPoints_i * (hasZValue ? 3 : 2) * sizeof(double);
@@ -466,39 +482,43 @@ inline static bool simplifyWkbGeometry( QGis::WkbType wkbType, unsigned char* so
466482
}
467483
}
468484
else
469-
if (flatType==QGis::WKBMultiLineString || flatType==QGis::WKBMultiPolygon)
485+
if ( flatType == QGis::WKBMultiLineString || flatType == QGis::WKBMultiPolygon )
470486
{
471-
int numGeoms = *((int*)sourceWkb);
487+
int numGeoms;
488+
memcpy( &numGeoms, sourceWkb, 4 );
472489
sourceWkb += 4;
473490
wkb1 += 4;
474491

475-
*((int*)targetWkb) = numGeoms;
492+
memcpy( targetWkb, &numGeoms, 4 );
476493
targetWkb += 4;
477494
targetWkbSize += 4;
478495

479-
for (int i = 0; i < numGeoms; ++i)
496+
for ( int i = 0; i < numGeoms; ++i )
480497
{
481498
size_t sourceWkbSize_i = 0;
482499
size_t targetWkbSize_i = 0;
483500

484501
// ... calculate the wkb-size of the current child complex geometry
485-
if (flatType==QGis::WKBMultiLineString)
502+
if ( flatType == QGis::WKBMultiLineString )
486503
{
487-
int numPoints_i = *((int*)(wkb1+5));
504+
int numPoints_i;
505+
memcpy( &numPoints_i, wkb1+5, 4 );
488506
int wkbSize_i = 4 + numPoints_i * (hasZValue ? 3 : 2) * sizeof(double);
489507

490508
sourceWkbSize_i += 5 + wkbSize_i;
491509
wkb1 += 5 + wkbSize_i;
492510
}
493511
else
494512
{
495-
int numPrings_i = *((int*)(wkb1+5));
513+
int numPrings_i;
514+
memcpy( &numPrings_i, wkb1+5, 4 );
496515
sourceWkbSize_i = 9;
497516
wkb1 += 9;
498517

499518
for (int j = 0; j < numPrings_i; ++j)
500519
{
501-
int numPoints_i = *((int*)(wkb1));
520+
int numPoints_i;
521+
memcpy( &numPoints_i, wkb1, 4);
502522
int wkbSize_i = 4 + numPoints_i * (hasZValue ? 3 : 2) * sizeof(double);
503523

504524
sourceWkbSize_i += wkbSize_i;
@@ -549,7 +569,7 @@ bool QgsFeatureRequest::simplifyGeometry( QgsGeometry* geometry, const QgsCoordi
549569

550570
// Check whether the geometry can be simplified using the map2pixel context
551571
QGis::GeometryType geometryType = geometry->type();
552-
if (!(geometryType==QGis::Line || geometryType==QGis::Polygon)) return false;
572+
if ( !(geometryType==QGis::Line || geometryType==QGis::Polygon) ) return false;
553573

554574
QgsRectangle envelope = geometry->boundingBox();
555575
QGis::WkbType wkbType = geometry->wkbType();
@@ -573,7 +593,7 @@ bool QgsFeatureRequest::simplifyGeometry( QgsGeometry* geometry, const QgsCoordi
573593
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 )
574594
{
575595
pointSimplifiedCount = pointCount;
576-
if (geometryType==QGis::Point || geometryType==QGis::UnknownGeometry) return false;
596+
if ( geometryType == QGis::Point || geometryType == QGis::UnknownGeometry ) return false;
577597
pointSimplifiedCount = 0;
578598

579599
double map2pixelTol = mapToPixelTol * calculateViewPixelTolerance( envelope, coordinateTransform, mtp );
@@ -587,20 +607,20 @@ bool QgsFeatureRequest::simplifyGeometry( QGis::GeometryType geometryType, const
587607

588608
for ( int i = 0, numPoints = geometryType==QGis::Polygon ? pointCount-1 : pointCount; i < numPoints; ++i )
589609
{
590-
x = *((double*)xsourcePtr); xsourcePtr += xStride;
591-
y = *((double*)ysourcePtr); ysourcePtr += yStride;
610+
memcpy( &x, xsourcePtr, sizeof( double ) ); xsourcePtr += xStride;
611+
memcpy( &y, ysourcePtr, sizeof( double ) ); ysourcePtr += yStride;
592612

593613
if ( i==0 || calculateLengthSquared2D(x,y,lastX,lastY)>map2pixelTol )
594614
{
595-
*((double*)xtargetPtr) = lastX = x; xtargetPtr += xStride;
596-
*((double*)ytargetPtr) = lastY = y; ytargetPtr += yStride;
615+
memcpy( xtargetPtr, &x, sizeof( double ) ); lastX = x; xtargetPtr += xStride;
616+
memcpy( ytargetPtr, &y, sizeof( double ) ); lastY = y; ytargetPtr += yStride;
597617
pointSimplifiedCount++;
598618
}
599619
}
600-
if ( geometryType==QGis::Polygon )
620+
if ( geometryType == QGis::Polygon )
601621
{
602-
*((double*)xtargetPtr) = *xptr;
603-
*((double*)ytargetPtr) = *yptr;
622+
memcpy( xtargetPtr, xptr, sizeof( double ) );
623+
memcpy( ytargetPtr, yptr, sizeof( double ) );
604624
pointSimplifiedCount++;
605625
}
606626
return pointSimplifiedCount!=pointCount;

‎src/core/symbology-ng/qgsrendererv2.cpp

Lines changed: 4 additions & 4 deletions
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

Lines changed: 8 additions & 8 deletions
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)
Please sign in to comment.