Merge pull request #32 from scaryrawr/makeshared

Remove uses of new
This commit is contained in:
Sergey Naumov
2020-10-25 21:02:38 +03:00
committed by GitHub
3 changed files with 32 additions and 37 deletions

View File

@@ -3,6 +3,7 @@
#include <cstdint>
#include <string>
#include <memory>
#include <vector>
#include <stdexcept>
// extern "C" {
@@ -93,8 +94,7 @@ enum class ReplyType : uint32_t {
* @brief i3 IPC message buffer
*/
struct buf_t {
uint32_t size; ///< @brief Size of whole buffer
uint8_t* data; ///< @brief Pointer to the message
std::vector<uint8_t> data;
/**
* @brief i3 IPC message header
@@ -110,8 +110,7 @@ struct buf_t {
*/
char* payload;
buf_t(uint32_t payload_size);
~buf_t();
explicit buf_t(uint32_t payload_size);
/**
* @brief Resize payload to the payload_size in the header

View File

@@ -30,25 +30,20 @@ errno_error::errno_error(const std::string& msg) : ipc_error(format_errno(msg))
static const std::string g_i3_ipc_magic = "i3-ipc";
buf_t::buf_t(uint32_t payload_size) : size(sizeof(header_t) + payload_size) {
data = new uint8_t[size];
header = (header_t*)data;
payload = (char*)(data + sizeof(header_t));
strncpy(header->magic, g_i3_ipc_magic.c_str(), sizeof(header->magic));
buf_t::buf_t(uint32_t payload_size) :
data(sizeof(header_t) + payload_size, 0),
header(reinterpret_cast<header_t*>(data.data())),
payload(reinterpret_cast<char*>(data.data() + sizeof(header_t)))
{
std::copy_n(std::begin(g_i3_ipc_magic), sizeof(header->magic), header->magic);
header->size = payload_size;
header->type = 0x0;
}
buf_t::~buf_t() {
delete[] data;
}
void buf_t::realloc_payload_to_header() {
uint8_t* new_data = new uint8_t[sizeof(header_t) + header->size];
memcpy(new_data, header, sizeof(header_t));
delete[] data;
data = new_data;
header = (header_t*)data;
payload = (char*)(data + sizeof(header_t));
data.resize(sizeof(*header) + header->size);
header = reinterpret_cast<header_t*>(data.data());
payload = reinterpret_cast<char*>(data.data() + sizeof(header_t));
}
@@ -78,10 +73,11 @@ void i3_disconnect(const int32_t sockfd) {
std::shared_ptr<buf_t> i3_pack(const ClientMessageType type, const std::string& payload) {
buf_t* buff = new buf_t(payload.length());
auto buff{std::make_shared<buf_t>(payload.length())};
buff->header->type = static_cast<uint32_t>(type);
strncpy(buff->payload, payload.c_str(), buff->header->size);
return std::shared_ptr<buf_t>(buff);
std::copy_n(std::begin(payload), buff->header->size, buff->payload);
return buff;
}
ssize_t writeall(int fd, const uint8_t* buf, size_t count) {
@@ -112,15 +108,15 @@ ssize_t swrite(int fd, const uint8_t* buf, size_t count) {
}
void i3_send(const int32_t sockfd, const buf_t& buff) {
swrite(sockfd, buff.data, buff.size);
swrite(sockfd, buff.data.data(), buff.data.size());
}
std::shared_ptr<buf_t> i3_recv(const int32_t sockfd) {
buf_t* buff = new buf_t(0);
auto buff{std::make_shared<buf_t>(0)};
const uint32_t header_size = sizeof(header_t);
{
uint8_t* header = (uint8_t*)buff->header;
uint8_t* header = reinterpret_cast<uint8_t*>(buff->header);
uint32_t readed = 0;
while (readed < header_size) {
int n = read(sockfd, header + readed, header_size - readed);
@@ -155,7 +151,7 @@ std::shared_ptr<buf_t> i3_recv(const int32_t sockfd) {
}
}
return std::shared_ptr<buf_t>(buff);
return buff;
}

View File

@@ -76,8 +76,8 @@ window_properties_t parse_window_props_from_json(const Json::Value& value) {
static std::shared_ptr<container_t> parse_container_from_json(const Json::Value& o, std::optional<std::string> workspace_name = std::nullopt) {
#define i3IPC_TYPE_STR "PARSE CONTAINER FROM JSON"
if (o.isNull())
return std::shared_ptr<container_t>();
std::shared_ptr<container_t> container (new container_t());
return nullptr;
auto container{std::make_shared<container_t>()};
IPC_JSON_ASSERT_TYPE_OBJECT(o, "o")
container->id = o["id"].asUInt64();
@@ -172,7 +172,7 @@ static std::shared_ptr<container_t> parse_container_from_json(const Json::Value
static std::shared_ptr<workspace_t> parse_workspace_from_json(const Json::Value& value) {
if (value.isNull())
return std::shared_ptr<workspace_t>();
return nullptr;
Json::Value num = value["num"];
Json::Value name = value["name"];
Json::Value visible = value["visible"];
@@ -181,7 +181,7 @@ static std::shared_ptr<workspace_t> parse_workspace_from_json(const Json::Value
Json::Value rect = value["rect"];
Json::Value output = value["output"];
std::shared_ptr<workspace_t> p (new workspace_t());
auto p{std::make_shared<workspace_t>()};
p->num = num.asInt();
p->name = name.asString();
p->visible = visible.asBool();
@@ -194,14 +194,14 @@ static std::shared_ptr<workspace_t> parse_workspace_from_json(const Json::Value
static std::shared_ptr<output_t> parse_output_from_json(const Json::Value& value) {
if (value.isNull())
return std::shared_ptr<output_t>();
return nullptr;
Json::Value name = value["name"];
Json::Value active = value["active"];
Json::Value primary = value["primary"];
Json::Value current_workspace = value["current_workspace"];
Json::Value rect = value["rect"];
std::shared_ptr<output_t> p (new output_t());
auto p{std::make_shared<output_t>()};
p->name = name.asString();
p->active = active.asBool();
p->primary = primary.asBool();
@@ -213,9 +213,9 @@ static std::shared_ptr<output_t> parse_output_from_json(const Json::Value& val
static std::shared_ptr<binding_t> parse_binding_from_json(const Json::Value& value) {
#define i3IPC_TYPE_STR "PARSE BINDING FROM JSON"
if (value.isNull())
return std::shared_ptr<binding_t>();
return nullptr;
IPC_JSON_ASSERT_TYPE_OBJECT(value, "binding")
std::shared_ptr<binding_t> b (new binding_t());
auto b{std::make_shared<binding_t>()};
b->command = value["command"].asString();
b->symbol = value["symbol"].asString();
@@ -245,11 +245,11 @@ static std::shared_ptr<binding_t> parse_binding_from_json(const Json::Value& v
static std::shared_ptr<mode_t> parse_mode_from_json(const Json::Value& value) {
if (value.isNull())
return std::shared_ptr<mode_t>();
return nullptr;
Json::Value change = value["change"];
Json::Value pango_markup = value["pango_markup"];
std::shared_ptr<mode_t> p (new mode_t());
auto p{std::make_shared<mode_t>()};
p->change = change.asString();
p->pango_markup = pango_markup.asBool();
return p;
@@ -259,9 +259,9 @@ static std::shared_ptr<mode_t> parse_mode_from_json(const Json::Value& value)
static std::shared_ptr<bar_config_t> parse_bar_config_from_json(const Json::Value& value) {
#define i3IPC_TYPE_STR "PARSE BAR CONFIG FROM JSON"
if (value.isNull())
return std::shared_ptr<bar_config_t>();
return nullptr;
IPC_JSON_ASSERT_TYPE_OBJECT(value, "(root)")
std::shared_ptr<bar_config_t> bc (new bar_config_t());
auto bc{std::make_shared<bar_config_t>()};
bc->id = value["id"].asString();
bc->status_command = value["status_command"].asString();