From 1278b939f299cd2465f72a77c60ad58aaa83fe8a Mon Sep 17 00:00:00 2001 From: gyroninja Date: Sat, 24 Jun 2017 04:35:50 -0700 Subject: [PATCH] Removed all occurrences of new and replaced them with their smart pointer countertypes --- src/graphics/opengl/Text.cpp | 10 +++++----- src/graphics/opengl/Text.h | 3 ++- src/graphics/opengl/Window.cpp | 6 +++--- src/graphics/text/TextRasterizer.cpp | 4 ++-- src/html/HTMLParser.cpp | 24 ++++++++++++------------ src/html/Node.h | 5 +++-- src/main.cpp | 6 +++--- 7 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/graphics/opengl/Text.cpp b/src/graphics/opengl/Text.cpp index 64e2121..2256b3f 100644 --- a/src/graphics/opengl/Text.cpp +++ b/src/graphics/opengl/Text.cpp @@ -6,7 +6,7 @@ Text::Text(const std::string &text, const int x, const int y, const int windowWi this->x = x; this->y = y; - const std::unique_ptr textRasterizer = std::unique_ptr(new TextRasterizer("DejaVuSerif.ttf", 50, 72)); + const std::unique_ptr textRasterizer = std::make_unique("DejaVuSerif.ttf", 50, 72); unsigned int glyphCount; std::unique_ptr glyphs = textRasterizer->rasterize(text, x, y, glyphCount); if (glyphs == nullptr) { @@ -22,7 +22,7 @@ Text::Text(const std::string &text, const int x, const int y, const int windowWi pointToViewport(vx0, vy0, windowWidth, windowHeight); pointToViewport(vx1, vy1, windowWidth, windowHeight); - float *vertices = new float[20]; + std::unique_ptr vertices = std::make_unique(20); vertices[(0 * 5) + 0] = vx0; vertices[(0 * 5) + 1] = vy0; vertices[(0 * 5) + 2] = 0.0f; @@ -43,7 +43,7 @@ Text::Text(const std::string &text, const int x, const int y, const int windowWi vertices[(3 * 5) + 2] = 0.0f; vertices[(3 * 5) + 3] = glyph.s1; vertices[(3 * 5) + 4] = glyph.t0; - glyphVertices.push_back(vertices); + glyphVertices.push_back(std::move(vertices)); vertexArrayObjects.push_back(0); vertexBufferObjects.push_back(0); @@ -55,7 +55,7 @@ Text::Text(const std::string &text, const int x, const int y, const int windowWi glBindVertexArray(vertexArrayObjects.back()); glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjects.back()); - glBufferData(GL_ARRAY_BUFFER, ((3 + 2) * 4) * sizeof(float), vertices, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, ((3 + 2) * 4) * sizeof(float), glyphVertices.back().get(), GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObjects.back()); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); @@ -88,7 +88,7 @@ void Text::render() { if (verticesDirty) { for (int i = 0; i < vertexArrayObjects.size(); i++) { glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjects[i]); - glBufferData(GL_ARRAY_BUFFER, ((3 + 2) * 4) * sizeof(float), glyphVertices[i], GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, ((3 + 2) * 4) * sizeof(float), glyphVertices[i].get(), GL_STATIC_DRAW); } verticesDirty = false; } diff --git a/src/graphics/opengl/Text.h b/src/graphics/opengl/Text.h index be38316..88703de 100644 --- a/src/graphics/opengl/Text.h +++ b/src/graphics/opengl/Text.h @@ -3,6 +3,7 @@ #include #include +#include #include class Text { @@ -17,7 +18,7 @@ private: 0, 2, 3 }; unsigned char data[1024][1024][4]; - std::vector glyphVertices; + std::vector> glyphVertices; std::vector vertexArrayObjects; std::vector vertexBufferObjects; std::vector elementBufferObjects; diff --git a/src/graphics/opengl/Window.cpp b/src/graphics/opengl/Window.cpp index e584f22..fe7edfa 100644 --- a/src/graphics/opengl/Window.cpp +++ b/src/graphics/opengl/Window.cpp @@ -19,9 +19,9 @@ bool Window::init() { } initGL(); - boxes.push_back(std::unique_ptr(new Box(0.0f, 1.0f, 1.0f, -64, windowWidth, windowHeight))); - boxes.push_back(std::unique_ptr(new Box(-1024, 0.0f, 1024, 1024, windowWidth, windowHeight))); - texts.push_back(std::unique_ptr(new Text("Hello, World!", 32, 32, windowWidth, windowHeight))); + boxes.push_back(std::make_unique(0.0f, 1.0f, 1.0f, -64, windowWidth, windowHeight)); + boxes.push_back(std::make_unique(-1024, 0.0f, 1024, 1024, windowWidth, windowHeight)); + texts.push_back(std::make_unique("Hello, World!", 32, 32, windowWidth, windowHeight)); return true; } diff --git a/src/graphics/text/TextRasterizer.cpp b/src/graphics/text/TextRasterizer.cpp index ec9db8f..3cdbe30 100644 --- a/src/graphics/text/TextRasterizer.cpp +++ b/src/graphics/text/TextRasterizer.cpp @@ -48,7 +48,7 @@ std::unique_ptr TextRasterizer::rasterize(const std::string &text const hb_glyph_info_t *glyphInfo = hb_buffer_get_glyph_infos(buffer, &glyphCount); const hb_glyph_position_t *glyphPos = hb_buffer_get_glyph_positions(buffer, &glyphCount); - std::unique_ptr glyphs = std::unique_ptr(new Glyph[glyphCount]); + std::unique_ptr glyphs = std::make_unique(glyphCount); int cx = x; int cy = y; @@ -70,7 +70,7 @@ std::unique_ptr TextRasterizer::rasterize(const std::string &text glyphs[i].textureWidth = pow(2, ceil(log(ftBitmap.width) / log(2))); glyphs[i].textureHeight = pow(2, ceil(log(ftBitmap.rows) / log(2))); - glyphs[i].textureData = std::unique_ptr(new unsigned char[glyphs[i].textureWidth * glyphs[i].textureHeight]); + glyphs[i].textureData = std::make_unique(glyphs[i].textureWidth * glyphs[i].textureHeight); for (int iy = 0; iy < ftBitmap.rows; iy++) { memcpy(glyphs[i].textureData.get() + iy * glyphs[i].textureWidth, ftBitmap.buffer + iy * ftBitmap.width, ftBitmap.width); } diff --git a/src/html/HTMLParser.cpp b/src/html/HTMLParser.cpp index c5238f0..7c392ee 100644 --- a/src/html/HTMLParser.cpp +++ b/src/html/HTMLParser.cpp @@ -1,9 +1,9 @@ #include "HTMLParser.h" #include "TextNode.h" #include -#include +#include -void printNode(Node *node, int indent) { +void printNode(std::shared_ptr node, int indent) { for (int i = 0; i < indent; i++) { std::cout << '\t'; } @@ -11,8 +11,8 @@ void printNode(Node *node, int indent) { std::cout << "ROOT" << std::endl; } if (node->nodeType == NodeType::TAG) { - std::cout << "TAG: " << dynamic_cast(node)->tag << std::endl; - for (const std::pair property : dynamic_cast(node)->properties) { + std::cout << "TAG: " << dynamic_cast(node.get())->tag << std::endl; + for (const std::pair property : dynamic_cast(node.get())->properties) { for (int i = 0; i < indent; i++) { std::cout << '\t'; } @@ -20,17 +20,17 @@ void printNode(Node *node, int indent) { } } else if (node->nodeType == NodeType::TEXT) { - std::cout << "TEXT: " << dynamic_cast(node)->text << std::endl; + std::cout << "TEXT: " << dynamic_cast(node.get())->text << std::endl; } - for (Node *child : node->children) { + for (std::shared_ptr child : node->children) { printNode(child, indent + 1); } } void HTMLParser::parse(const std::string &html) const { Node rootNode = Node(NodeType::ROOT); - Node *currentNode = &rootNode; + std::shared_ptr currentNode = std::make_shared(rootNode); std::vector starts; int cursor; int start = 0; @@ -49,7 +49,7 @@ void HTMLParser::parse(const std::string &html) const { state = 1; } else { - TagNode *tagNode = new TagNode(); + std::shared_ptr tagNode = std::make_shared(); currentNode->children.push_back(tagNode); tagNode->parent = currentNode; currentNode = tagNode; @@ -58,7 +58,7 @@ void HTMLParser::parse(const std::string &html) const { } } else { - TextNode *textNode = new TextNode(); + std::shared_ptr textNode = std::make_shared(); currentNode->children.push_back(textNode); textNode->parent = currentNode; currentNode = textNode; @@ -76,7 +76,7 @@ void HTMLParser::parse(const std::string &html) const { const int start = starts.back(); starts.pop_back(); std::string element = html.substr(start, cursor - start + 1); - parseTag(element, dynamic_cast(currentNode)); + parseTag(element, dynamic_cast(currentNode.get())); state = 0; } } @@ -84,13 +84,13 @@ void HTMLParser::parse(const std::string &html) const { if (html[cursor + 1] == '<' && html[cursor + 2] == '/') { const int start = starts.back(); starts.pop_back(); - dynamic_cast(currentNode)->text = html.substr(start, cursor - start + 1); + dynamic_cast(currentNode.get())->text = html.substr(start, cursor - start + 1); state = 0; } } } - printNode(&rootNode, 0); + printNode(std::make_shared(rootNode), 0); } void HTMLParser::parseTag(const std::string &element, TagNode* tagNode) const { diff --git a/src/html/Node.h b/src/html/Node.h index 61f9f53..c2b5ce3 100644 --- a/src/html/Node.h +++ b/src/html/Node.h @@ -1,6 +1,7 @@ #ifndef NODE_H #define NODE_H +#include #include enum class NodeType { @@ -14,8 +15,8 @@ public: Node(NodeType nodeType); virtual ~Node(); NodeType nodeType; - Node *parent; - std::vector children; + std::shared_ptr parent; + std::vector> children; }; #endif diff --git a/src/main.cpp b/src/main.cpp index b5d4dc7..4089c6b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,14 +35,14 @@ const std::string getHostFromURL(const std::string &url) { void handleRequest(const HTTPResponse &response) { if (response.statusCode == 200) { - const std::unique_ptr parser = std::unique_ptr(new HTMLParser()); + const std::unique_ptr parser = std::make_unique(); parser->parse(response.body); } else if (response.statusCode == 301) { const std::string location = response.properties.at("Location"); std::cout << "Redirect To: " << location << std::endl; std::cout << getHostFromURL(location) << "!" << getDocumentFromURL(location) << std::endl; - const std::unique_ptr request = std::unique_ptr(new HTTPRequest(getHostFromURL(location), getDocumentFromURL(location))); + const std::unique_ptr request = std::make_unique(getHostFromURL(location), getDocumentFromURL(location)); request->sendRequest(handleRequest); return; } @@ -56,7 +56,7 @@ int main(int argc, char *argv[]) { std::cout << "./netrunner " << std::endl; return 1; } - const std::unique_ptr request = std::unique_ptr(new HTTPRequest(getHostFromURL(argv[1]), getDocumentFromURL(argv[1]))); + const std::unique_ptr request = std::make_unique(getHostFromURL(argv[1]), getDocumentFromURL(argv[1])); request->sendRequest(handleRequest); const std::unique_ptr window = std::make_unique(); window->init();