Skip to content

Commit

Permalink
Modernize the embedded sources, make them less noisy
Browse files Browse the repository at this point in the history
  • Loading branch information
lefticus committed Jul 18, 2023
1 parent a98f5f4 commit ff32d5b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
6 changes: 3 additions & 3 deletions embedded/CreateEmbeddedSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int main(int argc, char *argv[])
unsigned length = 0;

if (outstream.is_open()) {
outstream << "static const uint8_t embedded_file_" << filenum << "[] = {";
outstream << "static constexpr uint8_t embedded_file_" << filenum << "[] = {";
do {
strm.avail_in = static_cast<uInt>(fread(in.data(), 1, CHUNK, source));
if (ferror(source) != 0) {
Expand Down Expand Up @@ -95,9 +95,9 @@ int main(int argc, char *argv[])
outstream << "};";

outstream << "\n";
outstream << "static const char *embedded_file_name_" << filenum << " = \"" << embeddedname << "\";";
outstream << "static constexpr const char *embedded_file_name_" << filenum << " = \"" << embeddedname << "\";";
outstream << "\n";
outstream << "static const size_t embedded_file_len_" << filenum << " = " << std::dec << length << ";";
outstream << "static constexpr size_t embedded_file_len_" << filenum << " = " << std::dec << length << ";";
outstream << std::endl;

outstream.close();
Expand Down
3 changes: 2 additions & 1 deletion embedded/EmbedFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function(embed_files FILES EMBEDDED_LOCATIONS CXX_OUTPUT_FILES)

# datas
foreach (arg RANGE ${END})
set(EMBEDDED_FILES "${EMBEDDED_FILES} embedded_file_${arg}")
set(EMBEDDED_FILES "${EMBEDDED_FILES} static_cast<const uint8_t *>(embedded_file_${arg})")
if (NOT arg EQUAL MAXFILECOUNT)
set(EMBEDDED_FILES "${EMBEDDED_FILES},")
endif()
Expand All @@ -92,6 +92,7 @@ function(embed_files FILES EMBEDDED_LOCATIONS CXX_OUTPUT_FILES)
if( NAMESPACE )
set(BEGIN_NAMESPACE "namespace ${NAMESPACE} {")
set(END_NAMESPACE "}")
set(NESTED_NAMESPACE "${NAMESPACE}::")
endif()

configure_file("${PROJECT_SOURCE_DIR}/embedded/embedded_files.hxx.in" "${CMAKE_CURRENT_BINARY_DIR}/embedded_files.hxx")
Expand Down
30 changes: 10 additions & 20 deletions embedded/embedded_files.cxx.in
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
#include "embedded_files.hxx"

@BEGIN_NAMESPACE@

namespace embedded_files {

static const size_t embedded_file_count = @NUMFILES@;
namespace @NESTED_NAMESPACE@embedded_files {


@EMBEDDED_FILE_INCLUDES@

static const char *embedded_file_names[] = {
static constexpr std::array<const char *, @NUMFILES@> embedded_file_names{
@EMBEDDED_FILE_NAMES@
};

static const size_t embedded_file_lens[] = {
static constexpr std::array<size_t, @NUMFILES@> embedded_file_lens{
@EMBEDDED_FILE_LENS@
};

static const uint8_t *embedded_files[] = {
static constexpr std::array<const uint8_t *, @NUMFILES@> embedded_files{
@EMBEDDED_FILES@
};

const std::vector<std::string> &fileNames() {
static const auto result = [](){
std::vector<std::string> vals;
vals.reserve(embedded_file_count);
for (size_t i = 0; i < embedded_file_count; ++i) {
vals.push_back(std::string(embedded_file_names[i]));
}
return vals;
}();

static const auto result = std::vector<std::string>{embedded_file_names.begin(), embedded_file_names.end()};
return result;
}

Expand All @@ -38,10 +29,10 @@ namespace embedded_files {
static const auto fs = [](){
std::map<std::string, std::pair<size_t, const uint8_t *>> vals;
if (vals.empty()){
for (size_t i = 0; i < embedded_file_count; ++i) {
vals.insert(std::make_pair(std::string(embedded_file_names[i]),
std::make_pair(embedded_file_lens[i],
embedded_files[i])));
for (size_t i = 0; i < embedded_file_names.size(); ++i) {
vals.insert(std::make_pair(std::string(embedded_file_names.at(i)),
std::make_pair(embedded_file_lens.at(i),
embedded_files.at(i))));
}
}
return vals;
Expand All @@ -51,5 +42,4 @@ namespace embedded_files {

}

@END_NAMESPACE@

0 comments on commit ff32d5b

Please sign in to comment.