diff --git a/libwccl/values/position.cpp b/libwccl/values/position.cpp index 074f7b3377b31eede23ce789a6b89dff3456af08..e71912e7a5eec6ff832abab832c71992db2954ce 100644 --- a/libwccl/values/position.cpp +++ b/libwccl/values/position.cpp @@ -1,5 +1,7 @@ #include <libwccl/values/position.h> +#include <libwccl/sentencecontext.h> #include <boost/lexical_cast.hpp> + namespace Wccl { const char* Position::type_name = "Position"; @@ -25,4 +27,10 @@ std::string Position::var_repr(const std::string &var_name) return ss.str(); } +bool Position::is_inside(const SentenceContext& context) const +{ + int abs_position = context.get_abs_position(*this); + return context.is_inside(abs_position); +} + } /* end ns Wccl */ diff --git a/libwccl/values/position.h b/libwccl/values/position.h index a87a8a98352fa1074fbbca907907177a620a9236..3283b849f3434ba2337e1c4dc6e27f30634c5447 100644 --- a/libwccl/values/position.h +++ b/libwccl/values/position.h @@ -7,6 +7,8 @@ namespace Wccl { +class SentenceContext; + class Position : public Value { public: @@ -32,10 +34,36 @@ public: /// Value override std::string to_raw_string() const; - bool equals(const Position& other) { + /** + * @returns True if Position is within bounds of a sentence, false otherwise. + */ + bool is_inside(const SentenceContext& context) const; + + /** + * @returns True if Position is outside of bounds of a sentence, false otherwise. + */ + bool is_outside(const SentenceContext& context) const { + return !is_inside(context); + } + /** + * @returns True if underlying position values are equal, false otherwise. + * @note This version does not take into account sentence context, only + * the raw value of position. + */ + bool equals(const Position& other) const { return val_ == other.val_; } + /** + * @returns True if positions are equal in context of a sentence, false otherwise. + * @note This version determines if position values point outside of a sentence, + * and if both of them do, they are considered to be equal as well (both "nowhere"). + */ + bool equals(const Position& other, const SentenceContext& context) const + { + return equals(other) || is_outside(context) && other.is_outside(context); + } + private: int val_; };