diff options
84 files changed, 728 insertions, 399 deletions
diff --git a/inc/gallery/model/Gallery.h b/inc/gallery/model/Gallery.h index 1a711f9..e17b1f5 100644 --- a/inc/gallery/model/Gallery.h +++ b/inc/gallery/model/Gallery.h @@ -31,7 +31,7 @@ namespace gallery { IMediaAlbumSRef getAlbum(); private: - friend class ucl::RefCountObj<Gallery>; + friend class ucl::ReffedObj<Gallery>; Gallery(); ucl::Result prepare(); diff --git a/inc/gallery/model/MediaItem.h b/inc/gallery/model/MediaItem.h index df35b8d..7b6eea0 100644 --- a/inc/gallery/model/MediaItem.h +++ b/inc/gallery/model/MediaItem.h @@ -87,7 +87,7 @@ namespace gallery { ucl::Result saveToDevice(); protected: - friend class ucl::RefCountObj<MediaItem>; + friend class ucl::ReffedObj<MediaItem>; MediaItem(int flags, MediaType type); ucl::Result prepare(media_info_h media); diff --git a/inc/gallery/model/SoundManager.h b/inc/gallery/model/SoundManager.h index df5e36a..e5adddb 100644 --- a/inc/gallery/model/SoundManager.h +++ b/inc/gallery/model/SoundManager.h @@ -43,7 +43,7 @@ namespace gallery { void delMediaVolumeChangeHandler(const NotiHandler &handler); private: - friend class ucl::RefCountObj<SoundManager>; + friend class ucl::ReffedObj<SoundManager>; SoundManager(); ~SoundManager(); diff --git a/inc/gallery/presenters/AlertDialog.h b/inc/gallery/presenters/AlertDialog.h index 59274a2..ce508ff 100644 --- a/inc/gallery/presenters/AlertDialog.h +++ b/inc/gallery/presenters/AlertDialog.h @@ -66,8 +66,8 @@ namespace gallery { virtual bool isDisposed() const final override; private: - friend class ucl::RefCountObj<AlertDialog>; - AlertDialog(ucl::RefCountObjBase &rc, + friend class ucl::ReffedObj<AlertDialog>; + AlertDialog(ucl::IRefCountObj &rc, const EventHandler &handler); virtual ~AlertDialog(); diff --git a/inc/gallery/presenters/AtspiHighlightHelper.h b/inc/gallery/presenters/AtspiHighlightHelper.h index b84471e..ca908e0 100644 --- a/inc/gallery/presenters/AtspiHighlightHelper.h +++ b/inc/gallery/presenters/AtspiHighlightHelper.h @@ -35,8 +35,8 @@ namespace gallery { void registerWidget(ucl::ElmWidget &widget); private: - friend class ucl::RefCountObj<AtspiHighlightHelper>; - AtspiHighlightHelper(ucl::RefCountObjBase &rc); + friend class ucl::ReffedObj<AtspiHighlightHelper>; + AtspiHighlightHelper(ucl::IRefCountObj &rc); virtual ~AtspiHighlightHelper(); ucl::Result prepare(ucl::ElmWidget &rootWidget); diff --git a/inc/gallery/presenters/Instance.h b/inc/gallery/presenters/Instance.h index 2fee458..bdc4fa6 100644 --- a/inc/gallery/presenters/Instance.h +++ b/inc/gallery/presenters/Instance.h @@ -33,7 +33,7 @@ namespace gallery { public ucl::IInstance, public ucl::IInstanceAppControlExt { public: - Instance(ucl::RefCountObjBase &rc, + Instance(ucl::IRefCountObj &rc, ucl::SysEventProvider &sysEventProvider); virtual ~Instance(); diff --git a/inc/gallery/presenters/MoreOptionsPresenter.h b/inc/gallery/presenters/MoreOptionsPresenter.h index c959b3c..de89f1a 100644 --- a/inc/gallery/presenters/MoreOptionsPresenter.h +++ b/inc/gallery/presenters/MoreOptionsPresenter.h @@ -76,8 +76,8 @@ namespace gallery { using MoreOptionsCSRef = ucl::SharedRef<const MoreOptions>; private: - friend class ucl::RefCountObj<MoreOptionsPresenter>; - MoreOptionsPresenter(ucl::RefCountObjBase &rc, + friend class ucl::ReffedObj<MoreOptionsPresenter>; + MoreOptionsPresenter(ucl::IRefCountObj &rc, const MoreOptionsCSRef &options); virtual ~MoreOptionsPresenter(); diff --git a/inc/gallery/presenters/NoContentPage.h b/inc/gallery/presenters/NoContentPage.h index b02900d..c61fbf2 100644 --- a/inc/gallery/presenters/NoContentPage.h +++ b/inc/gallery/presenters/NoContentPage.h @@ -37,8 +37,8 @@ namespace gallery { }; private: - friend class ucl::RefCountObj<NoContentPage>; - NoContentPage(ucl::RefCountObjBase &rc, const ucl::NaviframeSRef &navi, + friend class ucl::ReffedObj<NoContentPage>; + NoContentPage(ucl::IRefCountObj &rc, const ucl::NaviframeSRef &navi, const ExitRequestHandler &onExitRequest); virtual ~NoContentPage(); diff --git a/inc/gallery/presenters/Page.h b/inc/gallery/presenters/Page.h index 4be629e..c1a1b29 100644 --- a/inc/gallery/presenters/Page.h +++ b/inc/gallery/presenters/Page.h @@ -49,7 +49,7 @@ namespace gallery { ucl::NaviItem insertBefore(ARGS &&...args); protected: - Page(ucl::RefCountObjBase &rc, const ucl::NaviframeSRef &navi, + Page(ucl::IRefCountObj &rc, const ucl::NaviframeSRef &navi, const ExitRequestHandler &onExitRequest); virtual ~Page(); diff --git a/inc/gallery/presenters/Presenter.h b/inc/gallery/presenters/Presenter.h index 4ed3530..1142452 100644 --- a/inc/gallery/presenters/Presenter.h +++ b/inc/gallery/presenters/Presenter.h @@ -42,7 +42,7 @@ namespace gallery { void deactivateBy(void *deactivator); protected: - Presenter(ucl::RefCountObjBase &rc); + Presenter(ucl::IRefCountObj &rc); virtual ~Presenter(); ucl::Result prepare(ucl::ElmWidget &widget); diff --git a/inc/gallery/presenters/PreviewPage.h b/inc/gallery/presenters/PreviewPage.h index a93691b..c499db6 100644 --- a/inc/gallery/presenters/PreviewPage.h +++ b/inc/gallery/presenters/PreviewPage.h @@ -62,8 +62,8 @@ namespace gallery { int getCurrentItemIndex() const; private: - friend class ucl::RefCountObj<PreviewPage>; - PreviewPage(ucl::RefCountObjBase &rc, const ucl::NaviframeSRef &navi, + friend class ucl::ReffedObj<PreviewPage>; + PreviewPage(ucl::IRefCountObj &rc, const ucl::NaviframeSRef &navi, const ExitRequestHandler &onExitRequest, const IMediaAlbumSRef &album, bool selectModeStartup); virtual ~PreviewPage(); diff --git a/inc/gallery/presenters/ProcessingPresenter.h b/inc/gallery/presenters/ProcessingPresenter.h index 59ff50f..94e1e80 100644 --- a/inc/gallery/presenters/ProcessingPresenter.h +++ b/inc/gallery/presenters/ProcessingPresenter.h @@ -53,8 +53,8 @@ namespace gallery { void setDismissHandler(const DismissHandler &handler); private: - friend class ucl::RefCountObj<ProcessingPresenter>; - ProcessingPresenter(ucl::RefCountObjBase &rc); + friend class ucl::ReffedObj<ProcessingPresenter>; + ProcessingPresenter(ucl::IRefCountObj &rc); virtual ~ProcessingPresenter(); ucl::Result prepare(ucl::ElmWidget &parent, diff --git a/inc/gallery/presenters/SelectModePresenter.h b/inc/gallery/presenters/SelectModePresenter.h index 72c014f..2ba0a56 100644 --- a/inc/gallery/presenters/SelectModePresenter.h +++ b/inc/gallery/presenters/SelectModePresenter.h @@ -72,8 +72,8 @@ namespace gallery { void update(int selectCount, int totalCount = -1); private: - friend class ucl::RefCountObj<SelectModePresenter>; - SelectModePresenter(ucl::RefCountObjBase &rc, + friend class ucl::ReffedObj<SelectModePresenter>; + SelectModePresenter(ucl::IRefCountObj &rc, PageContent &content, int flags); virtual ~SelectModePresenter(); diff --git a/inc/gallery/presenters/ThumbnailPage.h b/inc/gallery/presenters/ThumbnailPage.h index ccfe14e..914d759 100644 --- a/inc/gallery/presenters/ThumbnailPage.h +++ b/inc/gallery/presenters/ThumbnailPage.h @@ -49,8 +49,8 @@ namespace gallery { }; private: - friend class ucl::RefCountObj<ThumbnailPage>; - ThumbnailPage(ucl::RefCountObjBase &rc, const ucl::NaviframeSRef &navi, + friend class ucl::ReffedObj<ThumbnailPage>; + ThumbnailPage(ucl::IRefCountObj &rc, const ucl::NaviframeSRef &navi, const ExitRequestHandler &onExitRequest, const IMediaAlbumSRef &album); virtual ~ThumbnailPage(); diff --git a/inc/gallery/presenters/VideoPlayerPage.h b/inc/gallery/presenters/VideoPlayerPage.h index 64732fa..9a61a4e 100644 --- a/inc/gallery/presenters/VideoPlayerPage.h +++ b/inc/gallery/presenters/VideoPlayerPage.h @@ -48,8 +48,8 @@ namespace gallery { }; private: - friend class ucl::RefCountObj<VideoPlayerPage>; - VideoPlayerPage(ucl::RefCountObjBase &rc, + friend class ucl::ReffedObj<VideoPlayerPage>; + VideoPlayerPage(ucl::IRefCountObj &rc, const ucl::NaviframeSRef &navi, const ExitRequestHandler &onExitRequest, const MediaItemSRef &media); diff --git a/inc/gallery/presenters/ViewerPage.h b/inc/gallery/presenters/ViewerPage.h index 6cb80ad..82ecc92 100644 --- a/inc/gallery/presenters/ViewerPage.h +++ b/inc/gallery/presenters/ViewerPage.h @@ -60,8 +60,8 @@ namespace gallery { const std::string &getMediaId() const; private: - friend class ucl::RefCountObj<ViewerPage>; - ViewerPage(ucl::RefCountObjBase &rc, const ucl::NaviframeSRef &navi, + friend class ucl::ReffedObj<ViewerPage>; + ViewerPage(ucl::IRefCountObj &rc, const ucl::NaviframeSRef &navi, const ExitRequestHandler &onExitRequest, const MediaItemSRef &media, bool exitOnZoomOut); virtual ~ViewerPage(); diff --git a/inc/gallery/view/ImageGrid.h b/inc/gallery/view/ImageGrid.h index 31aa0a5..e21a367 100644 --- a/inc/gallery/view/ImageGrid.h +++ b/inc/gallery/view/ImageGrid.h @@ -129,8 +129,8 @@ namespace gallery { struct LinearInfo; private: - friend class ucl::RefCountObj<ImageGrid>; - ImageGrid(ucl::RefCountObjBase *rc, Evas_Object *scroller, + friend class ucl::ReffedObj<ImageGrid>; + ImageGrid(ucl::IRefCountObj *rc, Evas_Object *scroller, Type type, bool selectModeStartup); virtual ~ImageGrid(); @@ -243,7 +243,7 @@ namespace gallery { private: const Info &m_info; - ucl::StyledWidgetWRef m_scroller; + ucl::StyledWidget *m_scroller; ucl::ElmWidget m_box; ucl::Widget m_rect1; ucl::Widget m_rect2; diff --git a/inc/gallery/view/ImageViewer.h b/inc/gallery/view/ImageViewer.h index 5b6f215..9b6e5d8 100644 --- a/inc/gallery/view/ImageViewer.h +++ b/inc/gallery/view/ImageViewer.h @@ -53,8 +53,8 @@ namespace gallery { bool isZoomedOut() const; private: - friend class ucl::RefCountObj<ImageViewer>; - ImageViewer(ucl::RefCountObjBase &rc, Evas_Object *scroller, + friend class ucl::ReffedObj<ImageViewer>; + ImageViewer(ucl::IRefCountObj &rc, Evas_Object *scroller, const std::string &highResPath, int loadSize, bool forceLoad); virtual ~ImageViewer(); @@ -85,7 +85,7 @@ namespace gallery { }; private: - ucl::StyledWidgetWRef m_scroller; + ucl::StyledWidget *m_scroller; ucl::Layout m_layout; ucl::Widget m_grid; ucl::Widget m_lowResImage; diff --git a/inc/gallery/view/PageContent.h b/inc/gallery/view/PageContent.h index 9e581b4..194c97b 100644 --- a/inc/gallery/view/PageContent.h +++ b/inc/gallery/view/PageContent.h @@ -63,8 +63,8 @@ namespace gallery { ucl::Result setMoreOptionsVisible(bool visible); private: - friend class ucl::RefCountObj<PageContent>; - PageContent(ucl::RefCountObjBase &rc, + friend class ucl::ReffedObj<PageContent>; + PageContent(ucl::IRefCountObj &rc, const ucl::LayoutSRef &layout, int flags); virtual ~PageContent(); @@ -76,7 +76,7 @@ namespace gallery { ucl::Layout *getTopLayout() const; private: - ucl::LayoutWRef m_moreOptions; + ucl::Layout *m_mainLayout; ucl::LayoutSRef m_selectMode; ucl::LayoutSRef m_bottomButton; }; diff --git a/inc/gallery/view/TouchParser.h b/inc/gallery/view/TouchParser.h index 484fb84..be391a3 100644 --- a/inc/gallery/view/TouchParser.h +++ b/inc/gallery/view/TouchParser.h @@ -30,8 +30,8 @@ namespace gallery { using TapHandler = ucl::WeakDelegate<void(int x, int y)>; private: - friend class ucl::RefCountObj<TouchParser>; - TouchParser(ucl::RefCountObjBase &rc, ucl::Widget &eventSource); + friend class ucl::ReffedObj<TouchParser>; + TouchParser(ucl::IRefCountObj &rc, ucl::Widget &eventSource); virtual ~TouchParser(); public: diff --git a/src/common.h b/src/common.h index d6d7bae..43890c0 100644 --- a/src/common.h +++ b/src/common.h @@ -46,8 +46,8 @@ namespace gallery { using ucl::isNotEmpty; using ucl::toEina; - using ucl::RefCountObj; - using ucl::RefCountObjBase; + using ucl::ReffedObj; + using ucl::IRefCountObj; using ucl::SharedRef; using ucl::WeakRef; diff --git a/src/model/GalleryAlbum.h b/src/model/GalleryAlbum.h index 57f7e48..715b708 100644 --- a/src/model/GalleryAlbum.h +++ b/src/model/GalleryAlbum.h @@ -47,7 +47,7 @@ namespace gallery { virtual void defragment() final override; private: - friend class ucl::RefCountObj<GalleryAlbum>; + friend class ucl::ReffedObj<GalleryAlbum>; GalleryAlbum(); ucl::Result prepare(); diff --git a/src/model/MediaItem.cpp b/src/model/MediaItem.cpp index ac8c3ef..a0db727 100644 --- a/src/model/MediaItem.cpp +++ b/src/model/MediaItem.cpp @@ -334,7 +334,10 @@ namespace gallery { if (!(m_flags & FLAG_REMOVE)) { LOG_RETURN(RES_NOT_SUPPORTED, "Operation not supported!"); } - if (!ecore_file_can_write(m_filePath.c_str())) { + + const bool exists = ecore_file_exists(m_filePath.c_str()); + + if (exists && !ecore_file_can_write(m_filePath.c_str())) { LOG_RETURN(RES_FAIL, "File can't be removed!"); } @@ -350,7 +353,7 @@ namespace gallery { m_isValid = false; } - if (!ecore_file_remove(m_filePath.c_str())) { + if (exists && !ecore_file_remove(m_filePath.c_str())) { FLOG("ecore_file_remove() failed! Attempting to rescan...."); MutexLock lock(getMediaMutex()); const int ret = media_content_scan_file(m_filePath.c_str()); diff --git a/src/presenters/AlertDialog.cpp b/src/presenters/AlertDialog.cpp index 57b06d6..da1ebe1 100644 --- a/src/presenters/AlertDialog.cpp +++ b/src/presenters/AlertDialog.cpp @@ -108,7 +108,7 @@ namespace gallery { // AlertDialog // - AlertDialog::AlertDialog(RefCountObjBase &rc, + AlertDialog::AlertDialog(IRefCountObj &rc, const EventHandler &handler) : Presenter(rc), m_handler(handler), @@ -170,14 +170,16 @@ namespace gallery { Result AlertDialog::createLayout(const LayoutTheme theme) { - m_layout = Layout::Builder(). + const auto layout = Layout::Builder(). setTheme(theme). build(*m_popup); - if (!m_layout) { + if (!layout) { LOG_RETURN(RES_FAIL, "Layout::build() failed!"); } - m_popup->setContent(*m_layout); + m_popup->setContent(*layout); + + m_layout = layout; return RES_OK; } @@ -215,15 +217,15 @@ namespace gallery { void AlertDialog::setTitle(const TString &title) { - if (m_layout) { - m_layout->setText(title, PART_TITLE); + if (const auto layout = m_layout.lock()) { + layout->setText(title, PART_TITLE); } } void AlertDialog::setText(const TString &text) { - if (m_layout) { - m_layout->setText(text); + if (const auto layout = m_layout.lock()) { + layout->setText(text); } } @@ -256,7 +258,6 @@ namespace gallery { void AlertDialog::handleEvent(const Event event) { - const auto keepAliver = asShared(*this); if (dispatchEvent(event)) { dismiss(); } diff --git a/src/presenters/AtspiHighlightHelper.cpp b/src/presenters/AtspiHighlightHelper.cpp index 08cd14e..ad589c6 100644 --- a/src/presenters/AtspiHighlightHelper.cpp +++ b/src/presenters/AtspiHighlightHelper.cpp @@ -40,7 +40,7 @@ namespace gallery { return result; } - AtspiHighlightHelper::AtspiHighlightHelper(RefCountObjBase &rc) : + AtspiHighlightHelper::AtspiHighlightHelper(IRefCountObj &rc) : Presenter(rc) { } diff --git a/src/presenters/Instance.cpp b/src/presenters/Instance.cpp index fbefd8e..2824d4e 100644 --- a/src/presenters/Instance.cpp +++ b/src/presenters/Instance.cpp @@ -45,7 +45,7 @@ namespace gallery { using ucl::Theme; using ucl::Naviframe; - Instance::Instance(RefCountObjBase &rc, + Instance::Instance(IRefCountObj &rc, SysEventProvider &sysEventProvider) : RefCountAware(&rc), m_sysEventProvider(sysEventProvider), @@ -58,8 +58,8 @@ namespace gallery { Instance::~Instance() { stopMediaContentScan(); - if (m_page) { - m_page->exitNoTransition(); + if (const auto page = m_page.lock()) { + page->exitNoTransition(); } } @@ -208,14 +208,14 @@ namespace gallery { { FAIL_RETURN(ensureGalleryModel(), "ensureGalleryModel() failed!"); - if (!m_page) { - if (isEmpty(m_gallery->getAlbum())) { - createNoContentPage(); - } else { - createThumbnailPage(); + if (const auto page = m_page.lock()) { + if (operation == APP_CONTROL_OPERATION_MAIN) { + page->deleteTo(); } - } else if (operation == APP_CONTROL_OPERATION_MAIN) { - m_page->deleteTo(); + } else if (isEmpty(m_gallery->getAlbum())) { + createNoContentPage(); + } else { + createThumbnailPage(); } return RES_OK; @@ -319,13 +319,13 @@ namespace gallery { void Instance::onAlbumChanged() { if (isEmpty(m_gallery->getAlbum())) { - if (dynamic_cast<ThumbnailPage *>(m_page.get())) { - m_page->exitNoTransition(); + if (auto page = dynamicRefCast<ThumbnailPage>(m_page).lock()) { + page->exitNoTransition(); createNoContentPage(); } } else { - if (dynamic_cast<NoContentPage *>(m_page.get())) { - m_page->exitNoTransition(); + if (auto page = dynamicRefCast<NoContentPage>(m_page).lock()) { + page->exitNoTransition(); createThumbnailPage(); } } diff --git a/src/presenters/MoreOptionsPresenter.cpp b/src/presenters/MoreOptionsPresenter.cpp index f0d902f..15492dc 100644 --- a/src/presenters/MoreOptionsPresenter.cpp +++ b/src/presenters/MoreOptionsPresenter.cpp @@ -99,7 +99,7 @@ namespace gallery { // MoreOptionsPresenter // - MoreOptionsPresenter::MoreOptionsPresenter(RefCountObjBase &rc, + MoreOptionsPresenter::MoreOptionsPresenter(IRefCountObj &rc, const MoreOptionsCSRef &options) : Presenter(rc), m_options(options), @@ -207,11 +207,10 @@ namespace gallery { void MoreOptionsPresenter::onOpened(Widget &widget, void *eventInfo) { stopTimer(); - const auto keepAliver = asShared(*this); sendDeactivateBy(*m_widget, this); activateBy(m_widget.get()); - if (m_listener) { - m_listener->onMoreOptionsOpened(*this); + if (const auto listener = m_listener.lock()) { + listener->onMoreOptionsOpened(*this); } if (!isActive()) { setOpened(false); @@ -221,20 +220,22 @@ namespace gallery { void MoreOptionsPresenter::onClosed(Widget &widget, void *eventInfo) { stopTimer(); - const auto keepAliver = asShared(*this); deactivateBy(m_widget.get()); sendActivateBy(*m_widget, this); - if (m_listener) { - m_listener->onMoreOptionsClosed(*this); + if (const auto listener = m_listener.lock()) { + listener->onMoreOptionsClosed(*this); } } void MoreOptionsPresenter::onItemClicked(Widget &widget, void *eventInfo) { - if (m_listener && isActive() && isOpened()) { + if (!isActive() || !isOpened()) { + return; + } + if (const auto listener = m_listener.lock()) { const auto item = m_map.get(eventInfo); if (item) { - m_listener->onMoreOptionClicked(*this, *item); + listener->onMoreOptionClicked(*this, *item); } else { ELOG("Invalid eventInfo!"); } @@ -243,10 +244,10 @@ namespace gallery { void MoreOptionsPresenter::onItemSelected(Widget &widget, void *eventInfo) { - if (m_listener) { + if (const auto listener = m_listener.lock()) { const auto item = m_map.get(eventInfo); if (item) { - m_listener->onMoreOptionSelected(*this, *item); + listener->onMoreOptionSelected(*this, *item); } else { ELOG("Invalid eventInfo!"); } diff --git a/src/presenters/NoContentPage.cpp b/src/presenters/NoContentPage.cpp index db5c2d6..5dc69b3 100644 --- a/src/presenters/NoContentPage.cpp +++ b/src/presenters/NoContentPage.cpp @@ -67,7 +67,7 @@ namespace gallery { // NoContentPage // - NoContentPage::NoContentPage(RefCountObjBase &rc, + NoContentPage::NoContentPage(IRefCountObj &rc, const NaviframeSRef &navi, const ExitRequestHandler &onExitRequest) : Page(rc, navi, onExitRequest) diff --git a/src/presenters/Page.cpp b/src/presenters/Page.cpp index 39a6930..43ed768 100644 --- a/src/presenters/Page.cpp +++ b/src/presenters/Page.cpp @@ -31,7 +31,7 @@ namespace gallery { using ucl::NAVI_TRANSITION_STARTED; using ucl::NAVI_TRANSITION_FINISHED; - Page::Page(RefCountObjBase &rc, const NaviframeSRef &navi, + Page::Page(IRefCountObj &rc, const NaviframeSRef &navi, const ExitRequestHandler &onExitRequest) : Presenter(rc), m_navi(navi), diff --git a/src/presenters/Presenter.cpp b/src/presenters/Presenter.cpp index 13f2856..dcbd7d9 100644 --- a/src/presenters/Presenter.cpp +++ b/src/presenters/Presenter.cpp @@ -28,7 +28,7 @@ namespace gallery { using ucl::Window; - Presenter::Presenter(RefCountObjBase &rc) : + Presenter::Presenter(IRefCountObj &rc) : RefCountAware(&rc), m_isPrepared(false) { diff --git a/src/presenters/PreviewPage.cpp b/src/presenters/PreviewPage.cpp index 05f3f12..b91c7fe 100644 --- a/src/presenters/PreviewPage.cpp +++ b/src/presenters/PreviewPage.cpp @@ -118,7 +118,7 @@ namespace gallery { class PreviewPage::Item : public RefCountAware { public: - Item(RefCountObjBase &rc, MediaItemSRef &&media, + Item(IRefCountObj &rc, MediaItemSRef &&media, ImageGrid &imageGrid, const int itemIndex) : RefCountAware(&rc), m_media(std::move(media)), @@ -199,7 +199,7 @@ namespace gallery { // PreviewPage // - PreviewPage::PreviewPage(RefCountObjBase &rc, + PreviewPage::PreviewPage(IRefCountObj &rc, const NaviframeSRef &navi, const ExitRequestHandler &onExitRequest, const IMediaAlbumSRef &album, @@ -215,9 +215,13 @@ namespace gallery { PreviewPage::~PreviewPage() { + if (m_album) { + m_album->delChangeHandler(WEAK_DELEGATE( + PreviewPage::onAlbumChanged, asWeak(*this))); + } closeTempViews(); - if (m_page) { - m_page->exitNoTransition(); + if (const auto page = m_page.lock()) { + page->exitNoTransition(); } if (m_imageGrid) { m_imageGrid->setListener(nullptr); @@ -253,9 +257,8 @@ namespace gallery { void PreviewPage::checkViewerPage() { - if (const ViewerPageSRef viewerPage = - dynamicRefCast<ViewerPage>(m_page)) { - const auto mediaId = viewerPage->getMediaId(); + if (const auto page = dynamicRefCast<ViewerPage>(m_page).lock()) { + const auto mediaId = page->getMediaId(); const auto it = std::find_if(m_items.begin(), m_items.end(), [&mediaId](const ItemSRef &item) { @@ -645,8 +648,8 @@ namespace gallery { void PreviewPage::closeTempViews() { - if (m_alert) { - m_alert->dispose(); + if (const auto alert = m_alert.lock()) { + alert->dispose(); } if (m_more) { m_more->setOpened(false); diff --git a/src/presenters/ProcessingPresenter.cpp b/src/presenters/ProcessingPresenter.cpp index 90e679e..63bf0cd 100644 --- a/src/presenters/ProcessingPresenter.cpp +++ b/src/presenters/ProcessingPresenter.cpp @@ -86,7 +86,7 @@ namespace gallery { // ProcessingPresenter // - ProcessingPresenter::ProcessingPresenter(RefCountObjBase &rc) : + ProcessingPresenter::ProcessingPresenter(IRefCountObj &rc) : Presenter(rc), m_iconType(IconType::NONE), m_timer(nullptr), @@ -202,14 +202,16 @@ namespace gallery { Result ProcessingPresenter::createIcon() { - m_icon = Layout::Builder(). + const auto icon = Layout::Builder(). setTheme(impl::LAYOUT_POPUP_ICON). build(*m_popup); - if (!m_icon) { + if (!icon) { LOG_RETURN(RES_FAIL, "Layout::build() failed!"); } - m_popup->setContent(*m_icon, impl::PART_TOAST_ICON); + m_popup->setContent(*icon, impl::PART_TOAST_ICON); + + m_icon = icon; return RES_OK; } @@ -283,9 +285,9 @@ namespace gallery { void ProcessingPresenter::animateIcon() { - if (m_icon) { + if (const auto icon = m_icon.lock()) { if (m_iconType == IconType::CHECK) { - m_icon->emit(impl::SIGNAL_ANIMATE_CHECK); + icon->emit(impl::SIGNAL_ANIMATE_CHECK); } } } @@ -321,7 +323,6 @@ namespace gallery { void ProcessingPresenter::onPopupDismissed(Widget &widget, void *eventInfo) { - const auto keepAliver = asShared(*this); if (m_dismissHandler) { m_dismissHandler(); } diff --git a/src/presenters/SelectModePresenter.cpp b/src/presenters/SelectModePresenter.cpp index d0fec2c..c3e0c9c 100644 --- a/src/presenters/SelectModePresenter.cpp +++ b/src/presenters/SelectModePresenter.cpp @@ -65,7 +65,7 @@ namespace gallery { // SelectModePresenter // - SelectModePresenter::SelectModePresenter(RefCountObjBase &rc, + SelectModePresenter::SelectModePresenter(IRefCountObj &rc, PageContent &content, const int flags) : Presenter(rc), m_content(asShared(content)), @@ -316,8 +316,8 @@ namespace gallery { void SelectModePresenter::dispatchEvent(const Event event) { - if (m_listener) { - m_listener->onSelectModeEvent(event); + if (const auto listener = m_listener.lock()) { + listener->onSelectModeEvent(event); } } diff --git a/src/presenters/ThumbnailPage.cpp b/src/presenters/ThumbnailPage.cpp index 65e51f3..c6defbb 100644 --- a/src/presenters/ThumbnailPage.cpp +++ b/src/presenters/ThumbnailPage.cpp @@ -89,7 +89,7 @@ namespace gallery { class ThumbnailPage::RealizedItem : public RefCountAware { public: - RealizedItem(RefCountObjBase &rc, + RealizedItem(IRefCountObj &rc, ThumbnailPage &parent, const int index) : RefCountAware(&rc), m_parent(parent), @@ -130,7 +130,7 @@ namespace gallery { // ThumbnailPage // - ThumbnailPage::ThumbnailPage(RefCountObjBase &rc, + ThumbnailPage::ThumbnailPage(IRefCountObj &rc, const NaviframeSRef &navi, const ExitRequestHandler &onExitRequest, const IMediaAlbumSRef &album) : @@ -141,11 +141,15 @@ namespace gallery { ThumbnailPage::~ThumbnailPage() { + if (m_album) { + m_album->delChangeHandler(WEAK_DELEGATE( + ThumbnailPage::onAlbumChanged, asWeak(*this))); + } if (m_more) { m_more->setOpened(false); } - if (m_page) { - m_page->exitNoTransition(); + if (const auto page = m_page.lock()) { + page->exitNoTransition(); } if (m_imageGrid) { m_imageGrid->setListener(nullptr); @@ -348,10 +352,9 @@ namespace gallery { void ThumbnailPage::onPageExitRequest(Page &page) { - if (const PreviewPageSRef previewPage = - dynamicRefCast<PreviewPage>(m_page)) { + if (const auto page = dynamicRefCast<PreviewPage>(m_page).lock()) { m_imageGrid->scrollToItem(getSafeItemIndex( - previewPage->getCurrentItemIndex())); + page->getCurrentItemIndex())); } m_page.reset(); popTo(); diff --git a/src/presenters/VideoPlayerPage.cpp b/src/presenters/VideoPlayerPage.cpp index 96c384d..de3dddb 100644 --- a/src/presenters/VideoPlayerPage.cpp +++ b/src/presenters/VideoPlayerPage.cpp @@ -133,7 +133,7 @@ namespace gallery { // VideoPlayerPage // - VideoPlayerPage::VideoPlayerPage(RefCountObjBase &rc, + VideoPlayerPage::VideoPlayerPage(IRefCountObj &rc, const NaviframeSRef &navi, const ExitRequestHandler &onExitRequest, const MediaItemSRef &media) : @@ -153,6 +153,13 @@ namespace gallery { VideoPlayerPage::~VideoPlayerPage() { + if (m_soundMgr) { + m_soundMgr->delMediaDeviceStateChangeHandler(WEAK_DELEGATE( + VideoPlayerPage::onMediaDeviceStateChanged, asWeak(*this))); + m_soundMgr->delMediaVolumeChangeHandler(WEAK_DELEGATE( + VideoPlayerPage::onMediaVolumeChanged, asWeak(*this))); + } + if (isWindowReady()) { setScreenAlwaysOn(false); } diff --git a/src/presenters/ViewerPage.cpp b/src/presenters/ViewerPage.cpp index e8a7d85..3dbd236 100644 --- a/src/presenters/ViewerPage.cpp +++ b/src/presenters/ViewerPage.cpp @@ -114,7 +114,7 @@ namespace gallery { // ViewerPage // - ViewerPage::ViewerPage(RefCountObjBase &rc, + ViewerPage::ViewerPage(IRefCountObj &rc, const NaviframeSRef &navi, const ExitRequestHandler &onExitRequest, const MediaItemSRef &media, diff --git a/src/view/ImageGrid.cpp b/src/view/ImageGrid.cpp index ece8828..a2725b9 100644 --- a/src/view/ImageGrid.cpp +++ b/src/view/ImageGrid.cpp @@ -249,8 +249,8 @@ namespace gallery { private: class Item : public RefCountAware { public: - friend class RefCountObj<Item>; - Item(RefCountObjBase &rc, + friend class ReffedObj<Item>; + Item(IRefCountObj &rc, ImageGrid &imageGrid, ElmWidget &parent) : RefCountAware(&rc), m_imageGrid(imageGrid), @@ -330,8 +330,8 @@ namespace gallery { if (itemIndex < m_imageGrid.m_itemCount) { m_realizeIndex = itemIndex; - if (m_imageGrid.m_listener) { - m_imageGrid.m_listener->onItemRealized(itemIndex); + if (const auto listener = m_imageGrid.m_listener.lock()) { + listener->onItemRealized(itemIndex); } } @@ -355,8 +355,8 @@ namespace gallery { const int itemIndex = m_realizeIndex; m_realizeIndex = -1; - if (m_imageGrid.m_listener) { - m_imageGrid.m_listener->onItemUnrealized(itemIndex); + if (const auto listener = m_imageGrid.m_listener.lock()) { + listener->onItemUnrealized(itemIndex); } } @@ -743,12 +743,12 @@ namespace gallery { // ImageGrid // - ImageGrid::ImageGrid(RefCountObjBase *const rc, Evas_Object *const scroller, + ImageGrid::ImageGrid(IRefCountObj *const rc, Evas_Object *const scroller, const Type type, const bool selectModeStartup) : ElmWidget(rc, scroller, true), m_info(getInfo(type)), - m_scroller(makeShared<StyledWidget>(scroller)), + m_scroller(makeShared<StyledWidget>(scroller).get()), m_box(elm_box_add(*m_scroller)), m_rect1(evas_object_rectangle_add(m_box.getEvas())), m_rect2(evas_object_rectangle_add(m_box.getEvas())), @@ -1009,8 +1009,8 @@ namespace gallery { eext_rotary_object_event_activated_set(m_circleScroller, EINA_TRUE); } - if (m_listener) { - m_listener->onTransitionFinished(); + if (const auto listener = m_listener.lock()) { + listener->onTransitionFinished(); } } @@ -1283,8 +1283,8 @@ namespace gallery { void ImageGrid::handleItemEvent(const int itemIndex, const ItemEvent event, const int x, const int y) const { - if (m_listener) { - m_listener->onItemEvent(itemIndex, event, x, y); + if (const auto listener = m_listener.lock()) { + listener->onItemEvent(itemIndex, event, x, y); } } @@ -1366,8 +1366,8 @@ namespace gallery { Elm_Interface_Atspi_Accessible *ImageGrid::requestAtspi(const int itemIndex) { if ((itemIndex < 0) || (itemIndex >= m_itemCount)) { - if (m_listener) { - return m_listener->onAccessObjectRequest( + if (const auto listener = m_listener.lock()) { + return listener->onAccessObjectRequest( (itemIndex >= m_itemCount)); } return nullptr; diff --git a/src/view/ImageViewer.cpp b/src/view/ImageViewer.cpp index 606e376..6a53008 100644 --- a/src/view/ImageViewer.cpp +++ b/src/view/ImageViewer.cpp @@ -74,12 +74,12 @@ namespace gallery { // ImageViewer // - ImageViewer::ImageViewer(RefCountObjBase &rc, Evas_Object *const scroller, + ImageViewer::ImageViewer(IRefCountObj &rc, Evas_Object *const scroller, const std::string &highResPath, const int loadSize, const bool forceLoad) : ElmWidget(&rc, scroller, true), - m_scroller(makeShared<StyledWidget>(scroller)), + m_scroller(makeShared<StyledWidget>(scroller).get()), m_layout(elm_layout_add(*m_scroller)), m_grid(evas_object_grid_add(m_layout.getEvas())), m_lowResImage(evas_object_image_filled_add(m_grid.getEvas())), diff --git a/src/view/PageContent.cpp b/src/view/PageContent.cpp index 21d601d..32db59e 100644 --- a/src/view/PageContent.cpp +++ b/src/view/PageContent.cpp @@ -91,10 +91,10 @@ namespace gallery { // PageContent // - PageContent::PageContent(RefCountObjBase &rc, + PageContent::PageContent(IRefCountObj &rc, const LayoutSRef &layout, const int flags) : ElmWidget(&rc, *layout, true), - m_moreOptions(layout) + m_mainLayout(layout.get()) { prepare(flags); } @@ -105,16 +105,16 @@ namespace gallery { void PageContent::prepare(const int flags) { - m_moreOptions->setIsOwner(false); + m_mainLayout->setIsOwner(false); - LayoutSRef parent = m_moreOptions; + Layout *parent = m_mainLayout; if (flags & FLAG_SELECT_BUTTON) { m_selectMode = Layout::Builder(). setTheme(impl::LAYOUT_SELECT_MODE). build(*parent); parent->setContent(*m_selectMode); - parent = m_selectMode; + parent = m_selectMode.get(); } if (flags & FLAG_BOTTOM_BUTTON) { @@ -122,7 +122,7 @@ namespace gallery { setTheme(impl::LAYOUT_BOTTOM_BUTTON). build(*parent); parent->setContent(*m_bottomButton); - parent = m_bottomButton; + parent = m_bottomButton.get(); } } @@ -169,10 +169,7 @@ namespace gallery { Result PageContent::setMoreOptionsVisible(const bool visible) { - if (!m_moreOptions) { - LOG_RETURN(RES_FAIL, "More options is not supported!"); - } - const auto content = m_moreOptions->getContent(impl::PART_MORE_OPTIONS); + const auto content = m_mainLayout->getContent(impl::PART_MORE_OPTIONS); if (!content) { LOG_RETURN(RES_FAIL, "More option is not created!"); } @@ -190,11 +187,11 @@ namespace gallery { case Part::DEFAULT: return impl::callSafe(getTopLayout(), func, PART_CONTENT); case Part::OVERLAY: - return impl::callSafe(m_moreOptions.get(), func, - impl::PART_OVERLAY); + func(*m_mainLayout, impl::PART_OVERLAY); + return RES_OK; case Part::MORE_OPTIONS: - return impl::callSafe(m_moreOptions.get(), func, - impl::PART_MORE_OPTIONS); + func(*m_mainLayout, impl::PART_MORE_OPTIONS); + return RES_OK; case Part::SELECT_BUTTON: return impl::callSafe(m_selectMode.get(), func, PART_ICON); case Part::BOTTOM_BUTTON: @@ -211,6 +208,6 @@ namespace gallery { if (m_selectMode) { return m_selectMode.get(); } - return m_moreOptions.get(); + return m_mainLayout; } } diff --git a/src/view/TouchParser.cpp b/src/view/TouchParser.cpp index d06d2f1..8678bdb 100644 --- a/src/view/TouchParser.cpp +++ b/src/view/TouchParser.cpp @@ -28,7 +28,7 @@ namespace gallery { namespace { namespace impl { namespace gallery { - TouchParser::TouchParser(RefCountObjBase &rc, Widget &eventSource) : + TouchParser::TouchParser(IRefCountObj &rc, Widget &eventSource) : RefCountAware(&rc), m_holdTimer(nullptr), m_downTime(0), diff --git a/ucl/inc/ucl/gui/EdjeWidget.h b/ucl/inc/ucl/gui/EdjeWidget.h index 8d20b50..0ee57f0 100644 --- a/ucl/inc/ucl/gui/EdjeWidget.h +++ b/ucl/inc/ucl/gui/EdjeWidget.h @@ -43,7 +43,7 @@ namespace ucl { EdjeSignalSrc("")); protected: - EdjeWidget(RefCountObjBase *rc, Evas_Object *eo, bool isOwner = false); + EdjeWidget(IRefCountObj *rc, Evas_Object *eo, bool isOwner = false); }; } diff --git a/ucl/inc/ucl/gui/EdjeWidget.hpp b/ucl/inc/ucl/gui/EdjeWidget.hpp index 5197f59..3f989c7 100644 --- a/ucl/inc/ucl/gui/EdjeWidget.hpp +++ b/ucl/inc/ucl/gui/EdjeWidget.hpp @@ -16,7 +16,7 @@ namespace ucl { - inline EdjeWidget::EdjeWidget(RefCountObjBase *const rc, + inline EdjeWidget::EdjeWidget(IRefCountObj *const rc, Evas_Object *const eo, const bool isOwner) : ElmWidget(rc, eo, isOwner) { diff --git a/ucl/inc/ucl/gui/ElmWidget.h b/ucl/inc/ucl/gui/ElmWidget.h index 96d5cf2..3856b49 100644 --- a/ucl/inc/ucl/gui/ElmWidget.h +++ b/ucl/inc/ucl/gui/ElmWidget.h @@ -47,8 +47,8 @@ namespace ucl { Window *getWindow() const; protected: - friend class RefCountObj<ElmWidget>; - ElmWidget(RefCountObjBase *rc, Evas_Object *eo, bool isOwner = false); + friend class ReffedObj<ElmWidget>; + ElmWidget(IRefCountObj *rc, Evas_Object *eo, bool isOwner = false); virtual void setFocusedImpl(bool value) final override; virtual bool isFocusedImpl() const final override; diff --git a/ucl/inc/ucl/gui/Layout.h b/ucl/inc/ucl/gui/Layout.h index ec21f45..37732ce 100644 --- a/ucl/inc/ucl/gui/Layout.h +++ b/ucl/inc/ucl/gui/Layout.h @@ -42,7 +42,7 @@ namespace ucl { }; public: - friend class RefCountObj<Layout>; + friend class ReffedObj<Layout>; using EdjeWidget::EdjeWidget; explicit Layout(Evas_Object *eo, bool isOwner = false); diff --git a/ucl/inc/ucl/gui/Naviframe.h b/ucl/inc/ucl/gui/Naviframe.h index 0cf36f6..4e4336c 100644 --- a/ucl/inc/ucl/gui/Naviframe.h +++ b/ucl/inc/ucl/gui/Naviframe.h @@ -85,8 +85,8 @@ namespace ucl { std::vector<NaviItem> getItems() const; private: - friend class RefCountObj<Naviframe>; - Naviframe(RefCountObjBase &rc, Evas_Object *eo); + friend class ReffedObj<Naviframe>; + Naviframe(IRefCountObj &rc, Evas_Object *eo); void onTransitionFinished(Widget &widget, void *eventInfo); diff --git a/ucl/inc/ucl/gui/StyledWidget.h b/ucl/inc/ucl/gui/StyledWidget.h index ad995c1..2a0280c 100644 --- a/ucl/inc/ucl/gui/StyledWidget.h +++ b/ucl/inc/ucl/gui/StyledWidget.h @@ -25,7 +25,7 @@ namespace ucl { class StyledWidget : public EdjeWidget { public: - friend class RefCountObj<StyledWidget>; + friend class ReffedObj<StyledWidget>; using EdjeWidget::EdjeWidget; explicit StyledWidget(Evas_Object *eo, bool isOwner = false); diff --git a/ucl/inc/ucl/gui/Widget.h b/ucl/inc/ucl/gui/Widget.h index 8dfcd26..c317962 100644 --- a/ucl/inc/ucl/gui/Widget.h +++ b/ucl/inc/ucl/gui/Widget.h @@ -97,8 +97,8 @@ namespace ucl { bool isFocused() const; protected: - friend class RefCountObj<Widget>; - Widget(RefCountObjBase *rc, Evas_Object *eo, bool isOwner = false); + friend class ReffedObj<Widget>; + Widget(IRefCountObj *rc, Evas_Object *eo, bool isOwner = false); virtual void setFocusedImpl(bool value); virtual bool isFocusedImpl() const; @@ -125,7 +125,7 @@ namespace ucl { protected: // This section MUST be protected! - // Signal to RefCountObj<T> to call onUniqueChanged() + // Signal to RefCountObj<T, C> to call onUniqueChanged() enum { ENABLE_ON_UNIQUE_CHANGED_DISPATCH }; void onUniqueChanged(bool isUnique); diff --git a/ucl/inc/ucl/gui/Window.h b/ucl/inc/ucl/gui/Window.h index 8e526b4..b611b25 100644 --- a/ucl/inc/ucl/gui/Window.h +++ b/ucl/inc/ucl/gui/Window.h @@ -80,8 +80,8 @@ namespace ucl { void lower(); private: - friend class RefCountObj<Window>; - Window(RefCountObjBase *rc, Evas_Object *eo, + friend class ReffedObj<Window>; + Window(IRefCountObj *rc, Evas_Object *eo, bool isOwner, Evas_Object *conform); private: diff --git a/ucl/inc/ucl/gui/Window.hpp b/ucl/inc/ucl/gui/Window.hpp index 3d4c959..d5b1828 100644 --- a/ucl/inc/ucl/gui/Window.hpp +++ b/ucl/inc/ucl/gui/Window.hpp @@ -84,7 +84,7 @@ namespace ucl { // Window // - inline Window::Window(RefCountObjBase *const rc, Evas_Object *const eo, + inline Window::Window(IRefCountObj *const rc, Evas_Object *const eo, const bool isOwner, Evas_Object *const conform) : ElmWidget(rc, eo, isOwner), m_conform(conform) diff --git a/ucl/inc/ucl/misc/Event.hpp b/ucl/inc/ucl/misc/Event.hpp index f0caec0..ed4e402 100644 --- a/ucl/inc/ucl/misc/Event.hpp +++ b/ucl/inc/ucl/misc/Event.hpp @@ -48,10 +48,7 @@ namespace ucl { template <class DELEGATE2> void Event<DELEGATE>::operator+=(DELEGATE2 &&delegate) { - if (delegate && std::find(m_delegates.begin(), m_delegates.end(), - delegate) == m_delegates.end()) { - m_delegates.emplace_back(std::forward<DELEGATE2>(delegate)); - } + m_delegates.emplace_back(std::forward<DELEGATE2>(delegate)); } template <class DELEGATE> @@ -73,7 +70,7 @@ namespace ucl { template <class DELEGATE> bool Event<DELEGATE>::isEmpty() const { - return (isLocked() ? false : m_delegates.empty()); + return m_delegates.empty(); } template <class DELEGATE> diff --git a/ucl/inc/ucl/misc/RefCountAware.h b/ucl/inc/ucl/misc/RefCountAware.h index 7ec13cc..ed05bfe 100644 --- a/ucl/inc/ucl/misc/RefCountAware.h +++ b/ucl/inc/ucl/misc/RefCountAware.h @@ -35,11 +35,14 @@ namespace ucl { WeakRef<T> asWeakThis(T *thisAlias) const; protected: - RefCountAware(RefCountObjBase *rc); + RefCountAware(IRefCountObj *rc); virtual ~RefCountAware() = default; protected: - RefCountObjBase *const m_rc; + enum { IS_REF_COUNT_AWARE }; + + protected: + IRefCountObj *const m_rc; }; // Non-member functions // diff --git a/ucl/inc/ucl/misc/RefCountAware.hpp b/ucl/inc/ucl/misc/RefCountAware.hpp index ae51c0d..3906d0e 100644 --- a/ucl/inc/ucl/misc/RefCountAware.hpp +++ b/ucl/inc/ucl/misc/RefCountAware.hpp @@ -18,7 +18,7 @@ namespace ucl { - inline RefCountAware::RefCountAware(RefCountObjBase *const rc) : + inline RefCountAware::RefCountAware(IRefCountObj *const rc) : m_rc(rc) { } diff --git a/ucl/inc/ucl/misc/Timeout.h b/ucl/inc/ucl/misc/Timeout.h index 707c25c..165265c 100644 --- a/ucl/inc/ucl/misc/Timeout.h +++ b/ucl/inc/ucl/misc/Timeout.h @@ -37,7 +37,7 @@ namespace ucl { bool isExpired() const; private: - friend class RefCountObj<Timeout>; + friend class ReffedObj<Timeout>; Timeout(const TimeoutHandler &handler); ~Timeout(); diff --git a/ucl/inc/ucl/util/delegation/BaseDelegate.h b/ucl/inc/ucl/util/delegation/BaseDelegate.h index d5c518c..62b92ff 100644 --- a/ucl/inc/ucl/util/delegation/BaseDelegate.h +++ b/ucl/inc/ucl/util/delegation/BaseDelegate.h @@ -42,8 +42,6 @@ namespace ucl { template <class FUNC_SIG> BaseDelegate(BaseDelegate2<FUNC_SIG, DATA> &&d) noexcept; - R operator()(ARGS ...args) const; - void reset() noexcept; const DATA &getData() const noexcept; @@ -53,7 +51,7 @@ namespace ucl { protected: BaseDelegate(const DATA &data, StubA stubA) noexcept; - private: + protected: DATA m_data; StubA m_stubA; }; diff --git a/ucl/inc/ucl/util/delegation/BaseDelegate.hpp b/ucl/inc/ucl/util/delegation/BaseDelegate.hpp index 4cb9741..f25c0a7 100644 --- a/ucl/inc/ucl/util/delegation/BaseDelegate.hpp +++ b/ucl/inc/ucl/util/delegation/BaseDelegate.hpp @@ -57,13 +57,6 @@ namespace ucl { } template <class R, class ...ARGS, class DATA> - inline R BaseDelegate<R(ARGS...), DATA>::operator()(ARGS ...args) const - { - return m_stubA( - static_cast<void *>(m_data), std::forward<ARGS>(args)...); - } - - template <class R, class ...ARGS, class DATA> inline void BaseDelegate<R(ARGS...), DATA>::reset() noexcept { *this = {}; diff --git a/ucl/inc/ucl/util/delegation/Delegate.h b/ucl/inc/ucl/util/delegation/Delegate.h index fd53a0a..e1e7416 100644 --- a/ucl/inc/ucl/util/delegation/Delegate.h +++ b/ucl/inc/ucl/util/delegation/Delegate.h @@ -29,6 +29,8 @@ namespace ucl { public: using BaseDelegate<R(ARGS...), void *>::BaseDelegate; + R operator()(ARGS ...args) const; + template <class CLASS, R(CLASS::*METHOD)(ARGS...)> static Delegate make(CLASS *data) noexcept; template <class CLASS, R(CLASS::*METHOD)(ARGS...) const> diff --git a/ucl/inc/ucl/util/delegation/Delegate.hpp b/ucl/inc/ucl/util/delegation/Delegate.hpp index 5f7be3d..632ec24 100644 --- a/ucl/inc/ucl/util/delegation/Delegate.hpp +++ b/ucl/inc/ucl/util/delegation/Delegate.hpp @@ -17,6 +17,12 @@ namespace ucl { template <class R, class ...ARGS> + inline R Delegate<R(ARGS...)>::operator()(ARGS ...args) const + { + return this->m_stubA(this->m_data, std::forward<ARGS>(args)...); + } + + template <class R, class ...ARGS> template <class CLASS, R(CLASS::*METHOD)(ARGS...)> inline Delegate<R(ARGS...)> Delegate<R(ARGS...)>::make(CLASS *const data) noexcept diff --git a/ucl/inc/ucl/util/delegation/Delegate2.h b/ucl/inc/ucl/util/delegation/Delegate2.h index c49d31c..56ff3fb 100644 --- a/ucl/inc/ucl/util/delegation/Delegate2.h +++ b/ucl/inc/ucl/util/delegation/Delegate2.h @@ -29,6 +29,8 @@ namespace ucl { public: using BaseDelegate2<R(ARGS...), void *>::BaseDelegate2; + R operator()(ARGS ...args) const; + template <class CLASS, R(CLASS::*METHOD)(ARGS...)> static Delegate2 make(CLASS *data) noexcept; template <class CLASS, R(CLASS::*METHOD)(ARGS...) const> diff --git a/ucl/inc/ucl/util/delegation/Delegate2.hpp b/ucl/inc/ucl/util/delegation/Delegate2.hpp index 4d77860..085ba46 100644 --- a/ucl/inc/ucl/util/delegation/Delegate2.hpp +++ b/ucl/inc/ucl/util/delegation/Delegate2.hpp @@ -17,6 +17,12 @@ namespace ucl { template <class R, class ...ARGS> + inline R Delegate2<R(ARGS...)>::operator()(ARGS ...args) const + { + return this->m_stubA(this->m_data, std::forward<ARGS>(args)...); + } + + template <class R, class ...ARGS> template <class CLASS, R(CLASS::*METHOD)(ARGS...)> inline Delegate2<R(ARGS...)> Delegate2<R(ARGS...)>::make(CLASS *const data) noexcept diff --git a/ucl/inc/ucl/util/delegation/helpers.h b/ucl/inc/ucl/util/delegation/helpers.h index 95e0481..54ebb99 100644 --- a/ucl/inc/ucl/util/delegation/helpers.h +++ b/ucl/inc/ucl/util/delegation/helpers.h @@ -64,16 +64,16 @@ namespace ucl { inline bool operator==(const BaseDelegate<R(ARGS...), DATA1> &lhs, const BaseDelegate<R(ARGS...), DATA2> &rhs) noexcept { - return ((lhs.getStubA() == rhs.getStubA()) && (static_cast<void *>( - lhs.getData()) == static_cast<void *>(rhs.getData()))); + return ((lhs.getStubA() == rhs.getStubA()) && ( + lhs.getData() == rhs.getData())); } template <class R, class ...ARGS, class DATA1, class DATA2> inline bool operator!=(const BaseDelegate<R(ARGS...), DATA1> &lhs, const BaseDelegate<R(ARGS...), DATA2> &rhs) noexcept { - return ((lhs.getStubA() != rhs.getStubA()) || (static_cast<void *>( - lhs.getData()) != static_cast<void *>(rhs.getData()))); + return ((lhs.getStubA() != rhs.getStubA()) || ( + lhs.getData() != rhs.getData())); } template <class R, class ...ARGS, class DATA1, class DATA2> @@ -81,8 +81,8 @@ namespace ucl { const BaseDelegate<R(ARGS...), DATA2> &rhs) noexcept { return ((lhs.getStubA() < rhs.getStubA()) || - ((lhs.getStubA() == rhs.getStubA()) && (static_cast<void *>( - lhs.getData()) < static_cast<void *>(rhs.getData())))); + ((lhs.getStubA() == rhs.getStubA()) && ( + lhs.getData() < rhs.getData()))); } template <class R, class ...ARGS, class DATA1, class DATA2> @@ -90,8 +90,8 @@ namespace ucl { const BaseDelegate<R(ARGS...), DATA2> &rhs) noexcept { return ((lhs.getStubA() < rhs.getStubA()) || - ((lhs.getStubA() == rhs.getStubA()) && (static_cast<void *>( - lhs.getData()) <= static_cast<void *>(rhs.getData())))); + ((lhs.getStubA() == rhs.getStubA()) && ( + lhs.getData() <= rhs.getData()))); } template <class R, class ...ARGS, class DATA1, class DATA2> @@ -99,8 +99,8 @@ namespace ucl { const BaseDelegate<R(ARGS...), DATA2> &rhs) noexcept { return ((lhs.getStubA() > rhs.getStubA()) || - ((lhs.getStubA() == rhs.getStubA()) && (static_cast<void *>( - lhs.getData()) > static_cast<void *>(rhs.getData())))); + ((lhs.getStubA() == rhs.getStubA()) && ( + lhs.getData() > rhs.getData()))); } template <class R, class ...ARGS, class DATA1, class DATA2> @@ -108,8 +108,8 @@ namespace ucl { const BaseDelegate<R(ARGS...), DATA2> &rhs) noexcept { return ((lhs.getStubA() > rhs.getStubA()) || - ((lhs.getStubA() == rhs.getStubA()) && (static_cast<void *>( - lhs.getData()) >= static_cast<void *>(rhs.getData())))); + ((lhs.getStubA() == rhs.getStubA()) && ( + lhs.getData() >= rhs.getData()))); } } diff --git a/ucl/inc/ucl/util/memory.h b/ucl/inc/ucl/util/memory.h index 1fd8523..cc33960 100644 --- a/ucl/inc/ucl/util/memory.h +++ b/ucl/inc/ucl/util/memory.h @@ -17,7 +17,6 @@ #ifndef __UCL_UTIL_MEMORY_H__ #define __UCL_UTIL_MEMORY_H__ -#include "memory/RefCountObjBase.h" #include "memory/RefCountObj.h" #include "memory/BaseRef.h" diff --git a/ucl/inc/ucl/util/memory/BaseRef.h b/ucl/inc/ucl/util/memory/BaseRef.h index 0aa1b59..f82a2c9 100644 --- a/ucl/inc/ucl/util/memory/BaseRef.h +++ b/ucl/inc/ucl/util/memory/BaseRef.h @@ -17,7 +17,7 @@ #ifndef __UCL_UTIL_MEMORY_BASE_REF_H__ #define __UCL_UTIL_MEMORY_BASE_REF_H__ -#include "RefCountObjBase.h" +#include "IRefCountObj.h" namespace ucl { @@ -34,23 +34,17 @@ namespace ucl { friend class WeakRef; public: - int getUseCount() const; - - T *operator->() const noexcept; - typename std::add_lvalue_reference<T>::type operator*() const noexcept; - - template <class U> - explicit operator U() const noexcept; + int getUseCount() const noexcept; protected: constexpr BaseRef() noexcept; - BaseRef(RefCountObjBase *rc, T *ptr) noexcept; + BaseRef(IRefCountObj *rc, T *ptr) noexcept; BaseRef(BaseRef<T> &&r) noexcept; template <class U> BaseRef(BaseRef<U> &&r) noexcept; protected: - RefCountObjBase *m_rc; + IRefCountObj *m_rc; T *m_ptr; }; } diff --git a/ucl/inc/ucl/util/memory/BaseRef.hpp b/ucl/inc/ucl/util/memory/BaseRef.hpp index d587bc1..adde1c9 100644 --- a/ucl/inc/ucl/util/memory/BaseRef.hpp +++ b/ucl/inc/ucl/util/memory/BaseRef.hpp @@ -25,7 +25,7 @@ namespace ucl { template <class T> inline BaseRef<T>::BaseRef( - RefCountObjBase *const rc, T *const ptr) noexcept : + IRefCountObj *const rc, T *const ptr) noexcept : m_rc(rc), m_ptr(ptr) { @@ -51,28 +51,8 @@ namespace ucl { } template <class T> - inline int BaseRef<T>::getUseCount() const + inline int BaseRef<T>::getUseCount() const noexcept { return (m_rc ? m_rc->getUseCount() : 0); } - - template <class T> - inline T *BaseRef<T>::operator->() const noexcept - { - return m_ptr; - } - - template <class T> - inline typename std::add_lvalue_reference<T>::type - BaseRef<T>::operator*() const noexcept - { - return *m_ptr; - } - - template <class T> - template <class U> - inline BaseRef<T>::operator U() const noexcept - { - return static_cast<U>(m_ptr); - } } diff --git a/ucl/inc/ucl/util/memory/RefCountObjBase.h b/ucl/inc/ucl/util/memory/IRefCountObj.h index 4e6edf0..567e12c 100644 --- a/ucl/inc/ucl/util/memory/RefCountObjBase.h +++ b/ucl/inc/ucl/util/memory/IRefCountObj.h @@ -14,38 +14,24 @@ * limitations under the License. */ -#ifndef __UCL_UTIL_MEMORY_REF_COUNT_OBJ_BASE_H__ -#define __UCL_UTIL_MEMORY_REF_COUNT_OBJ_BASE_H__ +#ifndef __UCL_UTIL_MEMORY_I_REF_COUNT_OBJ_H__ +#define __UCL_UTIL_MEMORY_I_REF_COUNT_OBJ_H__ #include "ucl/util/types/classTypes.h" namespace ucl { - class RefCountObjBase : public Polymorphic { + class IRefCountObj : public Polymorphic { public: - void ref() noexcept; - void unref() noexcept; - - void refWeak() noexcept; - void unrefWeak() noexcept; - - int getUseCount() const noexcept; - bool isDisposed() const noexcept; - + virtual void ref() noexcept = 0; + virtual void unref() noexcept = 0; + virtual bool refNz() noexcept = 0; + virtual void refWeak() noexcept = 0; + virtual void unrefWeak() noexcept = 0; + virtual unsigned getUseCount() const noexcept = 0; protected: - RefCountObjBase(); - virtual ~RefCountObjBase() = default; - - virtual void dispose() noexcept; - virtual void onUniqueChanged(bool isUnique) noexcept = 0; - - private: - int m_useRefs; - int m_weakRefs; - bool m_isDisposed; + virtual ~IRefCountObj() = default; }; } -#include "RefCountObjBase.hpp" - -#endif // __UCL_UTIL_MEMORY_REF_COUNT_OBJ_BASE_H__ +#endif // __UCL_UTIL_MEMORY_I_REF_COUNT_OBJ_H__ diff --git a/ucl/inc/ucl/util/memory/RefCountObj.h b/ucl/inc/ucl/util/memory/RefCountObj.h index 777f07e..e566179 100644 --- a/ucl/inc/ucl/util/memory/RefCountObj.h +++ b/ucl/inc/ucl/util/memory/RefCountObj.h @@ -17,58 +17,93 @@ #ifndef __UCL_UTIL_MEMORY_REF_COUNT_OBJ_H__ #define __UCL_UTIL_MEMORY_REF_COUNT_OBJ_H__ -#include "RefCountObjBase.h" +#include "IRefCountObj.h" +#include "ReffedObj.h" namespace ucl { - template <class T> - class RefCountObj : public RefCountObjBase { + template <class T, class C> + class RefCountObj final : public IRefCountObj { public: template <class ...ARGS> RefCountObj(ARGS &&...args); T *getObj() noexcept; - protected: - // RefCountObjBase // - virtual void dispose() noexcept final override; - virtual void onUniqueChanged(bool isUnique) noexcept final override; + // IRefCountObj // + + virtual void ref() noexcept final override; + virtual void unref() noexcept final override; + virtual bool refNz() noexcept final override; + virtual void refWeak() noexcept final override; + virtual void unrefWeak() noexcept final override; + virtual unsigned getUseCount() const noexcept final override; private: - // Consume-all SFINAE functions - template <class T2, class ...ARGS> - void createObj(const P<0> &, ARGS &&...args); + template <class T2, class = char[1]> + struct IsRefCountAware : std::false_type {}; + template <class T2> + struct IsRefCountAware<T2, char[T2::IS_REF_COUNT_AWARE * 0 + 1]> : + std::true_type {}; + + template <class T2, class = char[1]> + struct IsOnUniqueAware : std::false_type {}; template <class T2> - void dispatchOnUniqueChanged( - const P<0> &, const bool isUnique) noexcept; + struct IsOnUniqueAware<T2, + char[T2::ENABLE_ON_UNIQUE_CHANGED_DISPATCH * 0 + 1]> : + std::true_type {}; - // Specialized SFINAE functions + private: + virtual ~RefCountObj() = default; + + template <class T2, class ...ARGS, class = + typename std::enable_if<!IsRefCountAware<T2>::value>::type> + void createObj(const P<0> &, ARGS &&...args) + { + m_obj.create(std::forward<ARGS>(args)...); + } - template <class T2, class ...ARGS> + template <class T2, class ...ARGS, class = + typename std::enable_if<IsRefCountAware<T2>::value>::type> auto createObj(const P<1> &, ARGS &&...args) -> decltype( - (void)(new T2(this, std::forward<ARGS>(args)...))) + ReffedObj<T>::template check<T2>( + (IRefCountObj *)0, std::forward<ARGS>(args)...)) { - new (getObj()) T(this, std::forward<ARGS>(args)...); + m_obj.create(static_cast<IRefCountObj *>(this), + std::forward<ARGS>(args)...); } - template <class T2, class ...ARGS> + template <class T2, class ...ARGS, class = + typename std::enable_if<IsRefCountAware<T2>::value>::type> auto createObj(const P<2> &, ARGS &&...args) -> decltype( - (void)(new T2(*this, std::forward<ARGS>(args)...))) + ReffedObj<T>::template check<T2>( + *(IRefCountObj *)0, std::forward<ARGS>(args)...)) + { + m_obj.create(*static_cast<IRefCountObj *>(this), + std::forward<ARGS>(args)...); + } + + void dispatchOnUniqueChanged(const bool isUnique) + { + dispatchOnUniqueChanged<T>(P<1>(), isUnique); + } + + template <class T2> + void dispatchOnUniqueChanged(...) { - new (getObj()) T(*this, std::forward<ARGS>(args)...); } - template <class T2, - class = char(*)[T2::ENABLE_ON_UNIQUE_CHANGED_DISPATCH * 0 + 1]> - void dispatchOnUniqueChanged( - const P<1> &, const bool isUnique) noexcept + template <class T2, class = + typename std::enable_if<IsOnUniqueAware<T2>::value>::type> + void dispatchOnUniqueChanged(const P<1> &, const bool isUnique) { - getObj()->onUniqueChanged(isUnique); + m_obj.template dispatchOnUniqueChanged<T>(isUnique); } private: - // Proper-aligned storage for T - typename std::aligned_storage<sizeof(T), alignof(T)>::type m_obj; + ReffedObj<T> m_obj; + C m_useCounter; + C m_weakCounter; }; } diff --git a/ucl/inc/ucl/util/memory/RefCountObj.hpp b/ucl/inc/ucl/util/memory/RefCountObj.hpp index 3807bd3..8fce0e3 100644 --- a/ucl/inc/ucl/util/memory/RefCountObj.hpp +++ b/ucl/inc/ucl/util/memory/RefCountObj.hpp @@ -16,45 +16,71 @@ namespace ucl { - template <class T> + template <class T, class C> template <class ...ARGS> - inline RefCountObj<T>::RefCountObj(ARGS &&...args) + inline RefCountObj<T, C>::RefCountObj(ARGS &&...args) : + m_useCounter(1), + m_weakCounter(1) { createObj<T>(P<2>(), std::forward<ARGS>(args)...); } - template <class T> - inline T *RefCountObj<T>::getObj() noexcept + template <class T, class C> + inline T *RefCountObj<T, C>::getObj() noexcept { - return static_cast<T *>(static_cast<void *>(&m_obj)); + return m_obj.get(); } - template <class T> - inline void RefCountObj<T>::dispose() noexcept + template <class T, class C> + inline void RefCountObj<T, C>::ref() noexcept { - if (!isDisposed()) { - RefCountObjBase::dispose(); - getObj()->~T(); + if (m_useCounter.ref() == 2) { + dispatchOnUniqueChanged(false); } } - template <class T> - inline void RefCountObj<T>::onUniqueChanged(const bool isUnique) noexcept + template <class T, class C> + inline void RefCountObj<T, C>::unref() noexcept { - dispatchOnUniqueChanged<T>(P<1>(), isUnique); + const auto newCount = m_useCounter.unref(); + if (newCount == 0) { + m_obj.destroy(); + unrefWeak(); + } else if (newCount == 1) { + dispatchOnUniqueChanged(true); + } + } + + template <class T, class C> + inline bool RefCountObj<T, C>::refNz() noexcept + { + const auto newCount = m_useCounter.refNz(); + if (newCount == 0) { + return false; + } + if (newCount == 2) { + dispatchOnUniqueChanged(false); + } + return true; } - template <class T> - template <class T2, class ...ARGS> - inline void RefCountObj<T>::createObj(const P<0> &, ARGS &&...args) + template <class T, class C> + inline void RefCountObj<T, C>::refWeak() noexcept { - new (getObj()) T(std::forward<ARGS>(args)...); + m_weakCounter.ref(); + } + + template <class T, class C> + inline void RefCountObj<T, C>::unrefWeak() noexcept + { + if (m_weakCounter.unref() == 0) { + delete this; + } } - template <class T> - template <class T2> - inline void RefCountObj<T>::dispatchOnUniqueChanged( - const P<0> &, const bool isUnique) noexcept + template <class T, class C> + inline unsigned RefCountObj<T, C>::getUseCount() const noexcept { + return m_useCounter.get(); } } diff --git a/ucl/inc/ucl/util/memory/RefCountObjBase.hpp b/ucl/inc/ucl/util/memory/RefCountObjBase.hpp deleted file mode 100644 index c4b5d42..0000000 --- a/ucl/inc/ucl/util/memory/RefCountObjBase.hpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2017 Samsung Electronics Co., Ltd - * - * Licensed under the Flora License, Version 1.1 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -namespace ucl { - - inline RefCountObjBase::RefCountObjBase() : - m_useRefs(0), - m_weakRefs(1), - m_isDisposed(false) - { - } - - inline void RefCountObjBase::ref() noexcept - { - ++m_useRefs; - if (m_useRefs == 2) { - onUniqueChanged(false); - } - } - - inline void RefCountObjBase::unref() noexcept - { - --m_useRefs; - if (m_useRefs <= 1) { - if (m_useRefs == 1) { - onUniqueChanged(true); - } else { - dispose(); - unrefWeak(); - } - } - } - - inline void RefCountObjBase::refWeak() noexcept - { - ++m_weakRefs; - } - - inline void RefCountObjBase::unrefWeak() noexcept - { - --m_weakRefs; - if (m_weakRefs == 0) { - delete this; - } - } - - inline int RefCountObjBase::getUseCount() const noexcept - { - return m_useRefs; - } - - inline bool RefCountObjBase::isDisposed() const noexcept - { - return m_isDisposed; - } - - inline void RefCountObjBase::dispose() noexcept - { - m_isDisposed = true; - } -} diff --git a/ucl/inc/ucl/util/memory/RefCounterMT.h b/ucl/inc/ucl/util/memory/RefCounterMT.h new file mode 100644 index 0000000..1d2fb95 --- /dev/null +++ b/ucl/inc/ucl/util/memory/RefCounterMT.h @@ -0,0 +1,41 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __UCL_UTIL_MEMORY_REF_COUNTER_MT_H__ +#define __UCL_UTIL_MEMORY_REF_COUNTER_MT_H__ + +#include "ucl/util/types/baseTypes.h" + +namespace ucl { + + class RefCounterMT final { + public: + explicit RefCounterMT(const UInt count = 0) noexcept; + + UInt ref() noexcept; + UInt unref() noexcept; + UInt refNz() noexcept; + + UInt get() const noexcept; + + private: + std::atomic<UInt> m_counter; + }; +} + +#include "RefCounterMT.hpp" + +#endif // __UCL_UTIL_MEMORY_REF_COUNTER_MT_H__ diff --git a/ucl/inc/ucl/util/memory/RefCounterMT.hpp b/ucl/inc/ucl/util/memory/RefCounterMT.hpp new file mode 100644 index 0000000..1ff459b --- /dev/null +++ b/ucl/inc/ucl/util/memory/RefCounterMT.hpp @@ -0,0 +1,53 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace ucl { + + inline RefCounterMT::RefCounterMT(const UInt count) noexcept : + m_counter(count) + { + } + + inline UInt RefCounterMT::ref() noexcept + { + return (m_counter.fetch_add(1, std::memory_order_acq_rel) + 1); + } + + inline UInt RefCounterMT::unref() noexcept + { + return (m_counter.fetch_sub(1, std::memory_order_acq_rel) - 1); + } + + inline UInt RefCounterMT::refNz() noexcept + { + auto curCount = m_counter.load(std::memory_order_relaxed); + for (;;) { + if (curCount == 0) { + return 0; + } + const auto newCount = (curCount + 1); + if (m_counter.compare_exchange_weak(curCount, newCount, + std::memory_order_acq_rel, std::memory_order_relaxed)) { + return newCount; + } + } + } + + inline UInt RefCounterMT::get() const noexcept + { + return m_counter.load(std::memory_order_relaxed); + } +} diff --git a/ucl/inc/ucl/util/memory/RefCounterST.h b/ucl/inc/ucl/util/memory/RefCounterST.h new file mode 100644 index 0000000..30e5e35 --- /dev/null +++ b/ucl/inc/ucl/util/memory/RefCounterST.h @@ -0,0 +1,41 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __UCL_UTIL_MEMORY_REF_COUNTER_ST_H__ +#define __UCL_UTIL_MEMORY_REF_COUNTER_ST_H__ + +#include "ucl/util/types/baseTypes.h" + +namespace ucl { + + class RefCounterST final { + public: + explicit RefCounterST(const UInt count = 0) noexcept; + + UInt ref() noexcept; + UInt unref() noexcept; + UInt refNz() noexcept; + + UInt get() const noexcept; + + private: + UInt m_counter; + }; +} + +#include "RefCounterST.hpp" + +#endif // __UCL_UTIL_MEMORY_REF_COUNTER_ST_H__ diff --git a/ucl/inc/ucl/util/memory/RefCounterST.hpp b/ucl/inc/ucl/util/memory/RefCounterST.hpp new file mode 100644 index 0000000..45781c7 --- /dev/null +++ b/ucl/inc/ucl/util/memory/RefCounterST.hpp @@ -0,0 +1,46 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace ucl { + + inline RefCounterST::RefCounterST(const UInt count) noexcept : + m_counter(count) + { + } + + inline UInt RefCounterST::ref() noexcept + { + return (++m_counter); + } + + inline UInt RefCounterST::unref() noexcept + { + return (--m_counter); + } + + inline UInt RefCounterST::refNz() noexcept + { + if (m_counter == 0) { + return 0; + } + return ref(); + } + + inline UInt RefCounterST::get() const noexcept + { + return m_counter; + } +} diff --git a/ucl/inc/ucl/util/memory/ReffedObj.h b/ucl/inc/ucl/util/memory/ReffedObj.h new file mode 100644 index 0000000..ed84b66 --- /dev/null +++ b/ucl/inc/ucl/util/memory/ReffedObj.h @@ -0,0 +1,58 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __UCL_UTIL_MEMORY_REFFED_OBJ_H__ +#define __UCL_UTIL_MEMORY_REFFED_OBJ_H__ + +#include "RefCounterST.h" +#include "RefCounterMT.h" + +namespace ucl { + + template <class T, class C> + class RefCountObj; + + template <class T> + class ReffedObj final { + private: + friend class RefCountObj<T, RefCounterST>; + friend class RefCountObj<T, RefCounterMT>; + + template <class ...ARGS> + void create(ARGS &&...args); + void destroy() noexcept; + + T *get() noexcept; + + template <class T2> + void dispatchOnUniqueChanged(bool isUnique); + + private: + template <class T2, class ...ARGS> + static constexpr auto check(ARGS &&...args) -> decltype( + (void)(new T2(std::forward<ARGS>(args)...))) + { + return; + } + + private: + typename std::aligned_storage<sizeof(T), alignof(T)>::type m_obj; + }; +} + +#include "ReffedObj.hpp" + +#endif // __UCL_UTIL_MEMORY_REFFED_OBJ_H__ diff --git a/ucl/inc/ucl/util/memory/ReffedObj.hpp b/ucl/inc/ucl/util/memory/ReffedObj.hpp new file mode 100644 index 0000000..5e9f473 --- /dev/null +++ b/ucl/inc/ucl/util/memory/ReffedObj.hpp @@ -0,0 +1,44 @@ +/* + * Copyright 2017 Samsung Electronics Co., Ltd + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace ucl { + + template <class T> + template <class ...ARGS> + inline void ReffedObj<T>::create(ARGS &&...args) + { + new (get()) T(std::forward<ARGS>(args)...); + } + + template <class T> + inline void ReffedObj<T>::destroy() noexcept + { + get()->~T(); + } + + template <class T> + inline T *ReffedObj<T>::get() noexcept + { + return static_cast<T *>(static_cast<void *>(&m_obj)); + } + + template <class T> + template <class T2> + inline void ReffedObj<T>::dispatchOnUniqueChanged(const bool isUnique) + { + get()->onUniqueChanged(isUnique); + } +} diff --git a/ucl/inc/ucl/util/memory/SharedRef.h b/ucl/inc/ucl/util/memory/SharedRef.h index 4bd7ca0..396d176 100644 --- a/ucl/inc/ucl/util/memory/SharedRef.h +++ b/ucl/inc/ucl/util/memory/SharedRef.h @@ -22,7 +22,7 @@ namespace ucl { template <class T> - class SharedRef : public BaseRef<T> { + class SharedRef final : public BaseRef<T> { public: template <class U> friend void swap(SharedRef<U> &x, SharedRef<U> &y) noexcept; @@ -36,11 +36,12 @@ namespace ucl { constexpr SharedRef() noexcept; constexpr SharedRef(std::nullptr_t) noexcept; - SharedRef(RefCountObjBase *rc, T *ptr) noexcept; + SharedRef(IRefCountObj *rc, T *ptr) noexcept; + SharedRef(IRefCountObj *rc, T *ptr, bool noRef) noexcept; SharedRef(const SharedRef<T> &r) noexcept; template <class U> - SharedRef(const BaseRef<U> &r) noexcept; + SharedRef(const SharedRef<U> &r) noexcept; SharedRef(SharedRef<T> &&r) noexcept; template <class U> @@ -55,6 +56,9 @@ namespace ucl { T *get() const noexcept; operator bool() const noexcept; + T *operator->() const noexcept; + typename std::add_lvalue_reference<T>::type operator*() const noexcept; + template <class U, class = typename std::enable_if< std::is_convertible<T *, U *>::value && ( std::is_same<typename std::remove_cv<U>::type, void>::value || @@ -70,6 +74,8 @@ namespace ucl { template <class T, class ...ARGS> SharedRef<T> makeShared(ARGS &&...args); + template <class T, class ...ARGS> + SharedRef<T> makeSharedMT(ARGS &&...args); template <class T, class U> const SharedRef<T> &constRefCast(const SharedRef<U> &r) noexcept; diff --git a/ucl/inc/ucl/util/memory/SharedRef.hpp b/ucl/inc/ucl/util/memory/SharedRef.hpp index 5b5ad55..f085b9c 100644 --- a/ucl/inc/ucl/util/memory/SharedRef.hpp +++ b/ucl/inc/ucl/util/memory/SharedRef.hpp @@ -14,8 +14,6 @@ * limitations under the License. */ -#include "RefCountObj.h" - namespace ucl { template <class T> @@ -30,13 +28,20 @@ namespace ucl { template <class T> inline SharedRef<T>::SharedRef( - RefCountObjBase *const rc, T *const ptr) noexcept : + IRefCountObj *const rc, T *const ptr) noexcept : BaseRef<T>(rc, ptr) { this->m_rc->ref(); } template <class T> + inline SharedRef<T>::SharedRef( + IRefCountObj *const rc, T *const ptr, bool noRef) noexcept : + BaseRef<T>(rc, ptr) + { + } + + template <class T> inline SharedRef<T>::SharedRef(const SharedRef<T> &r) noexcept : BaseRef<T>(r.m_rc, r.m_ptr) { @@ -47,7 +52,7 @@ namespace ucl { template <class T> template <class U> - inline SharedRef<T>::SharedRef(const BaseRef<U> &r) noexcept : + inline SharedRef<T>::SharedRef(const SharedRef<U> &r) noexcept : BaseRef<T>(r.m_rc, r.m_ptr) { if (this->m_rc) { @@ -101,13 +106,35 @@ namespace ucl { return !!this->m_ptr; } + template <class T> + inline T *SharedRef<T>::operator->() const noexcept + { + return this->m_ptr; + } + + template <class T> + inline typename std::add_lvalue_reference<T>::type + SharedRef<T>::operator*() const noexcept + { + return *this->m_ptr; + } + // Non-member functions // template <class T, class ...ARGS> SharedRef<T> makeShared(ARGS &&...args) { - const auto rc = new RefCountObj<T>(std::forward<ARGS>(args)...); - return {rc, rc->getObj()}; + const auto rc = new RefCountObj<T, RefCounterST>( + std::forward<ARGS>(args)...); + return {rc, rc->getObj(), true}; + } + + template <class T, class ...ARGS> + SharedRef<T> makeSharedMT(ARGS &&...args) + { + const auto rc = new RefCountObj<T, RefCounterMT>( + std::forward<ARGS>(args)...); + return {rc, rc->getObj(), true}; } template <class T> diff --git a/ucl/inc/ucl/util/memory/WeakRef.h b/ucl/inc/ucl/util/memory/WeakRef.h index 78450a5..4cccbe6 100644 --- a/ucl/inc/ucl/util/memory/WeakRef.h +++ b/ucl/inc/ucl/util/memory/WeakRef.h @@ -22,7 +22,7 @@ namespace ucl { template <class T> - class WeakRef : public BaseRef<T> { + class WeakRef final : public BaseRef<T> { public: template <class U> friend void swap(WeakRef<U> &x, WeakRef<U> &y) noexcept; @@ -36,7 +36,7 @@ namespace ucl { constexpr WeakRef() noexcept; constexpr WeakRef(std::nullptr_t) noexcept; - WeakRef(RefCountObjBase *rc, T *ptr) noexcept; + WeakRef(IRefCountObj *rc, T *ptr) noexcept; WeakRef(const WeakRef<T> &r) noexcept; template <class U> @@ -52,7 +52,9 @@ namespace ucl { void reset() noexcept; - T *get() const noexcept; + SharedRef<T> lock() const noexcept; + + T *getUnsafePtr() const noexcept; operator bool() const noexcept; template <class U, class = typename std::enable_if< diff --git a/ucl/inc/ucl/util/memory/WeakRef.hpp b/ucl/inc/ucl/util/memory/WeakRef.hpp index b1681ed..6634f3c 100644 --- a/ucl/inc/ucl/util/memory/WeakRef.hpp +++ b/ucl/inc/ucl/util/memory/WeakRef.hpp @@ -28,7 +28,7 @@ namespace ucl { template <class T> inline WeakRef<T>::WeakRef( - RefCountObjBase *const rc, T *const ptr) noexcept : + IRefCountObj *const rc, T *const ptr) noexcept : BaseRef<T>(rc, ptr) { this->m_rc->refWeak(); @@ -88,7 +88,16 @@ namespace ucl { } template <class T> - inline T *WeakRef<T>::get() const noexcept + inline SharedRef<T> WeakRef<T>::lock() const noexcept + { + if (this->m_rc && this->m_rc->refNz()) { + return {this->m_rc, this->m_ptr, true}; + } + return {}; + } + + template <class T> + inline T *WeakRef<T>::getUnsafePtr() const noexcept { return (operator bool() ? this->m_ptr : nullptr); } @@ -96,7 +105,7 @@ namespace ucl { template <class T> inline WeakRef<T>::operator bool() const noexcept { - return (this->m_ptr && !this->m_rc->isDisposed()); + return (this->m_rc && (this->m_rc->getUseCount() > 0)); } // Non-member functions // @@ -117,13 +126,13 @@ namespace ucl { template <class T, class U> inline WeakRef<T> staticRefCast(const WeakRef<U> &r) noexcept { - return {r.m_rc, static_cast<T *>(r.get())}; + return {r.m_rc, static_cast<T *>(r.getUnsafePtr())}; } template <class T, class U> inline WeakRef<T> dynamicRefCast(const WeakRef<U> &r) noexcept { - const auto ptr = dynamic_cast<T *>(r.get()); + const auto ptr = dynamic_cast<T *>(r.getUnsafePtr()); if (!ptr) { return {}; } diff --git a/ucl/inc/ucl/util/memory/helpers.h b/ucl/inc/ucl/util/memory/helpers.h index 4e6c0e0..bef907b 100644 --- a/ucl/inc/ucl/util/memory/helpers.h +++ b/ucl/inc/ucl/util/memory/helpers.h @@ -37,52 +37,74 @@ namespace ucl { // Relation operators // + namespace himpl { + + template <class T, class = typename std::enable_if< + !std::is_base_of<BaseRef<typename T::Type>, T>::value>::type> + inline const T &getCmpPtr(const T &ptr) noexcept + { + return ptr; + } + + template <class T> + inline T *getCmpPtr(const SharedRef<T> &r) noexcept + { + return r.get(); + } + + template <class T> + inline T *getCmpPtr(const WeakRef<T> &r) noexcept + { + return r.getUnsafePtr(); + } + } + template <class T, class U, class = typename std::enable_if< - std::is_base_of<BaseRef<typename T::Type>, T>::value && + std::is_base_of<BaseRef<typename T::Type>, T>::value || std::is_base_of<BaseRef<typename U::Type>, U>::value>::type> inline bool operator==(const T &lhs, const U &rhs) noexcept { - return (lhs.get() == rhs.get()); + return (himpl::getCmpPtr(lhs) == himpl::getCmpPtr(rhs)); } template <class T, class U, class = typename std::enable_if< - std::is_base_of<BaseRef<typename T::Type>, T>::value && + std::is_base_of<BaseRef<typename T::Type>, T>::value || std::is_base_of<BaseRef<typename U::Type>, U>::value>::type> inline bool operator!=(const T &lhs, const U &rhs) noexcept { - return (lhs.get() != rhs.get()); + return (himpl::getCmpPtr(lhs) != himpl::getCmpPtr(rhs)); } template <class T, class U, class = typename std::enable_if< - std::is_base_of<BaseRef<typename T::Type>, T>::value && + std::is_base_of<BaseRef<typename T::Type>, T>::value || std::is_base_of<BaseRef<typename U::Type>, U>::value>::type> inline bool operator<(const T &lhs, const U &rhs) noexcept { - return (lhs.get() < rhs.get()); + return (himpl::getCmpPtr(lhs) < himpl::getCmpPtr(rhs)); } template <class T, class U, class = typename std::enable_if< - std::is_base_of<BaseRef<typename T::Type>, T>::value && + std::is_base_of<BaseRef<typename T::Type>, T>::value || std::is_base_of<BaseRef<typename U::Type>, U>::value>::type> inline bool operator<=(const T &lhs, const U &rhs) noexcept { - return (lhs.get() <= rhs.get()); + return (himpl::getCmpPtr(lhs) <= himpl::getCmpPtr(rhs)); } template <class T, class U, class = typename std::enable_if< - std::is_base_of<BaseRef<typename T::Type>, T>::value && + std::is_base_of<BaseRef<typename T::Type>, T>::value || std::is_base_of<BaseRef<typename U::Type>, U>::value>::type> inline bool operator>(const T &lhs, const U &rhs) noexcept { - return (lhs.get() > rhs.get()); + return (himpl::getCmpPtr(lhs) > himpl::getCmpPtr(rhs)); } template <class T, class U, class = typename std::enable_if< - std::is_base_of<BaseRef<typename T::Type>, T>::value && + std::is_base_of<BaseRef<typename T::Type>, T>::value || std::is_base_of<BaseRef<typename U::Type>, U>::value>::type> inline bool operator>=(const T &lhs, const U &rhs) noexcept { - return (lhs.get() >= rhs.get()); + return (himpl::getCmpPtr(lhs) >= himpl::getCmpPtr(rhs)); } template <class T, class = typename std::enable_if< diff --git a/ucl/inc/ucl/util/smartDelegation/WeakDelegate.h b/ucl/inc/ucl/util/smartDelegation/WeakDelegate.h index 0d29f0f..97e1ae8 100644 --- a/ucl/inc/ucl/util/smartDelegation/WeakDelegate.h +++ b/ucl/inc/ucl/util/smartDelegation/WeakDelegate.h @@ -31,6 +31,8 @@ namespace ucl { public: using BaseDelegate<R(ARGS...), WeakRef<void>>::BaseDelegate; + R operator()(ARGS ...args) const; + template <class CLASS, R(CLASS::*METHOD)(ARGS...)> static WeakDelegate make(const WeakRef<CLASS> &data) noexcept; diff --git a/ucl/inc/ucl/util/smartDelegation/WeakDelegate.hpp b/ucl/inc/ucl/util/smartDelegation/WeakDelegate.hpp index e4b4b5d..fa7b3f8 100644 --- a/ucl/inc/ucl/util/smartDelegation/WeakDelegate.hpp +++ b/ucl/inc/ucl/util/smartDelegation/WeakDelegate.hpp @@ -17,6 +17,16 @@ namespace ucl { template <class R, class ...ARGS> + inline R WeakDelegate<R(ARGS...)>::operator()(ARGS ...args) const + { + const auto tmp = this->m_data.lock(); + if (tmp) { + return this->m_stubA(tmp.get(), std::forward<ARGS>(args)...); + } + return R(); + } + + template <class R, class ...ARGS> template <class CLASS, R(CLASS::*METHOD)(ARGS...)> inline WeakDelegate<R(ARGS...)> WeakDelegate<R(ARGS...)>::make( const WeakRef<CLASS> &data) noexcept diff --git a/ucl/inc/ucl/util/types/baseTypes.h b/ucl/inc/ucl/util/types/baseTypes.h index 5b280de..c5062db 100644 --- a/ucl/inc/ucl/util/types/baseTypes.h +++ b/ucl/inc/ucl/util/types/baseTypes.h @@ -23,6 +23,7 @@ #include <string> #include <memory> +#include <atomic> #include <functional> #include <type_traits> #include <utility> diff --git a/ucl/src/gui/ElmWidget.cpp b/ucl/src/gui/ElmWidget.cpp index 73856be..7c39f81 100644 --- a/ucl/src/gui/ElmWidget.cpp +++ b/ucl/src/gui/ElmWidget.cpp @@ -21,7 +21,7 @@ namespace ucl { - ElmWidget::ElmWidget(RefCountObjBase *rc, Evas_Object *eo, bool isOwner) : + ElmWidget::ElmWidget(IRefCountObj *rc, Evas_Object *eo, bool isOwner) : Widget(rc, eo, isOwner), m_isAtspiGestureCbSet(false) { diff --git a/ucl/src/gui/Naviframe.cpp b/ucl/src/gui/Naviframe.cpp index 881390c..69d80a5 100644 --- a/ucl/src/gui/Naviframe.cpp +++ b/ucl/src/gui/Naviframe.cpp @@ -47,7 +47,7 @@ namespace ucl { // Naviframe // - Naviframe::Naviframe(RefCountObjBase &rc, Evas_Object *eo) : + Naviframe::Naviframe(IRefCountObj &rc, Evas_Object *eo) : StyledWidget(&rc, eo, true), m_isInTransition(false) { diff --git a/ucl/src/gui/Widget.cpp b/ucl/src/gui/Widget.cpp index 0d546f4..d2c0dd6 100644 --- a/ucl/src/gui/Widget.cpp +++ b/ucl/src/gui/Widget.cpp @@ -110,7 +110,7 @@ namespace ucl { // Widget // - Widget::Widget(RefCountObjBase *const rc, Evas_Object *const eo, + Widget::Widget(IRefCountObj *const rc, Evas_Object *const eo, const bool isOwner) : RefCountAware(rc), m_eo(eo), @@ -175,11 +175,9 @@ namespace ucl { void Widget::updateRefs() { - const auto rc = m_rc; - updateEoRef(); - if (rc && !rc->isDisposed()) { + if (m_rc) { updateSelfRef(); } } |