2 $$ This is a Pump source file. Please use Pump to convert it to
3 $$ cmock-function-mockers.h. Pump (pump.py) can be found in
6 $var n = 10 $$ The maximum arity we support.
7 // Copyright 2021, Hubert Jagodziński
8 // All rights reserved.
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
14 // * Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 // * Redistributions in binary form must reproduce the above
17 // copyright notice, this list of conditions and the following disclaimer
18 // in the documentation and/or other materials provided with the
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Author: hubert.jagodzinski@gmail.com (Hubert Jagodziński)
35 // C Mock - Google Mock's extension allowing a function mocking.
37 // This file implements CMockMocker class and CMOCK_MOCK_FUNCTIONn() macros of various arities.
39 #ifndef CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_CLASS_MOCKERS_H_
40 #define CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_CLASS_MOCKERS_H_
47 // Allows finding an instance of a class:
48 // class Foo : public CMockMocker<Foo> { ... }
49 // Foo *p = CMockMocker<Foo>::cmock_get_instance();
64 static T *cmock_get_instance() { return instance; }
71 T *CMockMocker<T>::instance = NULL;
73 // Find the real implementation of a mocked function
75 cmock_lookup(const char *fname)
77 return dlsym(RTLD_NEXT, fname);
84 $var call_args = [[$for j, [[cmock_a$j]]]]
85 $var declare_args = [[$for j, [[GMOCK_ARG_(, $j, F) cmock_a$j]]]]
86 $var matcher_args = [[$for j, [[GMOCK_MATCHER_(, $j, F) cmock_a$j]]]]
88 #define CMOCK_MOCK_FUNCTION$i(c, n, F) \
89 static GMOCK_RESULT_(, F) (*__cmock_real_##c##_##n)($declare_args) = \
90 (GMOCK_RESULT_(, F) (*)($declare_args))cmock_lookup(#n); \
92 GMOCK_RESULT_(, F) n($declare_args) { \
93 c *mock = c::cmock_get_instance(); \
95 return mock->n($call_args); \
98 if (__cmock_real_##c##_##n == NULL) { \
99 std::ostringstream msg; \
100 msg << "Error: Function " << #n; \
101 msg << " not found. Neither mock nor real function is present."; \
102 throw std::logic_error(msg.str()); \
104 return __cmock_real_##c##_##n($call_args); \
109 #endif // CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_CLASS_MOCKERS_H_