diff --git a/libwccl/sentencecontext.cpp b/libwccl/sentencecontext.cpp index c6ed70f211e16a3d27e7f7cc8cdbbd4dd18fd565..591d89c5bb376c5aabd9ea458313aca2422bcfe5 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 9215df205695754ee3fd85c29d44503cb379174e..1994db44f49401615bc439513b5eff11188941fd 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