1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \inpublicapi \file
36  * \brief
37  * Implements nblib utilities
38  *
39  * \author Victor Holanda <victor.holanda@cscs.ch>
40  * \author Joe Jordan <ejjordan@kth.se>
41  * \author Prashanth Kanduri <kanduri@cscs.ch>
42  * \author Sebastian Keller <keller@cscs.ch>
43  * \author Artem Zhmurov <zhmurov@gmail.com>
44  */
45 
46 #ifndef NBLIB_UTIL_UTIL_HPP
47 #define NBLIB_UTIL_UTIL_HPP
48 
49 #include <cassert>
50 
51 #include <sstream>
52 #include <string>
53 #include <tuple>
54 #include <type_traits>
55 #include <vector>
56 
57 namespace nblib
58 {
59 
60 /*! \brief A template to create structs as a type-safe alternative to using declarations
61  *
62  * \inpublicapi
63  * \ingroup nblib
64  *
65  * Used in public API functions where a distinction between different
66  * arguments of the same underlying type is desired. This provides a type-safe
67  * version to using declarations. Instead of naming a type alias, the name
68  * is used to define a struct that inherits from StrongType<T>, where T is
69  * the underlying type. For example:
70  *
71  * struct C6 : StrongType<real>
72  * {
73  *     using StrongType::StrongType;
74  *     using StrongType::operator=;
75  * };
76  *
77  * Due to the T() conversion and assignment from T,
78  * an instance of the resulting C6 struct behaves essentially like a real, while construction
79  * from real is disabled. This makes it impossible to pass a real as a function parameter
80  * of type C6.
81  */
82 template<class T, class Phantom>
83 struct StrongType
84 {
85     //! default ctor
StrongTypenblib::StrongType86     StrongType() : value_{} {}
87     //! construction from the underlying type T, implicit conversions disabled
StrongTypenblib::StrongType88     explicit StrongType(T v) : value_(std::move(v)) {}
89 
90     //! assignment from T
operator =nblib::StrongType91     StrongType& operator=(T v)
92     {
93         value_ = std::move(v);
94         return *this;
95     }
96 
97     //! conversion to T
operator Tnblib::StrongType98     operator T() const { return value_; }
99 
100     //! access the underlying value
valuenblib::StrongType101     T value() const { return value_; }
102 
103 private:
104     T value_;
105 };
106 
107 /*! \brief StrongType equality comparison
108  *
109  * Requires that both T and Phantom template parameters match.
110  * For the case where a comparison between StrongTypes with matching T, but differing Phantom
111  * parameters is desired, the underlying value attribute should be compared instead
112  */
113 template<class T, class Phantom>
operator ==(const StrongType<T,Phantom> & lhs,const StrongType<T,Phantom> & rhs)114 [[maybe_unused]] inline bool operator==(const StrongType<T, Phantom>& lhs, const StrongType<T, Phantom>& rhs)
115 {
116     return lhs.value() == rhs.value();
117 }
118 
119 //! comparison function <
120 template<class T, class Phantom>
operator <(const StrongType<T,Phantom> & lhs,const StrongType<T,Phantom> & rhs)121 inline bool operator<(const StrongType<T, Phantom>& lhs, const StrongType<T, Phantom>& rhs)
122 {
123     return lhs.value() < rhs.value();
124 }
125 
126 //! comparison function >
127 template<class T, class Phantom>
operator >(const StrongType<T,Phantom> & lhs,const StrongType<T,Phantom> & rhs)128 inline bool operator>(const StrongType<T, Phantom>& lhs, const StrongType<T, Phantom>& rhs)
129 {
130     return lhs.value() > rhs.value();
131 }
132 
133 //! \brief Utility to call function with each element in tuple_
134 template<class F, class... Ts>
for_each_tuple(F && func,std::tuple<Ts...> & tuple_)135 void for_each_tuple(F&& func, std::tuple<Ts...>& tuple_)
136 {
137     std::apply(
138             [f = func](auto&... args) {
139                 [[maybe_unused]] auto list = std::initializer_list<int>{ (f(args), 0)... };
140             },
141             tuple_);
142 }
143 
144 //! \brief Utility to call function with each element in tuple_ with const guarantee
145 template<class F, class... Ts>
for_each_tuple(F && func,const std::tuple<Ts...> & tuple_)146 void for_each_tuple(F&& func, const std::tuple<Ts...>& tuple_)
147 {
148     std::apply(
149             [f = func](auto&... args) {
150                 [[maybe_unused]] auto list = std::initializer_list<int>{ (f(args), 0)... };
151             },
152             tuple_);
153 }
154 
155 //! \brief Format strings for use in error messages
156 template<class... Args>
formatString(std::string fmt,Args...args)157 std::string formatString(std::string fmt, Args... args)
158 {
159     std::ostringstream os;
160     std::string        delimiter = "{}";
161 
162     auto next_token = [](std::string& s, const std::string& delimiter_)
163     {
164         std::string token = s.substr(0, s.find(delimiter_));
165 
166         std::size_t next = s.find(delimiter_);
167         if (next == std::string::npos)
168             s.clear();
169         else
170             s.erase(0, next + delimiter_.length());
171 
172         return token;
173     };
174 
175     [[maybe_unused]]
176     std::initializer_list<int> unused{ 0, (os << next_token(fmt, delimiter) << args, 0)... };
177 
178     os << next_token(fmt, delimiter);
179 
180     return os.str();
181 }
182 
183 } // namespace nblib
184 
185 #endif // NBLIB_UTIL_UTIL_HPP
186