Skip to content

Commit

Permalink
Generate support headers and normalize their names.
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaya-mankad committed Nov 12, 2023
1 parent b302564 commit a8b3621
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 8 deletions.
16 changes: 16 additions & 0 deletions lattice/cpp/generate_support_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from jinja2 import Template
import os
import sys
from lattice.file_io import dump
from lattice.util import snake_style
from pathlib import Path

# file_loader = FileSystemLoader(os.path.join(os.path.dirname(__file__), 'generation_templates'))
# env = Environment(loader=file_loader)

def generate_support_headers(namespace_name: str, output_directory: Path):
for template in Path(__file__).with_name("templates").iterdir():
enum_info = Template(template.read_text())
generated_file_name = "-".join(snake_style(template.stem).split("_"))
dump(enum_info.render(namespace=namespace_name), Path(output_directory) / generated_file_name)

16 changes: 16 additions & 0 deletions lattice/cpp/templates/enum-info.h.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef TYPEINFO_205_H_
#define TYPEINFO_205_H_

#include <string_view>

namespace {{namespace}} {

struct enum_info
{
std::string_view enumerant_name;
std::string_view display_text;
std::string_view description;
};
}

#endif // TYPEINFO_205_H_
33 changes: 33 additions & 0 deletions lattice/cpp/templates/load-object.h.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef LOADOBJECT_205_H_
#define LOADOBJECT_205_H_

#include <nlohmann/json.hpp>
#include <courierr/courierr.h>

namespace {{namespace}} {

template<class T>
void json_get(nlohmann::json j,
Courierr::Courierr& logger,
const char *subnode,
T& object,
bool& object_is_set,
bool required = false)
{
try
{
object = j.at(subnode).get<T>();
object_is_set = true;
}
catch (nlohmann::json::out_of_range & ex)
{
object_is_set = false;
if (required)
{
logger.warning(ex.what());
}
}
}
}

#endif
2 changes: 1 addition & 1 deletion lattice/cpp_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,4 @@ def _get_items_to_serialize(self, header_tree):
# .............................................................................................
def _add_included_headers(self, main_header):
self._preamble.clear()
self._preamble.append(f'#include <{snake_style(main_header)}.h>\n#include <loadobject_205.h>\n')
self._preamble.append(f'#include <{snake_style(main_header)}.h>\n#include <load-object.h>\n')
14 changes: 7 additions & 7 deletions lattice/header_entries.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import re
from .file_io import load, get_base_stem
from .util import snake_style
from .util import snake_style, bigladder_filename_style
from typing import Optional
import pathlib

Expand Down Expand Up @@ -600,7 +600,7 @@ def translate(self, input_file_path, top_namespace: str, forward_declarations_pa
'Name')
HeaderTranslator.modified_insertion_sort(self._namespace.child_entries)
# PerformanceMapBase object needs sibling grid/lookup vars to be created, so parse last
self._add_performance_overloads()
#self._add_performance_overloads()

# Final passes through dictionary in order to add elements related to serialization
for base_level_tag in (
Expand Down Expand Up @@ -657,21 +657,21 @@ def _add_included_headers(self, ref_list):
if ref_list:
includes = ''
for r in ref_list:
includes += f'#include <{snake_style(r)}.h>'
includes += f'#include <{bigladder_filename_style(r)}.h>'
includes += '\n'
self._preamble.append(includes)
self._preamble.append('#include <string>\n#include <vector>\n#include <nlohmann/json.hpp>\n#include <typeinfo_205.h>\n')
self._preamble.append('#include <string>\n#include <vector>\n#include <nlohmann/json.hpp>\n#include <enum-info.h>\n')

# .............................................................................................
def _add_member_headers(self, data_element):
if 'unique_ptr' in data_element.type:
m = re.search(r'\<(.*)\>', data_element.type)
if m:
include = f'#include <{snake_style(m.group(1))}.h>\n'
include = f'#include <{bigladder_filename_style(m.group(1))}.h>\n'
if include not in self._preamble:
self._preamble.append(include)
if data_element.superclass:
include = f'#include <{snake_style(data_element.superclass)}.h>\n'
include = f'#include <{bigladder_filename_style(data_element.superclass)}.h>\n'
if include not in self._preamble:
self._preamble.append(include)

Expand All @@ -680,7 +680,7 @@ def _add_function_overrides(self, parent_node, base_class_name):
'''Get base class virtual functions to be overridden.'''
base_class = os.path.join(os.path.dirname(__file__),
'src',
f'{snake_style(base_class_name)}.h')
f'{bigladder_filename_style(base_class_name)}.h')
try:
with open(base_class) as b:
for line in b:
Expand Down
2 changes: 2 additions & 0 deletions lattice/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .docs import HugoWeb, DocumentFile
from .header_entries import HeaderTranslator
from .cpp_entries import CPPTranslator
from lattice.cpp.generate_support_headers import generate_support_headers

class SchemaFile: # pylint:disable=R0902
"""Parse the components of a schema file."""
Expand Down Expand Up @@ -355,3 +356,4 @@ def generate_cpp_headers(self):
dump(str(h), schema.cpp_header_path)
c.translate(self.root_directory.name, h)
dump(str(c), schema.cpp_source_path)
generate_support_headers(self.root_directory.name, schema.cpp_header_path.parent)
3 changes: 3 additions & 0 deletions lattice/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ def snake_style(s):
#return ''.join(['_'+c.lower() if c.isupper() else c for c in s]).lstrip('_')
a = re.compile('((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))')
return a.sub(r'_\1', s).lower()

def bigladder_filename_style(s):
return "-".join(snake_style(s).split("_"))

0 comments on commit a8b3621

Please sign in to comment.