7ac55db93a25b782805d4e7cdb46f076e4bdc2d5
[platform/core/ml/aitt.git] / external / C-Mock / v0.3.1 / cmock / cmock-function-class-mockers.h.pump
1 $$ -*- mode: c++; -*-
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
4 $$ googletest/scripts.
5 $$
6 $var n = 10  $$ The maximum arity we support.
7 // Copyright 2021, Hubert Jagodziński
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
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
19 // distribution.
20 //
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.
32 //
33 // Author: hubert.jagodzinski@gmail.com (Hubert Jagodziński)
34
35 // C Mock - Google Mock's extension allowing a function mocking.
36 //
37 // This file implements CMockMocker class and CMOCK_MOCK_FUNCTIONn() macros of various arities.
38
39 #ifndef CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_CLASS_MOCKERS_H_
40 #define CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_CLASS_MOCKERS_H_
41
42 #include <dlfcn.h>
43
44 #include <sstream>
45 #include <stdexcept>
46
47 // Allows finding an instance of a class:
48 //   class Foo : public CMockMocker<Foo> { ... }
49 //   Foo *p = CMockMocker<Foo>::cmock_get_instance();
50 template<typename T>
51 class CMockMocker
52 {
53 public:
54         CMockMocker()
55         {
56                 instance = (T *)this;
57         }
58
59         ~CMockMocker()
60         {
61                 instance = (T *)NULL;
62         }
63
64         static T *cmock_get_instance() { return instance; }
65         
66 private:
67         static T *instance;
68 };
69
70 template<typename T>
71 T *CMockMocker<T>::instance = NULL;
72
73 // Find the real implementation of a mocked function
74 static inline void *
75 cmock_lookup(const char *fname)
76 {
77     return dlsym(RTLD_NEXT, fname);
78 }
79
80 $range i 0..n
81
82 $for i [[
83 $range j 1..i
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]]]]
87
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); \
91 \
92 GMOCK_RESULT_(, F) n($declare_args) { \
93     c *mock = c::cmock_get_instance(); \
94     if (mock != NULL) { \
95         return mock->n($call_args); \
96     } \
97         \
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()); \
103     } \
104     return __cmock_real_##c##_##n($call_args); \
105 } \
106
107 ]]
108
109 #endif // CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_CLASS_MOCKERS_H_