From 67d22c8e42b446f00f69dd5797f34a577c09ab81 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 12 Sep 2014 14:42:40 +0200 Subject: [PATCH 1/5] CTemplate: Switch from CSmartPtr to std::shared_ptr Signed-off-by: Uli Schlachter --- include/znc/Template.h | 13 +++++++------ src/Template.cpp | 10 +++++----- src/WebModules.cpp | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/include/znc/Template.h b/include/znc/Template.h index cdaa0ec3..7c25d6c0 100644 --- a/include/znc/Template.h +++ b/include/znc/Template.h @@ -18,9 +18,10 @@ #define _TEMPLATE_H #include -#include +#include #include #include +#include class CTemplate; @@ -124,7 +125,7 @@ public: Init(); } - CTemplate(const CSmartPtr& Options, CTemplate* pParent = NULL) : MCString(), m_spOptions(Options) { + CTemplate(const std::shared_ptr& Options, CTemplate* pParent = NULL) : MCString(), m_spOptions(Options) { Init(); m_pParent = pParent; } @@ -132,11 +133,11 @@ public: virtual ~CTemplate(); //! Class for implementing custom tags in subclasses - void AddTagHandler(CSmartPtr spTagHandler) { + void AddTagHandler(std::shared_ptr spTagHandler) { m_vspTagHandlers.push_back(spTagHandler); } - std::vector >& GetTagHandlers() { + std::vector >& GetTagHandlers() { if (m_pParent) { return m_pParent->GetTagHandlers(); } @@ -182,8 +183,8 @@ private: std::list > m_lsbPaths; std::map > m_mvLoops; std::vector m_vLoopContexts; - CSmartPtr m_spOptions; - std::vector > m_vspTagHandlers; + std::shared_ptr m_spOptions; + std::vector > m_vspTagHandlers; }; #endif // !_TEMPLATE_H diff --git a/src/Template.cpp b/src/Template.cpp index 760805b3..bb4e4ad4 100644 --- a/src/Template.cpp +++ b/src/Template.cpp @@ -581,14 +581,14 @@ bool CTemplate::Print(const CString& sFileName, ostream& oOut) { } } else if (bNotFound) { // Unknown tag that isn't being skipped... - vector >& vspTagHandlers = GetTagHandlers(); + vector >& vspTagHandlers = GetTagHandlers(); if (!vspTagHandlers.empty()) { // @todo this should go up to the top to grab handlers CTemplate* pTmpl = GetCurTemplate(); CString sCustomOutput; for (unsigned int j = 0; j < vspTagHandlers.size(); j++) { - CSmartPtr spTagHandler = vspTagHandlers[j]; + std::shared_ptr spTagHandler = vspTagHandlers[j]; if (spTagHandler->HandleTag(*pTmpl, sAction, sArgs, sCustomOutput)) { sOutput += sCustomOutput; @@ -826,14 +826,14 @@ CString CTemplate::GetValue(const CString& sArgs, bool bFromIf) { sRet = (it != end()) ? it->second : ""; } - vector >& vspTagHandlers = GetTagHandlers(); + vector >& vspTagHandlers = GetTagHandlers(); if (!vspTagHandlers.empty()) { // @todo this should go up to the top to grab handlers CTemplate* pTmpl = GetCurTemplate(); if (sRet.empty()) { for (unsigned int j = 0; j < vspTagHandlers.size(); j++) { - CSmartPtr spTagHandler = vspTagHandlers[j]; + std::shared_ptr spTagHandler = vspTagHandlers[j]; CString sCustomOutput; if (!bFromIf && spTagHandler->HandleVar(*pTmpl, sArgs.Token(0), sArgs.Token(1, true), sCustomOutput)) { @@ -847,7 +847,7 @@ CString CTemplate::GetValue(const CString& sArgs, bool bFromIf) { } for (unsigned int j = 0; j < vspTagHandlers.size(); j++) { - CSmartPtr spTagHandler = vspTagHandlers[j]; + std::shared_ptr spTagHandler = vspTagHandlers[j]; if (spTagHandler->HandleValue(*pTmpl, sRet, msArgs)) { break; diff --git a/src/WebModules.cpp b/src/WebModules.cpp index a7418b78..c5552811 100644 --- a/src/WebModules.cpp +++ b/src/WebModules.cpp @@ -188,7 +188,7 @@ void CWebAuth::Invalidate() { CWebSock::CWebSock(const CString& sURIPrefix) : CHTTPSock(NULL, sURIPrefix) { m_bPathsSet = false; - m_Template.AddTagHandler(new CZNCTagHandler(*this)); + m_Template.AddTagHandler(std::make_shared(*this)); } CWebSock::~CWebSock() { From 3953185b04e7ab923d19b21f80bb94eca33fd88c Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 12 Sep 2014 14:55:42 +0200 Subject: [PATCH 2/5] WebModules: Switch from CSmartPtr to std::shared_ptr Signed-off-by: Uli Schlachter --- include/znc/WebModules.h | 10 +++++----- modules/webadmin.cpp | 20 ++++++++++---------- src/WebModules.cpp | 18 +++++++++--------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/include/znc/WebModules.h b/include/znc/WebModules.h index 44e477e5..aa0a7310 100644 --- a/include/znc/WebModules.h +++ b/include/znc/WebModules.h @@ -27,7 +27,7 @@ class CWebSock; class CModule; class CWebSubPage; -typedef CSmartPtr TWebSubPage; +typedef std::shared_ptr TWebSubPage; typedef std::vector VWebSubPages; class CZNCTagHandler : public CTemplateTagHandler { @@ -102,9 +102,9 @@ private: VPair m_vParams; }; -class CWebSessionMap : public TCacheMap > { +class CWebSessionMap : public TCacheMap > { public: - CWebSessionMap(unsigned int uTTL = 5000) : TCacheMap >(uTTL) {} + CWebSessionMap(unsigned int uTTL = 5000) : TCacheMap >(uTTL) {} void FinishUserSessions(const CUser& User); }; @@ -131,7 +131,7 @@ public: void PrintErrorPage(const CString& sMessage); - CSmartPtr GetSession(); + std::shared_ptr GetSession(); virtual Csock* GetSockObj(const CString& sHost, unsigned short uPort); static CString GetSkinPath(const CString& sSkinName); @@ -162,7 +162,7 @@ private: CString m_sModName; CString m_sPath; CString m_sPage; - CSmartPtr m_spSession; + std::shared_ptr m_spSession; static const unsigned int m_uiMaxSessions; }; diff --git a/modules/webadmin.cpp b/modules/webadmin.cpp index f575e76c..f8d08067 100644 --- a/modules/webadmin.cpp +++ b/modules/webadmin.cpp @@ -76,10 +76,10 @@ public: MODCONSTRUCTOR(CWebAdminMod) { VPair vParams; vParams.push_back(make_pair("user", "")); - AddSubPage(new CWebSubPage("settings", "Global Settings", CWebSubPage::F_ADMIN)); - AddSubPage(new CWebSubPage("edituser", "Your Settings", vParams)); - AddSubPage(new CWebSubPage("traffic", "Traffic Info", CWebSubPage::F_ADMIN)); - AddSubPage(new CWebSubPage("listusers", "Manage Users", CWebSubPage::F_ADMIN)); + AddSubPage(std::make_shared("settings", "Global Settings", CWebSubPage::F_ADMIN)); + AddSubPage(std::make_shared("edituser", "Your Settings", vParams)); + AddSubPage(std::make_shared("traffic", "Traffic Info", CWebSubPage::F_ADMIN)); + AddSubPage(std::make_shared("listusers", "Manage Users", CWebSubPage::F_ADMIN)); } virtual ~CWebAdminMod() { @@ -164,7 +164,7 @@ public: } CUser* GetNewUser(CWebSock& WebSock, CUser* pUser) { - CSmartPtr spSession = WebSock.GetSession(); + std::shared_ptr spSession = WebSock.GetSession(); CString sUsername = WebSock.GetParam("newuser"); if (sUsername.empty()) { @@ -402,7 +402,7 @@ public: virtual CString GetWebMenuTitle() { return "webadmin"; } virtual bool OnWebRequest(CWebSock& WebSock, const CString& sPageName, CTemplate& Tmpl) { - CSmartPtr spSession = WebSock.GetSession(); + std::shared_ptr spSession = WebSock.GetSession(); if (sPageName == "settings") { // Admin Check @@ -597,7 +597,7 @@ public: } bool ChanPage(CWebSock& WebSock, CTemplate& Tmpl, CIRCNetwork* pNetwork, CChan* pChan = NULL) { - CSmartPtr spSession = WebSock.GetSession(); + std::shared_ptr spSession = WebSock.GetSession(); Tmpl.SetFile("add_edit_chan.tmpl"); CUser* pUser = pNetwork->GetUser(); @@ -727,7 +727,7 @@ public: } bool NetworkPage(CWebSock& WebSock, CTemplate& Tmpl, CUser* pUser, CIRCNetwork* pNetwork = NULL) { - CSmartPtr spSession = WebSock.GetSession(); + std::shared_ptr spSession = WebSock.GetSession(); Tmpl.SetFile("add_edit_network.tmpl"); if (!WebSock.GetParam("submitted").ToUInt()) { @@ -1073,7 +1073,7 @@ public: } bool UserPage(CWebSock& WebSock, CTemplate& Tmpl, CUser* pUser = NULL) { - CSmartPtr spSession = WebSock.GetSession(); + std::shared_ptr spSession = WebSock.GetSession(); Tmpl.SetFile("add_edit_user.tmpl"); if (!WebSock.GetParam("submitted").ToUInt()) { @@ -1380,7 +1380,7 @@ public: } bool ListUsersPage(CWebSock& WebSock, CTemplate& Tmpl) { - CSmartPtr spSession = WebSock.GetSession(); + std::shared_ptr spSession = WebSock.GetSession(); const map& msUsers = CZNC::Get().GetUserMap(); Tmpl["Title"] = "Manage Users"; Tmpl["Action"] = "listusers"; diff --git a/src/WebModules.cpp b/src/WebModules.cpp index c5552811..b359748c 100644 --- a/src/WebModules.cpp +++ b/src/WebModules.cpp @@ -153,7 +153,7 @@ void CWebSessionMap::FinishUserSessions(const CUser& User) { void CWebAuth::AcceptedLogin(CUser& User) { if (m_pWebSock) { - CSmartPtr spSession = m_pWebSock->GetSession(); + std::shared_ptr spSession = m_pWebSock->GetSession(); spSession->SetUser(&User); @@ -167,7 +167,7 @@ void CWebAuth::AcceptedLogin(CUser& User) { void CWebAuth::RefusedLogin(const CString& sReason) { if (m_pWebSock) { - CSmartPtr spSession = m_pWebSock->GetSession(); + std::shared_ptr spSession = m_pWebSock->GetSession(); spSession->AddError("Invalid login!"); spSession->SetUser(NULL); @@ -192,7 +192,7 @@ CWebSock::CWebSock(const CString& sURIPrefix) : CHTTPSock(NULL, sURIPrefix) { } CWebSock::~CWebSock() { - if (!m_spAuth.IsNull()) { + if (m_spAuth) { m_spAuth->Invalidate(); } @@ -819,13 +819,13 @@ static inline bool compareLastActive(const std::pairGetLastActive() < second.second->GetLastActive(); } -CSmartPtr CWebSock::GetSession() { - if (!m_spSession.IsNull()) { +std::shared_ptr CWebSock::GetSession() { + if (m_spSession) { return m_spSession; } const CString sCookieSessionId = GetRequestCookie("SessionId"); - CSmartPtr *pSession = Sessions.m_mspSessions.GetItem(sCookieSessionId); + std::shared_ptr *pSession = Sessions.m_mspSessions.GetItem(sCookieSessionId); if (pSession != NULL) { // Refresh the timeout @@ -855,7 +855,7 @@ CSmartPtr CWebSock::GetSession() { DEBUG("Auto generated session: [" + sSessionID + "]"); } while (Sessions.m_mspSessions.HasItem(sSessionID)); - CSmartPtr spSession(new CWebSession(sSessionID, GetRemoteIP())); + std::shared_ptr spSession(new CWebSession(sSessionID, GetRemoteIP())); Sessions.m_mspSessions.AddItem(spSession->GetId(), spSession); m_spSession = spSession; @@ -864,7 +864,7 @@ CSmartPtr CWebSock::GetSession() { } CString CWebSock::GetCSRFCheck() { - CSmartPtr pSession = GetSession(); + std::shared_ptr pSession = GetSession(); return pSession->GetId().MD5(); } @@ -889,7 +889,7 @@ Csock* CWebSock::GetSockObj(const CString& sHost, unsigned short uPort) { } CString CWebSock::GetSkinName() { - CSmartPtr spSession = GetSession(); + std::shared_ptr spSession = GetSession(); if (spSession->IsLoggedIn() && !spSession->GetUser()->GetSkinName().empty()) { return spSession->GetUser()->GetSkinName(); From 7704bc49d76b4f9866e705908454259682951c42 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 12 Sep 2014 15:12:46 +0200 Subject: [PATCH 3/5] client auth: Switch from CSmartPtr to std::shared_ptr Signed-off-by: Uli Schlachter --- include/znc/Client.h | 4 ++-- include/znc/Modules.h | 4 ++-- include/znc/WebModules.h | 2 +- include/znc/znc.h | 3 +-- modules/blockuser.cpp | 2 +- modules/certauth.cpp | 2 +- modules/cyrusauth.cpp | 2 +- modules/fail2ban.cpp | 2 +- modules/imapauth.cpp | 12 ++++++------ src/Client.cpp | 4 ++-- src/Modules.cpp | 4 ++-- src/WebModules.cpp | 2 +- src/znc.cpp | 2 +- 13 files changed, 22 insertions(+), 23 deletions(-) diff --git a/include/znc/Client.h b/include/znc/Client.h index b7aed570..d64b768f 100644 --- a/include/znc/Client.h +++ b/include/znc/Client.h @@ -19,8 +19,8 @@ #include #include -#include #include +#include // Forward Declarations class CZNC; @@ -169,7 +169,7 @@ protected: CString m_sPass; CString m_sUser; CString m_sNetwork; - CSmartPtr m_spAuth; + std::shared_ptr m_spAuth; SCString m_ssAcceptedCaps; }; diff --git a/include/znc/Modules.h b/include/znc/Modules.h index 159f0046..51283748 100644 --- a/include/znc/Modules.h +++ b/include/znc/Modules.h @@ -1013,7 +1013,7 @@ public: * @param Auth The necessary authentication info for this login attempt. * @return See CModule::EModRet. */ - virtual EModRet OnLoginAttempt(CSmartPtr Auth); + virtual EModRet OnLoginAttempt(std::shared_ptr Auth); /** Called after a client login was rejected. * @param sUsername The username that tried to log in. * @param sRemoteIP The IP address from which the client tried to login. @@ -1223,7 +1223,7 @@ public: bool OnAddUser(CUser& User, CString& sErrorRet); bool OnDeleteUser(CUser& User); bool OnClientConnect(CZNCSock* pSock, const CString& sHost, unsigned short uPort); - bool OnLoginAttempt(CSmartPtr Auth); + bool OnLoginAttempt(std::shared_ptr Auth); bool OnFailedLogin(const CString& sUsername, const CString& sRemoteIP); bool OnUnknownUserRaw(CClient* pClient, CString& sLine); bool OnClientCapLs(CClient* pClient, SCString& ssCaps); diff --git a/include/znc/WebModules.h b/include/znc/WebModules.h index aa0a7310..f4d01c14 100644 --- a/include/znc/WebModules.h +++ b/include/znc/WebModules.h @@ -158,7 +158,7 @@ private: bool m_bPathsSet; CTemplate m_Template; - CSmartPtr m_spAuth; + std::shared_ptr m_spAuth; CString m_sModName; CString m_sPath; CString m_sPage; diff --git a/include/znc/znc.h b/include/znc/znc.h index b4f3e8de..92a4ca37 100644 --- a/include/znc/znc.h +++ b/include/znc/znc.h @@ -88,8 +88,7 @@ public: // Authenticate a user. // The result is passed back via callbacks to CAuthBase. - // CSmartPtr handles freeing this pointer! - void AuthUser(CSmartPtr AuthClass); + void AuthUser(std::shared_ptr AuthClass); // Setters void SetConfigState(enum ConfigState e) { m_eConfigState = e; } diff --git a/modules/blockuser.cpp b/modules/blockuser.cpp index 1e2c261c..9a618a11 100644 --- a/modules/blockuser.cpp +++ b/modules/blockuser.cpp @@ -51,7 +51,7 @@ public: return true; } - virtual EModRet OnLoginAttempt(CSmartPtr Auth) { + virtual EModRet OnLoginAttempt(std::shared_ptr Auth) { if (IsBlocked(Auth->GetUsername())) { Auth->RefuseLogin(MESSAGE); return HALT; diff --git a/modules/certauth.cpp b/modules/certauth.cpp index 61e56870..10008934 100644 --- a/modules/certauth.cpp +++ b/modules/certauth.cpp @@ -101,7 +101,7 @@ public: return pair.second; } - virtual EModRet OnLoginAttempt(CSmartPtr Auth) { + virtual EModRet OnLoginAttempt(std::shared_ptr Auth) { const CString sUser = Auth->GetUsername(); Csock *pSock = Auth->GetSocket(); CUser *pUser = CZNC::Get().FindUser(sUser); diff --git a/modules/cyrusauth.cpp b/modules/cyrusauth.cpp index 515c6012..e1afd868 100644 --- a/modules/cyrusauth.cpp +++ b/modules/cyrusauth.cpp @@ -87,7 +87,7 @@ public: return true; } - virtual EModRet OnLoginAttempt(CSmartPtr Auth) { + virtual EModRet OnLoginAttempt(std::shared_ptr Auth) { const CString& sUsername = Auth->GetUsername(); const CString& sPassword = Auth->GetPassword(); CUser *pUser(CZNC::Get().FindUser(sUsername)); diff --git a/modules/fail2ban.cpp b/modules/fail2ban.cpp index 36e66de5..29ba8299 100644 --- a/modules/fail2ban.cpp +++ b/modules/fail2ban.cpp @@ -81,7 +81,7 @@ public: Add(sRemoteIP, 1); } - virtual EModRet OnLoginAttempt(CSmartPtr Auth) { + virtual EModRet OnLoginAttempt(std::shared_ptr Auth) { // e.g. webadmin ends up here const CString& sRemoteIP = Auth->GetRemoteIP(); diff --git a/modules/imapauth.cpp b/modules/imapauth.cpp index 368ae268..3773f926 100644 --- a/modules/imapauth.cpp +++ b/modules/imapauth.cpp @@ -22,7 +22,7 @@ class CIMAPAuthMod; class CIMAPSock : public CSocket { public: - CIMAPSock(CIMAPAuthMod* pModule, CSmartPtr Auth) + CIMAPSock(CIMAPAuthMod* pModule, std::shared_ptr Auth) : CSocket((CModule*) pModule), m_spAuth(Auth) { m_pIMAPMod = pModule; m_bSentReply = false; @@ -39,10 +39,10 @@ public: virtual void ReadLine(const CString& sLine); private: protected: - CIMAPAuthMod* m_pIMAPMod; - bool m_bSentLogin; - bool m_bSentReply; - CSmartPtr m_spAuth; + CIMAPAuthMod* m_pIMAPMod; + bool m_bSentLogin; + bool m_bSentReply; + std::shared_ptr m_spAuth; }; @@ -84,7 +84,7 @@ public: return true; } - virtual EModRet OnLoginAttempt(CSmartPtr Auth) { + virtual EModRet OnLoginAttempt(std::shared_ptr Auth) { CUser* pUser = CZNC::Get().FindUser(Auth->GetUsername()); if (!pUser) { // @todo Will want to do some sort of && !m_bAllowCreate in the future diff --git a/src/Client.cpp b/src/Client.cpp index cf9d8e4b..28d5e5aa 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -67,7 +67,7 @@ using std::vector; } CClient::~CClient() { - if (!m_spAuth.IsNull()) { + if (m_spAuth) { CClientAuth* pAuth = (CClientAuth*) &(*m_spAuth); pAuth->Invalidate(); } @@ -618,7 +618,7 @@ void CClient::AuthUser() { if (!m_bGotNick || !m_bGotUser || !m_bGotPass || m_bInCap || IsAttached()) return; - m_spAuth = new CClientAuth(this, m_sUser, m_sPass); + m_spAuth = std::make_shared(this, m_sUser, m_sPass); CZNC::Get().AuthUser(m_spAuth); } diff --git a/src/Modules.cpp b/src/Modules.cpp index 5420368a..08d29b5e 100644 --- a/src/Modules.cpp +++ b/src/Modules.cpp @@ -751,7 +751,7 @@ bool CModule::PutModNotice(const CString& sLine) { CModule::EModRet CModule::OnAddUser(CUser& User, CString& sErrorRet) { return CONTINUE; } CModule::EModRet CModule::OnDeleteUser(CUser& User) { return CONTINUE; } void CModule::OnClientConnect(CZNCSock* pClient, const CString& sHost, unsigned short uPort) {} -CModule::EModRet CModule::OnLoginAttempt(CSmartPtr Auth) { return CONTINUE; } +CModule::EModRet CModule::OnLoginAttempt(std::shared_ptr Auth) { return CONTINUE; } void CModule::OnFailedLogin(const CString& sUsername, const CString& sRemoteIP) {} CModule::EModRet CModule::OnUnknownUserRaw(CClient* pClient, CString& sLine) { return CONTINUE; } void CModule::OnClientCapLs(CClient* pClient, SCString& ssCaps) {} @@ -915,7 +915,7 @@ bool CModules::OnClientConnect(CZNCSock* pClient, const CString& sHost, unsigned return false; } -bool CModules::OnLoginAttempt(CSmartPtr Auth) { +bool CModules::OnLoginAttempt(std::shared_ptr Auth) { MODHALTCHK(OnLoginAttempt(Auth)); } diff --git a/src/WebModules.cpp b/src/WebModules.cpp index b359748c..7014f3b3 100644 --- a/src/WebModules.cpp +++ b/src/WebModules.cpp @@ -870,7 +870,7 @@ CString CWebSock::GetCSRFCheck() { bool CWebSock::OnLogin(const CString& sUser, const CString& sPass) { DEBUG("=================== CWebSock::OnLogin()"); - m_spAuth = new CWebAuth(this, sUser, sPass); + m_spAuth = std::make_shared(this, sUser, sPass); // Some authentication module could need some time, block this socket // until then. CWebAuth will UnPauseRead(). diff --git a/src/znc.cpp b/src/znc.cpp index 913e0af8..dfc0604d 100644 --- a/src/znc.cpp +++ b/src/znc.cpp @@ -1783,7 +1783,7 @@ CZNC::TrafficStatsMap CZNC::GetTrafficStats(TrafficStatsPair &Users, return ret; } -void CZNC::AuthUser(CSmartPtr AuthClass) { +void CZNC::AuthUser(std::shared_ptr AuthClass) { // TODO unless the auth module calls it, CUser::IsHostAllowed() is not honoured bool bReturn = false; GLOBALMODULECALL(OnLoginAttempt(AuthClass), &bReturn); From 62328b2c961ba1671b23001c8a29585afa21c10e Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 12 Sep 2014 15:25:57 +0200 Subject: [PATCH 4/5] CSmartPtr: Remove, superseded by std::shared_ptr Signed-off-by: Uli Schlachter --- include/znc/Utils.h | 151 -------------------------------------------- 1 file changed, 151 deletions(-) diff --git a/include/znc/Utils.h b/include/znc/Utils.h index 01499e34..d3c51348 100644 --- a/include/znc/Utils.h +++ b/include/znc/Utils.h @@ -354,155 +354,4 @@ protected: unsigned int m_uTTL; //!< Default time-to-live duration }; -/** - * @class CSmartPtr - * @author prozac - * @brief This is a standard reference counting pointer. Be careful not to have two of these point to the same raw pointer or one will be deleted while the other still thinks it is valid. - */ -template -class CSmartPtr { -public: - /** - * @brief Standard constructor, points to nothing - */ - CSmartPtr() { - m_pType = NULL; - m_puCount = NULL; - } - - /** - * @brief Attach to an existing raw pointer, be CAREFUL not to have more than one CSmartPtr attach to the same raw pointer or bad things will happen - * @param pRawPtr The raw pointer to attach to - */ - CSmartPtr(T* pRawPtr) { - m_pType = NULL; - m_puCount = NULL; - - Attach(pRawPtr); - } - - /** - * @brief Copy constructor, will copy the raw pointer and counter locations and increment the reference counter - * @param CopyFrom A reference of another CSmartPtr to copy from - */ - CSmartPtr(const CSmartPtr& CopyFrom) { - m_pType = NULL; - m_puCount = NULL; - - *this = CopyFrom; - } - - /** - * @brief Destructor will Release() the raw pointer and delete it if this was the last reference - */ - ~CSmartPtr() { - Release(); - } - - // Overloaded operators - T& operator *() const { assert(m_pType); return *m_pType; } - T* operator ->() const { assert(m_pType); return m_pType; } - - /** - * @brief Attach() to a raw pointer - * @param pRawPtr The raw pointer to keep track of, ***WARNING*** Do _NOT_ allow more than one CSmartPtr keep track of the same raw pointer - * @return Reference to self - */ - CSmartPtr& operator =(T* pRawPtr) { Attach(pRawPtr); return *this; } - - /** - * @brief Copies an existing CSmartPtr adding another reference to the counter - * @param CopyFrom A reference to another CSmartPtr to be copied - * @return Reference to self - */ - CSmartPtr& operator =(const CSmartPtr& CopyFrom) { - if (&CopyFrom != this) { // Check for assignment to self - Release(); // Release the current pointer - - if (CopyFrom.IsNull()) { // If the source raw pointer is null - return *this; // Then just bail out - } - - m_pType = CopyFrom.m_pType; // Make our pointers reference the same raw pointer and counter - m_puCount = CopyFrom.m_puCount; - - assert(m_puCount); // We now point to something valid, so increment the counter - (*m_puCount)++; - } - - return *this; - } - // !Overloaded operators - - /** - * @brief Implicit type conversion to bool for things like if (!ptr) {} and if (ptr) {} - * @return @see IsNull() - */ - operator bool() const { - return !IsNull(); - } - - /** - * @brief Check to see if the underlying raw pointer is null - * @return Whether or not underlying raw pointer is null - */ - bool IsNull() const { - return (m_pType == NULL); - } - - /** - * @brief Attach to a given raw pointer, it will Release() the current raw pointer and assign the new one - * @param pRawPtr The raw pointer to keep track of, ***WARNING*** Do _NOT_ allow more than one CSmartPtr keep track of the same raw pointer - * @return Reference to self - */ - CSmartPtr& Attach(T* pRawPtr) { - if (pRawPtr != m_pType) { // Check for assignment to self - Release(); // Release the current pointer - m_pType = pRawPtr; // Point to the passed raw pointer - - if (m_pType) { // If the passed pointer was valid - m_puCount = new unsigned int(1); // Create a new counter starting at 1 (us) - } - } - - return *this; - } - - /** - * @brief Releases the underlying raw pointer and cleans up if we were the last reference to said pointer - */ - void Release() { - if (m_pType) { // Only release if there is something to be released - assert(m_puCount); - (*m_puCount)--; // Decrement our counter - - if (!*m_puCount) { // If we were the last reference to this pointer, then clean up - delete m_puCount; - delete m_pType; - } - - m_pType = NULL; // Get rid of our references - m_puCount = NULL; - } - } - - // Getters - T* GetPtr() const { return m_pType; } - unsigned int GetCount() const { return (m_puCount) ? *m_puCount : 0; } - // !Getters -private: - T* m_pType; //!< Raw pointer to the class being referenced - unsigned int* m_puCount; //!< Counter of how many CSmartPtr's are referencing the same raw pointer -}; - -template -bool operator ==(T* lhs, const CSmartPtr& rhs) { return (lhs == rhs.GetPtr()); } - -template -bool operator ==(const CSmartPtr& lhs, T* rhs) { return (lhs.GetPtr() == rhs); } - -template -bool operator ==(const CSmartPtr& lhs, const CSmartPtr& rhs) { return (lhs.GetPtr() == rhs.GetPtr()); } - #endif // !_UTILS_H - From de96cd95d036190450c3614b72e8dc985a528878 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Mon, 15 Sep 2014 12:07:13 +0200 Subject: [PATCH 5/5] modpython: Switch from CSmartPtr to std::shared_ptr Patch by DarthGandalf, I just committed it to git. Signed-off-by: Uli Schlachter --- modules/modperl/modperl.i | 2 +- modules/modpython/codegen.pl | 2 +- modules/modpython/functions.in | 2 +- modules/modpython/modpython.i | 7 ++++--- modules/modpython/module.h | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/modules/modperl/modperl.i b/modules/modperl/modperl.i index 739799a8..fbfc4c26 100644 --- a/modules/modperl/modperl.i +++ b/modules/modperl/modperl.i @@ -242,7 +242,7 @@ typedef std::vector > VPair; %inline %{ TWebSubPage _CreateWebSubPage(const CString& sName, const CString& sTitle, const VPair& vParams, unsigned int uFlags) { - return new CWebSubPage(sName, sTitle, vParams, uFlags); + return std::make_shared(sName, sTitle, vParams, uFlags); } %} diff --git a/modules/modpython/codegen.pl b/modules/modpython/codegen.pl index 39c6c97c..b0729756 100755 --- a/modules/modpython/codegen.pl +++ b/modules/modpython/codegen.pl @@ -292,7 +292,7 @@ while (<$in>) { say $out "Py_BuildValue(\"l\", (long int)$a->{var});"; } } - when (/^CSmartPtr/) { + when (/^std::shared_ptr/) { say $out "SWIG_NewInstanceObj(new $a->{type}($a->{var}), SWIG_TypeQuery(\"$a->{type}*\"), SWIG_POINTER_OWN);"; } when (/\*$/) { diff --git a/modules/modpython/functions.in b/modules/modpython/functions.in index 4a9e17e3..5bf893d2 100644 --- a/modules/modpython/functions.in +++ b/modules/modpython/functions.in @@ -78,4 +78,4 @@ EModRet OnModuleUnloading(CModule* pModule, bool& bSuccess, CString& sRetMsg) EModRet OnGetModInfo(CModInfo& ModInfo, const CString& sModule, bool& bSuccess, CString& sRetMsg) void OnGetAvailableMods(std::set& ssMods, CModInfo::EModuleType eType) void OnClientCapLs(CClient* pClient, SCString& ssCaps) -EModRet OnLoginAttempt(CSmartPtr Auth) +EModRet OnLoginAttempt(std::shared_ptr Auth) diff --git a/modules/modpython/modpython.i b/modules/modpython/modpython.i index 8495d09a..1fc8899b 100644 --- a/modules/modpython/modpython.i +++ b/modules/modpython/modpython.i @@ -58,6 +58,7 @@ using std::allocator; %include %include %include +%include %include "modpython/cstring.i" %template(_stringlist) std::list; @@ -129,8 +130,8 @@ class MCString : public std::map {}; %include "../include/znc/defines.h" %include "../include/znc/Utils.h" %include "../include/znc/Threads.h" -%template(PAuthBase) CSmartPtr; -%template(WebSession) CSmartPtr; +%template(PAuthBase) std::shared_ptr; +%template(WebSession) std::shared_ptr; %include "../include/znc/Config.h" %include "../include/znc/Csocket.h" %template(ZNCSocketManager) TSocketManager; @@ -308,7 +309,7 @@ typedef std::vector > VPair; %inline %{ TWebSubPage CreateWebSubPage_(const CString& sName, const CString& sTitle, const VPair& vParams, unsigned int uFlags) { - return new CWebSubPage(sName, sTitle, vParams, uFlags); + return std::make_shared(sName, sTitle, vParams, uFlags); } %} diff --git a/modules/modpython/module.h b/modules/modpython/module.h index 2a4e265b..3a8dd46a 100644 --- a/modules/modpython/module.h +++ b/modules/modpython/module.h @@ -137,7 +137,7 @@ public: bool& bSuccess, CString& sRetMsg); virtual void OnGetAvailableMods(std::set& ssMods, CModInfo::EModuleType eType); virtual void OnClientCapLs(CClient* pClient, SCString& ssCaps); - virtual EModRet OnLoginAttempt(CSmartPtr Auth); + virtual EModRet OnLoginAttempt(std::shared_ptr Auth); }; static inline CPyModule* AsPyModule(CModule* p) {