GEOSX
KeyIndexT.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
19 #ifndef GEOS_DATAREPOSITORY_KEYINDEXT_HPP_
20 #define GEOS_DATAREPOSITORY_KEYINDEXT_HPP_
21 
22 
23 #include <ostream>
24 
37 template< typename KEY_TYPE = std::string,
38  typename INDEX_TYPE = int,
39  int INVALID_INDEX = -1 >
40 class KeyIndexT
41 {
42 public:
44  using key_type = KEY_TYPE;
45 
47  using index_type = INDEX_TYPE;
48 
50  constexpr static INDEX_TYPE invalid_index = INVALID_INDEX;
51 
55  KeyIndexT() = delete;
56 
61  KeyIndexT( KEY_TYPE const & key ):
62  m_key( key ),
63  m_index( INVALID_INDEX )
64  {}
65 
69  KeyIndexT( KeyIndexT const & ) = default;
70 
75  KeyIndexT & operator=( KeyIndexT const & ) = default;
76 
80  KeyIndexT( KeyIndexT && ) = default;
81 
86  KeyIndexT & operator=( KeyIndexT && ) = default;
87 
93  bool operator==( KEY_TYPE const & key ) const
94  { return m_key == key; }
95 
99  virtual ~KeyIndexT() {}
100 
105  KEY_TYPE const & key() const
106  { return m_key; }
107 
112  INDEX_TYPE const & index() const
113  { return m_index; }
114 
119  bool isIndexSet() const
120  {
121  return m_index==INVALID_INDEX ? false : true;
122  }
123 
128  void setIndex( INDEX_TYPE const & index ) const
129  {
130  m_index = index;
131  }
132 
133 private:
135  KEY_TYPE const m_key;
136 
138  INDEX_TYPE mutable m_index;
139 };
140 
150 template< typename KEY_TYPE, typename INDEX_TYPE, int INVALID_INDEX >
151 std::ostream & operator<<( std::ostream & os, const KeyIndexT< KEY_TYPE, INDEX_TYPE, INVALID_INDEX > key )
152 {
153  os << key.key();
154  return os;
155 }
156 
157 #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:151
constexpr static INDEX_TYPE invalid_index
the value of an invalid index
Definition: KeyIndexT.hpp:50
bool isIndexSet() const
Check to see of the index has been set.
Definition: KeyIndexT.hpp:119
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:47
void setIndex(INDEX_TYPE const &index) const
Set the index.
Definition: KeyIndexT.hpp:128
KeyIndexT(KeyIndexT const &)=default
Copy constructor.
bool operator==(KEY_TYPE const &key) const
Comparison equals operator.
Definition: KeyIndexT.hpp:93
KEY_TYPE key_type
the type used for the map key
Definition: KeyIndexT.hpp:44
KEY_TYPE const & key() const
Access for the key.
Definition: KeyIndexT.hpp:105
KeyIndexT(KEY_TYPE const &key)
Constructor that sets the key.
Definition: KeyIndexT.hpp:61
KeyIndexT(KeyIndexT &&)=default
Move constructor.
virtual ~KeyIndexT()
Destructor.
Definition: KeyIndexT.hpp:99
INDEX_TYPE const & index() const
Access for the index.
Definition: KeyIndexT.hpp:112
std::string string
String type.
Definition: DataTypes.hpp:131