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:
parent
1b7ed5ed78
commit
520162f36a
8 changed files with 26 additions and 13 deletions
src
13
src/util.h
13
src/util.h
|
@ -25,6 +25,7 @@
|
|||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
static inline int
|
||||
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();
|
||||
}
|
||||
|
||||
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 elementsN(obj) const size_t obj##N = ARRAY_SIZE(obj)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue