Skip to content

Commit

Permalink
Enable build against C++03 stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Eilemann authored and Stefan Eilemann committed Aug 27, 2014
1 parent 5bbc72f commit 039a69e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ project(FlatBuffers)

# NOTE: Code coverage only works on Linux & OSX.
option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF)
option(FLATBUFFERS_USE_BOOST
"Use boost classes to enable C++03 stdlib compatibility" OFF)

if(FLATBUFFERS_USE_BOOST)
find_package(Boost 1.41.0 REQUIRED)
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
endif()

configure_file(include/flatbuffers/options.in.h
"${PROJECT_BINARY_DIR}/include/flatbuffers/options.h")
include_directories("${PROJECT_BINARY_DIR}/include")

set(FlatBuffers_Compiler_SRCS
"${PROJECT_BINARY_DIR}/include/flatbuffers/options.h"
include/flatbuffers/flatbuffers.h
include/flatbuffers/idl.h
include/flatbuffers/util.h
Expand Down Expand Up @@ -69,6 +81,8 @@ add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS})
add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS})

install(DIRECTORY include/flatbuffers DESTINATION include)
install(FILES "${PROJECT_BINARY_DIR}/include/flatbuffers/options.h"
DESTINATION include/flatbuffers)
install(TARGETS flatc DESTINATION bin)

add_test(NAME flattest
Expand Down
26 changes: 20 additions & 6 deletions include/flatbuffers/flatbuffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,29 @@
#ifndef FLATBUFFERS_H_
#define FLATBUFFERS_H_

#include <flatbuffers/options.h>

#include <assert.h>

#include <cstdint>
#ifdef __APPLE__
# include <stdint.h>
#else
# include <cstdint>
#endif
#include <cstring>
#include <string>
#include <type_traits>
#include <vector>
#include <algorithm>

#ifdef FLATBUFFERS_USE_BOOST
# include <boost/type_traits/conditional.hpp>
# include <boost/move/move.hpp>
namespace flatbufferstd = boost;
#else
# include <type_traits>
namespace flatbufferstd = std;
#endif

#if __cplusplus <= 199711L && \
(!defined(_MSC_VER) || _MSC_VER < 1600) && \
(!defined(__GNUC__) || \
Expand Down Expand Up @@ -186,20 +200,20 @@ template<typename T> struct IndirectHelper<const T *> {
template<typename T, bool bConst>
struct VectorIterator : public
std::iterator < std::input_iterator_tag,
typename std::conditional < bConst,
typename flatbufferstd::conditional < bConst,
const typename IndirectHelper<T>::return_type,
typename IndirectHelper<T>::return_type > ::type, uoffset_t > {

typedef std::iterator<std::input_iterator_tag,
typename std::conditional<bConst,
typename flatbufferstd::conditional<bConst,
const typename IndirectHelper<T>::return_type,
typename IndirectHelper<T>::return_type>::type, uoffset_t> super_type;

public:
VectorIterator(const uint8_t *data, uoffset_t i) :
data_(data + IndirectHelper<T>::element_stride * i) {};
VectorIterator(const VectorIterator &other) : data_(other.data_) {}
VectorIterator(VectorIterator &&other) : data_(std::move(other.data_)) {}
VectorIterator(VectorIterator &&other) : data_(flatbufferstd::move(other.data_)) {}

VectorIterator &operator=(const VectorIterator &other) {
data_ = other.data_;
Expand Down Expand Up @@ -410,7 +424,7 @@ class FlatBufferBuilder {

template<typename T> void AssertScalarT() {
// The code assumes power of 2 sizes and endian-swap-ability.
static_assert(std::is_scalar<T>::value
static_assert(flatbufferstd::is_scalar<T>::value
// The Offset<T> type is essentially a scalar but fails is_scalar.
|| sizeof(T) == sizeof(Offset<void>),
"T must be a scalar type");
Expand Down

0 comments on commit 039a69e

Please sign in to comment.