xlnt/include/xlnt/worksheet/range.hpp

171 lines
4.5 KiB
C++
Raw Normal View History

// Copyright (c) 2015 Thomas Fussell
2014-06-06 04:19:31 +08:00
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
2014-05-22 05:48:51 +08:00
#pragma once
#include <iterator>
2014-05-22 05:48:51 +08:00
#include <memory>
#include <string>
#include <vector>
#include "cell_vector.hpp"
#include "major_order.hpp"
2014-06-06 04:19:31 +08:00
#include "range_reference.hpp"
#include "worksheet.hpp"
2014-05-22 05:48:51 +08:00
namespace xlnt {
class range
2014-05-22 05:48:51 +08:00
{
public:
template <bool is_const = true>
class common_iterator : public std::iterator<std::bidirectional_iterator_tag, cell>
2014-05-22 05:48:51 +08:00
{
public:
common_iterator(worksheet ws, const range_reference &start_cell, major_order order = major_order::row)
: ws_(ws), current_cell_(start_cell.get_top_left()), range_(start_cell), order_(order)
{
}
common_iterator(const common_iterator<false> &other)
{
*this = other;
}
cell_vector operator*();
bool operator==(const common_iterator &other) const
{
return ws_ == other.ws_ && current_cell_ == other.current_cell_ && order_ == other.order_;
}
bool operator!=(const common_iterator &other) const
{
return !(*this == other);
}
common_iterator &operator--()
{
if (order_ == major_order::row)
{
current_cell_.set_row(current_cell_.get_row() - 1);
}
else
{
current_cell_.set_column_index(current_cell_.get_column_index() - 1);
}
return *this;
}
common_iterator operator--(int)
{
iterator old = *this;
--*this;
return old;
}
common_iterator &operator++()
{
if (order_ == major_order::row)
{
current_cell_.set_row(current_cell_.get_row() + 1);
}
else
{
current_cell_.set_column_index(current_cell_.get_column_index() + 1);
}
return *this;
}
common_iterator operator++(int)
{
iterator old = *this;
++*this;
return old;
}
friend class common_iterator<true>;
private:
2014-05-22 05:48:51 +08:00
worksheet ws_;
cell_reference current_cell_;
range_reference range_;
major_order order_;
2014-05-22 05:48:51 +08:00
};
using iterator = common_iterator<false>;
using const_iterator = common_iterator<true>;
range(worksheet ws, const range_reference &reference, major_order order = major_order::row);
2014-05-22 05:48:51 +08:00
~range();
2014-07-20 04:59:05 +08:00
cell_vector operator[](std::size_t vector_index);
2014-05-22 05:48:51 +08:00
2014-07-20 04:59:05 +08:00
const cell_vector operator[](std::size_t vector_index) const;
2014-05-22 05:48:51 +08:00
bool operator==(const range &comparand) const;
bool operator!=(const range &comparand) const
{
return !(*this == comparand);
}
2014-05-22 05:48:51 +08:00
2014-07-20 04:59:05 +08:00
cell_vector get_vector(std::size_t vector_index);
2014-05-22 05:48:51 +08:00
2014-07-20 04:59:05 +08:00
const cell_vector get_vector(std::size_t vector_index) const;
2014-05-22 05:48:51 +08:00
cell get_cell(const cell_reference &ref);
const cell get_cell(const cell_reference &ref) const;
range_reference get_reference() const;
2014-07-20 04:59:05 +08:00
std::size_t length() const;
bool contains(const cell_reference &ref);
2014-05-22 05:48:51 +08:00
iterator begin();
iterator end();
const_iterator begin() const
{
return cbegin();
}
const_iterator end() const
{
return cend();
}
2014-05-22 05:48:51 +08:00
const_iterator cbegin() const;
const_iterator cend() const;
private:
2014-05-22 05:48:51 +08:00
worksheet ws_;
range_reference ref_;
major_order order_;
2014-05-22 05:48:51 +08:00
};
} // namespace xlnt