vlastachu
vlastachu

Reputation: 257

Is it posible to collect data at compile time

Objective: In different parts of code I am loading files (textures and fonts in my case). Thus downloading files can occur at any time of execution of the program (although all cached, but still.)I would like to download happens only once at startup.

Possible solutions:

  1. Manually fill in the cache object with textures and fonts.
  2. Scan the folder /textures/ /fonts/
  3. Magic

Actually I'll do for the texture as in the second case (because it is not critical for memory). And for fonts I'll use first case (because they will have different render for each size). But still I'm wondering how you can automate this process.

Should forget about the textures and fonts to narrow down the problem and consider the vector with strings.

class Files{
    static vector<string> files;
    static void addFile(string file){/* magic. may be template<string file> */}
    static void loadFiles()
    {
         for(auto file:files){
            cout << file; //for example
         }
    }

}

int main(){
    Files::loadFiles();//=> foobarsmthing
    Files::addFile("foo");
    Files::addFile("bar");
    Files::addFile("smthing");
    return 0;
}

I know that it's possible to compute some expressions with templates. And think that answer to my question is just "No".

Upvotes: 1

Views: 171

Answers (1)

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247989

Sure, it's possible. Just not as part of C++ template metaprogramming. TMP is turing-complete, meaning that it is as powerful as any other programming language when it comes to what it can compute. But it can't talk to the outside world. It can't communicate over network sockets, it can't scan your harddrive for files, it can't play sounds and it can't render a teapot on your desktop.

But you can run other scripts as part of your compilation. Both MSBuild and all flavors of makefiles have support for running arbitrary scripts during compilation. You don't have to limit yourself to running only the C++ compiler.

Upvotes: 3

Related Questions