LCOV - code coverage report
Current view: top level - include/xapian - valueiterator.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 23 23 100.0 %
Date: 2019-06-30 05:20:33 Functions: 7 7 100.0 %
Branches: 7 10 70.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file  valueiterator.h
       2                 :            :  *  @brief Class for iterating over document values.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2008,2009,2010,2011,2012,2013,2014,2015 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2 of the
       9                 :            :  * License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
      19                 :            :  * USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef XAPIAN_INCLUDED_VALUEITERATOR_H
      23                 :            : #define XAPIAN_INCLUDED_VALUEITERATOR_H
      24                 :            : 
      25                 :            : #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
      26                 :            : # error "Never use <xapian/valueiterator.h> directly; include <xapian.h> instead."
      27                 :            : #endif
      28                 :            : 
      29                 :            : #include <iterator>
      30                 :            : #include <string>
      31                 :            : 
      32                 :            : #include <xapian/attributes.h>
      33                 :            : #include <xapian/derefwrapper.h>
      34                 :            : #include <xapian/types.h>
      35                 :            : #include <xapian/visibility.h>
      36                 :            : 
      37                 :            : namespace Xapian {
      38                 :            : 
      39                 :            : /// Class for iterating over document values.
      40                 :            : class XAPIAN_VISIBILITY_DEFAULT ValueIterator {
      41                 :            :     void decref();
      42                 :            : 
      43                 :            :   public:
      44                 :            :     /// Class representing the ValueIterator internals.
      45                 :            :     class Internal;
      46                 :            :     /// @private @internal Reference counted internals.
      47                 :            :     Internal * internal;
      48                 :            : 
      49                 :            :     /** @private @internal Wrap an existing Internal. */
      50                 :            :     XAPIAN_VISIBILITY_INTERNAL
      51                 :            :     explicit ValueIterator(Internal *internal_);
      52                 :            : 
      53                 :            :     /// Copy constructor.
      54                 :            :     ValueIterator(const ValueIterator & o);
      55                 :            : 
      56                 :            :     /// Assignment.
      57                 :            :     ValueIterator & operator=(const ValueIterator & o);
      58                 :            : 
      59                 :            :     /// Move constructor.
      60                 :          1 :     ValueIterator(ValueIterator && o)
      61                 :          1 :         : internal(o.internal) {
      62                 :          1 :         o.internal = nullptr;
      63                 :          1 :     }
      64                 :            : 
      65                 :            :     /// Move assignment operator.
      66                 :     273846 :     ValueIterator & operator=(ValueIterator && o) {
      67         [ +  - ]:     273846 :         if (this != &o) {
      68         [ +  + ]:     273846 :             if (internal) decref();
      69                 :     273846 :             internal = o.internal;
      70                 :     273846 :             o.internal = nullptr;
      71                 :            :         }
      72                 :     273846 :         return *this;
      73                 :            :     }
      74                 :            : 
      75                 :            :     /** Default constructor.
      76                 :            :      *
      77                 :            :      *  Creates an uninitialised iterator, which can't be used before being
      78                 :            :      *  assigned to, but is sometimes syntactically convenient.
      79                 :            :      */
      80                 :    3830352 :     XAPIAN_NOTHROW(ValueIterator())
      81                 :    3830352 :         : internal(0) { }
      82                 :            : 
      83                 :            :     /// Destructor.
      84                 :    4213819 :     ~ValueIterator() {
      85         [ +  + ]:    4213819 :         if (internal) decref();
      86                 :    4213819 :     }
      87                 :            : 
      88                 :            :     /// Return the value at the current position.
      89                 :            :     std::string operator*() const;
      90                 :            : 
      91                 :            :     /// Advance the iterator to the next position.
      92                 :            :     ValueIterator & operator++();
      93                 :            : 
      94                 :            :     /// Advance the iterator to the next position (postfix version).
      95                 :         12 :     DerefWrapper_<std::string> operator++(int) {
      96                 :         12 :         const std::string & value(**this);
      97         [ +  - ]:         12 :         operator++();
      98         [ +  - ]:         12 :         return DerefWrapper_<std::string>(value);
      99                 :            :     }
     100                 :            : 
     101                 :            :     /** Return the docid at the current position.
     102                 :            :      *
     103                 :            :      *  If we're iterating over values of a document, this method will throw
     104                 :            :      *  Xapian::InvalidOperationError.
     105                 :            :      */
     106                 :            :     Xapian::docid get_docid() const;
     107                 :            : 
     108                 :            :     /** Return the value slot number for the current position.
     109                 :            :      *
     110                 :            :      *  If the iterator is over all values in a slot, this returns that slot's
     111                 :            :      *  number.  If the iterator is over the values in a particular document,
     112                 :            :      *  it returns the number of each slot in turn.
     113                 :            :      */
     114                 :            :     Xapian::valueno get_valueno() const;
     115                 :            : 
     116                 :            :     /** Advance the iterator to document id or value slot @a docid_or_slot.
     117                 :            :      *
     118                 :            :      *  If this iterator is over values in a document, then this method
     119                 :            :      *  advances the iterator to value slot @a docid_or_slot, or the first slot
     120                 :            :      *  after it if there is no value in slot @a slot.
     121                 :            :      *
     122                 :            :      *  If this iterator is over values in a particular slot, then this
     123                 :            :      *  method advances the iterator to document id @a docid_or_slot, or the
     124                 :            :      *  first document id after it if there is no value in the slot we're
     125                 :            :      *  iterating over for document @a docid_or_slot.
     126                 :            :      *
     127                 :            :      *  Note: The "two-faced" nature of this method is due to how C++
     128                 :            :      *  overloading works.  Xapian::docid and Xapian::valueno are both typedefs
     129                 :            :      *  for the same unsigned integer type, so overloading can't distinguish
     130                 :            :      *  them.
     131                 :            :      *
     132                 :            :      *  @param docid_or_slot    The docid/slot to advance to.
     133                 :            :      */
     134                 :            :     void skip_to(Xapian::docid docid_or_slot);
     135                 :            : 
     136                 :            :     /** Check if the specified docid occurs.
     137                 :            :      *
     138                 :            :      *  The caller is required to ensure that the specified document id
     139                 :            :      *  @a did actually exists in the database.
     140                 :            :      *
     141                 :            :      *  This method acts like skip_to() if that can be done at little extra
     142                 :            :      *  cost, in which case it then returns true.  This is how
     143                 :            :      *  glass databases behave because they store values in streams which allow
     144                 :            :      *  for an efficient implementation of skip_to().
     145                 :            :      *
     146                 :            :      *  Otherwise it simply checks if a particular docid is present.  If it
     147                 :            :      *  is, it returns true.  If it isn't, it returns false, and leaves the
     148                 :            :      *  position unspecified (and hence the result of calling methods which
     149                 :            :      *  depend on the current position, such as get_docid(), are also
     150                 :            :      *  unspecified).  In this state, next() will advance to the first matching
     151                 :            :      *  position after document @a did, and skip_to() will act as it would if
     152                 :            :      *  the position was the first matching position after document @a did.
     153                 :            :      *
     154                 :            :      *  Currently the inmemory and remote backends behave in the
     155                 :            :      *  latter way because they don't support streamed values and so skip_to()
     156                 :            :      *  must check each document it skips over which is significantly slower.
     157                 :            :      *
     158                 :            :      *  @param docid    The document id to check.
     159                 :            :      */
     160                 :            : #ifndef check
     161                 :            :     bool check(Xapian::docid docid);
     162                 :            : #else
     163                 :            :     // The AssertMacros.h header in the OS X SDK currently defines a check
     164                 :            :     // macro.  Apple have deprecated check() in favour of __Check() and
     165                 :            :     // plan to remove check() in a "future release", but for now prevent
     166                 :            :     // expansion of check by adding parentheses in the method prototype:
     167                 :            :     // https://www.opensource.apple.com/source/CarbonHeaders/CarbonHeaders-18.1/AssertMacros.h
     168                 :            :     //
     169                 :            :     // We do this conditionally, as these parentheses trip up SWIG's
     170                 :            :     // parser:
     171                 :            :     // https://github.com/swig/swig/issues/45
     172                 :            :     bool (check)(Xapian::docid docid);
     173                 :            : #endif
     174                 :            : 
     175                 :            :     /// Return a string describing this object.
     176                 :            :     std::string get_description() const;
     177                 :            : 
     178                 :            :     /** @private @internal ValueIterator is what the C++ STL calls an
     179                 :            :      *  input_iterator.
     180                 :            :      *
     181                 :            :      *  The following typedefs allow std::iterator_traits<> to work so that
     182                 :            :      *  this iterator can be used with the STL.
     183                 :            :      *
     184                 :            :      *  These are deliberately hidden from the Doxygen-generated docs, as the
     185                 :            :      *  machinery here isn't interesting to API users.  They just need to know
     186                 :            :      *  that Xapian iterator classes are compatible with the STL.
     187                 :            :      */
     188                 :            :     // @{
     189                 :            :     /// @private
     190                 :            :     typedef std::input_iterator_tag iterator_category;
     191                 :            :     /// @private
     192                 :            :     typedef std::string value_type;
     193                 :            :     /// @private
     194                 :            :     typedef Xapian::doccount_diff difference_type;
     195                 :            :     /// @private
     196                 :            :     typedef std::string * pointer;
     197                 :            :     /// @private
     198                 :            :     typedef std::string & reference;
     199                 :            :     // @}
     200                 :            : };
     201                 :            : 
     202                 :            : bool
     203                 :            : XAPIAN_NOTHROW(operator==(const ValueIterator &a, const ValueIterator &b));
     204                 :            : 
     205                 :            : /// Equality test for ValueIterator objects.
     206                 :            : inline bool
     207                 :    3408224 : operator==(const ValueIterator &a, const ValueIterator &b) XAPIAN_NOEXCEPT
     208                 :            : {
     209                 :            :     // Use a pointer comparison - this ensures both that (a == a) and correct
     210                 :            :     // handling of end iterators (which we ensure have NULL internals).
     211                 :    3408224 :     return a.internal == b.internal;
     212                 :            : }
     213                 :            : 
     214                 :            : bool
     215                 :            : XAPIAN_NOTHROW(operator!=(const ValueIterator &a, const ValueIterator &b));
     216                 :            : 
     217                 :            : /// Inequality test for ValueIterator objects.
     218                 :            : inline bool
     219                 :    3250201 : operator!=(const ValueIterator &a, const ValueIterator &b) XAPIAN_NOEXCEPT
     220                 :            : {
     221                 :    3250201 :     return !(a == b);
     222                 :            : }
     223                 :            : 
     224                 :            : }
     225                 :            : 
     226                 :            : #endif // XAPIAN_INCLUDED_VALUEITERATOR_H

Generated by: LCOV version 1.11