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 Total, S.A
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 GEOSX_DATAREPOSITORY_KEYINDEXT_HPP_
20 #define GEOSX_DATAREPOSITORY_KEYINDEXT_HPP_
21 
22 #include <string>
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 /* GEOSX_DATAREPOSITORY_KEYINDEXT_HPP_ */
bool operator==(KEY_TYPE const &key) const
Comparison equals operator.
Definition: KeyIndexT.hpp:93
void setIndex(INDEX_TYPE const &index) const
Set the index.
Definition: KeyIndexT.hpp:128
KeyIndexT(KEY_TYPE const &key)
Constructor that sets the key.
Definition: KeyIndexT.hpp:61
KeyIndexT & operator=(KeyIndexT const &)=default
Copy assignment operator.
virtual ~KeyIndexT()
Destructor.
Definition: KeyIndexT.hpp:99
KEY_TYPE key_type
the type used for the map key
Definition: KeyIndexT.hpp:44
static constexpr INDEX_TYPE invalid_index
the value of an invalid index
Definition: KeyIndexT.hpp:50
KEY_TYPE const & Key() const
Access for the key.
Definition: KeyIndexT.hpp:105
INDEX_TYPE index_type
the type used for the index
Definition: KeyIndexT.hpp:47
KeyIndexT()=delete
Deleted default constructor.
std::string string
String type.
Definition: DataTypes.hpp:131
INDEX_TYPE const & Index() const
Access for the index.
Definition: KeyIndexT.hpp:112
bool isIndexSet() const
Check to see of the index has been set.
Definition: KeyIndexT.hpp:119