Add C++ modules support#442
Conversation
dda4512 to
59cb898
Compare
|
Hi, thanks for the PR, I think we need to consume somewhere Lightweight module to ensure that everything works, for instance chinook example is a good place: https://github.com/LASTRADA-Software/Lightweight/tree/master/src/examples/test_chinook |
|
Sure, I can add a test for the modules. |
|
I have a test now for the modules |
d4fc580 to
a1bddf1
Compare
a1bddf1 to
f3482ad
Compare
|
@Yaraslaut @christianparpart could you review the tests? |
|
I think we should avoid duplication of the source code, the easiest would be to update existing chinook example with the module usage, since we also check this example in the CI, no need for duplication, because then we need make sure that everything is syncronized |
|
@Yaraslaut The code is now unified, with macro guards to decide between module imports and includes. |
Yaraslaut
left a comment
There was a problem hiding this comment.
Thanks a lot for the update, nothing big, the only point that i am worried about is qualified names, such that name resolution is as simple as possible
|
|
||
| #include "Artist.hpp" | ||
|
|
||
| #include <Lightweight/DataMapper/DataMapper.hpp> |
There was a problem hiding this comment.
do we need to import Lightweight; here instead? since this should be enough to include this header to start using the struct, and order of the includes should not matter
There was a problem hiding this comment.
I didn't do it with the headers, because I made sure the standard library would be included beforehand.
I guess you're suggesting something like
#ifdef LIGHTWEIGHT_BUILD_MODULES
import Lightweight;
#else
#include <Lightweight/DataMapper/DataMapper.hpp>
#endifbut this is a bad idea. import statements MUST appear all at the top of a file, and if we were to do this, we would get something like
#include "entities/Album.hpp"
#include "entities/Artist.hpp"could expand into
import Lightweight;
struct Album final { /* ... */ };
import Lightweight;
struct Artist final { /* ... */ };and an import statement would be coming after code, which is rejected by the compiler.
There was a problem hiding this comment.
Oh interesting, you can notice that one of the ci checks is failing that checks ddl2cpp. This is a small tool that generates this header files for the database, see: https://github.com/LASTRADA-Software/Lightweight/actions/runs/22004219452/job/63585918320
So I guess we need to find a way to deal with this hear
| using std::basic_string_view; | ||
| using std::format_string; | ||
| using std::optional; | ||
| using std::string; | ||
| using std::string_view; | ||
|
|
||
| using Lightweight::BelongsTo; | ||
| using Lightweight::DataMapper; | ||
| using Lightweight::Field; | ||
| using Lightweight::PrimaryKey; | ||
| using Lightweight::SqlConnection; | ||
| using Lightweight::SqlConnectionString; | ||
| using Lightweight::SqlDateTime; | ||
| using Lightweight::SqlDynamicUtf16String; | ||
| using Lightweight::SqlNullable; | ||
| using Lightweight::SqlNumeric; | ||
| using Lightweight::SqlRealName; |
There was a problem hiding this comment.
I do not think that this is a good idea to drop all qualifiers, such as namespaces
There was a problem hiding this comment.
The example previously already used using namespace Lightweight; I'm doing this because if we had done that already, then there isn't much of a reason not to do this, but instead of using namespace individual using statements are probably better to make it clear what types originate from what.
But, on the topic of qualification, I think in these examples it's usually clear from context what the symbols refer to. using gives us readability, while not losing context of what library symbols originate from where.
There was a problem hiding this comment.
oh, did not notice, indeed the case, we can keep it like this, this is ok
| Field<int32_t, PrimaryKey::ServerSideAutoIncrement, SqlRealName { "TrackId" }> TrackId; | ||
| Field<SqlDynamicUtf16String<200>, SqlRealName { "Name" }> Name; | ||
| BelongsTo<&Album::AlbumId, SqlRealName { "AlbumId" }, SqlNullable::Null> AlbumId; | ||
| BelongsTo<&Mediatype::MediaTypeId, SqlRealName { "MediaTypeId" }> MediaTypeId; | ||
| BelongsTo<&Genre::GenreId, SqlRealName { "GenreId" }, SqlNullable::Null> GenreId; | ||
| Field<optional<SqlDynamicUtf16String<220>>, SqlRealName { "Composer" }> Composer; | ||
| Field<int32_t, SqlRealName { "Milliseconds" }> Milliseconds; | ||
| Field<optional<int32_t>, SqlRealName { "Bytes" }> Bytes; | ||
| Field<SqlNumeric<10, 2>, SqlRealName { "UnitPrice" }> UnitPrice; |
There was a problem hiding this comment.
I am slightly confused
why qualifiers removed here?
There was a problem hiding this comment.
I removed the qualifiers because I ensured they were added into scope before the #include directives were used. (As for why I de-qualified, I think it's easier to read and maintain, not to mention the original example already used using namespace Lightweight; previously.
|
Is it a problem in this example? The symbols all belong to either the library or std, so I decided to import names into scope to make code easier to read |
f101a6d to
efa2172
Compare
Yaraslaut
left a comment
There was a problem hiding this comment.
I think that everything is ok, just need to fix compilation and do something about ddl2cpp.
I guess what we want to do, is to add some kind of guard in this headers for the module support, and introduce this guard in the ddl2cpp as well
| using std::basic_string_view; | ||
| using std::format_string; | ||
| using std::optional; | ||
| using std::string; | ||
| using std::string_view; | ||
|
|
||
| using Lightweight::BelongsTo; | ||
| using Lightweight::DataMapper; | ||
| using Lightweight::Field; | ||
| using Lightweight::PrimaryKey; | ||
| using Lightweight::SqlConnection; | ||
| using Lightweight::SqlConnectionString; | ||
| using Lightweight::SqlDateTime; | ||
| using Lightweight::SqlDynamicUtf16String; | ||
| using Lightweight::SqlNullable; | ||
| using Lightweight::SqlNumeric; | ||
| using Lightweight::SqlRealName; |
There was a problem hiding this comment.
oh, did not notice, indeed the case, we can keep it like this, this is ok
|
|
||
| #include "Artist.hpp" | ||
|
|
||
| #include <Lightweight/DataMapper/DataMapper.hpp> |
There was a problem hiding this comment.
Oh interesting, you can notice that one of the ci checks is failing that checks ddl2cpp. This is a small tool that generates this header files for the database, see: https://github.com/LASTRADA-Software/Lightweight/actions/runs/22004219452/job/63585918320
So I guess we need to find a way to deal with this hear
efa2172 to
dea95b8
Compare
|
OK, let's see if this fixes the ddl2cpp CI. |
dea95b8 to
aaadbb7
Compare
This PR adds support for C++ modules.
Using statements are sorted in alphabetical order so that it is easier to maintain.