xlnt
cell_reference.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2024 xlnt-community
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE
21 //
22 // @license: http://www.opensource.org/licenses/mit-license.php
23 // @author: see AUTHORS file
24 
25 #pragma once
26 
27 #include <xlnt/xlnt_config.hpp>
28 #include <xlnt/cell/index_types.hpp>
29 
30 #include <cstdint>
31 #include <functional>
32 #include <string>
33 #include <tuple>
34 #include <utility>
35 
36 namespace xlnt {
37 
38 class cell_reference;
39 class range_reference;
40 
45 struct XLNT_API cell_reference_hash
46 {
50  std::size_t operator()(const cell_reference &k) const;
51 };
52 
60 class XLNT_API cell_reference
61 {
62 public:
66  static std::pair<std::string, row_t> split_reference(const std::string &reference_string);
67 
74  static std::pair<std::string, row_t> split_reference(
75  const std::string &reference_string, bool &absolute_column, bool &absolute_row);
76 
77  // constructors
78 
83 
84  // TODO: should these be explicit? The implicit conversion is nice sometimes.
85 
89  cell_reference(const char *reference_string);
90 
94  cell_reference(const std::string &reference_string);
95 
99  cell_reference(column_t column, row_t row);
100 
101  // absoluteness
102 
116  cell_reference &make_absolute(bool absolute_column = true, bool absolute_row = true);
117 
121  bool column_absolute() const;
122 
127  void column_absolute(bool absolute_column);
128 
132  bool row_absolute() const;
133 
138  void row_absolute(bool absolute_row);
139 
140  // getters/setters
141 
146  column_t column() const;
147 
151  void column(const std::string &column_string);
152 
156  column_t::index_t column_index() const;
157 
161  void column_index(column_t column);
162 
166  row_t row() const;
167 
171  void row(row_t row);
172 
179  cell_reference make_offset(int column_offset, int row_offset) const;
180 
184  std::string to_string() const;
185 
189  range_reference to_range() const;
190 
191  // operators
192 
198  range_reference operator,(const cell_reference &other) const;
199 
204  bool operator==(const cell_reference &comparand) const;
205 
210  bool operator==(const std::string &reference_string) const;
211 
216  bool operator==(const char *reference_string) const;
217 
222  bool operator!=(const cell_reference &comparand) const;
223 
228  bool operator!=(const std::string &reference_string) const;
229 
234  bool operator!=(const char *reference_string) const;
235 
236 private:
241  column_t column_;
242 
247  row_t row_;
248 
252  bool absolute_row_;
253 
257  bool absolute_column_;
258 };
259 
260 } // namespace xlnt
261 
262 namespace std {
263 template <>
264 struct hash<xlnt::cell_reference>
265 {
266  size_t operator()(const xlnt::cell_reference &x) const
267  {
268  static_assert(std::is_same<decltype(x.row()), std::uint32_t>::value, "this hash function expects both row and column to be 32-bit numbers");
269  static_assert(std::is_same<decltype(x.column_index()), std::uint32_t>::value, "this hash function expects both row and column to be 32-bit numbers");
270  return hash<std::uint64_t>{}(x.row() | static_cast<std::uint64_t>(x.column_index()) << 32);
271  }
272 };
273 } // namespace std
std::uint32_t index_t
Alias declaration for the internal numeric type of this column.
Definition: index_types.hpp:54
std::uint32_t row_t
All rows should be referred to by an instance of this type.
Definition: index_types.hpp:41
Definition: cell_reference.hpp:262
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:37
Functor for hashing a cell reference. Allows for use of std::unordered_set<cell_reference, cel_reference_hash> and similar.
Definition: cell_reference.hpp:45
bool operator==(std::nullptr_t, const cell &cell)
Returns true if this cell is uninitialized.
An object used to refer to a cell. References have two parts, the column and the row. In Excel, the reference string A1 refers to the top-left-most cell. A cell_reference can be initialized from a string of this form or a 1-indexed ordered pair of the form column, row.
Definition: cell_reference.hpp:60
column_t::index_t column_index() const
Returns a 1-indexed numeric index of the column of this reference.
Columns can be referred to as a string A,B,...Z,AA,AB,..,ZZ,AAA,...,ZZZ or as a 1-indexed index...
Definition: index_types.hpp:48
bool operator!=(const std::string &reference_string, const range_reference &ref)
Returns true if the string representation of the range is not equivalent to ref.
A range_reference describes a rectangular area of a worksheet with positive width and height defined ...
Definition: range_reference.hpp:36
row_t row() const
Returns a 1-indexed numeric index of the row of this reference.