From d521f08fc31cc65f7ffdfe0e0ddfe3635efaa0d3 Mon Sep 17 00:00:00 2001 From: ilor <kailoran@gmail.com> Date: Mon, 20 Jun 2011 10:44:13 +0200 Subject: [PATCH] sentence cleanup WIP --- libcorpus2/sentence.h | 51 +++++++++++++++++++++++++++++++-------- libpwrutils/tokensource.h | 10 ++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/libcorpus2/sentence.h b/libcorpus2/sentence.h index 6eb8522..e4d2ee6 100644 --- a/libcorpus2/sentence.h +++ b/libcorpus2/sentence.h @@ -23,23 +23,44 @@ or FITNESS FOR A PARTICULAR PURPOSE. namespace Corpus2 { +/** + * A Sentence is the primary structure passed around in Corpus2, + * tokens are rarely processed outside of a Sentence. + * + * Sentences should always be kept as shared_ptrs and passed around + * as shared pointers or const references. Derived classes of + * Sentence may provide extra functionality. + * + * Tokens are kept as raw pointers. A Sentence owns the tokens + * it contains and will delete them upon destruction. Sometimes + * this is not desirable, in this case the release_tokens function + * can be used (carefully). + */ class Sentence : private boost::noncopyable { public: + /// Convenience typedef for a shared pointer to a Sentence typedef boost::shared_ptr<Sentence> Ptr; + + /// Convenience typedef for a shared pointer to a const Sentence typedef boost::shared_ptr<const Sentence> ConstPtr; - /// Empty constructor + /// Empty constructor. Creates a Sentence with no tokens. Sentence(); + /// Sentence cloning. All the tokens are duplicated. virtual Ptr clone_shared() const; - /// Destructor + /// Destructor. Deletes contained tokens. virtual ~Sentence(); + /// Releases ownership of all tokens, makes the sentence empty. + /// Warning: This will cause an obvious memory leak if the tokens are + /// not stored somewhere first (e.g. append()ed to another Sentence). void release_tokens(); - bool empty() const { + /// Convenience function to check if the sentence is empty + bool empty() const { return tokens_.empty(); } @@ -48,27 +69,37 @@ public: return tokens_.size(); } - /// Token accessor + /// Token accessor (no range checking) Token* operator[](size_t idx) { return tokens_[idx]; } - /// Token accessor, const + /// Token accessor, const (no range checking) const Token* operator[](size_t idx) const { return tokens_[idx]; } + /// Token accessor, non-operator (no range checking) + Token* at(size_t idx) { + return tokens_[idx]; + } + + /// Token accessor, const (no range checking) + const Token* at(size_t idx) const { + return tokens_[idx]; + } + /// Underlying vector accessor, const const std::vector<Token*>& tokens() const { return tokens_; } - /// Underlying vector accessor - std::vector<Token*>& tokens() { - return tokens_; - } + /// Underlying vector accessor, nonconst (dangerous) + //std::vector<Token*>& tokens() { + // return tokens_; + //} - /// Helper function for appending tokens + /// Helper function for appending tokens. Prefer using this. /// Might be overriden in a child class to make adding a token keep /// extra invariants virtual void append(Token* t) { diff --git a/libpwrutils/tokensource.h b/libpwrutils/tokensource.h index 429b65f..63342fa 100644 --- a/libpwrutils/tokensource.h +++ b/libpwrutils/tokensource.h @@ -75,6 +75,16 @@ namespace detail { { typedef T type; }; + template<typename T> + struct deptr<const T*> + { + typedef T type; + }; + template<typename T> + struct deptr<T* const> + { + typedef const T type; + }; } /* end ns detail */ /** -- GitLab