boost-ext / di

C++14 Dependency Injection Library
https://boost-ext.github.io/di
1.16k stars 140 forks source link

Empty brackets in uml dumper extension #457

Open dagophil opened 4 years ago

dagophil commented 4 years ago

The uml dumper extension seems to write empty brackets [] for unnamed arguments. Take a look at the following object tree:

#include "di.hpp"
#include "uml_dumper.hpp"

struct MyLogger {};

struct App {
    App(MyLogger&) {}
};

int main() {
    auto const injector = boost::di::make_injector<boost::di::extension::uml_dumper>();
    auto app = injector.create<App>();
}

Expected Behavior

The uml dumper should print the following output (no empty brackets []):

@startuml uml_dumper.png
"struct App" .. "struct MyLogger"
@enduml

Actual Behavior

The uml dumper does write empty brackets:

@startuml uml_dumper.png
"struct App" .. "struct MyLogger []"
@enduml

Steps to Reproduce the Problem

Run the code from above.

Specifications

dagophil commented 4 years ago

The cause of the problem seems to be this line in uml_dumper.hpp: https://github.com/boost-experimental/di/blob/cb065b20bf846642c229485492a9e0af66ef8748/extension/include/boost/di/extension/policies/uml_dumper.hpp#L43-L44

For unnamed arguments, the code (*(name*)(0))() calls operator() of the no_name placeholder, which returns an empty string "": https://github.com/boost-experimental/di/blob/cb065b20bf846642c229485492a9e0af66ef8748/include/boost/di.hpp#L292-L294

This means that the code in uml_dumper.hpp resolves to something like this:

char const* const n = "";
[...] << (n ? std::string(" [") + n + std::string("]") : "") << [...]

The n? checks whether n is a nullptr. I think here, one must instead check that n is not empty. One could, for example, copy n into some std::string.