Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Adding extern C block for C++ use (#3)
Browse files Browse the repository at this point in the history
* Adding extern C block for C++ use

* Fixing unit tests
  • Loading branch information
BrianSipos authored Jan 17, 2024
1 parent 574250f commit 35e7243
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/camp/generators/create_gen_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def write(self, outfile: TextIO):

self.write_defines(outfile)
self.write_includes(outfile)
outfile.write(campch.make_cplusplus_open())
self.write_adm_template_documentation(outfile)

self.write_agent_nickname_definitions(outfile)
Expand All @@ -62,6 +63,7 @@ def write(self, outfile: TextIO):

self.write_initialization_functions(outfile)

outfile.write(campch.make_cplusplus_close())
self.write_endifs(outfile)

#
Expand Down Expand Up @@ -90,8 +92,9 @@ def write_endifs(self, outfile):
name_upper = self.adm.norm_name.upper()
ns_upper = self.adm.norm_namespace.replace("/", "_").upper()
endifs_str = """\
#endif /* _HAVE_{1}_ADM_ */
#endif // ADM_{0}_H_
#endif /* ADM_{0}_H_ */
"""
outfile.write(endifs_str.format(name_upper, ns_upper))

Expand Down Expand Up @@ -491,7 +494,7 @@ def write_initialization_functions(self, outfile):
name = self.adm.norm_namespace
body = (
"/* Initialization functions. */\n"
"void {0}_init();\n\n"
"void {0}_init();\n"
).format(name)

outfile.write(body.format(name))
4 changes: 3 additions & 1 deletion src/camp/generators/create_impl_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def write(self, outfile: TextIO):

self._scraper.write_custom_includes(outfile)
self.write_includes(outfile)
outfile.write(campch.make_cplusplus_open())

self._scraper.write_custom_type_enums(outfile)

Expand All @@ -75,6 +76,7 @@ def write(self, outfile: TextIO):
self.write_operator_functions(outfile)
self.write_table_functions(outfile)

outfile.write(campch.make_cplusplus_close())
self.write_endifs(outfile)

#
Expand All @@ -95,7 +97,7 @@ def write_endifs(self, outfile):
name_upper = self.adm.norm_name.upper()
endifs_str = """\
#endif // ADM_{0}_IMPL_H_
#endif /* ADM_{0}_IMPL_H_ */
"""
outfile.write(endifs_str.format(name_upper))

Expand Down
18 changes: 18 additions & 0 deletions src/camp/generators/lib/campch.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,3 +691,21 @@ def write_init_function(c_file, adm: ace.models.AdmFile, g_var_idx: str, mgr: bo

c_file.write(init_decls + "\n")
write_formatted_init_function(c_file, adm.norm_namespace, None, body)

def make_cplusplus_open():
''' Open an "extern C" block for C++ inclusion. '''
return """\
#ifdef __cplusplus
extern "C" {
#endif
"""

def make_cplusplus_close():
''' Close an "extern C" block for C++ inclusion. '''
return """\
#ifdef __cplusplus
}
#endif
"""
10 changes: 9 additions & 1 deletion tests/data/gen_ch/agent/adm_test_adm_impl.h.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include "shared/primitives/table.h"
#include "shared/primitives/tnv.h"

#ifdef __cplusplus
extern "C" {
#endif

/* START typeENUM */
/* TODO */
/* STOP typeENUM */
Expand Down Expand Up @@ -76,4 +80,8 @@ tnv_t *test_ns_op_plusreal64(vector_t *stack);
tbl_t *test_ns_tblt_variables(ari_t *id);
tbl_t *test_ns_tblt_rptts(ari_t *id);

#endif // ADM_TEST_ADM_IMPL_H_
#ifdef __cplusplus
}
#endif

#endif /* ADM_TEST_ADM_IMPL_H_ */
10 changes: 9 additions & 1 deletion tests/data/gen_ch/shared/adm/adm_test_adm.h.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#include "shared/utils/nm_types.h"
#include "shared/adm/adm.h"

#ifdef __cplusplus
extern "C" {
#endif


/*
* +---------------------------------------------------------------------------------------------+
Expand Down Expand Up @@ -193,5 +197,9 @@ extern vec_idx_t g_test_ns_idx[11];
/* Initialization functions. */
void test_ns_init();

#ifdef __cplusplus
}
#endif

#endif /* _HAVE_TEST_NS_ADM_ */
#endif // ADM_TEST_ADM_H_
#endif /* ADM_TEST_ADM_H_ */

0 comments on commit 35e7243

Please sign in to comment.