From c9db1d8ba3c8511332f4ce3037037c181fb2faa6 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 1 Oct 2015 01:45:42 +0200 Subject: [PATCH] Wrap the unique_ptr so that dtor is called from the cpp. This make it unnecessary to include scrapast.hpp in whatever cpp takes ownership of the unique_ptr. Without this tho, the destructor of unique_ptr would force you to include scrapast. --- src/scraplang/scraplang.cpp | 17 +++++++++++++++-- src/scraplang/scraplang.hpp | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/scraplang/scraplang.cpp b/src/scraplang/scraplang.cpp index 15bac0a..6ea5db2 100644 --- a/src/scraplang/scraplang.cpp +++ b/src/scraplang/scraplang.cpp @@ -70,9 +70,9 @@ namespace duck { }; } //unnamed namespace - std::unique_ptr parse_scraplang (const std::string& parData) { + ScrapNodePtr parse_scraplang (const std::string& parData) { ScrapGrammar gramm; - std::unique_ptr retval(new ScrapNode); + ScrapNodePtr retval(new ScrapNode); auto it_start = parData.cbegin(); qi::phrase_parse(it_start, parData.cend(), gramm, sp::ascii::space, *retval); @@ -85,4 +85,17 @@ namespace duck { boost::apply_visitor(xpath_vis, parAST); return std::move(retval); } + + ScrapNodePtr::ScrapNodePtr (ScrapNode* parPtr) : + m_ptr(parPtr) + { + } + + ScrapNodePtr::ScrapNodePtr (ScrapNodePtr&& parOther) : + m_ptr(std::move(parOther.m_ptr)) + { + } + + ScrapNodePtr::~ScrapNodePtr() noexcept { + } } //namespace duck diff --git a/src/scraplang/scraplang.hpp b/src/scraplang/scraplang.hpp index f3b4f79..9c394c7 100644 --- a/src/scraplang/scraplang.hpp +++ b/src/scraplang/scraplang.hpp @@ -9,7 +9,22 @@ namespace duck { struct ScrapNode; struct element_def; - std::unique_ptr parse_scraplang ( const std::string& parData ); + class ScrapNodePtr { + public: + explicit ScrapNodePtr ( ScrapNode* parPtr ); + ScrapNodePtr ( ScrapNodePtr&& parOther ); + ~ScrapNodePtr ( void ) noexcept; + + ScrapNode& operator* ( void ) { return *m_ptr; } + const ScrapNode& operator* ( void ) const { return *m_ptr; } + ScrapNode& operator-> ( void ) { return *m_ptr; } + const ScrapNode& operator-> ( void ) const { return *m_ptr; } + + private: + std::unique_ptr m_ptr; + }; + + ScrapNodePtr parse_scraplang ( const std::string& parData ); std::vector get_xpath_definitions ( const ScrapNode& parAST ); } //namespace duck