Browse Source

Removed all occurrences of new and replaced them with their smart pointer countertypes

pull/1/head
gyroninja 5 years ago
parent
commit
1278b939f2
  1. 10
      src/graphics/opengl/Text.cpp
  2. 3
      src/graphics/opengl/Text.h
  3. 6
      src/graphics/opengl/Window.cpp
  4. 4
      src/graphics/text/TextRasterizer.cpp
  5. 24
      src/html/HTMLParser.cpp
  6. 5
      src/html/Node.h
  7. 6
      src/main.cpp

10
src/graphics/opengl/Text.cpp

@ -6,7 +6,7 @@ Text::Text(const std::string &text, const int x, const int y, const int windowWi @@ -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> textRasterizer = std::unique_ptr<TextRasterizer>(new TextRasterizer("DejaVuSerif.ttf", 50, 72));
const std::unique_ptr<TextRasterizer> textRasterizer = std::make_unique<TextRasterizer>("DejaVuSerif.ttf", 50, 72);
unsigned int glyphCount;
std::unique_ptr<const Glyph[]> 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 @@ -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<float[]> vertices = std::make_unique<float[]>(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 @@ -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 @@ -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() { @@ -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;
}

3
src/graphics/opengl/Text.h

@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
#include <GL/glew.h>
#include <array>
#include <memory>
#include <vector>
class Text {
@ -17,7 +18,7 @@ private: @@ -17,7 +18,7 @@ private:
0, 2, 3
};
unsigned char data[1024][1024][4];
std::vector<float*> glyphVertices;
std::vector<std::unique_ptr<float[]>> glyphVertices;
std::vector<GLuint> vertexArrayObjects;
std::vector<GLuint> vertexBufferObjects;
std::vector<GLuint> elementBufferObjects;

6
src/graphics/opengl/Window.cpp

@ -19,9 +19,9 @@ bool Window::init() { @@ -19,9 +19,9 @@ bool Window::init() {
}
initGL();
boxes.push_back(std::unique_ptr<Box>(new Box(0.0f, 1.0f, 1.0f, -64, windowWidth, windowHeight)));
boxes.push_back(std::unique_ptr<Box>(new Box(-1024, 0.0f, 1024, 1024, windowWidth, windowHeight)));
texts.push_back(std::unique_ptr<Text>(new Text("Hello, World!", 32, 32, windowWidth, windowHeight)));
boxes.push_back(std::make_unique<Box>(0.0f, 1.0f, 1.0f, -64, windowWidth, windowHeight));
boxes.push_back(std::make_unique<Box>(-1024, 0.0f, 1024, 1024, windowWidth, windowHeight));
texts.push_back(std::make_unique<Text>("Hello, World!", 32, 32, windowWidth, windowHeight));
return true;
}

4
src/graphics/text/TextRasterizer.cpp

@ -48,7 +48,7 @@ std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text @@ -48,7 +48,7 @@ std::unique_ptr<const Glyph[]> 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<Glyph[]> glyphs = std::unique_ptr<Glyph[]>(new Glyph[glyphCount]);
std::unique_ptr<Glyph[]> glyphs = std::make_unique<Glyph[]>(glyphCount);
int cx = x;
int cy = y;
@ -70,7 +70,7 @@ std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text @@ -70,7 +70,7 @@ std::unique_ptr<const Glyph[]> 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<unsigned char[]>(new unsigned char[glyphs[i].textureWidth * glyphs[i].textureHeight]);
glyphs[i].textureData = std::make_unique<unsigned char[]>(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);
}

24
src/html/HTMLParser.cpp

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#include "HTMLParser.h"
#include "TextNode.h"
#include <iostream>
#include <unistd.h>
#include <memory>
void printNode(Node *node, int indent) {
void printNode(std::shared_ptr<Node> node, int indent) {
for (int i = 0; i < indent; i++) {
std::cout << '\t';
}
@ -11,8 +11,8 @@ void printNode(Node *node, int indent) { @@ -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<TagNode*>(node)->tag << std::endl;
for (const std::pair<std::string, std::string> property : dynamic_cast<TagNode*>(node)->properties) {
std::cout << "TAG: " << dynamic_cast<TagNode*>(node.get())->tag << std::endl;
for (const std::pair<std::string, std::string> property : dynamic_cast<TagNode*>(node.get())->properties) {
for (int i = 0; i < indent; i++) {
std::cout << '\t';
}
@ -20,17 +20,17 @@ void printNode(Node *node, int indent) { @@ -20,17 +20,17 @@ void printNode(Node *node, int indent) {
}
}
else if (node->nodeType == NodeType::TEXT) {
std::cout << "TEXT: " << dynamic_cast<TextNode*>(node)->text << std::endl;
std::cout << "TEXT: " << dynamic_cast<TextNode*>(node.get())->text << std::endl;
}
for (Node *child : node->children) {
for (std::shared_ptr<Node> 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<Node> currentNode = std::make_shared<Node>(rootNode);
std::vector<int> starts;
int cursor;
int start = 0;
@ -49,7 +49,7 @@ void HTMLParser::parse(const std::string &html) const { @@ -49,7 +49,7 @@ void HTMLParser::parse(const std::string &html) const {
state = 1;
}
else {
TagNode *tagNode = new TagNode();
std::shared_ptr<TagNode> tagNode = std::make_shared<TagNode>();
currentNode->children.push_back(tagNode);
tagNode->parent = currentNode;
currentNode = tagNode;
@ -58,7 +58,7 @@ void HTMLParser::parse(const std::string &html) const { @@ -58,7 +58,7 @@ void HTMLParser::parse(const std::string &html) const {
}
}
else {
TextNode *textNode = new TextNode();
std::shared_ptr<TextNode> textNode = std::make_shared<TextNode>();
currentNode->children.push_back(textNode);
textNode->parent = currentNode;
currentNode = textNode;
@ -76,7 +76,7 @@ void HTMLParser::parse(const std::string &html) const { @@ -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<TagNode*>(currentNode));
parseTag(element, dynamic_cast<TagNode*>(currentNode.get()));
state = 0;
}
}
@ -84,13 +84,13 @@ void HTMLParser::parse(const std::string &html) const { @@ -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<TextNode*>(currentNode)->text = html.substr(start, cursor - start + 1);
dynamic_cast<TextNode*>(currentNode.get())->text = html.substr(start, cursor - start + 1);
state = 0;
}
}
}
printNode(&rootNode, 0);
printNode(std::make_shared<Node>(rootNode), 0);
}
void HTMLParser::parseTag(const std::string &element, TagNode* tagNode) const {

5
src/html/Node.h

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
#ifndef NODE_H
#define NODE_H
#include <memory>
#include <vector>
enum class NodeType {
@ -14,8 +15,8 @@ public: @@ -14,8 +15,8 @@ public:
Node(NodeType nodeType);
virtual ~Node();
NodeType nodeType;
Node *parent;
std::vector<Node*> children;
std::shared_ptr<Node> parent;
std::vector<std::shared_ptr<Node>> children;
};
#endif

6
src/main.cpp

@ -35,14 +35,14 @@ const std::string getHostFromURL(const std::string &url) { @@ -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<HTMLParser> parser = std::unique_ptr<HTMLParser>(new HTMLParser());
const std::unique_ptr<HTMLParser> parser = std::make_unique<HTMLParser>();
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<HTTPRequest> request = std::unique_ptr<HTTPRequest>(new HTTPRequest(getHostFromURL(location), getDocumentFromURL(location)));
const std::unique_ptr<HTTPRequest> request = std::make_unique<HTTPRequest>(getHostFromURL(location), getDocumentFromURL(location));
request->sendRequest(handleRequest);
return;
}
@ -56,7 +56,7 @@ int main(int argc, char *argv[]) { @@ -56,7 +56,7 @@ int main(int argc, char *argv[]) {
std::cout << "./netrunner <url>" << std::endl;
return 1;
}
const std::unique_ptr<HTTPRequest> request = std::unique_ptr<HTTPRequest>(new HTTPRequest(getHostFromURL(argv[1]), getDocumentFromURL(argv[1])));
const std::unique_ptr<HTTPRequest> request = std::make_unique<HTTPRequest>(getHostFromURL(argv[1]), getDocumentFromURL(argv[1]));
request->sendRequest(handleRequest);
const std::unique_ptr<Window> window = std::make_unique<Window>();
window->init();

Loading…
Cancel
Save