.. _program_listing_file_src_layers_factory.h: Program Listing for File factory.h ================================== |exhale_lsh| :ref:`Return to documentation for file ` (``src/layers/factory.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include "marian.h" namespace marian { class Factory : public std::enable_shared_from_this { protected: Ptr options_; public: // construct with empty options Factory() : options_(New()) {} // construct with options Factory(Ptr options) : Factory() { options_->merge(options); } // construct with one or more individual option parameters // Factory("var1", val1, "var2", val2, ...) template Factory(const std::string& key, T value, Args&&... moreArgs) : Factory() { setOpts(key, value, std::forward(moreArgs)...); } // construct with options and one or more individual option parameters // Factory(options, "var1", val1, "var2", val2, ...) template Factory(Ptr options, Args&&... args) : Factory(options) { setOpts(std::forward(args)...); } Factory(const Factory& factory) = default; virtual ~Factory() {} std::string asYamlString() { return options_->asYamlString(); } // retrieve an option // auto val = opt("var"); template T opt(const char* const key) { return options_->get(key); } template T opt(const char* const key, T defaultValue) { return options_->get(key, defaultValue); } template T opt(const std::string& key) { return options_->get(key.c_str()); } template T opt(const std::string& key, T defaultValue) { return options_->get(key.c_str(), defaultValue); } // set a single option // setOpt("var", val); template void setOpt(const std::string& key, T value) { options_->set(key, value); } // set one or more options at once // setOpts("var1", val1, "var2", val2, ...); template void setOpts(const std::string& key, T value, Args&&... moreArgs) { options_->set(key, value, std::forward(moreArgs)...); } void mergeOpts(Ptr options) { options_->merge(options); } template inline Ptr as() { return std::dynamic_pointer_cast(shared_from_this()); } // @TODO: this fails with 'target type must be a pointer or reference to a defined class' //template //inline bool is() { return dynamic_cast(this) != nullptr; } template inline bool is() { return std::dynamic_pointer_cast(shared_from_this()) != nullptr; } }; template struct ConstructingFactory : public Factory { using Factory::Factory; Ptr construct(Ptr graph) { return New(graph, options_); } }; template // where BaseFactory : Factory class Accumulator : public BaseFactory { typedef BaseFactory Factory; public: Accumulator() : Factory() {} Accumulator(Ptr options) : Factory(options) {} template Accumulator(Ptr options, Args&&... moreArgs) : Factory(options, std::forward(moreArgs)...) {} template Accumulator(const std::string& key, T value, Args&&... moreArgs) : Factory(key, value, std::forward(moreArgs)...) {} Accumulator(const Factory& factory) : Factory(factory) {} Accumulator(const Accumulator&) = default; Accumulator(Accumulator&&) = default; // deprecated chaining syntax template Accumulator& operator()(const std::string& key, T value) { Factory::setOpt(key, value); return *this; } Accumulator& operator()(Ptr options) { Factory::mergeOpts(options); return *this; } Accumulator clone() { return Accumulator(Factory::clone()); } }; } // namespace marian