Use safe way to get at a vector's data pointer

&std::vector<C>[0] is not guaranteed to not throw if the
vector is empty. Better safe than sorry.
This commit is contained in:
Jonas Kulla 2014-10-09 19:02:29 +02:00
parent 1b7ed5ed78
commit 520162f36a
8 changed files with 26 additions and 13 deletions

View File

@ -101,7 +101,7 @@ int RWMemGetData(SDL_RWops *ops, void *buffer)
ByteVec *v = getRWPrivate(ops); ByteVec *v = getRWPrivate(ops);
if (buffer) if (buffer)
memcpy(buffer, &(*v)[0], v->size()); memcpy(buffer, dataPtr(*v), v->size());
return v->size(); return v->size();
} }

View File

@ -67,7 +67,7 @@ struct GlobalIBO
} }
IBO::bind(ibo); IBO::bind(ibo);
IBO::uploadData(buffer.size() * sizeof(index_t), &buffer[0]); IBO::uploadData(buffer.size() * sizeof(index_t), dataPtr(buffer));
IBO::unbind(); IBO::unbind();
} }
}; };

View File

@ -86,7 +86,7 @@ struct QuadArray
{ {
/* New data exceeds already allocated size. /* New data exceeds already allocated size.
* Reallocate VBO. */ * Reallocate VBO. */
VBO::uploadData(size, &vertices[0], GL_DYNAMIC_DRAW); VBO::uploadData(size, dataPtr(vertices), GL_DYNAMIC_DRAW);
vboSize = size; vboSize = size;
shState->ensureQuadIBO(quadCount); shState->ensureQuadIBO(quadCount);
@ -94,7 +94,7 @@ struct QuadArray
else else
{ {
/* New data fits in allocated size */ /* New data fits in allocated size */
VBO::uploadSubData(0, size, &vertices[0]); VBO::uploadSubData(0, size, dataPtr(vertices));
} }
VBO::unbind(); VBO::unbind();

View File

@ -265,7 +265,7 @@ createShadowSet()
/* Fill rects with half opacity black */ /* Fill rects with half opacity black */
uint32_t color = (0x80808080 & am); uint32_t color = (0x80808080 & am);
SDL_FillRects(surf, &rects[0], rects.size(), color); SDL_FillRects(surf, dataPtr(rects), rects.size(), color);
return surf; return surf;
} }

View File

@ -766,7 +766,7 @@ struct TilemapPrivate
VBO::bind(tiles.vbo); VBO::bind(tiles.vbo);
VBO::allocEmpty(quadDataSize(quadCount)); VBO::allocEmpty(quadDataSize(quadCount));
VBO::uploadSubData(0, quadDataSize(groundQuadCount), &groundVert[0]); VBO::uploadSubData(0, quadDataSize(groundQuadCount), dataPtr(groundVert));
for (size_t i = 0; i < zlayersMax; ++i) for (size_t i = 0; i < zlayersMax; ++i)
{ {
@ -774,7 +774,7 @@ struct TilemapPrivate
continue; continue;
VBO::uploadSubData(quadDataSize(zlayerBases[i]), VBO::uploadSubData(quadDataSize(zlayerBases[i]),
quadDataSize(zlayerSize(i)), &zlayerVert[i][0]); quadDataSize(zlayerSize(i)), dataPtr(zlayerVert[i]));
} }
VBO::unbind(); VBO::unbind();
@ -855,7 +855,7 @@ struct TilemapPrivate
return; return;
VBO::bind(flash.vbo); VBO::bind(flash.vbo);
VBO::uploadData(sizeof(CVertex) * vertices.size(), &vertices[0]); VBO::uploadData(sizeof(CVertex) * vertices.size(), dataPtr(vertices));
VBO::unbind(); VBO::unbind();
/* Ensure global IBO size */ /* Ensure global IBO size */

View File

@ -236,8 +236,8 @@ struct TilemapVXPrivate : public ViewportElement, TileAtlasVX::Reader
allocQuads = totalQuads; allocQuads = totalQuads;
} }
VBO::uploadSubData(0, quadBytes(groundQuads), &groundVert[0]); VBO::uploadSubData(0, quadBytes(groundQuads), dataPtr(groundVert));
VBO::uploadSubData(quadBytes(groundQuads), quadBytes(aboveQuads), &aboveVert[0]); VBO::uploadSubData(quadBytes(groundQuads), quadBytes(aboveQuads), dataPtr(aboveVert));
VBO::unbind(); VBO::unbind();

View File

@ -25,6 +25,7 @@
#include <stdio.h> #include <stdio.h>
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <vector>
static inline int static inline int
wrapRange(int value, int min, int max) wrapRange(int value, int min, int max)
@ -101,6 +102,18 @@ inline bool contains(const C &c, const V &v)
return std::find(c.begin(), c.end(), v) != c.end(); return std::find(c.begin(), c.end(), v) != c.end();
} }
template<typename C>
inline const C *dataPtr(const std::vector<C> &v)
{
return v.empty() ? (C*)0 : &v[0];
}
template<typename C>
inline C *dataPtr(std::vector<C> &v)
{
return v.empty() ? (C*)0 : &v[0];
}
#define ARRAY_SIZE(obj) (sizeof(obj) / sizeof((obj)[0])) #define ARRAY_SIZE(obj) (sizeof(obj) / sizeof((obj)[0]))
#define elementsN(obj) const size_t obj##N = ARRAY_SIZE(obj) #define elementsN(obj) const size_t obj##N = ARRAY_SIZE(obj)

View File

@ -374,7 +374,7 @@ struct WindowVXPrivate
base.vert.resize(count); base.vert.resize(count);
Vertex *vert = &base.vert.vertices[0]; Vertex *vert = dataPtr(base.vert.vertices);
size_t i = 0; size_t i = 0;
/* Stretched background */ /* Stretched background */
@ -518,7 +518,7 @@ struct WindowVXPrivate
}; };
size_t i = 0; size_t i = 0;
Vertex *vert = &ctrlVert.vertices[0]; Vertex *vert = dataPtr(ctrlVert.vertices);
if (!nullOrDisposed(contents) && arrowsVisible) if (!nullOrDisposed(contents) && arrowsVisible)
{ {
@ -600,7 +600,7 @@ struct WindowVXPrivate
quads += 1; quads += 1;
cursorVert.resize(quads); cursorVert.resize(quads);
Vertex *vert = &cursorVert.vertices[0]; Vertex *vert = dataPtr(cursorVert.vertices);
size_t i = 0; size_t i = 0;
i += Quad::setTexPosRect(&vert[i*4], src.corners.tl, cornerPos.tl); i += Quad::setTexPosRect(&vert[i*4], src.corners.tl, cornerPos.tl);