From 8584857578c3a2946a03de98ff3803431143297a Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 30 Aug 2018 20:43:23 -0400 Subject: Refactored tweet and user classes The only API-visible change is that these classes are no longer default constructible. Other than that, tweet now uses hatkirby::recptr to wrap its retweeted_status (tweet) and author (user) member objects, removing the need to implement the Rule of Five. --- src/tweet.cpp | 101 +++++++++++++++--------------------------------- src/tweet.h | 122 ++++++++++++++++++++++++---------------------------------- src/user.cpp | 31 +++++++-------- src/user.h | 94 +++++++++++++++++++------------------------- 4 files changed, 138 insertions(+), 210 deletions(-) (limited to 'src') diff --git a/src/tweet.cpp b/src/tweet.cpp index 1003647..7f10a5c 100644 --- a/src/tweet.cpp +++ b/src/tweet.cpp @@ -7,83 +7,48 @@ namespace twitter { - tweet::tweet(std::string data) try - : _valid(true) + tweet::tweet(std::string data) { - auto json = nlohmann::json::parse(data); - _id = json["id"].get(); - _text = json["text"].get(); - _author = std::make_unique(json["user"].dump()); - - std::tm ctt = { 0 }; - std::stringstream createdAtStream; - createdAtStream << json["created_at"].get(); - createdAtStream >> std::get_time(&ctt, "%a %b %d %H:%M:%S +0000 %Y"); - _created_at = twitter::timegm(&ctt); - - if (!json["retweeted_status"].is_null()) + try { - _is_retweet = true; + auto json = nlohmann::json::parse(data); + _id = json["id"].get(); + _text = json["text"].get(); + _author = new user(json["user"].dump()); + + std::tm ctt = { 0 }; + std::stringstream createdAtStream; + createdAtStream << json["created_at"].get(); + createdAtStream >> std::get_time(&ctt, "%a %b %d %H:%M:%S +0000 %Y"); + _created_at = twitter::timegm(&ctt); + + if (!json["retweeted_status"].is_null()) + { + _is_retweet = true; - _retweeted_status = std::make_unique(json["retweeted_status"].dump()); - } + _retweeted_status = new tweet(json["retweeted_status"].dump()); + } - if (!json["entities"].is_null()) - { - auto entities = json["entities"]; - if (!entities["user_mentions"].is_null()) + if (!json["entities"].is_null()) { - for (auto mention : entities["user_mentions"]) + auto entities = json["entities"]; + if (!entities["user_mentions"].is_null()) { - _mentions.push_back(std::make_pair(mention["id"].get(), mention["screen_name"].get())); + for (auto mention : entities["user_mentions"]) + { + _mentions.emplace_back( + mention["id"].get(), + mention["screen_name"].get()); + } } } - } - } catch (const std::invalid_argument& error) - { - std::throw_with_nested(malformed_object("tweet", data)); - } catch (const std::domain_error& error) - { - std::throw_with_nested(malformed_object("tweet", data)); - } - - tweet::tweet(const tweet& other) - { - _valid = other._valid; - _id = other._id; - _text = other._text; - _author = std::make_unique(*other._author); - _is_retweet = other._is_retweet; - - if (_is_retweet) + } catch (const std::invalid_argument& error) { - _retweeted_status = std::make_unique(*other._retweeted_status); + std::throw_with_nested(malformed_object("tweet", data)); + } catch (const std::domain_error& error) + { + std::throw_with_nested(malformed_object("tweet", data)); } - - _mentions = other._mentions; - } - - tweet::tweet(tweet&& other) : tweet() - { - swap(*this, other); - } - - tweet& tweet::operator=(tweet other) - { - swap(*this, other); - - return *this; - } - - void swap(tweet& first, tweet& second) - { - std::swap(first._valid, second._valid); - std::swap(first._id, second._id); - std::swap(first._text, second._text); - std::swap(first._author, second._author); - std::swap(first._is_retweet, second._is_retweet); - std::swap(first._retweeted_status, second._retweeted_status); - std::swap(first._mentions, second._mentions); } std::string tweet::generateReplyPrefill(const user& me) const @@ -104,8 +69,6 @@ namespace twitter { std::string tweet::getURL() const { - assert(_valid); - std::ostringstream urlstr; urlstr << "https://twitter.com/"; urlstr << _author->getScreenName(); diff --git a/src/tweet.h b/src/tweet.h index 71a27bd..23882e2 100644 --- a/src/tweet.h +++ b/src/tweet.h @@ -4,94 +4,72 @@ #include #include #include -#include -#include -#include #include +#include "../vendor/hkutil/hkutil/recptr.h" #include "user.h" namespace twitter { - class client; - typedef unsigned long long tweet_id; class tweet { - public: - - tweet() {} - tweet(std::string data); - - tweet(const tweet& other); - tweet(tweet&& other); - - tweet& operator=(tweet other); - - friend void swap(tweet& first, tweet& second); - - tweet_id getID() const + public: + + tweet(std::string data); + + tweet_id getID() const + { + return _id; + } + + std::string getText() const + { + return _text; + } + + const user& getAuthor() const + { + return *_author; + } + + const std::time_t& getCreatedAt() const + { + return _created_at; + } + + bool isRetweet() const + { + return _is_retweet; + } + + const tweet& getRetweet() const + { + if (!_is_retweet) { - assert(_valid); - - return _id; + throw std::logic_error("Tweet is not a retweet"); } - std::string getText() const - { - assert(_valid); + return *_retweeted_status; + } - return _text; - } - - const user& getAuthor() const - { - assert(_valid); - - return *_author; - } - - const std::time_t& getCreatedAt() const - { - assert(_valid); - - return _created_at; - } - - bool isRetweet() const - { - assert(_valid); - - return _is_retweet; - } - - const tweet& getRetweet() const - { - assert(_valid && _is_retweet); - - return *_retweeted_status; - } - - const std::vector>& getMentions() const - { - assert(_valid); - - return _mentions; - } + const std::vector>& getMentions() const + { + return _mentions; + } - std::string generateReplyPrefill(const user& me) const; + std::string generateReplyPrefill(const user& me) const; - std::string getURL() const; + std::string getURL() const; - private: + private: - bool _valid = false; - tweet_id _id; - std::string _text; - std::unique_ptr _author; - std::time_t _created_at; - bool _is_retweet = false; - std::unique_ptr _retweeted_status; - std::vector> _mentions; + tweet_id _id; + std::string _text; + hatkirby::recptr _author; + std::time_t _created_at; + bool _is_retweet = false; + hatkirby::recptr _retweeted_status; + std::vector> _mentions; }; }; diff --git a/src/user.cpp b/src/user.cpp index d2fa592..8109123 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -1,24 +1,25 @@ #include "user.h" #include #include "codes.h" -#include "client.h" namespace twitter { - user::user(std::string data) try - : _valid(true) + user::user(std::string data) { - auto json = nlohmann::json::parse(data); - _id = json["id"].get(); - _screen_name = json["screen_name"].get(); - _name = json["name"].get(); - _protected = json["protected"].get(); - } catch (const std::invalid_argument& error) - { - std::throw_with_nested(malformed_object("user", data)); - } catch (const std::domain_error& error) - { - std::throw_with_nested(malformed_object("user", data)); + try + { + auto json = nlohmann::json::parse(data); + _id = json["id"].get(); + _screen_name = json["screen_name"].get(); + _name = json["name"].get(); + _protected = json["protected"].get(); + } catch (const std::invalid_argument& error) + { + std::throw_with_nested(malformed_object("user", data)); + } catch (const std::domain_error& error) + { + std::throw_with_nested(malformed_object("user", data)); + } } -}; +} diff --git a/src/user.h b/src/user.h index acd62d0..b743074 100644 --- a/src/user.h +++ b/src/user.h @@ -2,66 +2,52 @@ #define USER_H_BF3AB38C #include -#include -#include namespace twitter { - class client; - typedef unsigned long long user_id; class user { - public: - - user() {} - user(std::string data); - - user_id getID() const - { - assert(_valid); - - return _id; - } - - std::string getScreenName() const - { - assert(_valid); - - return _screen_name; - } - - std::string getName() const - { - assert(_valid); - - return _name; - } - - bool isProtected() const - { - assert(_valid); - - return _protected; - } - - bool operator==(const user& other) const - { - return _id == other._id; - } - - bool operator!=(const user& other) const - { - return _id != other._id; - } - - private: - - bool _valid = false; - user_id _id; - std::string _screen_name; - std::string _name; - bool _protected = false; + public: + + user(std::string data); + + user_id getID() const + { + return _id; + } + + std::string getScreenName() const + { + return _screen_name; + } + + std::string getName() const + { + return _name; + } + + bool isProtected() const + { + return _protected; + } + + bool operator==(const user& other) const + { + return _id == other._id; + } + + bool operator!=(const user& other) const + { + return _id != other._id; + } + + private: + + user_id _id; + std::string _screen_name; + std::string _name; + bool _protected = false; }; }; -- cgit 1.4.1