c++ - Dealing with compilation times when editing headers -
i'm working on huge project uses system that, when running make, generates header files contain constants used everywhere in code. due size of project if 1 of headers changed (a constant removed or added) whole project must recompiled (which takes several hours).
my initial solution write sort of constantmanager
class has map each code-value pair , getter returns given code string returns value (pretty straight forward) , change header generator define constants #define
s expand instance of constantmanager
, call getter. problem not work switch
statements (well... case
statements actually) because return values not constant expressions.
my question is: there alternative solutions problem or trick make mine work switches?
you split huge header smaller ones , include those. might lot of initial work straight forward , compatible current solution.
another option create make constmanager class have constexpr members. not need cannot use map.
constmanager.h
namespace constmanager { namespace detail { struct constant { char const * const name; char const * const value; }; constant const * const constants; unsigned int total_variables; } inline char const * const constmanager::get(char const * needle) constexpr { using namespace constmanager::detail; /* not able find constexpr compatible binary search function*/ if(int = 0; < total_variables; ++i){ constant & const c = constants[i]; if(strcmp(c.name, needle) == 0){ return c.value; } } return nullptr; } }
constmanager.c should generated
constmanager::detail::constant constmanager::detail::constants [] = { {"first var", "first value"}, {"second var", "second value"} }; unsigned int constmanager::detail::total_variables = 2;
first solution proposed (this before first edit):
i think should replace defines regular external variables.
although 2 caveats:
- it not work if defines used concatenate strings
- it might make inlining harder if not impossible compiler if matters profiler can tell.
constmgr.h
// make needed constansts externs namespace constants { extern int const thefirstconstant; extern char const * const somestringconstant; }
and source file generated @ build time.
constexpr.cpp
int const constants::thefirstconstant = 1000; char const * const constants::somestringconstant = "hihihoho";
Comments
Post a Comment