Giter Club home page Giter Club logo

Comments (3)

poletti-marco avatar poletti-marco commented on August 21, 2024

Hi, can you share a snippet of the code that hit that error?

Getting a std::shared_ptr<T> from the injector is supported, and as the documentation section you cited this injects the bound type T (note that this is not std::shared_ptr<T>).

If the factories return a std::shared_ptr<T>, perhaps you attempted to bind a std::shared_ptr<T> as opposed to binding a T?
That is not allowed, but if it's what you want you can wrap it in a struct, e.g.:

struct WrappedSharedPtr<typename T> {
    std::shared_ptr<T> value;
};

Then this doesn't get the special std::shared_ptr behavior so you can bind a WrappedSharedPtr<T> and then inject it.

from fruit.

IrisPeter avatar IrisPeter commented on August 21, 2024

Hi,

I had hoped to make my message a bit lit leaner by hiding the compile errors under a details tag, but I see this was probably just confusing, I will edit the message show that it shows up, instead of needing to be opened with a mouse click.

What I did was I went to the "Scaling Doubles Injection Test" and scaler.h

  • Edited line 29 substituting unique_ptr for shared_ptr

    using ScalerFactory = std::function<std::shared_ptr<Scaler>(int)>;

Went to main.cpp

  • Edited line 27 substituting unique_ptr for shared_ptr
    std::shared_ptr<Scaler> scaler = scalerFactory(12.1);)

Whilst main.cpp compiled without errors, compiling scaler.cpp produced errors see first message which already contains the compile errors produced

from fruit.

poletti-marco avatar poletti-marco commented on August 21, 2024

I had hoped to make my message a bit lit leaner by hiding the compile errors under a details tag, but I see this was probably just confusing, I will edit the message show that it shows up, instead of needing to be opened with a mouse click.

Hi, yeah I saw the compile errors before too, it's the code you tried I was asking for.
(but that is in your last message, thanks)

You can make that work using registerFactory, changing getScalerComponent to be:

Component<ScalerFactory> getScalerComponent() {
  return createComponent()
      .registerFactory<std::shared_ptr<Scaler>(fruit::Assisted<double>, Multiplier*)>(
          [](double factor, Multiplier* multiplier) {
              return std::dynamic_pointer_cast<Scaler>(std::make_shared<ScalerImpl>(factor, multiplier));
          })
      .install(getMultiplierComponent);
}

(then you no longer need the INJECT/ASSISTED in the constructor, since Fruit no longer needs to call that directly)

from fruit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.