Skip to content

Commit

Permalink
workaround for not calling virtual method from constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
rbouqueau committed May 2, 2016
1 parent 95bab10 commit 758310e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 30 deletions.
26 changes: 8 additions & 18 deletions src/lib_modules/core/output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,21 @@ class IOutputCap {
protected:
template <typename InstanceType, typename ...Args>
InstanceType* addOutput(Args&&... args) {
auto p = createOutput<InstanceType>(getAllocatorSize(), std::forward<Args>(args)...);
addOutputInternal(p);
auto p = createOutput<InstanceType>(allocatorSize, std::forward<Args>(args)...);
outputs.push_back(uptr(p));
return safe_cast<InstanceType>(p);
}

virtual size_t getAllocatorSize() const = 0;
virtual void addOutputInternal(IOutput *p) = 0;
/*FIXME: we need to have factories to move these back to the implementation - otherwise pins created from the constructor may crash*/
std::vector<std::unique_ptr<IOutput>> outputs;
/*const*/ size_t allocatorSize;
};

class OutputCap : public virtual IOutputCap {
public:
OutputCap(size_t allocatorSize) : allocatorSize(allocatorSize) {}
OutputCap(size_t allocatorSize) {
this->allocatorSize = allocatorSize;
}
virtual ~OutputCap() noexcept(false) {}

virtual size_t getNumOutputs() const override {
Expand All @@ -95,19 +98,6 @@ class OutputCap : public virtual IOutputCap {
virtual IOutput* getOutput(size_t i) const override {
return outputs[i].get();
}

protected:
virtual size_t getAllocatorSize() const override {
assert(allocatorSize > 0);
return allocatorSize;
}
virtual void addOutputInternal(IOutput *p) override {
outputs.push_back(uptr(p));
}

private:
std::vector<std::unique_ptr<IOutput>> outputs;
const size_t allocatorSize;
};

}
2 changes: 1 addition & 1 deletion src/lib_modules/utils/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Modules {

/* this default factory creates output pins with the default output - create another one for other uses such as low latency */
template <typename InstanceType>
struct ModuleDefault : public virtual OutputCap, public virtual InstanceType {
struct ModuleDefault : public OutputCap, public InstanceType {
template <typename ...Args>
ModuleDefault(size_t allocatorSize, Args&&... args) : OutputCap(allocatorSize), InstanceType(std::forward<Args>(args)...) {}
};
Expand Down
12 changes: 1 addition & 11 deletions src/lib_modules/utils/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,8 @@ class PipelinedInput : public IInput {
IProcessExecutor &executor;
};

/* Fake: requested by IOuputCap */
struct OutputCapFake : public virtual IOutputCap {
virtual size_t getAllocatorSize() const override final {
throw Exception("getAllocatorSize() not implemented for Pipeline");
}
virtual void addOutputInternal(IOutput *p) override final {
throw Exception("addOutputInternal() not implemented for Pipeline");
}
};

/* Wrapper around the module. */
class PipelinedModule : public ICompletionNotifier, public IPipelinedModule, public InputCap, public OutputCapFake {
class PipelinedModule : public ICompletionNotifier, public IPipelinedModule, public InputCap {
public:
/* take ownership of module */
PipelinedModule(IModule *module, ICompletionNotifier *notify)
Expand Down

0 comments on commit 758310e

Please sign in to comment.