Interface Documentation
Version: invalid
const_string.hh
Go to the documentation of this file.
1 /*
2  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
3  /@@///// /@@ @@////@@ @@////// /@@
4  /@@ /@@ @@@@@ @@ // /@@ /@@
5  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
6  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
7  /@@ /@@/@@//// //@@ @@ /@@/@@
8  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
9  // /// ////// ////// //////// //
10 
11  Copyright (c) 2016, Triad National Security, LLC
12  All rights reserved.
13  */
14 #pragma once
15 
18 #include "flecsi/util/hash.hh"
19 #include "flecsi/util/macros.hh"
20 
21 #include <cstring>
22 #include <limits>
23 #include <stdexcept>
24 
25 namespace flecsi {
26 namespace util {
27 
36 {
37 public:
38  using hash_type_t = std::size_t;
39 
46  template<hash_type_t N>
47  constexpr const_string_t(const char (&str)[N])
48  : str_(str), size_(N - 1) {} // const_string_t
49 
54  constexpr const char * c_str() const {
55  return str_;
56  } // c_str
57 
62  constexpr hash_type_t size() const {
63  return size_;
64  } // size
65 
70  constexpr char operator[](const hash_type_t i) const {
71  return i < size_ ? str_[i] : throw std::out_of_range("invalid index");
72  } // operator []
73 
78  constexpr hash_type_t hash() const {
79  return string_hash<hash_type_t>(str_, size_);
80  } // hash
81 
82 private:
83  constexpr bool equal_(const const_string_t & t, const std::size_t i) const {
84  return i == size_ ? true : (*this)[i] == t[i] && equal_(t, i + 1);
85  } // equal_
86 
87 public:
90  constexpr bool operator==(const const_string_t & t) const {
91  return size_ == t.size_ && equal_(t, 0);
92  } // operator ==
93 
96  constexpr bool operator!=(const const_string_t & t) const {
97  return !(*this == t);
98  } // operator !=
99 
100 private:
101  const char * const str_;
102  const hash_type_t size_;
103 
104 }; // const_string_t
105 
110 
114  std::size_t operator()(const const_string_t & str) const {
115  return str.hash();
116  } // operator ()
117 
118 }; // const_string_hasher_t
119 
120 } // namespace util
121 } // namespace flecsi
122 
123 /*----------------------------------------------------------------------------*
124  Helper interface.
125  *----------------------------------------------------------------------------*/
126 
137 #define flecsi_internal_string_hash(name) \
138  ::flecsi::util::const_string_t{name}.hash()
139 
150 #define flecsi_internal_hash(name) \
151  ::flecsi::util::const_string_t{flecsi_internal_stringify(name)}.hash()
Definition: const_string.hh:109
constexpr const char * c_str() const
Definition: const_string.hh:54
constexpr hash_type_t size() const
Definition: const_string.hh:62
constexpr const_string_t(const char(&str)[N])
Definition: const_string.hh:47
constexpr char operator[](const hash_type_t i) const
Definition: const_string.hh:70
Definition: const_string.hh:35
constexpr hash_type_t hash() const
Definition: const_string.hh:78
Definition: control.hh:31