From 09859b9c21890d9e0bb8b399fb1cb21aa71b3df2 Mon Sep 17 00:00:00 2001 From: ilor <kailoran@gmail.com> Date: Sat, 30 Oct 2010 21:09:42 +0200 Subject: [PATCH] add SentenceContext clone and duplicate --- libwccl/sentencecontext.cpp | 12 ++++++++++++ libwccl/sentencecontext.h | 11 ++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/libwccl/sentencecontext.cpp b/libwccl/sentencecontext.cpp index c6ed70f..591d89c 100644 --- a/libwccl/sentencecontext.cpp +++ b/libwccl/sentencecontext.cpp @@ -7,4 +7,16 @@ SentenceContext::SentenceContext(const boost::shared_ptr<Corpus2::Sentence>& s) { } +SentenceContext SentenceContext::duplicate() const +{ + SentenceContext dup(*this); + dup.sentence_.reset(sentence_->clone()); + return dup; +} + +SentenceContext* SentenceContext::clone() const +{ + return new SentenceContext(duplicate()); +} + } /* end ns Wccl */ diff --git a/libwccl/sentencecontext.h b/libwccl/sentencecontext.h index 9215df2..1994db4 100644 --- a/libwccl/sentencecontext.h +++ b/libwccl/sentencecontext.h @@ -9,13 +9,20 @@ namespace Wccl { /** * A wrapper for a Corpus2 Sentence that adds a "current position" * and several convenience functions. + * + * Copying contexts is cheap and safe since the sentence is kept through + * a shared pointer, use duplicate() or clone() to get a copy with a separate + * underlying sentence (which is slower). */ class SentenceContext { public: - /// Constructor, wraps the Sentence and stes position to 0 + /// Constructor, wraps the Sentence and sets position to 0 SentenceContext(const boost::shared_ptr<Corpus2::Sentence>& s); + /// Returns a copy of this with a cloned underlyiong sentence + SentenceContext duplicate() const; + /// Cloning -- clones the underlying sentence as well SentenceContext* clone() const; @@ -97,6 +104,8 @@ private: int position_; }; +// TODO ConstSentenceContext ? + } /* end ns Wccl */ #endif // LIBWCCL_SENTENCECONTEXT_H -- GitLab