Created macro for simple named param classes declaration and definition.

This commit is contained in:
Kirill Kirilenko 2017-02-15 21:43:18 +03:00
parent 592751a50c
commit ea28b547eb
5 changed files with 49 additions and 77 deletions

View file

@ -1567,7 +1567,7 @@ ENABLE_PREPROCESSING = YES
# compilation will be performed. Macro expansion can be done in a controlled # compilation will be performed. Macro expansion can be done in a controlled
# way by setting EXPAND_ONLY_PREDEF to YES. # way by setting EXPAND_ONLY_PREDEF to YES.
MACRO_EXPANSION = NO MACRO_EXPANSION = YES
# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES # If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES
# then the macro expansion is limited to the macros specified with the # then the macro expansion is limited to the macros specified with the

View file

@ -4,6 +4,24 @@
#include <string> #include <string>
#include <vector> #include <vector>
#define TELEBOTXX_DECLARE_BOOL_PARAM_CLASS(Name, DEFAULT) class Name \
{ \
public: \
explicit Name(bool value = DEFAULT); \
bool getValue() const; \
private: \
bool value_; \
};
#define TELEBOTXX_DECLARE_STRING_PARAM_CLASS(Name) class Name \
{ \
public: \
explicit Name(const std::string& value); \
const std::string& getValue() const; \
private: \
std::string value_; \
};
namespace telebotxx { namespace telebotxx {
class ChatId class ChatId
@ -28,8 +46,8 @@ private:
}; };
}; };
using Text = std::string; TELEBOTXX_DECLARE_STRING_PARAM_CLASS(Text);
using Caption = std::string; TELEBOTXX_DECLARE_STRING_PARAM_CLASS(Caption);
enum class ParseMode enum class ParseMode
{ {
@ -38,23 +56,9 @@ enum class ParseMode
Html Html
}; };
class DisableWebPagePreview TELEBOTXX_DECLARE_BOOL_PARAM_CLASS(DisableWebPagePreview, true)
{
public:
DisableWebPagePreview(bool disabled = true);
bool value() const;
private:
bool disable_;
};
class DisableNotification TELEBOTXX_DECLARE_BOOL_PARAM_CLASS(DisableNotification, true)
{
public:
DisableNotification(bool disabled = true);
bool value() const;
private:
bool disable_;
};
class ReplyTo class ReplyTo
{ {
@ -79,23 +83,9 @@ private:
std::string filename_; std::string filename_;
}; };
class File TELEBOTXX_DECLARE_STRING_PARAM_CLASS(File)
{
public:
explicit File(const std::string& filename);
const std::string& getFilename() const;
private:
std::string filename_;
};
class Url TELEBOTXX_DECLARE_STRING_PARAM_CLASS(Url)
{
public:
explicit Url(const std::string& url);
const std::string& getUrl() const;
private:
std::string url_;
};
class Photo class Photo
{ {

View file

@ -1,5 +1,11 @@
#include <telebotxx/RequestOptions.hpp> #include <telebotxx/RequestOptions.hpp>
#define TELEBOTXX_DEFINE_BOOL_PARAM_CLASS(Name) Name::Name(bool value) : value_(value) { } \
bool Name::getValue() const { return value_; }
#define TELEBOTXX_DEFINE_STRING_PARAM_CLASS(Name) Name::Name(const std::string& value) : value_(value) { } \
const std::string& Name::getValue() const { return value_; }
namespace telebotxx { namespace telebotxx {
ChatId::ChatId(int id) ChatId::ChatId(int id)
@ -59,27 +65,19 @@ const std::string ChatId::getUsername() const
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
DisableWebPagePreview::DisableWebPagePreview(bool disabled) TELEBOTXX_DEFINE_STRING_PARAM_CLASS(Text)
: disable_(disabled)
{
}
bool DisableWebPagePreview::value() const
{
return disable_;
}
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
DisableNotification::DisableNotification(bool disabled) TELEBOTXX_DEFINE_STRING_PARAM_CLASS(Caption)
: disable_(disabled)
{
}
bool DisableNotification::value() const ////////////////////////////////////////////////////////////////
{
return disable_; TELEBOTXX_DEFINE_BOOL_PARAM_CLASS(DisableWebPagePreview)
}
////////////////////////////////////////////////////////////////
TELEBOTXX_DEFINE_BOOL_PARAM_CLASS(DisableNotification)
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
@ -122,27 +120,11 @@ const std::string Buffer::filename() const
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
File::File(const std::string& filename) TELEBOTXX_DEFINE_STRING_PARAM_CLASS(File)
: filename_(filename)
{
}
const std::string& File::getFilename() const
{
return filename_;
}
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
const std::string& Url::getUrl() const TELEBOTXX_DEFINE_STRING_PARAM_CLASS(Url)
{
return url_;
}
Url::Url(const std::string& url)
: url_(url)
{
}
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////

View file

@ -56,7 +56,7 @@ public:
writer.String(chatId_.getUsername().c_str()); writer.String(chatId_.getUsername().c_str());
writer.String("text"); writer.String("text");
writer.String(text_.c_str()); writer.String(text_.getValue().c_str());
// Add parse_mode // Add parse_mode
if (parseMode_) if (parseMode_)
@ -70,14 +70,14 @@ public:
if (disableWebPagePreview_) if (disableWebPagePreview_)
{ {
writer.String("disable_web_page_preview"); writer.String("disable_web_page_preview");
writer.Bool(disableWebPagePreview_->value()); writer.Bool(disableWebPagePreview_->getValue());
} }
// Add disable_notification // Add disable_notification
if (disableNotification_) if (disableNotification_)
{ {
writer.String("disable_notification"); writer.String("disable_notification");
writer.Bool(disableNotification_->value()); writer.Bool(disableNotification_->getValue());
} }
// Add reply_to_message_id // Add reply_to_message_id

View file

@ -55,17 +55,17 @@ public:
multipart.parts.push_back({"photo", cpr::Buffer(data, data + size, filename)}); multipart.parts.push_back({"photo", cpr::Buffer(data, data + size, filename)});
} }
else if (photo_.getType() == Photo::Type::File) else if (photo_.getType() == Photo::Type::File)
multipart.parts.push_back({"photo", cpr::File(photo_.getFile().getFilename())}); multipart.parts.push_back({"photo", cpr::File(photo_.getFile().getValue())});
else if (photo_.getType() == Photo::Type::Url) else if (photo_.getType() == Photo::Type::Url)
multipart.parts.push_back({"photo", photo_.getUrl().getUrl()}); multipart.parts.push_back({"photo", photo_.getUrl().getValue()});
// Add caption // Add caption
if (caption_) if (caption_)
multipart.parts.push_back({"caption", *caption_}); multipart.parts.push_back({"caption", caption_->getValue()});
// Add disable_notification // Add disable_notification
if (disableNotification_) if (disableNotification_)
multipart.parts.push_back({"disable_notification", disableNotification_->value()}); multipart.parts.push_back({"disable_notification", disableNotification_->getValue()});
// Add reply_to_message_id // Add reply_to_message_id
if (replyToMessageId_) if (replyToMessageId_)