Skip to content

Commit

Permalink
Add header to core.h when there's a forward-declared type.
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaya-mankad committed Jul 18, 2024
1 parent 05f551b commit 9a3399d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ More complete examples of projects using the ASHRAE Standard 232P framework incl

### C++ Library Code Generation

Lattice's C++ code generation is achieved by calling the function `generate_cpp_project()`. Its only parameter is a list of submodule URLs.

#### Translations

Schema can be converted into C++ classes with the following mappings:
Expand Down Expand Up @@ -83,7 +85,7 @@ Schema can be converted into C++ classes with the following mappings:

#### Inheritance

The code generator will assume that *Data Group* schema elements with a *Data Group Template* parameter use that template as the element's superclass. However, the code for the superclass itself is _not_ generated; it must be provided by the **lattice** user. An `#include` statement for the expected superclass file is listed at the top of the schema's implemenation (cpp) file (note: see Big Ladder file naming conventions). If the superclass file is not found, the C++ generator will create a stub file for the class. If it is found, any virtual functions in the superclass will appear as overridden function stubs in the subclassed *Data Group*'s struct.
The code generator will assume that *Data Group* schema elements with a *Data Group Template* parameter use that template as the element's superclass. An `#include` statement for the expected superclass file is listed at the top of the schema's implemenation (cpp) file (note: see Big Ladder file naming conventions). If the superclass file is not found, the C++ generator will create a stub file for the class. If it is found, any virtual functions in the superclass will appear as overridden function stubs in the subclassed *Data Group*'s struct. Any additional code (members, methods) for the superclass itself must be provided by the **lattice** user.

In the event that the source schema contains a *Data Element* with a "selector constraint" (i.e. a list of possible *Data Type*s combined with an associated list of possible enumerator values for the selector *Data Element*), the C++ generated code will assume that the *Data Type*s in the list all derive from a common base class, named by the *Data Group Template* of the first *Data Type* in the list. An `#include` statement for the base class will be generated, as above. The code that populates the *Data Group* (the struct's `from_json` function) will use a conditional statement to create a new object of the correct subclass and assign it to a member `unique_ptr`. The base class declaration requires an initialize() function, which must be provided by the superclass implementation with the following signature:

Expand Down
10 changes: 9 additions & 1 deletion lattice/header_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, name, parent=None):
self.type = "namespace"
self.name = name
self.superclass = None
self.external_reference_sources = list()
self._opener = "{"
self._closure = "}"
self._parent_entry = parent
Expand Down Expand Up @@ -261,6 +262,7 @@ def _get_simple_type(self, type_str):
# schema name; its value will be a list of matchable data object names
for key in self._refs:
if internal_type in self._refs[key]:
self.external_reference_sources.append(f"{snake_style(key)}")
return f"{snake_style(key)}_ns::{internal_type}"

try:
Expand Down Expand Up @@ -633,7 +635,7 @@ def translate(self, input_file_path, top_namespace: str, forward_declarations_pa
def _load_meta_info(self, schema_section):
"""Store the global/common types and the types defined by any named references."""
self._root_data_group = schema_section.get("Root Data Group")
refs = {
refs: dict = {
f"{self._schema_name}": os.path.join(self._source_dir, f"{self._schema_name}.schema.yaml"),
"core": os.path.join(os.path.dirname(__file__), "core.schema.yaml"),
}
Expand Down Expand Up @@ -704,6 +706,12 @@ def _add_member_includes(self, data_element):
if include not in self._preamble:
self._preamble.append(include)
self._required_base_classes.append(data_element.superclass)
for external_source in data_element.external_reference_sources:
# This piece captures any "forward-declared" types that need to be
# processed by the DataElement type-finding mechanism before their header is known.
include = f"#include <{external_source}.h>"
if include not in self._preamble:
self._preamble.append(include)

def _add_function_overrides(self, parent_node, base_class_name):
"""Get base class virtual functions to be overridden."""
Expand Down
3 changes: 0 additions & 3 deletions lattice/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,8 @@ def generate_cpp_project(self, submodules: list[str]):
"""Generate CPP header files, source files, and build support files."""
h = HeaderTranslator()
c = CPPTranslator()
# root_groups = []
for schema in self.cpp_schemas:
h.translate(schema.path, self.root_directory.name, self.schema_directory_path)
# if h._root_data_group is not None:
# root_groups.append(h._root_data_group)
dump(str(h), schema.cpp_header_path)
c.translate(self.root_directory.name, h)
dump(str(c), schema.cpp_source_path)
Expand Down

0 comments on commit 9a3399d

Please sign in to comment.