GEOS
KeyIndexT.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 Total, S.A
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_DATAREPOSITORY_KEYINDEXT_HPP_
21 #define GEOS_DATAREPOSITORY_KEYINDEXT_HPP_
22 
23 
24 #include <ostream>
25 
38 template< typename KEY_TYPE = std::string,
39  typename INDEX_TYPE = int,
40  int INVALID_INDEX = -1 >
41 class KeyIndexT
42 {
43 public:
45  using key_type = KEY_TYPE;
46 
48  using index_type = INDEX_TYPE;
49 
51  constexpr static INDEX_TYPE invalid_index = INVALID_INDEX;
52 
56  KeyIndexT() = delete;
57 
62  KeyIndexT( KEY_TYPE const & key ):
63  m_key( key ),
64  m_index( INVALID_INDEX )
65  {}
66 
70  KeyIndexT( KeyIndexT const & ) = default;
71 
76  KeyIndexT & operator=( KeyIndexT const & ) = default;
77 
81  KeyIndexT( KeyIndexT && ) = default;
82 
87  KeyIndexT & operator=( KeyIndexT && ) = default;
88 
94  bool operator==( KEY_TYPE const & key ) const
95  { return m_key == key; }
96 
100  virtual ~KeyIndexT() {}
101 
106  KEY_TYPE const & key() const
107  { return m_key; }
108 
113  INDEX_TYPE const & index() const
114  { return m_index; }
115 
120  bool isIndexSet() const
121  {
122  return m_index==INVALID_INDEX ? false : true;
123  }
124 
129  void setIndex( INDEX_TYPE const & index ) const
130  {
131  m_index = index;
132  }
133 
134 private:
136  KEY_TYPE const m_key;
137 
139  INDEX_TYPE mutable m_index;
140 };
141 
151 template< typename KEY_TYPE, typename INDEX_TYPE, int INVALID_INDEX >
152 std::ostream & operator<<( std::ostream & os, const KeyIndexT< KEY_TYPE, INDEX_TYPE, INVALID_INDEX > key )
153 {
154  os << key.key();
155  return os;
156 }
157 
158 #endif /* GEOS_DATAREPOSITORY_KEYINDEXT_HPP_ */
std::ostream & operator<<(std::ostream &os, const KeyIndexT< KEY_TYPE, INDEX_TYPE, INVALID_INDEX > key)
Print the KeyIndex to an output stream.
Definition: KeyIndexT.hpp:152
constexpr static INDEX_TYPE invalid_index
the value of an invalid index
Definition: KeyIndexT.hpp:51
bool isIndexSet() const
Check to see of the index has been set.
Definition: KeyIndexT.hpp:120
KeyIndexT & operator=(KeyIndexT const &)=default
Copy assignment operator.
KeyIndexT & operator=(KeyIndexT &&)=default
Move assignment operator.
KeyIndexT()=delete
Deleted default constructor.
INDEX_TYPE index_type
the type used for the index
Definition: KeyIndexT.hpp:48
void setIndex(INDEX_TYPE const &index) const
Set the index.
Definition: KeyIndexT.hpp:129
KeyIndexT(KeyIndexT const &)=default
Copy constructor.
bool operator==(KEY_TYPE const &key) const
Comparison equals operator.
Definition: KeyIndexT.hpp:94
KEY_TYPE key_type
the type used for the map key
Definition: KeyIndexT.hpp:45
KEY_TYPE const & key() const
Access for the key.
Definition: KeyIndexT.hpp:106
KeyIndexT(KEY_TYPE const &key)
Constructor that sets the key.
Definition: KeyIndexT.hpp:62
KeyIndexT(KeyIndexT &&)=default
Move constructor.
virtual ~KeyIndexT()
Destructor.
Definition: KeyIndexT.hpp:100
INDEX_TYPE const & index() const
Access for the index.
Definition: KeyIndexT.hpp:113
std::string string
String type.
Definition: DataTypes.hpp:91