Debian Codegraph @

testing/projects/cpp_func_overloads/include/overload/overload.hpp

raw
1 /***************************************************************************************
2 * This file is dedicated to the public domain. If your jurisdiction requires a *
3 * specific license: *
4 * *
5 * Copyright (c) Stephen McDowell, 2017-2024 *
6 * License: CC0 1.0 Universal *
7 * License Text: https://creativecommons.org/publicdomain/zero/1.0/legalcode *
8 **************************************************************************************/
9 /**
10 * \file
11 *
12 * \brief Various function overloads to ensure the right signature is parsed.
13 */
14 #pragma once
15
16 #include <cstddef>
17 #include <string>
18 #include <vector>
19 #include <type_traits>
20
21 namespace overload {
22 /// This function does nothing.
23 inline void blargh() { }
24
25 /// Returns the integer ``x``.
26 inline int blargh(int x) { return x; }
27
28 /// Returns the integer ``x + y``.
29 inline int blargh(int x, int y) { return x + y; }
30
31 /// Returns the integer ``x + y + z``.
32 inline int blargh(int x, int y, int z) { return x + y + z; }
33
34 /// Returns the sum of the input parameters. Params unnamed for internal testing.
35 int blargh(int, int, int, int);
36
37 /// Returns the float ``-1.0f * x``.
38 inline float blargh(float x) { return -1.0f * x; }
39
40 /// Returns the float ``-1.0f * (x + y)``.
41 inline float blargh(float x, float y) { return -1.0f * (x + y); }
42
43 /// Returns the float ``-1.0f * (x + y + z)``.
44 inline float blargh(float x, float y, float z) { return -1.0f * (x + y + z); }
45
46 /// Returns the float ``-1.0f * (sum of parameters)``. Params unnamed for internal testing.
47 float blargh(float, float, float, float);
48
49 /// Returns a copy of the string ``x``.
50 inline std::string blargh(const std::string &x) { return x; }
51
52 /// Returns a concatenation of ``x + y``.
53 inline std::string blargh(const std::string &x, const std::string &y) { return x + y; }
54
55 /// Returns a concatenation of ``x + y + z``.
56 inline std::string blargh(const std::string &x, const std::string &y, const std::string &z) {
57 return x + y + z;
58 }
59
60 /// Returns a concatenation of the input parameters. Params unnamed for internal testing.
61 std::string blargh(const std::string&,
62 const std::string&,
63 const std::string&,
64 const std::string&);
65
66 /// Returns ``x + s.length()``.
67 inline std::size_t blargh(std::size_t x, const std::string &s) {
68 return x + s.length();
69 }
70
71 /// Returns ``x + static_cast<std::size_t>(y) + static_cast<std::size_t>(z) + s.length()``.
72 inline std::size_t blargh(std::size_t x, const float &y, double z, const std::string &s) {
73 return x + static_cast<std::size_t>(y) + static_cast<std::size_t>(z) + s.length();
74 }
75
76 /// Adds ``"blargh"`` to the end of ``v``.
77 inline void blargh(std::vector<std::string> &v) { v.emplace_back("blargh"); }
78
79 /// Adds ``std::vector<int>({0})`` to the end of ``v``.
80 inline void blargh(std::vector<std::vector<int>> &v){
81 v.emplace_back(std::move(std::vector<int>({0})));
82 }
83
84 /// Sums: ``z_i = x_i + y_i`` for ``i`` in range ``[0, N)``. No error checks performed.
85 inline void blargh(const float *x, const float *y, float *z, std::size_t N) {
86 for (std::size_t i = 0; i < N; ++i) {
87 z[i] = x[i] + y[i];
88 }
89 }
90
91 /// Returns (a copy of) input parameter ``t``.
92 template <class C>
93 typename C::type blargh(typename C::type t) {
94 return t;
95 }
96
97 #if !defined(DOXYGEN_SHOULD_SKIP_THIS)
98 // NOTE: skip these to make cpp_func_overloads easier to test (don't intro structs).
99
100 /// A super struct of structs for template specialization.
101 struct SuperStruct {
102 using type = int;///< The type for typing things.
103 };
104
105 namespace nested {
106 /// A super struct of structs in a nested workspace for template specialization.
107 struct SuperStruct {
108 using type = int;///< The type for typing things.
109 };
110 } // namespace nested
111 #endif // DOXYGEN_SHOULD_SKIP_THIS
112
113 /// A specialization of ``template <class C> typename C::type blargh``.
114 template <> inline int blargh<SuperStruct>(int x) { return x; }
115
116 /// A specialization of ``template <class C> typename C::type blargh``.
117 template <> inline int blargh<nested::SuperStruct>(int x) { return x; }
118
119 /// Returns parameter ``t`` converted to a ``T`` (enabled when ``std::is_convertible`` is ``true``).
120 template <class C, typename T>
121 typename std::enable_if<
122 std::is_convertible<typename C::type, T>::value, T
123 >::type blargh(typename C::type t) {
124 return t;
125 }
126
127 /// Returns a default constructed ``T`` (enabled when ``std::is_convertible`` is ``false``).
128 template <class C, typename T>
129 typename std::enable_if<
130 !std::is_convertible<typename C::type, T>::value, T
131 >::type blargh(typename C::type t) {
132 return T();
133 }
134 }// namespace overload
135
136 /// A free hanging function shows up in the file XML page, the others are in the namespace XML.
137 inline int blargh(int x) { return x; }