From 30a93ab6b522538904bfafd1a5f16c5c517f4df4 Mon Sep 17 00:00:00 2001 From: Haavard Date: Thu, 1 Sep 2016 12:45:45 +0000 Subject: SimpleConstantValue template replacing various wrappers --- .../eval/value_cache/constant_tensor_loader.cpp | 29 ++++++---------------- .../vespalib/eval/value_cache/constant_value.h | 15 +++++++++++ 2 files changed, 23 insertions(+), 21 deletions(-) (limited to 'vespalib/src') diff --git a/vespalib/src/vespa/vespalib/eval/value_cache/constant_tensor_loader.cpp b/vespalib/src/vespa/vespalib/eval/value_cache/constant_tensor_loader.cpp index a44a1e0ea53..501171083b4 100644 --- a/vespalib/src/vespa/vespalib/eval/value_cache/constant_tensor_loader.cpp +++ b/vespalib/src/vespa/vespalib/eval/value_cache/constant_tensor_loader.cpp @@ -66,42 +66,28 @@ struct AddressExtractor : ObjectTraverser { } }; -struct LoadError : ConstantValue { - ValueType my_type; - ErrorValue my_value; - LoadError() : my_type(ValueType::error_type()), my_value() {} - const ValueType &type() const override { return my_type; } - const Value &value() const override { return my_value; } -}; - -struct TensorConstant : ConstantValue { - ValueType my_type; - TensorValue my_value; - TensorConstant(std::unique_ptr tensor) - : my_type(tensor->engine().type_of(*tensor)), my_value(std::move(tensor)) {} - const ValueType &type() const override { return my_type; } - const Value &value() const override { return my_value; } -}; - } // namespace vespalib::eval:: +using ErrorConstant = SimpleConstantValue; +using TensorConstant = SimpleConstantValue; + ConstantValue::UP ConstantTensorLoader::create(const vespalib::string &path, const vespalib::string &type) const { ValueType value_type = ValueType::from_spec(type); if (value_type.is_error()) { LOG(warning, "invalid type specification: %s", type.c_str()); - return std::make_unique(); + return std::make_unique(ValueType::error_type()); } File file(path); if (!file.valid()) { LOG(warning, "could not read file: %s", path.c_str()); - return std::make_unique(); + return std::make_unique(ValueType::error_type()); } Slime slime; if (slime::JsonFormat::decode(Memory(file.data, file.size), slime) == 0) { LOG(warning, "file contains invalid json: %s", path.c_str()); - return std::make_unique(); + return std::make_unique(ValueType::error_type()); } std::set indexed; for (const auto &dimension: value_type.dimensions()) { @@ -117,7 +103,8 @@ ConstantTensorLoader::create(const vespalib::string &path, const vespalib::strin cells[i]["address"].traverse(extractor); spec.add(address, cells[i]["value"].asDouble()); } - return std::make_unique(_engine.create(spec)); + auto tensor = _engine.create(spec); + return std::make_unique(_engine.type_of(*tensor), std::move(tensor)); } } // namespace vespalib::eval diff --git a/vespalib/src/vespa/vespalib/eval/value_cache/constant_value.h b/vespalib/src/vespa/vespalib/eval/value_cache/constant_value.h index fbdb32ede88..570276a50ab 100644 --- a/vespalib/src/vespa/vespalib/eval/value_cache/constant_value.h +++ b/vespalib/src/vespa/vespalib/eval/value_cache/constant_value.h @@ -21,6 +21,21 @@ struct ConstantValue { virtual ~ConstantValue() {} }; +/** + * A simple implementation of a constant value that bundles together a + * ValueType instance with a specific Value subclass instance. + **/ +template +struct SimpleConstantValue : ConstantValue { + ValueType my_type; + VALUE my_value; + template + SimpleConstantValue(const ValueType &type_in, Args &&...args) + : my_type(type_in), my_value(std::forward(args)...) {} + const ValueType &type() const override { return my_type; } + const Value &value() const override { return my_value; } +}; + /** * An abstract factory of constant values. The typical use-case for * this will be to load constant values from file with a cache on top -- cgit v1.2.3