Move Message assertions to special cast operators.

Don't use static_cast directly anymore, but use these operators.
This commit is contained in:
Alexey Sokolov
2015-11-21 12:13:54 +00:00
parent f33809585f
commit 103ed00861
3 changed files with 45 additions and 36 deletions
+22 -13
View File
@@ -102,6 +102,28 @@ public:
CString ToString(unsigned int uFlags = IncludeAll) const;
void Parse(CString sMessage);
// Implicit and explicit conversion to a subclass reference.
template <typename M>
M& As() & {
static_assert(sizeof(M) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
return static_cast<M&>(*this);
}
template <typename M>
const M& As() const & {
static_assert(sizeof(M) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
return static_cast<const M&>(*this);
}
template <typename M>
operator M&() & {
return As<M>();
}
template <typename M>
operator const M&() const & {
return As<M>();
}
private:
void InitTime();
void InitType();
@@ -126,14 +148,12 @@ public:
CString GetTarget() const { return GetParam(0); }
void SetTarget(const CString& sTarget) { SetParam(0, sTarget); }
};
static_assert(sizeof(CTargetMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
class CActionMessage : public CTargetMessage {
public:
CString GetText() const { return GetParam(1).TrimPrefix_n("\001ACTION ").TrimSuffix_n("\001"); }
void SetText(const CString& sText) { SetParam(1, "\001ACTION " + sText + "\001"); }
};
static_assert(sizeof(CActionMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
class CCTCPMessage : public CTargetMessage {
public:
@@ -141,20 +161,17 @@ public:
CString GetText() const { return GetParam(1).TrimPrefix_n("\001").TrimSuffix_n("\001"); }
void SetText(const CString& sText) { SetParam(1, "\001" + sText + "\001"); }
};
static_assert(sizeof(CCTCPMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
class CJoinMessage : public CTargetMessage {
public:
CString GetKey() const { return GetParam(1); }
void SetKey(const CString& sKey) { SetParam(1, sKey); }
};
static_assert(sizeof(CJoinMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
class CModeMessage : public CTargetMessage {
public:
CString GetModes() const { return GetParams(1).TrimPrefix_n(":"); }
};
static_assert(sizeof(CModeMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
class CNickMessage : public CMessage {
public:
@@ -162,20 +179,17 @@ public:
CString GetNewNick() const { return GetParam(0); }
void SetNewNick(const CString& sNick) { SetParam(0, sNick); }
};
static_assert(sizeof(CNickMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
class CNoticeMessage : public CTargetMessage {
public:
CString GetText() const { return GetParam(1); }
void SetText(const CString& sText) { SetParam(1, sText); }
};
static_assert(sizeof(CNoticeMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
class CNumericMessage : public CMessage {
public:
unsigned int GetCode() const { return GetCommand().ToUInt(); }
};
static_assert(sizeof(CNumericMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
class CKickMessage : public CTargetMessage {
public:
@@ -184,34 +198,29 @@ public:
CString GetReason() const { return GetParam(2); }
void SetReason(const CString& sReason) { SetParam(2, sReason); }
};
static_assert(sizeof(CKickMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
class CPartMessage : public CTargetMessage {
public:
CString GetReason() const { return GetParam(1); }
void SetReason(const CString& sReason) { SetParam(1, sReason); }
};
static_assert(sizeof(CPartMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
class CQuitMessage : public CMessage {
public:
CString GetReason() const { return GetParam(0); }
void SetReason(const CString& sReason) { SetParam(0, sReason); }
};
static_assert(sizeof(CQuitMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
class CTextMessage : public CTargetMessage {
public:
CString GetText() const { return GetParam(1); }
void SetText(const CString& sText) { SetParam(1, sText); }
};
static_assert(sizeof(CTextMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
class CTopicMessage : public CTargetMessage {
public:
CString GetTopic() const { return GetParam(1); }
void SetTopic(const CString& sTopic) { SetParam(1, sTopic); }
};
static_assert(sizeof(CTopicMessage) == sizeof(CMessage), "No data members allowed in CMessage subclasses.");
#endif // !ZNC_MESSAGE_H
+11 -11
View File
@@ -173,22 +173,22 @@ void CClient::ReadLine(const CString& sData) {
switch (Message.GetType()) {
case CMessage::Type::Action:
bReturn = OnActionMessage(static_cast<CActionMessage&>(Message));
bReturn = OnActionMessage(Message);
break;
case CMessage::Type::CTCP:
bReturn = OnCTCPMessage(static_cast<CCTCPMessage&>(Message));
bReturn = OnCTCPMessage(Message);
break;
case CMessage::Type::Join:
bReturn = OnJoinMessage(static_cast<CJoinMessage&>(Message));
bReturn = OnJoinMessage(Message);
break;
case CMessage::Type::Mode:
bReturn = OnModeMessage(static_cast<CModeMessage&>(Message));
bReturn = OnModeMessage(Message);
break;
case CMessage::Type::Notice:
bReturn = OnNoticeMessage(static_cast<CNoticeMessage&>(Message));
bReturn = OnNoticeMessage(Message);
break;
case CMessage::Type::Part:
bReturn = OnPartMessage(static_cast<CPartMessage&>(Message));
bReturn = OnPartMessage(Message);
break;
case CMessage::Type::Ping:
bReturn = OnPingMessage(Message);
@@ -197,13 +197,13 @@ void CClient::ReadLine(const CString& sData) {
bReturn = OnPongMessage(Message);
break;
case CMessage::Type::Quit:
bReturn = OnQuitMessage(static_cast<CQuitMessage&>(Message));
bReturn = OnQuitMessage(Message);
break;
case CMessage::Type::Text:
bReturn = OnTextMessage(static_cast<CTextMessage&>(Message));
bReturn = OnTextMessage(Message);
break;
case CMessage::Type::Topic:
bReturn = OnTopicMessage(static_cast<CTopicMessage&>(Message));
bReturn = OnTopicMessage(Message);
break;
default:
bReturn = OnOtherMessage(Message);
@@ -479,7 +479,7 @@ bool CClient::PutClient(const CMessage& Message)
const CIRCSock* pIRCSock = GetIRCSock();
if (pIRCSock) {
if (Msg.GetType() == CMessage::Type::Numeric) {
unsigned int uCode = static_cast<CNumericMessage&>(Msg).GetCode();
unsigned int uCode = Msg.As<CNumericMessage>().GetCode();
if (uCode == 352) { // RPL_WHOREPLY
if (!m_bNamesx && pIRCSock->HasNamesx()) {
@@ -524,7 +524,7 @@ bool CClient::PutClient(const CMessage& Message)
}
} else if (Msg.GetType() == CMessage::Type::Join) {
if (!m_bExtendedJoin && pIRCSock->HasExtendedJoin()) {
Msg.SetParams({static_cast<CJoinMessage&>(Msg).GetTarget()});
Msg.SetParams({Msg.As<CJoinMessage>().GetTarget()});
}
}
}
+12 -12
View File
@@ -168,7 +168,7 @@ void CIRCSock::ReadLine(const CString& sData) {
bReturn = OnAccountMessage(Message);
break;
case CMessage::Type::Action:
bReturn = OnActionMessage(static_cast<CActionMessage&>(Message));
bReturn = OnActionMessage(Message);
break;
case CMessage::Type::Away:
bReturn = OnAwayMessage(Message);
@@ -177,7 +177,7 @@ void CIRCSock::ReadLine(const CString& sData) {
bReturn = OnCapabilityMessage(Message);
break;
case CMessage::Type::CTCP:
bReturn = OnCTCPMessage(static_cast<CCTCPMessage&>(Message));
bReturn = OnCTCPMessage(Message);
break;
case CMessage::Type::Error:
bReturn = OnErrorMessage(Message);
@@ -186,25 +186,25 @@ void CIRCSock::ReadLine(const CString& sData) {
bReturn = OnInviteMessage(Message);
break;
case CMessage::Type::Join:
bReturn = OnJoinMessage(static_cast<CJoinMessage&>(Message));
bReturn = OnJoinMessage(Message);
break;
case CMessage::Type::Kick:
bReturn = OnKickMessage(static_cast<CKickMessage&>(Message));
bReturn = OnKickMessage(Message);
break;
case CMessage::Type::Mode:
bReturn = OnModeMessage(static_cast<CModeMessage&>(Message));
bReturn = OnModeMessage(Message);
break;
case CMessage::Type::Nick:
bReturn = OnNickMessage(static_cast<CNickMessage&>(Message));
bReturn = OnNickMessage(Message);
break;
case CMessage::Type::Notice:
bReturn = OnNoticeMessage(static_cast<CNoticeMessage&>(Message));
bReturn = OnNoticeMessage(Message);
break;
case CMessage::Type::Numeric:
bReturn = OnNumericMessage(static_cast<CNumericMessage&>(Message));
bReturn = OnNumericMessage(Message);
break;
case CMessage::Type::Part:
bReturn = OnPartMessage(static_cast<CPartMessage&>(Message));
bReturn = OnPartMessage(Message);
break;
case CMessage::Type::Ping:
bReturn = OnPingMessage(Message);
@@ -213,13 +213,13 @@ void CIRCSock::ReadLine(const CString& sData) {
bReturn = OnPongMessage(Message);
break;
case CMessage::Type::Quit:
bReturn = OnQuitMessage(static_cast<CQuitMessage&>(Message));
bReturn = OnQuitMessage(Message);
break;
case CMessage::Type::Text:
bReturn = OnTextMessage(static_cast<CTextMessage&>(Message));
bReturn = OnTextMessage(Message);
break;
case CMessage::Type::Topic:
bReturn = OnTopicMessage(static_cast<CTopicMessage&>(Message));
bReturn = OnTopicMessage(Message);
break;
case CMessage::Type::Wallops:
bReturn = OnWallopsMessage(Message);