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 +++++++++++++++++++--------------------------------------- 1 file changed, 32 insertions(+), 69 deletions(-) (limited to 'src/tweet.cpp') 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(); -- cgit 1.4.1