From d6654ccaed8b522b6de3156c1a271f6c60a67572 Mon Sep 17 00:00:00 2001 From: artemp Date: Thu, 6 Feb 2014 10:25:36 +0000 Subject: [PATCH] c++ * fix typedef's * fix virtual dtor's * make ctor's with one argument explicit * util::noncopyable utility class ( class my_object : private noncopyable {} ) * update kTileURL --- include/llmr/geometry/binpack.hpp | 13 +++---------- include/llmr/geometry/fill_buffer.hpp | 5 +++-- include/llmr/geometry/geometry.hpp | 14 ++++++++------ include/llmr/geometry/line_buffer.hpp | 6 ++++-- include/llmr/geometry/vertex_buffer.hpp | 5 +++-- include/llmr/map/map.hpp | 12 +++--------- include/llmr/map/settings.hpp | 13 ++++--------- include/llmr/map/tile.hpp | 10 +++------- include/llmr/map/transform.hpp | 9 ++------- include/llmr/platform/platform.hpp | 2 +- include/llmr/renderer/bucket.hpp | 12 +++--------- include/llmr/renderer/painter.hpp | 10 ++-------- include/llmr/renderer/shader.hpp | 12 +++--------- include/llmr/util/animation.hpp | 11 +++-------- include/llmr/util/noncopyable.hpp | 23 +++++++++++++++++++++++ src/geometry/fill_buffer.cpp | 12 +++++++----- src/geometry/line_buffer.cpp | 4 ++-- src/geometry/vertex_buffer.cpp | 2 +- 18 files changed, 78 insertions(+), 97 deletions(-) create mode 100644 include/llmr/util/noncopyable.hpp diff --git a/include/llmr/geometry/binpack.hpp b/include/llmr/geometry/binpack.hpp index 03b34c63d..d7405d7d3 100644 --- a/include/llmr/geometry/binpack.hpp +++ b/include/llmr/geometry/binpack.hpp @@ -1,6 +1,7 @@ #ifndef LLMR_GEOMETRY_BINPACK #define LLMR_GEOMETRY_BINPACK +#include #include #include @@ -14,23 +15,15 @@ struct Rect { }; template -class BinPack { +class BinPack : private noncopyable { public: BinPack(T width, T height) : free(1, { 0, 0, width, height }) {} - -private: - // Make noncopyable - BinPack(const BinPack&) = delete; - BinPack(const BinPack&&) = delete; - BinPack& operator=(const BinPack&) = delete; - BinPack& operator=(const BinPack && ) = delete; - public: Rect allocate(T width, T height) { // Find the smallest free rect angle auto smallest = free.end(); - for (auto it = free.begin(); it != free.end(); it++) { + for (auto it = free.begin(); it != free.end(); ++it) { const Rect& ref = *it; const Rect& rect = *smallest; if (width <= ref.w && height <= ref.h) { diff --git a/include/llmr/geometry/fill_buffer.hpp b/include/llmr/geometry/fill_buffer.hpp index 626d66179..b141825fc 100644 --- a/include/llmr/geometry/fill_buffer.hpp +++ b/include/llmr/geometry/fill_buffer.hpp @@ -9,6 +9,7 @@ namespace llmr { class FillBuffer { public: typedef int16_t vertex_type; + typedef uint16_t element_type; const uint8_t components = 2; FillBuffer(); @@ -19,13 +20,13 @@ public: void addDegenerate(); void addCoordinate(vertex_type x, vertex_type y); - void addElements(uint16_t a, uint16_t b, uint16_t c); + void addElements(element_type a, element_type b, element_type c); void bind(); private: std::vector vertices; - std::vector elements; + std::vector elements; uint32_t vertex_buffer; uint32_t element_buffer; bool dirty; diff --git a/include/llmr/geometry/geometry.hpp b/include/llmr/geometry/geometry.hpp index 680832429..f8286402d 100644 --- a/include/llmr/geometry/geometry.hpp +++ b/include/llmr/geometry/geometry.hpp @@ -2,15 +2,17 @@ #define LLMR_GEOMETRY_GEOMETRY #include +#include #include namespace llmr { -class Geometry { -public: - inline Geometry(pbf& data); +class Geometry : private util::noncopyable { +public: + inline explicit Geometry(pbf& data); + enum command { end = 0, move_to = 1, @@ -37,13 +39,13 @@ Geometry::Geometry(pbf& data) Geometry::command Geometry::next(int32_t &rx, int32_t &ry) { if (data.data < data.end) { - if (!length) { - uint32_t cmd_length = (uint32_t)data.varint(); + if (length == 0) { + uint32_t cmd_length = static_cast(data.varint()); cmd = cmd_length & 0x7; length = cmd_length >> 3; } - length--; + --length; if (cmd == move_to || cmd == line_to) { rx = (x += data.svarint()); diff --git a/include/llmr/geometry/line_buffer.hpp b/include/llmr/geometry/line_buffer.hpp index 4965a919a..412ba860c 100644 --- a/include/llmr/geometry/line_buffer.hpp +++ b/include/llmr/geometry/line_buffer.hpp @@ -9,6 +9,8 @@ namespace llmr { class LineBuffer { public: + typedef int16_t vertex_type; + ~LineBuffer(); /* @@ -30,7 +32,7 @@ public: * @param {number} tx texture normal * @param {number} ty texture normal */ - void add(int16_t x, int16_t y, float ex, float ey, int8_t tx, int8_t ty, int32_t linesofar = 0); + void add(vertex_type x, vertex_type y, float ex, float ey, int8_t tx, int8_t ty, int32_t linesofar = 0); /* * Add a degenerate triangle to the buffer @@ -56,7 +58,7 @@ public: private: bool dirty = true; - std::vector array; + std::vector array; uint32_t buffer = 0; }; diff --git a/include/llmr/geometry/vertex_buffer.hpp b/include/llmr/geometry/vertex_buffer.hpp index 667e3fc8a..5dd5ec719 100644 --- a/include/llmr/geometry/vertex_buffer.hpp +++ b/include/llmr/geometry/vertex_buffer.hpp @@ -9,7 +9,8 @@ namespace llmr { class VertexBuffer { public: - VertexBuffer(std::initializer_list init); + typedef int16_t vertex_type; + VertexBuffer(std::initializer_list init); ~VertexBuffer(); /* @@ -24,7 +25,7 @@ public: void bind(); private: - const std::vector array; + const std::vector array; uint32_t buffer = 0; }; diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index 46b7dfb3c..189327c52 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -13,18 +14,11 @@ namespace llmr { class Settings; -class Map { +class Map : private util::noncopyable { public: - Map(Settings& settings); + explicit Map(Settings& settings); ~Map(); - - // Make noncopyable - Map(const Map&) = delete; - Map(const Map&&) = delete; - Map &operator=(const Map&) = delete; - Map &operator=(const Map&&) = delete; - /* setup */ void setup(); void loadStyle(const uint8_t *const data, uint32_t bytes); diff --git a/include/llmr/map/settings.hpp b/include/llmr/map/settings.hpp index 8f2e12810..3e5395b80 100644 --- a/include/llmr/map/settings.hpp +++ b/include/llmr/map/settings.hpp @@ -1,19 +1,14 @@ #ifndef LLMR_MAP_SETTINGS #define LLMR_MAP_SETTINGS +#include + namespace llmr { -class Settings { +class Settings : private util::noncopyable { public: virtual void save() = 0; - - // Make noncopyable - Settings() = default; - Settings(const Settings&) = delete; - Settings(const Settings&&) = delete; - Settings &operator=(const Settings&) = delete; - Settings &operator=(const Settings&&) = delete; - + virtual ~Settings() {} public: // position double longitude = 0; diff --git a/include/llmr/map/tile.hpp b/include/llmr/map/tile.hpp index 2569764cc..7896d91eb 100644 --- a/include/llmr/map/tile.hpp +++ b/include/llmr/map/tile.hpp @@ -2,6 +2,7 @@ #define LLMR_MAP_TILE #include +#include #include #include @@ -24,7 +25,8 @@ class FillBuffer; class LineBuffer; class PlainShader; -class Tile : public std::enable_shared_from_this { +class Tile : public std::enable_shared_from_this, + private util::noncopyable { public: struct exception : std::exception {}; struct geometry_too_long_exception : exception {}; @@ -45,12 +47,6 @@ public: Tile(ID id, const Style& style); ~Tile(); - // Make noncopyable - Tile(const Tile&) = delete; - Tile(const Tile&&) = delete; - Tile& operator=(const Tile&) = delete; - Tile& operator=(const Tile && ) = delete; - // Start loading the tile. void request(); diff --git a/include/llmr/map/transform.hpp b/include/llmr/map/transform.hpp index 9b39aa149..521bcd75a 100644 --- a/include/llmr/map/transform.hpp +++ b/include/llmr/map/transform.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -11,16 +12,10 @@ namespace llmr { struct box; -class Transform { + class Transform : private util::noncopyable { public: Transform(); - // Make noncopyable - Transform(const Transform&) = delete; - Transform(const Transform&&) = delete; - Transform &operator=(const Transform&) = delete; - Transform &operator=(const Transform&&) = delete; - // Animations bool needsAnimation() const; void updateAnimations(); diff --git a/include/llmr/platform/platform.hpp b/include/llmr/platform/platform.hpp index b31a112b8..ff6330241 100644 --- a/include/llmr/platform/platform.hpp +++ b/include/llmr/platform/platform.hpp @@ -5,7 +5,7 @@ #include #include -#define kTileURL "http://mapbox:magic@kkaefer.net/gl/tiles/plain/%d-%d-%d.vector.pbf" +#define kTileURL "http://a.gl-api-us-east-1.tilestream.net/v3/mapbox.mapbox-streets-v4/%d/%d/%d.gl.pbf" #define kSpriteURL "http://mapbox:magic@kkaefer.net/gl/debug/img/sprite" namespace llmr { diff --git a/include/llmr/renderer/bucket.hpp b/include/llmr/renderer/bucket.hpp index 7ae0d2d00..9326aaed1 100644 --- a/include/llmr/renderer/bucket.hpp +++ b/include/llmr/renderer/bucket.hpp @@ -3,22 +3,16 @@ #include #include +#include namespace llmr { class Painter; -class Bucket { +class Bucket : private util::noncopyable { public: - Bucket() = default; virtual void render(Painter& painter, const std::string& layer_name, const Tile::ID& id) = 0; - -private: - // Make noncopyable - Bucket(const Bucket&) = delete; - Bucket(const Bucket&&) = delete; - Bucket& operator=(const Bucket&) = delete; - Bucket& operator=(const Bucket && ) = delete; + virtual ~Bucket() {} }; } diff --git a/include/llmr/renderer/painter.hpp b/include/llmr/renderer/painter.hpp index b991c9592..925defd03 100644 --- a/include/llmr/renderer/painter.hpp +++ b/include/llmr/renderer/painter.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -22,18 +23,11 @@ class Tile; class FillBucket; class LineBucket; -class Painter { +class Painter : private util::noncopyable { public: Painter(Transform& transform, Settings& settings, Style& style); - // Make noncopyable - Painter(const Painter&) = delete; - Painter(const Painter&&) = delete; - Painter& operator=(const Painter&) = delete; - Painter& operator=(const Painter && ) = delete; - void setup(); - void clear(); void changeMatrix(const Tile::ID& id); void render(const std::shared_ptr& tile); diff --git a/include/llmr/renderer/shader.hpp b/include/llmr/renderer/shader.hpp index 26674d2d6..77fd7a6f6 100644 --- a/include/llmr/renderer/shader.hpp +++ b/include/llmr/renderer/shader.hpp @@ -2,20 +2,14 @@ #define LLMR_RENDERER_SHADER #include +#include namespace llmr { -class Shader { +class Shader : private util::noncopyable { public: Shader(const char *vertex, const char *fragment); - virtual ~Shader(); - - // Make noncopyable - Shader(const Shader&) = delete; - Shader(const Shader&&) = delete; - Shader &operator=(const Shader&) = delete; - Shader &operator=(const Shader&&) = delete; - + ~Shader(); bool valid; uint32_t program; diff --git a/include/llmr/util/animation.hpp b/include/llmr/util/animation.hpp index c31f1ff9d..4095fcba1 100644 --- a/include/llmr/util/animation.hpp +++ b/include/llmr/util/animation.hpp @@ -1,10 +1,12 @@ #ifndef LLMR_UTIL_ANIMATION #define LLMR_UTIL_ANIMATION +#include + namespace llmr { namespace util { -class animation { +class animation : private noncopyable { public: enum state { running, @@ -12,13 +14,6 @@ public: }; animation(double from, double to, double &value, double duration); - - // Make noncopyable - animation(const animation&) = delete; - animation(const animation&&) = delete; - animation &operator=(const animation&) = delete; - animation &operator=(const animation&&) = delete; - state update() const; private: diff --git a/include/llmr/util/noncopyable.hpp b/include/llmr/util/noncopyable.hpp new file mode 100644 index 000000000..7891e529e --- /dev/null +++ b/include/llmr/util/noncopyable.hpp @@ -0,0 +1,23 @@ +#ifndef LLMR_UTIL_NONCOPYABLE +#define LLMR_UTIL_NONCOPYABLE + +namespace llmr { namespace util { + +namespace non_copyable_ +{ + +class noncopyable +{ +protected: + constexpr noncopyable() = default; + ~noncopyable() = default; + noncopyable( noncopyable const& ) = delete; + noncopyable& operator=(noncopyable const& ) = delete; +}; +} + +typedef non_copyable_::noncopyable noncopyable; + +}} + +#endif //LLMR_UTIL_NONCOPYABLE diff --git a/src/geometry/fill_buffer.cpp b/src/geometry/fill_buffer.cpp index 36d276538..ba6cfcb40 100644 --- a/src/geometry/fill_buffer.cpp +++ b/src/geometry/fill_buffer.cpp @@ -1,6 +1,8 @@ #include #include +#include + using namespace llmr; FillBuffer::FillBuffer() @@ -32,16 +34,16 @@ uint32_t FillBuffer::elements_length() const { } void FillBuffer::addDegenerate() { - vertices.push_back(32767); + vertices.push_back(std::numeric_limits::max()); vertices.push_back(0); } -void FillBuffer::addCoordinate(int16_t x, int16_t y) { +void FillBuffer::addCoordinate(vertex_type x, vertex_type y) { vertices.push_back(x); vertices.push_back(y); } -void FillBuffer::addElements(uint16_t a, uint16_t b, uint16_t c) { +void FillBuffer::addElements(element_type a, element_type b, element_type c) { elements.push_back(a); elements.push_back(b); elements.push_back(c); @@ -60,8 +62,8 @@ void FillBuffer::bind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer); if (dirty) { - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(uint16_t), vertices.data(), GL_STATIC_DRAW); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(uint16_t), elements.data(), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertex_type), vertices.data(), GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(element_type), elements.data(), GL_STATIC_DRAW); dirty = false; } } diff --git a/src/geometry/line_buffer.cpp b/src/geometry/line_buffer.cpp index ff95a5079..4e1200d2f 100644 --- a/src/geometry/line_buffer.cpp +++ b/src/geometry/line_buffer.cpp @@ -15,7 +15,7 @@ uint32_t LineBuffer::length() const { return array.size() / 4; } -void LineBuffer::add(int16_t x, int16_t y, float ex, float ey, int8_t tx, int8_t ty, int32_t linesofar) { +void LineBuffer::add(vertex_type x, vertex_type y, float ex, float ey, int8_t tx, int8_t ty, int32_t linesofar) { array.push_back((x * 2) | tx); array.push_back((y * 2) | ty); array.push_back(linesofar); @@ -40,7 +40,7 @@ void LineBuffer::bind() { glBindBuffer(GL_ARRAY_BUFFER, buffer); if (dirty) { - glBufferData(GL_ARRAY_BUFFER, array.size() * sizeof(int16_t), array.data(), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, array.size() * sizeof(vertex_type), array.data(), GL_STATIC_DRAW); dirty = false; } } diff --git a/src/geometry/vertex_buffer.cpp b/src/geometry/vertex_buffer.cpp index 591b0c13d..2d209f6be 100644 --- a/src/geometry/vertex_buffer.cpp +++ b/src/geometry/vertex_buffer.cpp @@ -21,7 +21,7 @@ void VertexBuffer::bind() { if (buffer == 0) { glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); - glBufferData(GL_ARRAY_BUFFER, array.size() * sizeof(int16_t), array.data(), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, array.size() * sizeof(vertex_type), array.data(), GL_STATIC_DRAW); } else { glBindBuffer(GL_ARRAY_BUFFER, buffer); }