check_size_match.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_ERR_CHECK_SIZE_MATCH_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_CHECK_SIZE_MATCH_HPP
3 
4 #include <boost/type_traits/common_type.hpp>
7 #include <sstream>
8 #include <string>
9 
10 namespace stan {
11 namespace math {
12 
13 /**
14  * Check if the provided sizes match.
15  *
16  * @tparam T_size1 Type of size 1
17  * @tparam T_size2 Type of size 2
18  *
19  * @param function Function name (for error messages)
20  * @param name_i Variable name 1 (for error messages)
21  * @param i Size 1
22  * @param name_j Variable name 2 (for error messages)
23  * @param j Size 2
24  *
25  * @throw <code>std::invalid_argument</code> if the sizes
26  * do not match
27  */
28 template <typename T_size1, typename T_size2>
29 inline void check_size_match(const char* function, const char* name_i,
30  T_size1 i, const char* name_j, T_size2 j) {
31  if (likely(i == static_cast<T_size1>(j)))
32  return;
33 
34  std::ostringstream msg;
35  msg << ") and " << name_j << " (" << j << ") must match in size";
36  std::string msg_str(msg.str());
37  invalid_argument(function, name_i, i, "(", msg_str.c_str());
38 }
39 
40 /**
41  * Check if the provided sizes match.
42  *
43  * @tparam T_size1 Type of size 1
44  * @tparam T_size2 Type of size 2
45  *
46  * @param function Function name (for error messages)
47  * @param expr_i Expression for variable name 1 (for error messages)
48  * @param name_i Variable name 1 (for error messages)
49  * @param i Size 1
50  * @param expr_j Expression for variable name 2 (for error messages)
51  * @param name_j Variable name 2 (for error messages)
52  * @param j Size 2
53  *
54  * @throw <code>std::invalid_argument</code> if the sizes
55  * do not match
56  */
57 template <typename T_size1, typename T_size2>
58 inline void check_size_match(const char* function, const char* expr_i,
59  const char* name_i, T_size1 i, const char* expr_j,
60  const char* name_j, T_size2 j) {
61  if (likely(i == static_cast<T_size1>(j)))
62  return;
63  std::ostringstream updated_name;
64  updated_name << expr_i << name_i;
65  std::string updated_name_str(updated_name.str());
66  std::ostringstream msg;
67  msg << ") and " << expr_j << name_j << " (" << j << ") must match in size";
68  std::string msg_str(msg.str());
69  invalid_argument(function, updated_name_str.c_str(), i, "(", msg_str.c_str());
70 }
71 
72 } // namespace math
73 } // namespace stan
74 #endif
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
const double j
Definition: BetheBloch.cxx:29
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
#define likely(expr)
Definition: lz4.cxx:120