SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_initializer_function.h
Go to the documentation of this file.
1/*****************************************************************************
2
3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4 more contributor license agreements. See the NOTICE file distributed
5 with this work for additional information regarding copyright ownership.
6 Accellera licenses this file to you under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with the
8 License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 implied. See the License for the specific language governing
16 permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21 sc_initializer_function.h -- Helper class and macros to enable in-class
22 initialization of complex objects
23
24 Original Authors: Roman I. Popov, Intel
25 Philipp A. Hartmann, Intel
26 *****************************************************************************/
27
28
29#ifndef SC_CORE_SC_INITIALIZER_FUNCTION_H_INCLUDED_
30#define SC_CORE_SC_INITIALIZER_FUNCTION_H_INCLUDED_
31
33
36#include <functional>
37#include <utility>
38
39#define SC_INIT(object_name) \
40 sc_core::sc_initializer_function \
41 SC_CONCAT_HELPER_(object_name, _initialization_fn_lambda) { [this]() { \
42 SC_CONCAT_HELPER_(object_name, _initialization_fn)(); \
43 }};\
44 void SC_CONCAT_HELPER_(object_name,_initialization_fn)()
45
46#define SC_NAMED_WITH_INIT(object_name,...) \
47 object_name { sc_core::sc_initializer_function_name_fwd(SC_STRINGIFY_HELPER_(object_name), \
48 [this]{ SC_CONCAT_HELPER_(object_name,_initialization_fn)(); }), __VA_ARGS__ }; \
49 void SC_CONCAT_HELPER_(object_name,_initialization_fn)()
50
51#define SC_THREAD_IMP(thread_name, ...) \
52 SC_INIT(thread_name) { \
53 SC_THREAD(thread_name); \
54 { __VA_ARGS__ } \
55 } \
56 void thread_name()
57
58#define SC_CTHREAD_IMP(thread_name, edge, ...) \
59 SC_INIT(thread_name) { \
60 SC_CTHREAD(thread_name, edge); \
61 { __VA_ARGS__ } \
62 } \
63 void thread_name()
64
65#define SC_METHOD_IMP(method_name, ...) \
66 SC_INIT(method_name) { \
67 SC_METHOD(method_name); \
68 { __VA_ARGS__ } \
69 } \
70 void method_name()
71
72
73namespace sc_core {
74
76public:
77 template<class F>
78 explicit sc_initializer_function(F&& fn) {
79
80 auto simctx = sc_get_curr_simcontext();
81
82 if (simctx->elaboration_done())
83 SC_REPORT_ERROR(SC_ID_INSERT_INITIALIZER_FN_, "after elaboration done");
84 else {
85 auto curr_module = static_cast<sc_module*>( simctx->hierarchy_curr() );
86 if (curr_module == nullptr)
87 SC_REPORT_ERROR(SC_ID_INSERT_INITIALIZER_FN_, "outside of module hierarchy");
88 else {
89 sc_assert(curr_module->m_module_name_p != nullptr);
90 curr_module->m_module_name_p->m_initializer_fn_vec.emplace_back(std::move(fn));
91 }
92
93 }
94 }
95};
96
97template <class F>
98inline const char * sc_initializer_function_name_fwd (const char *name, F&& fn)
99{
100 sc_initializer_function(std::move(fn));
101 return name;
102}
103
104
105}
106
107#endif // SC_CORE_SC_INITIALIZER_FUNCTION_H_INCLUDED_
#define sc_assert(expr)
Definition: sc_report.h:248
#define SC_REPORT_ERROR(msg_type, msg)
Definition: sc_report.h:217
sc_simcontext * sc_get_curr_simcontext()
const char * sc_initializer_function_name_fwd(const char *name, F &&fn)
const char SC_ID_INSERT_INITIALIZER_FN_[]