text_base_types.h

Go to the documentation of this file.
00001 /*
00002  * File Name: text_base_types.h
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-plaintext.
00007  *
00008  * uds-plugin-plaintext is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-plaintext is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #ifndef TEXT_BASE_TYPES_H
00028 #define TEXT_BASE_TYPES_H
00029 
00030 #include <string>
00031 #include <deque>
00032 #include <pango/pango.h>
00033 
00034 namespace text
00035 {
00036 
00037 using namespace std;
00038 
00039 struct ViewPosition
00040 {
00041     int x;              ///< X coordinate, in pixels.
00042     int y;              ///< Y coordinate, in pixels.
00043 };
00044 
00045 struct Rect
00046 {
00047 public:
00048     Rect(int _x, int _y, int _width, int _height)
00049     : x(_x), y(_y), width(_width), height(_height)
00050     {
00051     }
00052 public:
00053     int x;
00054     int y;
00055     int width;
00056     int height;
00057 };
00058 
00059 /// @brief Position type for text plugin.
00060 class Position
00061 {
00062 public:
00063     /// @brief Constructors and destructors
00064     explicit Position(unsigned int paragraph_index = 0, unsigned int offset = 0);
00065     explicit Position(const std::string& from_string);
00066 
00067     Position(const Position& r)
00068     {
00069         if (this != &r)
00070         {
00071             this->paragraph = r.paragraph;
00072             this->offset    = r.offset;
00073         }
00074     }
00075 
00076     Position& operator=(const Position& r)
00077     {
00078         if (this != &r)
00079         {
00080             this->paragraph = r.paragraph;
00081             this->offset    = r.offset;
00082         }
00083         return *this;
00084     }
00085 
00086     friend bool operator==(const Position& l, const Position& r)
00087     {
00088         return (l.paragraph == r.paragraph) && (l.offset == r.offset);
00089     }
00090 
00091     friend bool operator!=(const Position& l, const Position& r)
00092     {
00093         return (l.paragraph != r.paragraph) || (l.offset != r.offset);
00094     }
00095 
00096     friend bool operator<(const Position& l, const Position& r)
00097     {
00098         return (l.paragraph < r.paragraph) ||
00099                (l.paragraph == r.paragraph && l.offset < r.offset);
00100     }
00101 
00102     friend bool operator>(const Position& l, const Position& r)
00103     {
00104         return (l.paragraph > r.paragraph) ||
00105                (l.paragraph == r.paragraph && l.offset > r.offset);
00106     }
00107 
00108     friend bool operator<=(const Position& l, const Position& r)
00109     {
00110         return (l.paragraph < r.paragraph) ||
00111                (l.paragraph == r.paragraph && l.offset <= r.offset);
00112     }
00113 
00114     friend bool operator>=(const Position& l, const Position& r)
00115     {
00116         return (l.paragraph > r.paragraph) ||
00117                (l.paragraph == r.paragraph && l.offset >= r.offset);
00118     }
00119 
00120     ~Position();
00121 
00122 public:
00123     /// @brief Convert internel position type to string.
00124     ///  The string-based position can be used as anchors.
00125     std::string to_string() const;
00126 
00127 public:
00128     unsigned int paragraph;
00129     unsigned int offset;
00130 };
00131 
00132 /// @brief Information about one page, used for pagination.
00133 struct Range
00134 {
00135 public:
00136     Range()
00137     : start(Position(0, 0)), end(Position(0, 0))
00138     {
00139     }
00140 
00141     Range(const Position& start_pos, const Position& end_pos)
00142     : start(start_pos), end(end_pos)
00143     {
00144     }
00145 
00146 public:
00147     Position start;
00148     Position end;
00149 };
00150 
00151 typedef Range PageInfo;
00152 
00153 enum SearchType
00154 {
00155     SEARCH_ALL,
00156     SEARCH_NEXT
00157 };
00158 
00159 struct SearchContext
00160 {
00161 public:
00162     SearchContext()
00163     : search_id(0),
00164       search_type(SEARCH_ALL),
00165       from(0, 0),
00166       pattern(""),
00167       case_sensitive(true),
00168       forward(true),
00169       match_whole_word(true)
00170     {
00171     }
00172 
00173     SearchContext(unsigned int     _id,
00174                   SearchType       _type, 
00175                   const Position&  _from,
00176                   const string&    _pattern,
00177                   bool             _case_sensitive,
00178                   bool             _forward,
00179                   bool             _match_whole_word)
00180     : search_id(_id),
00181       search_type(_type),
00182       from(_from),
00183       pattern(_pattern),
00184       case_sensitive(_case_sensitive),
00185       forward(_forward),
00186       match_whole_word(_match_whole_word)
00187     {
00188     }
00189 
00190     SearchContext(const SearchContext& r)
00191     {
00192         search_id = r.search_id;
00193         search_type = r.search_type;
00194         from = r.from;
00195         pattern = r.pattern;
00196         case_sensitive = r.case_sensitive;
00197         forward = r.forward;
00198         match_whole_word = r.match_whole_word;
00199     }
00200 
00201     SearchContext& operator=(const SearchContext& r)
00202     {
00203         if (this != &r)
00204         {
00205             search_id = r.search_id;
00206             search_type = r.search_type;
00207             from = r.from;
00208             pattern = r.pattern;
00209             case_sensitive = r.case_sensitive;
00210             forward = r.forward;
00211             match_whole_word = r.match_whole_word;
00212         }
00213 
00214         return *this;
00215     }
00216 
00217 public:
00218     unsigned int search_id;     ///< Search id, to identify a specific search
00219     SearchType search_type;     ///< Search type, search all or search next
00220     Position from;              ///< From which position the search starts
00221     std::string pattern;        ///< Search pattern
00222     bool case_sensitive;        ///< Case sensitive
00223     bool forward;               ///< Search previous or next occurrence
00224     bool match_whole_word;      ///< Match the whole word
00225 };
00226 
00227 template <typename T>
00228 class SafeDeque
00229 {
00230 public:
00231     /// @brief Constructors and destructors.
00232     SafeDeque()
00233     {
00234         g_static_rw_lock_init(&rw_lock);
00235     }
00236 
00237     ~SafeDeque()
00238     {
00239         g_static_rw_lock_free(&rw_lock);
00240     }
00241 
00242     // Read operations.
00243     size_t size()
00244     {
00245         g_static_rw_lock_reader_lock(&rw_lock);
00246         size_t size = data.size();
00247         g_static_rw_lock_reader_unlock(&rw_lock);
00248         return size;
00249     }
00250 
00251     bool empty()
00252     {
00253         g_static_rw_lock_reader_lock(&rw_lock);
00254         bool is_empty = data.empty();
00255         g_static_rw_lock_reader_unlock(&rw_lock);
00256         return is_empty;
00257     }
00258 
00259     T& back()
00260     {
00261         g_static_rw_lock_reader_lock(&rw_lock);
00262         T& back = data.back();
00263         g_static_rw_lock_reader_unlock(&rw_lock);
00264         return back;
00265     }
00266 
00267     T& front()
00268     {
00269         g_static_rw_lock_reader_lock(&rw_lock);
00270         T& front = data.front();
00271         g_static_rw_lock_reader_unlock(&rw_lock);
00272         return front;
00273     }
00274 
00275     T& operator[](unsigned int idx)
00276     {
00277         g_static_rw_lock_reader_lock(&rw_lock);
00278         T& value = data[idx];
00279         g_static_rw_lock_reader_unlock(&rw_lock);
00280         return value;
00281     }
00282 
00283     // Write operations.
00284     void push_back(const T& value)
00285     {
00286         g_static_rw_lock_writer_lock(&rw_lock);
00287         data.push_back(value);
00288         g_static_rw_lock_writer_unlock(&rw_lock);
00289     }
00290 
00291     void push_front(const T& value)
00292     {
00293         g_static_rw_lock_writer_lock(&rw_lock);
00294         data.push_front(value);
00295         g_static_rw_lock_writer_unlock(&rw_lock);
00296     }
00297 
00298     void clear()
00299     {
00300         g_static_rw_lock_writer_lock(&rw_lock);
00301         data.clear();
00302         g_static_rw_lock_writer_unlock(&rw_lock);
00303     }
00304 
00305 private:
00306     std::deque<T> data;
00307     GStaticRWLock rw_lock;
00308 };
00309 
00310 }; // namespace text
00311 
00312 #endif // TEXT_BASE_TYPES_H
00313 
00314 
Generated by  doxygen 1.6.2-20100208