C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Preprocessor

From: BAMBYK <uravasinski_at_[hidden]>
Date: Mon, 14 Aug 2023 12:02:08 +0300
There is often much of files in a specific directory and write each time
directory name before file is annoying and a bit loss readability in some
cases.

Yes, you can create a one common file that includes all other. However i
consider it is not always suitable. Sometimes you would better include
files right where you need instead of creating separate files. It is not
always nice to walk throughout such "reference" files. Also write #go and
#exit then is the most fast way than those two above.

With this keyword, it seems adds more code structure understand. You right
understand what and where is located by just looking at code.


And, i could agree if this feature would cause compiler low perfomance. But
it doesn't cause perfomance issues and simple to implement.

The example:

#include <vector>
#include <string>
#include <regex>
using namespace std;

vector<string> dirs = {};
const regex goDefinition(
    R"(^\s*#\s\bgo\b\s*"([^"])"\s*$)"
);
const regex exitDefinition(
    R"(^\s*#\s\bexit\b\s*$)"
);
const regex inclDefinition(
    R"(^\s*#\s\binclude\b\s*"([^"])"\s*$)"
);

// imagine file lines
vector<string> lines = {
    "#go \"output\""
    " // output"
    " #include \"helloWorld.h\""
    " #go \"messages\""
    " // output/messages"
    " #include \"messageData1.h\""
    " #include \"messageData2.h\"
    " #exit"
    "#exit"
}

for (const string line : lines) {
      smatch match
      if (regex_match(line, match, goDefinition) {
            const string dir = match[1].str();
            dirs.push_back(dir);
     } else if (regex_match(line, match, inclDefinition)) {
           string path = match[1].str();
           // imagine we have join feature
           // which joins array to string by separator
           path = join(dirs, "/") + "/" + path;
           // process include with new path...
     } else if (regex_match(line, match, exitDefinition)) {
           if (dirs.size() == 0) {
               // error
           }
           dirs.pop_back();
     }
}


Now the feature is implemented and can be used.


On Mon, Aug 14, 2023, 11:09 bmon Dor via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Why not just use #include "output/messages/messageData1.h"
> or just write a messageData.h in "output/messages/" with code:
> `
> #include "messageData1.h"
> #include "messageData2.h"
> `
>
> BAMBYK via Std-Proposals <std-proposals_at_[hidden]> 于2023年8月14日周一
> 12:57写道:
>
>> We often surround out code into separated folders. And we always must
>> write the path to the folder before a file we want to include. You may use
>> -I flag but it is not clearly comfortable. This feature would lead to
>> better code structure and faster include.
>>
>>
>> #go "output"
>> // output
>> #include "helloWorld.h"
>> #go "messages"
>> // output/messages
>> #include "messageData1.h"
>> #include "messageData2.h"
>> #exit
>> #exit
>>
>>
>> In this example #go means set folder the files are included from. The
>> compiler simply adds "path + '/' before each include. The #exit quit from
>> previous #go block.
>> These keywords can be nested the same as directories. So the nested #go
>> affect each other. It does not affect at anything else.
>>
>>
>> Of course any path can be specified:
>>
>> #go "output/messages"
>> #include "messageData1.h"
>> // ...
>> #exit
>>
>>
>> if the directory has only alphanumeric characters it can be used without
>> quotes.
>>
>> #go output
>> // ...
>> #exit
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-08-14 09:02:22