Small fixes to the core C++ FlatBuffers implementation.

- Ensured weak linkage with the version string is not used on Windows,
  especially cygwin (which throws a linker error).
- Avoided a VS debug error for taking the address of the first element
  of an empty vector.
- Made copy/assignment constructors for downward_vector and
  FlatBufferBuilder private, to avoid people unintentionally making
  expensive copies.
- Using the more correct _WIN32 instead of WIN32

Change-Id: I801b5c8b159e3721af6d1ef0978a3247ba168bab
Tested: on Windows (VS + Cygwin) and Linux.
parent 74d5f370
......@@ -54,12 +54,6 @@
#endif
#endif // !defined(FLATBUFFERS_LITTLEENDIAN)
#if !defined(_MSC_VER)
#define FLATBUFFERS_WEAK __attribute__((weak))
#else
#define FLATBUFFERS_WEAK __declspec(selectany)
#endif // WIN32
#define FLATBUFFERS_VERSION_MAJOR 1
#define FLATBUFFERS_VERSION_MINOR 0
#define FLATBUFFERS_VERSION_REVISION 0
......@@ -345,6 +339,10 @@ class vector_downward {
void pop(size_t bytes_to_remove) { cur_ += bytes_to_remove; }
private:
// You shouldn't really be copying instances of this class.
vector_downward(const vector_downward &);
vector_downward &operator=(const vector_downward &);
size_t reserved_;
uint8_t *buf_;
uint8_t *cur_; // Points at location between empty (below) and used (above).
......@@ -594,7 +592,7 @@ class FlatBufferBuilder {
}
template<typename T> Offset<Vector<T>> CreateVector(const std::vector<T> &v){
return CreateVector(&v[0], v.size());
return CreateVector(v.begin(), v.size());
}
template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
......@@ -607,7 +605,7 @@ class FlatBufferBuilder {
template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
const std::vector<T> &v) {
return CreateVectorOfStructs(&v[0], v.size());
return CreateVectorOfStructs(v.begin(), v.size());
}
// Finish serializing a buffer by writing the root offset.
......@@ -618,6 +616,10 @@ class FlatBufferBuilder {
}
private:
// You shouldn't really be copying instances of this class.
FlatBufferBuilder(const FlatBufferBuilder &);
FlatBufferBuilder &operator=(const FlatBufferBuilder &);
struct FieldLoc {
uoffset_t off;
voffset_t id;
......@@ -877,13 +879,18 @@ inline int LookupEnum(const char **names, const char *name) {
// to measure popularity. You are free to remove it (of course) but we would
// appreciate if you left it in.
extern volatile FLATBUFFERS_WEAK const char *flatbuffer_version_string;
volatile FLATBUFFERS_WEAK const char *flatbuffer_version_string =
// Weak linkage is culled by VS & doesn't work on cygwin.
#if !defined(_WIN32) && !defined(__CYGWIN__)
extern volatile __attribute__((weak)) const char *flatbuffer_version_string;
volatile __attribute__((weak)) const char *flatbuffer_version_string =
"FlatBuffers "
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
#endif // !defined(_WIN32) && !defined(__CYGWIN__)
} // namespace flatbuffers
#endif // FLATBUFFERS_H_
......@@ -106,7 +106,7 @@ std::string StripExtension(const std::string &filename) {
std::string StripPath(const std::string &filename) {
size_t i = filename.find_last_of(
#ifdef WIN32
#ifdef _WIN32
"\\:"
#else
"/"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment