From 014411a8a3828bdd80700b1a6fd9fe55e594d4ff Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Thu, 16 Feb 2017 00:49:39 +0300 Subject: [PATCH] Used boost::variant in Union-like classes. --- include/telebotxx/RequestOptions.hpp | 16 +--- src/RequestOptions.cpp | 105 +++++---------------------- 2 files changed, 24 insertions(+), 97 deletions(-) diff --git a/include/telebotxx/RequestOptions.hpp b/include/telebotxx/RequestOptions.hpp index b1ff334..33bd2b7 100644 --- a/include/telebotxx/RequestOptions.hpp +++ b/include/telebotxx/RequestOptions.hpp @@ -4,6 +4,8 @@ #include #include +#include + #define TELEBOTXX_DECLARE_BOOL_PARAM_CLASS(Name, DEFAULT) class Name \ { \ public: \ @@ -39,11 +41,7 @@ public: const std::string getUsername() const; private: Type type_; - union - { - int id_; - std::string username_; - }; + boost::variant value_; }; TELEBOTXX_DECLARE_STRING_PARAM_CLASS(Text); @@ -108,13 +106,7 @@ public: private: Type type_; - union - { - int id_; - Buffer buffer_; - File file_; - Url url_; - }; + boost::variant value_; }; } diff --git a/src/RequestOptions.cpp b/src/RequestOptions.cpp index 3472e33..54f80ae 100644 --- a/src/RequestOptions.cpp +++ b/src/RequestOptions.cpp @@ -1,5 +1,7 @@ #include +#include + #define TELEBOTXX_DEFINE_BOOL_PARAM_CLASS(Name) Name::Name(bool value) : value_(value) { } \ bool Name::getValue() const { return value_; } @@ -9,44 +11,18 @@ const std::string& Name::getValue() const { return value_; } namespace telebotxx { ChatId::ChatId(int id) - : type_(Type::Id), id_(id) + : type_(Type::Id), value_(id) { } ChatId::ChatId(const std::string& str) - : type_(Type::Username), username_(str) + : type_(Type::Username), value_(str) { } -ChatId::ChatId(const ChatId& other) - : type_(other.type_) -{ - if (type_ == Type::Id) - id_ = other.id_; - else - new(&username_) std::string(other.username_); -} - -ChatId::ChatId(ChatId&& other) - : type_(std::move(other.type_)) -{ - if (type_ == Type::Id) - { - id_ = other.id_; - other.id_ = 0; - } - else - { - new(&username_) std::string(std::move(other.username_)); - other.username_.~basic_string(); - } -} - -ChatId::~ChatId() -{ - if (type_ == Type::Username) - username_.~basic_string(); -} +ChatId::ChatId(const ChatId& other) = default; +ChatId::ChatId(ChatId&& other) = default; +ChatId::~ChatId() = default; ChatId::Type ChatId::getType() const { @@ -55,12 +31,12 @@ ChatId::Type ChatId::getType() const const int ChatId::getId() const { - return id_; + return boost::get(value_); } const std::string ChatId::getUsername() const { - return username_; + return boost::get(value_); } //////////////////////////////////////////////////////////////// @@ -129,69 +105,28 @@ TELEBOTXX_DEFINE_STRING_PARAM_CLASS(Url) //////////////////////////////////////////////////////////////// Photo::Photo(int id) - : type_(Type::Id), id_(id) + : type_(Type::Id), value_(id) { } Photo::Photo(const Buffer& buffer) - : type_(Type::Buffer), buffer_(buffer) + : type_(Type::Buffer), value_(buffer) { } Photo::Photo(const File& file) - : type_(Type::File), file_(file) + : type_(Type::File), value_(file) { } Photo::Photo(const Url& url) - : type_(Type::Url), url_(url) + : type_(Type::Url), value_(url) { } -Photo::Photo(const Photo& other) - : type_(other.type_) -{ - if (type_ == Type::Id) - id_ = other.id_; - else if (type_ == Type::Buffer) - new(&buffer_) Buffer(other.buffer_); - else if (type_ == Type::File) - new(&file_) File(other.file_); - else - new(&url_) Url(other.url_); -} - -Photo::Photo(Photo&& other) -{ - if (type_ == Type::Id) - { - id_ = other.id_; - other.id_ = 0; - } - else if (type_ == Type::Buffer) - { - new(&buffer_) Buffer(std::move(other.buffer_)); - other.buffer_.~Buffer(); - } - else if (type_ == Type::File) - { - new(&file_) File(std::move(other.file_)); - other.file_.~File(); - } - else - { - new(&url_) Url(std::move(other.url_)); - other.url_.~Url(); - } -} - -Photo::~Photo() -{ - if (type_ == Type::File) - file_.~File(); - else if (type_ == Type::Url) - url_.~Url(); -} +Photo::Photo(const Photo& other) = default; +Photo::Photo(Photo&& other) = default; +Photo::~Photo() = default; Photo::Type Photo::getType() const { @@ -200,22 +135,22 @@ Photo::Type Photo::getType() const int Photo::getId() const { - return id_; + return boost::get(value_); } const Buffer& Photo::getBuffer() const { - return buffer_; + return boost::get(value_); } const File& Photo::getFile() const { - return file_; + return boost::get(value_); } const Url& Photo::getUrl() const { - return url_; + return boost::get(value_); } }