diff options
29 files changed, 318 insertions, 345 deletions
diff --git a/config/src/tests/configagent/configagent.cpp b/config/src/tests/configagent/configagent.cpp index 4843b2f0647..10f321c181d 100644 --- a/config/src/tests/configagent/configagent.cpp +++ b/config/src/tests/configagent/configagent.cpp @@ -114,7 +114,7 @@ private: ConfigValue createValue(const std::string & myField, const std::string & xxhash64) { - std::vector< vespalib::string > lines; + StringVector lines; lines.push_back("myField \"" + myField + "\""); return ConfigValue(lines, xxhash64); } diff --git a/config/src/tests/configgen/map_inserter.cpp b/config/src/tests/configgen/map_inserter.cpp index 2a37d401792..bf1b856972e 100644 --- a/config/src/tests/configgen/map_inserter.cpp +++ b/config/src/tests/configgen/map_inserter.cpp @@ -1,8 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include <vespa/vespalib/testkit/test_kit.h> -#include <vespa/config/configgen/map_inserter.h> #include <vespa/vespalib/data/slime/slime.h> +#include <vespa/config/configgen/map_inserter.hpp> using namespace config; using namespace config::internal; diff --git a/config/src/tests/configgen/vector_inserter.cpp b/config/src/tests/configgen/vector_inserter.cpp index b8611317650..e2b0d7c875f 100644 --- a/config/src/tests/configgen/vector_inserter.cpp +++ b/config/src/tests/configgen/vector_inserter.cpp @@ -1,7 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include <vespa/vespalib/testkit/test_kit.h> -#include <vespa/config/configgen/vector_inserter.h> +#include <vespa/config/common/types.h> +#include <vespa/config/configgen/vector_inserter.hpp> #include <vespa/vespalib/data/slime/slime.h> using namespace config; @@ -27,7 +28,7 @@ TEST("require that vector of ints can be inserted") { root.addLong(3); root.addLong(2); root.addLong(6); - VectorInserter<int32_t> inserter(vector); + VectorInserter inserter(vector); root.traverse(inserter); ASSERT_EQUAL(3u, vector.size()); ASSERT_EQUAL(3, vector[0]); @@ -45,7 +46,7 @@ TEST("require that vector of struct can be inserted") { Cursor & two = root.addObject(); two.setLong("foo", 1); two.setLong("bar", 6); - VectorInserter<MyType> inserter(typeVector); + VectorInserter inserter(typeVector); root.traverse(inserter); ASSERT_EQUAL(2u, typeVector.size()); ASSERT_EQUAL(3, typeVector[0].foo); @@ -61,7 +62,7 @@ TEST("require that vector of long can be inserted") { root.addLong(3); root.addLong(2); root.addLong(6); - VectorInserter<int64_t> inserter(vector); + VectorInserter inserter(vector); root.traverse(inserter); ASSERT_EQUAL(3u, vector.size()); ASSERT_EQUAL(3, vector[0]); @@ -76,7 +77,7 @@ TEST("require that vector of double can be inserted") { root.addDouble(3.1); root.addDouble(2.4); root.addDouble(6.6); - VectorInserter<double> inserter(vector); + VectorInserter inserter(vector); root.traverse(inserter); ASSERT_EQUAL(3u, vector.size()); ASSERT_EQUAL(3.1, vector[0]); @@ -91,7 +92,7 @@ TEST("require that vector of bool can be inserted") { root.addBool(true); root.addBool(false); root.addBool(true); - VectorInserter<bool> inserter(vector); + VectorInserter inserter(vector); root.traverse(inserter); ASSERT_EQUAL(3u, vector.size()); ASSERT_TRUE(vector[0]); @@ -106,7 +107,22 @@ TEST("require that vector of string can be inserted") { root.addString("foo"); root.addString("bar"); root.addString("baz"); - VectorInserter<vespalib::string> inserter(vector); + VectorInserter inserter(vector); + root.traverse(inserter); + ASSERT_EQUAL(3u, vector.size()); + ASSERT_EQUAL("foo", vector[0]); + ASSERT_EQUAL("bar", vector[1]); + ASSERT_EQUAL("baz", vector[2]); +} + +TEST("require that vector of string can be inserted") { + StringVector vector; + Slime slime; + Cursor & root = slime.setArray(); + root.addString("foo"); + root.addString("bar"); + root.addString("baz"); + VectorInserter inserter(vector); root.traverse(inserter); ASSERT_EQUAL(3u, vector.size()); ASSERT_EQUAL("foo", vector[0]); diff --git a/config/src/tests/configmanager/configmanager.cpp b/config/src/tests/configmanager/configmanager.cpp index 9bed974f628..a7171b11137 100644 --- a/config/src/tests/configmanager/configmanager.cpp +++ b/config/src/tests/configmanager/configmanager.cpp @@ -17,7 +17,7 @@ namespace { ConfigValue createValue(const std::string & myField, const std::string & md5) { - std::vector< vespalib::string > lines; + StringVector lines; lines.push_back("myField \"" + myField + "\""); return ConfigValue(lines, md5); } diff --git a/config/src/tests/configparser/configparser.cpp b/config/src/tests/configparser/configparser.cpp index 3e569a2d3fb..32043ae79dc 100644 --- a/config/src/tests/configparser/configparser.cpp +++ b/config/src/tests/configparser/configparser.cpp @@ -3,6 +3,7 @@ #include <vespa/config/common/configparser.h> #include <vespa/config/common/exceptions.h> #include <vespa/config/common/configvalue.h> +#include <vespa/config/common/misc.h> #include "config-foo.h" #include <fstream> #include <vespa/vespalib/stllike/asciistream.h> @@ -23,7 +24,7 @@ namespace { ConfigValue readConfig(const vespalib::string & fileName) { asciistream is(asciistream::createFromFile(fileName)); - return ConfigValue(is.getlines(), ""); + return ConfigValue(getlines(is), ""); } } diff --git a/config/src/vespa/config/common/configparser.cpp b/config/src/vespa/config/common/configparser.cpp index 3e18d6ae4e7..97373071ed5 100644 --- a/config/src/vespa/config/common/configparser.cpp +++ b/config/src/vespa/config/common/configparser.cpp @@ -124,10 +124,9 @@ getValueForKey(vespalib::stringref key, vespalib::stringref line, } StringVector -ConfigParser::getLinesForKey(vespalib::stringref key, - const vsvector & lines) +ConfigParser::getLinesForKey(vespalib::stringref key, Cfg lines) { - vsvector retval; + StringVector retval; for (uint32_t i = 0; i < lines.size(); i++) { vespalib::string value; @@ -140,6 +139,18 @@ ConfigParser::getLinesForKey(vespalib::stringref key, return retval; } +std::set<vespalib::string> +ConfigParser::getUniqueNonWhiteSpaceLines(Cfg config) { + std::set<vespalib::string> unique; + for (uint32_t i = 0; i < config.size(); i++) { + vespalib::string line = stripWhitespace(config[i]); + if (!line.empty()) { + unique.insert(line); + } + } + return unique; +} + void ConfigParser::stripLinesForKey(vespalib::stringref key, std::set<vespalib::string>& config) @@ -155,10 +166,10 @@ ConfigParser::stripLinesForKey(vespalib::stringref key, } } -std::map<vespalib::string, ConfigParser::vsvector> -ConfigParser::splitMap(const vsvector & config) +std::map<vespalib::string, StringVector> +ConfigParser::splitMap(Cfg config) { - std::map<vespalib::string, vsvector> items; + std::map<vespalib::string, StringVector> items; vespalib::string lastValue; @@ -178,7 +189,7 @@ ConfigParser::splitMap(const vsvector & config) vespalib::string value = config[i].substr(pos + 1); if (key != lastValue) { - items[key] = vsvector(); + items[key] = StringVector(); lastValue = key; } @@ -191,10 +202,10 @@ ConfigParser::splitMap(const vsvector & config) return items; } -std::vector<ConfigParser::vsvector> -ConfigParser::splitArray(const vsvector & config) +std::vector<StringVector> +ConfigParser::splitArray(Cfg config) { - std::vector<vsvector> items; + std::vector<StringVector> items; vespalib::string lastValue; @@ -214,7 +225,7 @@ ConfigParser::splitArray(const vsvector & config) vespalib::string value = config[i].substr(pos + 1); if (key != lastValue) { - items.push_back(vsvector()); + items.push_back(StringVector()); lastValue = key; } @@ -266,7 +277,7 @@ ConfigParser::stripWhitespace(vespalib::stringref source) } vespalib::string -ConfigParser::arrayToString(const vsvector & array) +ConfigParser::arrayToString(Cfg array) { vespalib::asciistream ost; if (array.size() == 0) { @@ -281,13 +292,13 @@ ConfigParser::arrayToString(const vsvector & array) template<> bool -ConfigParser::convert<bool>(const vsvector & config) +ConfigParser::convert<bool>(const StringVector & config) { if (config.size() != 1) { throw InvalidConfigException("Expected single line with bool value, " "got " + arrayToString(config), VESPA_STRLOC); } - std::string value = stripWhitespace(deQuote(config[0])); + vespalib::string value = stripWhitespace(deQuote(config[0])); if (value == "true") { return true; @@ -301,13 +312,13 @@ ConfigParser::convert<bool>(const vsvector & config) template<> int32_t -ConfigParser::convert<int32_t>(const vsvector & config) +ConfigParser::convert<int32_t>(const StringVector & config) { if (config.size() != 1) { throw InvalidConfigException("Expected single line with int32_t value, " "got " + arrayToString(config), VESPA_STRLOC); } - std::string value(deQuote(stripWhitespace(config[0]))); + vespalib::string value(deQuote(stripWhitespace(config[0]))); const char *startp = value.c_str(); char *endp; @@ -321,13 +332,13 @@ ConfigParser::convert<int32_t>(const vsvector & config) template<> int64_t -ConfigParser::convert<int64_t>(const vsvector & config) +ConfigParser::convert<int64_t>(const StringVector & config) { if (config.size() != 1) { throw InvalidConfigException("Expected single line with int64_t value, " "got " + arrayToString(config), VESPA_STRLOC); } - std::string value(deQuote(stripWhitespace(config[0]))); + vespalib::string value(deQuote(stripWhitespace(config[0]))); const char *startp = value.c_str(); char *endp; @@ -341,13 +352,13 @@ ConfigParser::convert<int64_t>(const vsvector & config) template<> double -ConfigParser::convert<double>(const vsvector & config) +ConfigParser::convert<double>(const StringVector & config) { if (config.size() != 1) { throw InvalidConfigException("Expected single line with double value, " "got " + arrayToString(config), VESPA_STRLOC); } - std::string value(deQuote(stripWhitespace(config[0]))); + vespalib::string value(deQuote(stripWhitespace(config[0]))); const char *startp = value.c_str(); char *endp; @@ -362,14 +373,14 @@ ConfigParser::convert<double>(const vsvector & config) template<> vespalib::string -ConfigParser::convert<vespalib::string>(const vsvector & config) +ConfigParser::convert<vespalib::string>(const StringVector & config) { if (config.size() != 1) { throw InvalidConfigException("Expected single line with string value, " "got " + arrayToString(config), VESPA_STRLOC); } - std::string value = stripWhitespace(config[0]); + vespalib::string value = stripWhitespace(config[0]); return deQuote(value); } diff --git a/config/src/vespa/config/common/configparser.h b/config/src/vespa/config/common/configparser.h index dc89930c23e..42ee684eb59 100644 --- a/config/src/vespa/config/common/configparser.h +++ b/config/src/vespa/config/common/configparser.h @@ -14,69 +14,83 @@ namespace config { */ class ConfigParser { public: - using vsvector = StringVector; + class Cfg { + public: + Cfg(const std::vector<vespalib::string> & v) + : _cfg(&v[0]), _sz(v.size()) + { } + Cfg(const std::vector<vespalib::string, vespalib::allocator_large<vespalib::string>> & v) : + _cfg(&v[0]), + _sz(v.size()) + { } + size_t size() const { return _sz; } + const vespalib::string & operator[] (size_t idx) const { return _cfg[idx]; } + private: + const vespalib::string * _cfg; + size_t _sz; + }; private: - static vsvector getLinesForKey(vespalib::stringref key, const vsvector & config); + static StringVector getLinesForKey(vespalib::stringref key, Cfg config); - static std::vector<vsvector> splitArray( const vsvector & config); - static std::map<vespalib::string, vsvector> splitMap( const vsvector & config); + static std::vector<StringVector> splitArray(Cfg config); + static std::map<vespalib::string, StringVector> splitMap(Cfg config); static vespalib::string deQuote(const vespalib::string & source); static void throwNoDefaultValue(vespalib::stringref key); template<typename T> - static T convert(const vsvector &); + static T convert(const StringVector & config); - static vespalib::string arrayToString(const vsvector &); + static vespalib::string arrayToString(Cfg config); - template<typename T, typename V> - static T parseInternal(vespalib::stringref key, const V & config); - template<typename T, typename V> - static T parseInternal(vespalib::stringref key, const V & config, T defaultValue); + template<typename T> + static T parseInternal(vespalib::stringref key, Cfg config); + template<typename T> + static T parseInternal(vespalib::stringref key, Cfg config, T defaultValue); - template<typename T, typename V> - static std::vector<T> parseArrayInternal(vespalib::stringref key, const V & config); - template<typename T, typename V> - static std::map<vespalib::string, T> parseMapInternal(vespalib::stringref key, const V & config); - template<typename T, typename V> - static T parseStructInternal(vespalib::stringref key, const V & config); + template<typename V> + static V parseArrayInternal(vespalib::stringref key, Cfg config); + template<typename T> + static std::map<vespalib::string, T> parseMapInternal(vespalib::stringref key, Cfg config); + template<typename T> + static T parseStructInternal(vespalib::stringref key, Cfg config); public: - static void stripLinesForKey(vespalib::stringref key, - std::set<vespalib::string>& config); + static void stripLinesForKey(vespalib::stringref key, std::set<vespalib::string>& config); + static std::set<vespalib::string> getUniqueNonWhiteSpaceLines(Cfg config); static vespalib::string stripWhitespace(vespalib::stringref source); template<typename T> - static T parse(vespalib::stringref key, const vsvector & config) { - return parseInternal<T, vsvector>(key, config); + static T parse(vespalib::stringref key, Cfg config) { + return parseInternal<T>(key, config); } template<typename T> - static T parse(vespalib::stringref key, const vsvector & config, T defaultValue) { + static T parse(vespalib::stringref key, Cfg config, T defaultValue) { return parseInternal(key, config, defaultValue); } - template<typename T> - static std::vector<T> parseArray(vespalib::stringref key, const vsvector & config) { - return parseArrayInternal<T, vsvector>(key, config); + template<typename V> + static V parseArray(vespalib::stringref key, Cfg config) { + return parseArrayInternal<V>(key, config); } template<typename T> - static std::map<vespalib::string, T> parseMap(vespalib::stringref key, const vsvector & config) { - return parseMapInternal<T, vsvector>(key, config); + static std::map<vespalib::string, T> parseMap(vespalib::stringref key, Cfg config) { + return parseMapInternal<T>(key, config); } template<typename T> - static T parseStruct(vespalib::stringref key, const vsvector & config) { - return parseStructInternal<T, vsvector>(key, config); + static T parseStruct(vespalib::stringref key, Cfg config) { + return parseStructInternal<T>(key, config); } }; -template<typename T, typename V> +template<typename T> T -ConfigParser::parseInternal(vespalib::stringref key, const V & config) +ConfigParser::parseInternal(vespalib::stringref key, Cfg config) { - V lines = getLinesForKey(key, config); + StringVector lines = getLinesForKey(key, config); if (lines.size() == 0) { throwNoDefaultValue(key); @@ -84,11 +98,11 @@ ConfigParser::parseInternal(vespalib::stringref key, const V & config) return convert<T>(lines); } -template<typename T, typename V> +template<typename T> T -ConfigParser::parseInternal(vespalib::stringref key, const V & config, T defaultValue) +ConfigParser::parseInternal(vespalib::stringref key, Cfg config, T defaultValue) { - V lines = getLinesForKey(key, config); + StringVector lines = getLinesForKey(key, config); if (lines.size() == 0) { return defaultValue; @@ -99,67 +113,68 @@ ConfigParser::parseInternal(vespalib::stringref key, const V & config, T default template<typename T> T -ConfigParser::convert(const vsvector & lines) { +ConfigParser::convert(const StringVector & lines) { return T(lines); } -template<typename T, typename V> +template<typename T> std::map<vespalib::string, T> -ConfigParser::parseMapInternal(vespalib::stringref key, const V & config) +ConfigParser::parseMapInternal(vespalib::stringref key, Cfg config) { - V lines = getLinesForKey(key, config); - typedef std::map<vespalib::string, V> SplittedMap; + StringVector lines = getLinesForKey(key, config); + using SplittedMap = std::map<vespalib::string, StringVector>; SplittedMap s = splitMap(lines); std::map<vespalib::string, T> retval; - for (typename SplittedMap::iterator it(s.begin()), mt(s.end()); it != mt; it++) { - retval[it->first] = convert<T>(it->second); + for (const auto & e : s) { + retval[e.first] = convert<T>(e.second); } return retval; } -template<typename T, typename V> -std::vector<T> -ConfigParser::parseArrayInternal(vespalib::stringref key, const V & config) +template<typename V> +V +ConfigParser::parseArrayInternal(vespalib::stringref key, Cfg config) { - V lines = getLinesForKey(key, config); - std::vector<V> split = splitArray(lines); + StringVector lines = getLinesForKey(key, config); + std::vector<StringVector> split = splitArray(lines); - std::vector<T> retval; + V retval; + retval.reserve(split.size()); for (uint32_t i = 0; i < split.size(); i++) { - retval.push_back(convert<T>(split[i])); + retval.push_back(convert<typename V::value_type>(split[i])); } return retval; } -template<typename T, typename V> +template<typename T> T -ConfigParser::parseStructInternal(vespalib::stringref key, const V & config) +ConfigParser::parseStructInternal(vespalib::stringref key, Cfg config) { - V lines = getLinesForKey(key, config); + StringVector lines = getLinesForKey(key, config); return convert<T>(lines); } template<> bool -ConfigParser::convert<bool>(const vsvector & config); +ConfigParser::convert<bool>(const StringVector & config); template<> int32_t -ConfigParser::convert<int32_t>(const vsvector & config); +ConfigParser::convert<int32_t>(const StringVector & config); template<> int64_t -ConfigParser::convert<int64_t>(const vsvector & config); +ConfigParser::convert<int64_t>(const StringVector & config); template<> double -ConfigParser::convert<double>(const vsvector & config); +ConfigParser::convert<double>(const StringVector & config); template<> vespalib::string -ConfigParser::convert<vespalib::string>(const vsvector & config); +ConfigParser::convert<vespalib::string>(const StringVector & config); } // config diff --git a/config/src/vespa/config/common/configstate.h b/config/src/vespa/config/common/configstate.h index a0229f85cd5..0895517343c 100644 --- a/config/src/vespa/config/common/configstate.h +++ b/config/src/vespa/config/common/configstate.h @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #pragma once -#include <vespa/vespalib/stllike/string.h> #include "misc.h" namespace config { diff --git a/config/src/vespa/config/common/misc.cpp b/config/src/vespa/config/common/misc.cpp index 7eba045a2c7..31c66cd4eb8 100644 --- a/config/src/vespa/config/common/misc.cpp +++ b/config/src/vespa/config/common/misc.cpp @@ -39,6 +39,16 @@ calculateContentXxhash64(const StringVector & fileContents) return ss.str(); } +StringVector +getlines(vespalib::asciistream & is, char delim) +{ + StringVector lines; + while (!is.eof()) { + lines.push_back(is.getline(delim)); + } + return lines; +} + bool isGenerationNewer(int64_t newGen, int64_t oldGen) { diff --git a/config/src/vespa/config/common/misc.h b/config/src/vespa/config/common/misc.h index f26ee3df850..584a7ca527b 100644 --- a/config/src/vespa/config/common/misc.h +++ b/config/src/vespa/config/common/misc.h @@ -5,6 +5,7 @@ #include <memory> namespace vespalib { + class asciistream; class Slime; namespace slime { struct Inspector; @@ -32,4 +33,6 @@ typedef std::shared_ptr<const vespalib::Slime> SlimePtr; */ void copySlimeObject(const vespalib::slime::Inspector & src, vespalib::slime::Cursor & dest); +StringVector getlines(vespalib::asciistream & is, char delim='\n'); + } diff --git a/config/src/vespa/config/common/types.h b/config/src/vespa/config/common/types.h index b59685ffaa0..0c9ad15d046 100644 --- a/config/src/vespa/config/common/types.h +++ b/config/src/vespa/config/common/types.h @@ -2,12 +2,14 @@ #pragma once #include <vespa/vespalib/stllike/string.h> +#include <vespa/vespalib/stllike/allocator.h> #include <vector> #include <map> namespace config { -using StringVector = std::vector<vespalib::string>; +//using StringVector = std::vector<vespalib::string>; +using StringVector = std::vector<vespalib::string, vespalib::allocator_large<vespalib::string>>; using BoolVector = std::vector<bool>; using DoubleVector = std::vector<double>; using LongVector = std::vector<int64_t>; diff --git a/config/src/vespa/config/configgen/map_inserter.h b/config/src/vespa/config/configgen/map_inserter.h index 9f596912c5f..e35ee8cb0ac 100644 --- a/config/src/vespa/config/configgen/map_inserter.h +++ b/config/src/vespa/config/configgen/map_inserter.h @@ -4,12 +4,9 @@ #include "value_converter.h" #include <vespa/vespalib/data/slime/object_traverser.h> #include <vespa/vespalib/stllike/string.h> - #include <map> -namespace config { - -namespace internal { +namespace config::internal { template<typename T, typename Converter = config::internal::ValueConverter<T> > class MapInserter : public ::vespalib::slime::ObjectTraverser { @@ -20,9 +17,4 @@ private: std::map<vespalib::string, T> & _map; }; -} // namespace internal - -} // namespace config - -#include "map_inserter.hpp" - +} diff --git a/config/src/vespa/config/configgen/map_inserter.hpp b/config/src/vespa/config/configgen/map_inserter.hpp index 6ffdb0e893b..9bd04c95b11 100644 --- a/config/src/vespa/config/configgen/map_inserter.hpp +++ b/config/src/vespa/config/configgen/map_inserter.hpp @@ -1,7 +1,10 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -namespace config { -namespace internal { +#pragma once + +#include "map_inserter.h" + +namespace config::internal { template<typename T, typename Converter> MapInserter<T, Converter>::MapInserter(std::map<vespalib::string, T> & map) @@ -16,6 +19,4 @@ MapInserter<T, Converter>::field(const ::vespalib::Memory & symbol, const ::vesp _map[symbol.make_string()] = converter(inspector); } -} // namespace internal - } diff --git a/config/src/vespa/config/configgen/value_converter.cpp b/config/src/vespa/config/configgen/value_converter.cpp index 4b25c6040da..36843456b25 100644 --- a/config/src/vespa/config/configgen/value_converter.cpp +++ b/config/src/vespa/config/configgen/value_converter.cpp @@ -54,7 +54,7 @@ template<> vespalib::string convertValue(const ::vespalib::slime::Inspector & __inspector) { return __inspector.asString().make_string(); } void -requireValid(const vespalib::string & __fieldName, const ::vespalib::slime::Inspector & __inspector) { +requireValid(vespalib::stringref __fieldName, const ::vespalib::slime::Inspector & __inspector) { if (!__inspector.valid()) { throw ::config::InvalidConfigException("Value for '" + __fieldName + "' required but not found"); } diff --git a/config/src/vespa/config/configgen/value_converter.h b/config/src/vespa/config/configgen/value_converter.h index 695e798b015..c583f1595dc 100644 --- a/config/src/vespa/config/configgen/value_converter.h +++ b/config/src/vespa/config/configgen/value_converter.h @@ -7,7 +7,7 @@ namespace config::internal { -void requireValid(const vespalib::string & __fieldName, const ::vespalib::slime::Inspector & __inspector); +void requireValid(vespalib::stringref __fieldName, const ::vespalib::slime::Inspector & __inspector); template<typename T> T convertValue(const ::vespalib::slime::Inspector & __inspector) { return T(::config::ConfigPayload(__inspector)); } @@ -29,7 +29,7 @@ vespalib::string convertValue(const ::vespalib::slime::Inspector & __inspector); template<typename T> struct ValueConverter { - T operator()(const vespalib::string & __fieldName, const ::vespalib::slime::Inspector & __inspector) { + T operator()(vespalib::stringref __fieldName, const ::vespalib::slime::Inspector & __inspector) { requireValid(__fieldName, __inspector); return convertValue<T>(__inspector); } diff --git a/config/src/vespa/config/configgen/vector_inserter.h b/config/src/vespa/config/configgen/vector_inserter.h index 3c5a406ef67..4b8c7a5e0a1 100644 --- a/config/src/vespa/config/configgen/vector_inserter.h +++ b/config/src/vespa/config/configgen/vector_inserter.h @@ -5,22 +5,15 @@ #include <vespa/vespalib/data/slime/array_traverser.h> #include <vespa/vespalib/stllike/string.h> -namespace config { +namespace config::internal { -namespace internal { - -template<typename T, typename Converter = ::config::internal::ValueConverter<T> > +template<typename V, typename Converter = ::config::internal::ValueConverter<typename V::value_type> > class VectorInserter : public ::vespalib::slime::ArrayTraverser { public: - VectorInserter(std::vector<T> & vector); + VectorInserter(V & vector); void entry(size_t idx, const ::vespalib::slime::Inspector & inspector) override; private: - std::vector<T> & _vector; + V & _vector; }; -} // namespace internal - -} // namespace config - -#include "vector_inserter.hpp" - +} diff --git a/config/src/vespa/config/configgen/vector_inserter.hpp b/config/src/vespa/config/configgen/vector_inserter.hpp index ed43dda07d6..31c3c52a358 100644 --- a/config/src/vespa/config/configgen/vector_inserter.hpp +++ b/config/src/vespa/config/configgen/vector_inserter.hpp @@ -1,22 +1,23 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -namespace config { -namespace internal { +#pragma once -template<typename T, typename Converter> -VectorInserter<T, Converter>::VectorInserter(std::vector<T> & vector) +#include "vector_inserter.h" + +namespace config::internal { + +template<typename V, typename Converter> +VectorInserter<V, Converter>::VectorInserter(V & vector) : _vector(vector) {} -template<typename T, typename Converter> +template<typename V, typename Converter> void -VectorInserter<T, Converter>::entry(size_t idx, const ::vespalib::slime::Inspector & inspector) +VectorInserter<V, Converter>::entry(size_t idx, const ::vespalib::slime::Inspector & inspector) { (void) idx; Converter converter; _vector.push_back(converter(inspector)); } -} // namespace internal - } diff --git a/config/src/vespa/config/file/filesource.cpp b/config/src/vespa/config/file/filesource.cpp index 1585bd1bd53..011868f7c06 100644 --- a/config/src/vespa/config/file/filesource.cpp +++ b/config/src/vespa/config/file/filesource.cpp @@ -53,7 +53,7 @@ StringVector FileSource::readConfigFile(const vespalib::string & fileName) { asciistream is(asciistream::createFromFile(fileName)); - return is.getlines(); + return getlines(is); } void diff --git a/config/src/vespa/config/raw/rawsource.cpp b/config/src/vespa/config/raw/rawsource.cpp index f37f30bb2f7..cf551106405 100644 --- a/config/src/vespa/config/raw/rawsource.cpp +++ b/config/src/vespa/config/raw/rawsource.cpp @@ -36,7 +36,7 @@ StringVector RawSource::readConfig() { vespalib::asciistream is(_payload); - return is.getlines(); + return getlines(is); } } diff --git a/config/src/vespa/config/set/configinstancesourcefactory.cpp b/config/src/vespa/config/set/configinstancesourcefactory.cpp index 64abaada0a4..15a6125d096 100644 --- a/config/src/vespa/config/set/configinstancesourcefactory.cpp +++ b/config/src/vespa/config/set/configinstancesourcefactory.cpp @@ -14,7 +14,7 @@ public: { } void close() override { } void getConfig() override { - _holder->handle(std::make_unique<config::ConfigUpdate>(config::ConfigValue(_buffer.getlines()), true, _generation)); + _holder->handle(std::make_unique<config::ConfigUpdate>(config::ConfigValue(config::getlines(_buffer)), true, _generation)); } void reload(int64_t generation) override { _generation = generation; } diff --git a/config/src/vespa/config/set/configsetsource.cpp b/config/src/vespa/config/set/configsetsource.cpp index 94547fbe63d..41886a12d01 100644 --- a/config/src/vespa/config/set/configsetsource.cpp +++ b/config/src/vespa/config/set/configsetsource.cpp @@ -30,7 +30,7 @@ ConfigSetSource::getConfig() vespalib::asciistream ss; AsciiConfigWriter writer(ss); writer.write(*instance); - StringVector lines(ss.getlines()); + StringVector lines(getlines(ss)); vespalib::string currentXxhash64(calculateContentXxhash64(lines)); if (isGenerationNewer(_generation, _lastState.generation) && currentXxhash64.compare(_lastState.xxhash64) != 0) { diff --git a/configgen/src/main/java/com/yahoo/config/codegen/CppClassBuilder.java b/configgen/src/main/java/com/yahoo/config/codegen/CppClassBuilder.java index 149bcced29c..8cfc38e5076 100644 --- a/configgen/src/main/java/com/yahoo/config/codegen/CppClassBuilder.java +++ b/configgen/src/main/java/com/yahoo/config/codegen/CppClassBuilder.java @@ -3,14 +3,10 @@ package com.yahoo.config.codegen; import java.io.FileWriter; import java.io.File; -import java.io.BufferedReader; import java.io.IOException; -import java.io.FileReader; import java.io.StringWriter; import java.io.Writer; import java.util.Map; -import java.util.HashMap; -import java.util.Collections; import java.util.Arrays; import java.util.StringTokenizer; import java.util.stream.Collectors; @@ -24,39 +20,31 @@ public class CppClassBuilder implements ClassBuilder { private final NormalizedDefinition nd; private final File rootDir; private final String relativePathUnderRoot; - private static final Map<String, String> vectorTypeDefs; - static { - Map<String, String> map = new HashMap<String, String>(); - map.put("bool", "BoolVector"); - map.put("int32_t", "IntVector"); - map.put("int64_t", "LongVector"); - map.put("double", "DoubleVector"); - map.put("vespalib::string", "StringVector"); - vectorTypeDefs = Collections.unmodifiableMap(map); - } - private static final Map<String, String> mapTypeDefs; - static { - Map<String, String> map = new HashMap<>(); - map.put("bool", "BoolMap"); - map.put("int32_t", "IntMap"); - map.put("int64_t", "LongMap"); - map.put("double", "DoubleMap"); - map.put("vespalib::string", "StringMap"); - mapTypeDefs = Collections.unmodifiableMap(map); - } - private static final Map<String, String> slimeTypeMap; - static { - Map<String, String> map = new HashMap<String, String>(); - map.put("bool", "Bool"); - map.put("int", "Long"); - map.put("long", "Long"); - map.put("double", "Double"); - map.put("string", "String"); - map.put("enum", "String"); - map.put("file", "String"); - map.put("reference", "String"); - slimeTypeMap = Collections.unmodifiableMap(map); - } + private static final Map<String, String> vectorTypeDefs = Map.of( + "bool", "::config::BoolVector", + "int32_t", "::config::IntVector", + "int64_t", "::config::LongVector", + "double", "::config::DoubleVector", + "vespalib::string", "::config::StringVector" + ); + private static final Map<String, String> mapTypeDefs = Map.of( + "bool", "::config::BoolMap", + "int32_t", "::config::IntMap", + "int64_t", "::config::LongMap", + "double", "::config::DoubleMap", + "vespalib::string", "::config::StringMap" + ); + + private static final Map<String, String> slimeTypeMap = Map.of( + "bool", "Bool", + "int", "Long", + "long", "Long", + "double", "Double", + "string", "String", + "enum", "String", + "file", "String", + "reference", "String" + ); public CppClassBuilder(CNode root, NormalizedDefinition nd, File rootDir, String relativePathUnderRoot) { this.root = root; @@ -69,19 +57,6 @@ public class CppClassBuilder implements ClassBuilder { generateConfig(root, nd); } - String readFile(File f) throws IOException { - if (!f.isFile()) return null; - StringBuilder sb = new StringBuilder(); - try (BufferedReader sr = new BufferedReader(new FileReader(f))) { - while (true) { - String line = sr.readLine(); - if (line == null) break; - sb.append(line).append("\n"); - } - return sb.toString(); - } - } - void writeFile(File f, String content) throws IOException { FileWriter fw = new FileWriter(f); fw.write(content); @@ -118,7 +93,7 @@ public class CppClassBuilder implements ClassBuilder { static String removeDashesAndUpperCaseAllFirstChars(String source, boolean capitalizeFirst) { // Create upper case chars after each dash - String parts[] = source.split("[-_]"); + String [] parts = source.split("[-_]"); StringBuilder sb = new StringBuilder(); for (String s : parts) { sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1)); @@ -149,7 +124,7 @@ public class CppClassBuilder implements ClassBuilder { * Class to generate noexcept specifier if default constructor, copy constructor or copy assignment is non-throwing */ private static class NoExceptSpecifier { - private boolean enabled; + private final boolean enabled; public NoExceptSpecifier(CNode node) { enabled = checkNode(node); @@ -267,9 +242,7 @@ public class CppClassBuilder implements ClassBuilder { + "#define CLOUD_CONFIG_" + defineName + "_H\n" + "\n" + "#include <vespa/config/configgen/configinstance.h>\n" - + "#include <vespa/vespalib/stllike/string.h>\n" - + "#include <vector>\n" - + "#include <map>\n" + + "#include <vespa/config/common/types.h>\n" + "\n"); w.write("namespace config {\n"); w.write(" class ConfigValue;\n"); @@ -295,7 +268,7 @@ public class CppClassBuilder implements ClassBuilder { void writeTypeDeclarations(Writer w, CNode node, String indent) throws IOException { - java.util.Set<String> declaredTypes = new java.util.HashSet<String>(); + java.util.Set<String> declaredTypes = new java.util.HashSet<>(); for (CNode child : node.getChildren()) { boolean complexType = (child instanceof InnerCNode || child instanceof LeafCNode.EnumLeaf); if (complexType && !declaredTypes.contains(child.getName())) { @@ -357,7 +330,7 @@ public class CppClassBuilder implements ClassBuilder { } void writeStructFunctionDeclarations(Writer w, String className, CNode node, String indent) throws IOException { - w.write(indent + className + "(const std::vector<vespalib::string> & __lines);\n"); + w.write(indent + className + "(const " + vectorTypeDefs.get("vespalib::string") + " & __lines);\n"); w.write(indent + className + "(const vespalib::slime::Inspector & __inspector);\n"); w.write(indent + className + "(const ::config::ConfigPayload & __payload);\n"); writeCommonFunctionDeclarations(w, className, node, indent); @@ -370,6 +343,12 @@ public class CppClassBuilder implements ClassBuilder { void writeClassAssignmentOperatorDeclaration(Writer w, String className, NoExceptSpecifier noexcept, String indent) throws IOException { w.write(indent + className + " & operator = (const " + className + " & __rhs)" + noexcept + ";\n"); } + void writeClassMoveConstructorDeclaration(Writer w, String className, NoExceptSpecifier noexcept, String indent) throws IOException { + w.write(indent + className + "(" + className + " && __rhs)" + noexcept + ";\n"); + } + void writeClassMoveOperatorDeclaration(Writer w, String className, NoExceptSpecifier noexcept, String indent) throws IOException { + w.write(indent + className + " & operator = (" + className + " && __rhs)" + noexcept + ";\n"); + } void writeConfigClassCopyConstructorDefinition(Writer w, String parent, String className, NoExceptSpecifier noexcept) throws IOException { w.write(parent + "::" + className + "(const " + className + " & __rhs)" + noexcept + " = default;\n"); @@ -383,6 +362,11 @@ public class CppClassBuilder implements ClassBuilder { NoExceptSpecifier noexcept = new NoExceptSpecifier(node); w.write(parent + "::" + typeName + "(const " + typeName + " & __rhs)" + noexcept + " = default;\n"); } + void writeClassMoveConstructorDefinition(Writer w, String parent, CNode node) throws IOException { + String typeName = getTypeName(node, false); + NoExceptSpecifier noexcept = new NoExceptSpecifier(node); + w.write(parent + "::" + typeName + "(" + typeName + " && __rhs)" + noexcept + " = default;\n"); + } void writeClassAssignmentOperatorDefinition(Writer w, String parent, CNode node) throws IOException { String typeName = getTypeName(node, false); @@ -391,8 +375,15 @@ public class CppClassBuilder implements ClassBuilder { w.write(parent + " & " + parent + "::" + "operator = (const " + typeName + " & __rhs)" + noexcept + " = default;\n"); } + void writeClassMoveOperatorDefinition(Writer w, String parent, CNode node) throws IOException { + String typeName = getTypeName(node, false); + NoExceptSpecifier noexcept = new NoExceptSpecifier(node); + // Write empty constructor + w.write(parent + " & " + parent + "::" + "operator = (" + typeName + " && __rhs)" + noexcept + " = default;\n"); + } + void writeDestructor(Writer w, String parent, String className) throws IOException { - w.write(parent + "~" + className + "() { } \n"); + w.write(parent + "~" + className + "() = default; \n"); } void writeCommonFunctionDeclarations(Writer w, String className, CNode node, String indent) throws IOException { @@ -400,6 +391,8 @@ public class CppClassBuilder implements ClassBuilder { w.write("" + indent + className + "() " + noexcept + ";\n"); writeClassCopyConstructorDeclaration(w, className, noexcept, indent); writeClassAssignmentOperatorDeclaration(w, className, noexcept, indent); + writeClassMoveConstructorDeclaration(w, className, noexcept, indent); + writeClassMoveOperatorDeclaration(w, className, noexcept, indent); w.write("" + indent + "~" + className + "();\n"); w.write("\n" + indent + "bool operator==(const " + className + "& __rhs) const noexcept;\n" @@ -460,7 +453,7 @@ public class CppClassBuilder implements ClassBuilder { + indent + "static const vespalib::string CONFIG_DEF_VERSION;\n" + indent + "static const vespalib::string CONFIG_DEF_NAME;\n" + indent + "static const vespalib::string CONFIG_DEF_NAMESPACE;\n" - + indent + "static const std::vector<vespalib::string> CONFIG_DEF_SCHEMA;\n" + + indent + "static const ::config::StringVector CONFIG_DEF_SCHEMA;\n" + indent + "static const int64_t CONFIG_DEF_SERIALIZE_VERSION;\n" + "\n" ); @@ -469,7 +462,7 @@ public class CppClassBuilder implements ClassBuilder { void writeComment(Writer w, String indent, String comment, boolean javadoc) throws IOException { - /** If simple one liner comment, write on one line. */ + // If simple one liner comment, write on one line. if (javadoc && comment.indexOf('\n') == -1 && comment.length() <= 80 - (indent.length() + 7)) { @@ -481,7 +474,7 @@ public class CppClassBuilder implements ClassBuilder { w.write(indent + "// " + comment + "\n"); return; } - /** If not we need to write multi line comment. */ + // If not we need to write multi line comment. int maxLineLen = 80 - (indent.length() + 3); if (javadoc) w.write(indent + "/**\n"); do { @@ -543,18 +536,6 @@ public class CppClassBuilder implements ClassBuilder { void writeHeaderTypeDefs(Writer w, CNode root, String indent) throws IOException { w.write(indent + "typedef std::unique_ptr<const " + getInternalClassName(root) + "> UP;\n"); - for (Map.Entry<String, String> entry : vectorTypeDefs.entrySet()) { - String typeName = entry.getKey(); - String vectorName = entry.getValue(); - String typeDef = "typedef std::vector<" + typeName + "> " + vectorName; - w.write(indent + typeDef + ";\n"); - } - for (Map.Entry<String, String> entry : mapTypeDefs.entrySet()) { - String typeName = entry.getKey(); - String mapName = entry.getValue(); - String typeDef = "typedef std::map<vespalib::string, " + typeName + "> " + mapName; - w.write(indent + typeDef + ";\n"); - } } private static String getInternalClassName(CNode root) { @@ -599,11 +580,11 @@ public class CppClassBuilder implements ClassBuilder { w.write("#include <vespa/config/configgen/configpayload.h>\n"); w.write("#include <vespa/config/print/configdatabuffer.h>\n"); w.write("#include <vespa/config/common/configparser.h>\n"); - w.write("#include <vespa/config/configgen/vector_inserter.h>\n"); - w.write("#include <vespa/config/configgen/map_inserter.h>\n"); w.write("#include <vespa/vespalib/data/slime/convenience.h>\n"); w.write("#include <vespa/vespalib/data/slime/slime.h>\n"); w.write("#include <vespa/vespalib/stllike/asciistream.h>\n"); + w.write("#include <vespa/config/configgen/vector_inserter.hpp>\n"); + w.write("#include <vespa/config/configgen/map_inserter.hpp>\n"); w.write("\n"); writeNameSpaceBegin(w, generateCppNameSpace(root)); w.write("\nnamespace internal {\n\n"); @@ -627,7 +608,7 @@ public class CppClassBuilder implements ClassBuilder { w.write("\"" + line.replace("\"", "\\\"") + "\",\n"); } w.write("};\n"); - w.write("const std::vector<vespalib::string> " + typeName + "::CONFIG_DEF_SCHEMA(__internalDefSchema,\n"); + w.write("const ::config::StringVector " + typeName + "::CONFIG_DEF_SCHEMA(__internalDefSchema,\n"); w.write(" __internalDefSchema + (sizeof(__internalDefSchema) / \n"); w.write(" sizeof(__internalDefSchema[0])));\n"); w.write("\n"); @@ -640,7 +621,7 @@ public class CppClassBuilder implements ClassBuilder { root = true; } final String parent = fullClassName + "::"; - java.util.Set<String> declaredTypes = new java.util.HashSet<String>(); + java.util.Set<String> declaredTypes = new java.util.HashSet<>(); for (CNode child : node.getChildren()) { boolean complexType = (child instanceof InnerCNode || child instanceof LeafCNode.EnumLeaf); if (complexType && !declaredTypes.contains(child.getName())) { @@ -758,6 +739,8 @@ public class CppClassBuilder implements ClassBuilder { } else { writeClassCopyConstructorDefinition(w, fullClassName, node); writeClassAssignmentOperatorDefinition(w, fullClassName, node); + writeClassMoveConstructorDefinition(w, fullClassName, node); + writeClassMoveOperatorDefinition(w, fullClassName, node); } writeDestructor(w, parent, typeName); @@ -768,30 +751,20 @@ public class CppClassBuilder implements ClassBuilder { + "{\n" + indent + "try {\n"); indent = " "; - w.write(indent + "const std::vector<vespalib::string> & __lines(__value.getLines());\n"); + w.write(indent + "const " + vectorTypeDefs.get("vespalib::string") + " & __lines(__value.getLines());\n"); } else { - w.write(parent + typeName + "(const std::vector<vespalib::string> & __lines)\n" + w.write(parent + typeName + "(const " + vectorTypeDefs.get("vespalib::string") +" & __lines)\n" + "{\n"); } - w.write("" - + indent + "std::set<vespalib::string> __remainingValuesToParse(" - + "__lines.begin(), __lines.end());\n"); - w.write(indent + "for(std::set<vespalib::string>::iterator __rVTPiter = __remainingValuesToParse.begin();\n" - + indent + " __rVTPiter != __remainingValuesToParse.end();)\n" - + indent + "{\n" - + indent + " if (ConfigParser::stripWhitespace(*__rVTPiter).empty()) {\n" - + indent + " std::set<vespalib::string>::iterator __rVTPiter2 = __rVTPiter++;\n" - + indent + " __remainingValuesToParse.erase(__rVTPiter2);\n" - + indent + " } else {\n" - + indent + " ++__rVTPiter;\n" - + indent + " }\n" - + indent + "}\n"); + w.write(indent + "std::set<vespalib::string> __remainingValuesToParse = ConfigParser::getUniqueNonWhiteSpaceLines(__lines);\n"); for (CNode child : node.getChildren()) { String childType = getTypeName(child, false); String childName = getIdentifier(child.getName()); + String childVectorType = null; if (child instanceof LeafCNode.EnumLeaf) { if (child.isArray) { - w.write(indent + "std::vector<vespalib::string> " + childName + "__ValueList(\n "); + childVectorType = "::config::StringVector"; + w.write(indent + childVectorType + " " + childName + "__ValueList(\n "); } else if (child.isMap) { w.write(indent + "std::map<vespalib::string, vespalib::string> " + childName + "__ValueMap(\n "); } else { @@ -802,18 +775,17 @@ public class CppClassBuilder implements ClassBuilder { w.write(indent + childName + " = "); } if (child.isArray) { - w.write("ConfigParser::parseArray<" + childType + ">(\"" - + child.getName() + "\", __lines)"); + if (childVectorType == null) { + childVectorType = getTypeName(child, true); + } + w.write("ConfigParser::parseArray<" + childVectorType + ">(\"" + child.getName() + "\", __lines)"); } else if (child.isMap) { - w.write("ConfigParser::parseMap<" + childType + ">(\"" - + child.getName() + "\", __lines)"); + w.write("ConfigParser::parseMap<" + childType + ">(\"" + child.getName() + "\", __lines)"); } else { if (child instanceof LeafCNode) { - w.write("ConfigParser::parse<" + childType + ">(\"" - + child.getName() + "\", __lines"); + w.write("ConfigParser::parse<" + childType + ">(\"" + child.getName() + "\", __lines"); } else { - w.write("ConfigParser::parseStruct<" + childType + ">(\"" - + child.getName() + "\", __lines"); + w.write("ConfigParser::parseStruct<" + childType + ">(\"" + child.getName() + "\", __lines"); } if (child instanceof LeafCNode && ((LeafCNode) child).getDefaultValue() != null) { LeafCNode leaf = (LeafCNode) child; @@ -832,26 +804,20 @@ public class CppClassBuilder implements ClassBuilder { w.write(");\n"); if (child.isArray) { w.write(indent + childName + ".reserve(" + childName + "__ValueList.size());\n" - + indent + "for (std::vector<vespalib::string>::const_iterator __it\n" - + indent + " = " + childName + "__ValueList.begin();\n" - + indent + " __it != " + childName + "__ValueList.end(); ++__it)\n" - + indent + "{\n" - + indent + " " + childName + ".push_back(get" + childType + "(*__it));\n" + + indent + "for (const auto & item : " + childName + "__ValueList) {\n" + + indent + " " + childName + ".push_back(get" + childType + "(item));\n" + indent + "}\n" ); } else if (child.isMap) { - w.write(indent + "typedef std::map<vespalib::string, vespalib::string> __ValueMap;\n"); - w.write(indent + "for (__ValueMap::iterator __it(" + childName + "__ValueMap.begin()), __mt(" + childName + "__ValueMap.end()); __it != __mt; __it++) {\n" - + " " + childName + "[__it->first] = get" + childType + "(__it->second);\n" + w.write(indent + "for (const auto & entry : " + childName + "__ValueMap) {\n" + + " " + childName + "[entry.first] = get" + childType + "(entry.second);\n" + "}\n" ); } } else { w.write(";\n"); } - w.write(indent + "ConfigParser::stripLinesForKey(\"" - + child.getName() + "\", " - + "__remainingValuesToParse);\n"); + w.write(indent + "ConfigParser::stripLinesForKey(\"" + child.getName() + "\", " + "__remainingValuesToParse);\n"); } if (root) { indent = " "; @@ -864,9 +830,8 @@ public class CppClassBuilder implements ClassBuilder { + "\n" ); // Write operator== - String lineBreak = (parent.length() + typeName.length() < 50 ? "" : "\n"); w.write("bool\n" - + parent + lineBreak + "operator==(const " + typeName + "& __rhs) const noexcept\n" + + parent + lineBreak(parent, typeName) + "operator==(const " + typeName + "& __rhs) const noexcept\n" + "{\n" + " return (" ); @@ -883,9 +848,8 @@ public class CppClassBuilder implements ClassBuilder { + "\n" ); // Write operator!= - lineBreak = (parent.length() + typeName.length() < 50 ? "" : "\n"); w.write("bool\n" - + parent + lineBreak + "operator!=(const " + typeName + "& __rhs) const noexcept\n" + + parent + lineBreak(parent, typeName) + "operator!=(const " + typeName + "& __rhs) const noexcept\n" + "{\n" + " return !(operator==(__rhs));\n" + "}\n" @@ -896,6 +860,10 @@ public class CppClassBuilder implements ClassBuilder { writeSlimeConstructor(w, node, parent, root); } + private static String lineBreak(String parent, String typeName) { + return (parent.length() + typeName.length() < 50 ? "" : "\n"); + } + public void writeSlimeEncoder(Writer w, CNode node, String parent, boolean root) throws IOException { String indent = " "; @@ -929,55 +897,54 @@ public class CppClassBuilder implements ClassBuilder { if (child.isArray) { w.write(indent + "__c.setString(\"type\", \"array\");\n"); w.write(indent + "vespalib::slime::Cursor & __c2 = __c.setArray(\"value\");\n"); - w.write(indent + "for (size_t __i = 0; __i < " + childName + ".size(); __i++) {\n"); + w.write(indent + "for (const auto & child : " + childName +") {\n"); w.write(indent + " vespalib::slime::Cursor & __c3 = __c2.addObject();\n"); if (child instanceof LeafCNode.EnumLeaf) { String repType = slimeTypeMap.get("enum"); w.write(indent + " __c3.setString(\"type\", \"enum\");\n"); w.write(indent + " __c3.set" + repType); - w.write("(\"value\", vespalib::Memory(get" + childType + "Name(" + childName + "[__i])));\n"); + w.write("(\"value\", vespalib::Memory(get" + childType + "Name(child)));\n"); } else if (child instanceof LeafCNode) { String type = ((LeafCNode) child).getType(); String repType = slimeTypeMap.get(type); w.write(indent + " __c3.setString(\"type\", \"" + type + "\");\n"); w.write(indent + " __c3.set" + repType); if ("String".equals(repType)) { - w.write("(\"value\", vespalib::Memory(" + childName + "[__i]));\n"); + w.write("(\"value\", vespalib::Memory(child));\n"); } else { - w.write("(\"value\", " + childName + "[__i]);\n"); + w.write("(\"value\", child);\n"); } } else { w.write(indent + " __c3.setString(\"type\", \"struct\");\n"); w.write(indent + " Cursor & __c4 = __c3.setObject(\"value\");\n"); - w.write(indent + " " + childName + "[__i].serialize(__c4);\n"); + w.write(indent + " child.serialize(__c4);\n"); } w.write(indent + "}\n"); } else if (child.isMap) { w.write(indent + "__c.setString(\"type\", \"map\");\n"); w.write(indent + "vespalib::slime::Cursor & __c2 = __c.setArray(\"value\");\n"); - String childMapType = getTypeName(child, true); - w.write(indent + "for (" + childMapType + "::const_iterator it(" + childName + ".begin()), mt(" + childName + ".end()); it != mt; it++) {\n"); + w.write(indent + "for (const auto & entry : " + childName + ") {\n"); w.write(indent + " vespalib::slime::Cursor & __c3 = __c2.addObject();\n"); - w.write(indent + " __c3.setString(\"key\", vespalib::Memory(it->first));\n"); + w.write(indent + " __c3.setString(\"key\", vespalib::Memory(entry.first));\n"); if (child instanceof LeafCNode.EnumLeaf) { String repType = slimeTypeMap.get("enum"); w.write(indent + " __c3.setString(\"type\", \"enum\");\n"); w.write(indent + " __c3.set" + repType); - w.write("(\"value\", vespalib::Memory(get" + childType + "Name(it->second)));\n"); + w.write("(\"value\", vespalib::Memory(get" + childType + "Name(entry.second)));\n"); } else if (child instanceof LeafCNode) { String type = ((LeafCNode) child).getType(); String repType = slimeTypeMap.get(type); w.write(indent + " __c3.setString(\"type\", \"" + type + "\");\n"); w.write(indent + " __c3.set" + repType); if ("String".equals(repType)) { - w.write("(\"value\", vespalib::Memory(it->second));\n"); + w.write("(\"value\", vespalib::Memory(entry.second));\n"); } else { - w.write("(\"value\", it->second);\n"); + w.write("(\"value\", entry.second);\n"); } } else { w.write(indent + " __c3.setString(\"type\", \"struct\");\n"); w.write(indent + " Cursor & __c4 = __c3.setObject(\"value\");\n"); - w.write(indent + " it->second.serialize(__c4);\n"); + w.write(indent + " entry.second.serialize(__c4);\n"); } w.write(indent + "}\n"); } else { @@ -1033,39 +1000,13 @@ public class CppClassBuilder implements ClassBuilder { if (child.isArray) { w.write(indent + "for (size_t __i = 0; __i < " + inspectorLine + ".children(); __i++) {\n"); w.write(indent + " " + childName + ".push_back("); - if (child instanceof LeafCNode.EnumLeaf) { - String repType = slimeTypeMap.get("enum"); - w.write("get" + childType + "(" + inspectorLine + "[__i][\"value\"].as" + repType + "().make_string())"); - } else if (child instanceof LeafCNode) { - String type = ((LeafCNode) child).getType(); - String repType = slimeTypeMap.get(type); - if ("String".equals(repType)) { - w.write("" + inspectorLine + "[__i][\"value\"].as" + repType + "().make_string()"); - } else { - w.write("" + inspectorLine + "[__i][\"value\"].as" + repType + "()"); - } - } else { - w.write(childType + "(" + inspectorLine + "[__i][\"value\"])"); - } + writeSlimeChild(w, child, childType, inspectorLine); w.write(");\n"); w.write(indent + "}\n"); } else if (child.isMap) { w.write(indent + "for (size_t __i = 0; __i < " + inspectorLine + ".children(); __i++) {\n"); w.write(indent + " " + childName + "[" + inspectorLine + "[__i][\"key\"].asString().make_string()] = "); - if (child instanceof LeafCNode.EnumLeaf) { - String repType = slimeTypeMap.get("enum"); - w.write("get" + childType + "(" + inspectorLine + "[__i][\"value\"].as" + repType + "().make_string())"); - } else if (child instanceof LeafCNode) { - String type = ((LeafCNode) child).getType(); - String repType = slimeTypeMap.get(type); - if ("String".equals(repType)) { - w.write("" + inspectorLine + "[__i][\"value\"].as" + repType + "().make_string()"); - } else { - w.write("" + inspectorLine + "[__i][\"value\"].as" + repType + "()"); - } - } else { - w.write(childType + "(" + inspectorLine + "[__i][\"value\"])"); - } + writeSlimeChild(w, child, childType, inspectorLine); w.write(";\n"); w.write(indent + "}\n"); } else { @@ -1090,6 +1031,23 @@ public class CppClassBuilder implements ClassBuilder { w.write("}\n\n"); } + private void writeSlimeChild(Writer w, CNode child, String childType, String inspectorLine) throws IOException { + if (child instanceof LeafCNode.EnumLeaf) { + String repType = slimeTypeMap.get("enum"); + w.write("get" + childType + "(" + inspectorLine + "[__i][\"value\"].as" + repType + "().make_string())"); + } else if (child instanceof LeafCNode) { + String type = ((LeafCNode) child).getType(); + String repType = slimeTypeMap.get(type); + if ("String".equals(repType)) { + w.write("" + inspectorLine + "[__i][\"value\"].as" + repType + "().make_string()"); + } else { + w.write("" + inspectorLine + "[__i][\"value\"].as" + repType + "()"); + } + } else { + w.write(childType + "(" + inspectorLine + "[__i][\"value\"])"); + } + } + public void writeSlimeConstructor(Writer w, CNode node, String parent, boolean root) throws IOException { String tmpName = getTypeName(node, false); String typeName = root ? getInternalClassName(node) : tmpName; @@ -1110,7 +1068,8 @@ public class CppClassBuilder implements ClassBuilder { String childInspector = "__inspector[\"" + child.getName() + "\"]"; if (child.isArray) { String inserterName = "__" + childName + "Inserter"; - w.write(indent + "::config::internal::VectorInserter<" + childType); + String childVectorType = getTypeName(child, true); + w.write(indent + "::config::internal::VectorInserter<" + childVectorType); if (child instanceof LeafCNode.EnumLeaf) { w.write(", Internal" + childType + "Converter"); } diff --git a/document/src/vespa/document/fieldvalue/document.cpp b/document/src/vespa/document/fieldvalue/document.cpp index 15198013c5f..36526f09dd6 100644 --- a/document/src/vespa/document/fieldvalue/document.cpp +++ b/document/src/vespa/document/fieldvalue/document.cpp @@ -10,7 +10,6 @@ #include <vespa/vespalib/objects/nbostream.h> #include <vespa/document/util/serializableexceptions.h> #include <vespa/document/fieldset/fieldsets.h> -#include <vespa/document/util/bytebuffer.h> #include <vespa/vespalib/data/databuffer.h> #include <vespa/vespalib/util/xmlstream.h> #include <vespa/vespalib/stllike/hash_map.hpp> diff --git a/searchcommon/src/vespa/searchcommon/common/schema.cpp b/searchcommon/src/vespa/searchcommon/common/schema.cpp index 0127f727069..c6a2a4532a3 100644 --- a/searchcommon/src/vespa/searchcommon/common/schema.cpp +++ b/searchcommon/src/vespa/searchcommon/common/schema.cpp @@ -48,7 +48,7 @@ writeFieldSets(vespalib::asciistream &os, struct FieldName { vespalib::string name; - FieldName(const std::vector<vespalib::string> & lines) + FieldName(const config::StringVector & lines) : name(ConfigParser::parse<vespalib::string>("name", lines)) { } @@ -64,8 +64,7 @@ getFieldId(vespalib::stringref name, const T &map) } // namespace -namespace search { -namespace index { +namespace search::index { const uint32_t Schema::UNKNOWN_FIELD_ID(std::numeric_limits<uint32_t>::max()); @@ -88,7 +87,7 @@ Schema::Field::Field(vespalib::stringref n, DataType dt, CollectionType ct, vesp } // XXX: Resource leak if exception is thrown. -Schema::Field::Field(const std::vector<vespalib::string> & lines) +Schema::Field::Field(const config::StringVector & lines) : _name(ConfigParser::parse<vespalib::string>("name", lines)), _dataType(schema::dataTypeFromName(ConfigParser::parse<vespalib::string>("datatype", lines))), _collectionType(schema::collectionTypeFromName(ConfigParser::parse<vespalib::string>("collectiontype", lines))) @@ -140,7 +139,7 @@ Schema::IndexField::IndexField(vespalib::stringref name, DataType dt, { } -Schema::IndexField::IndexField(const std::vector<vespalib::string> &lines) +Schema::IndexField::IndexField(const config::StringVector &lines) : Field(lines), _avgElemLen(ConfigParser::parse<int32_t>("averageelementlen", lines, 512)), _interleaved_features(ConfigParser::parse<bool>("interleavedfeatures", lines, false)) @@ -181,11 +180,11 @@ Schema::IndexField::operator!=(const IndexField &rhs) const _interleaved_features != rhs._interleaved_features; } -Schema::FieldSet::FieldSet(const std::vector<vespalib::string> & lines) : +Schema::FieldSet::FieldSet(const config::StringVector & lines) : _name(ConfigParser::parse<vespalib::string>("name", lines)), _fields() { - std::vector<FieldName> fn = ConfigParser::parseArray<FieldName>("field", lines); + std::vector<FieldName> fn = ConfigParser::parseArray<std::vector<FieldName>>("field", lines); for (size_t i = 0; i < fn.size(); ++i) { _fields.push_back(fn[i].name); } @@ -238,16 +237,16 @@ Schema::loadFromFile(const vespalib::string & fileName) LOG(warning, "Could not open input file '%s' as part of loadFromFile()", fileName.c_str()); return false; } - std::vector<vespalib::string> lines; + config::StringVector lines; std::string tmpLine; while (file) { getline(file, tmpLine); lines.push_back(tmpLine); } - _indexFields = ConfigParser::parseArray<IndexField>("indexfield", lines); - _attributeFields = ConfigParser::parseArray<AttributeField>("attributefield", lines); - _summaryFields = ConfigParser::parseArray<SummaryField>("summaryfield", lines); - _fieldSets = ConfigParser::parseArray<FieldSet>("fieldset", lines); + _indexFields = ConfigParser::parseArray<std::vector<IndexField>>("indexfield", lines); + _attributeFields = ConfigParser::parseArray<std::vector<AttributeField>>("attributefield", lines); + _summaryFields = ConfigParser::parseArray<std::vector<SummaryField>>("summaryfield", lines); + _fieldSets = ConfigParser::parseArray<std::vector<FieldSet>>("fieldset", lines); _importedAttributeFields.clear(); // NOTE: these are not persisted to disk _indexIds.clear(); for (size_t i(0), m(_indexFields.size()); i < m; i++) { @@ -579,5 +578,4 @@ Schema::empty() const _importedAttributeFields.empty(); } -} // namespace search::index -} // namespace search +} diff --git a/searchcommon/src/vespa/searchcommon/common/schema.h b/searchcommon/src/vespa/searchcommon/common/schema.h index f86bb3068c5..3a9bcbdd904 100644 --- a/searchcommon/src/vespa/searchcommon/common/schema.h +++ b/searchcommon/src/vespa/searchcommon/common/schema.h @@ -3,10 +3,9 @@ #pragma once #include "datatype.h" -#include <vespa/vespalib/stllike/string.h> +#include <vespa/config/common/types.h> #include <vespa/vespalib/stllike/hash_map.h> #include <vespa/vespalib/util/ptrholder.h> -#include <vector> namespace vespalib { class asciistream; } namespace search::index { @@ -20,7 +19,6 @@ class Schema public: using UP = std::unique_ptr<Schema>; using SP = std::shared_ptr<Schema>; - using PH = vespalib::PtrHolder<Schema>; using DataType = schema::DataType; using CollectionType = schema::CollectionType; @@ -45,7 +43,7 @@ public: /** * Create this field based on the given config lines. **/ - Field(const std::vector<vespalib::string> & lines); + Field(const config::StringVector & lines); Field(const Field &) noexcept; Field & operator = (const Field &) noexcept; Field(Field &&) noexcept; @@ -92,7 +90,7 @@ public: /** * Create this index field based on the given config lines. **/ - IndexField(const std::vector<vespalib::string> &lines); + IndexField(const config::StringVector &lines); IndexField &setAvgElemLen(uint32_t avgElemLen) { _avgElemLen = avgElemLen; return *this; } IndexField &set_interleaved_features(bool value) { @@ -133,7 +131,7 @@ public: /** * Create this field collection based on the given config lines. **/ - FieldSet(const std::vector<vespalib::string> & lines); + FieldSet(const config::StringVector & lines); ~FieldSet(); @@ -143,8 +141,9 @@ public: } const vespalib::string &getName() const { return _name; } - const std::vector<vespalib::string> &getFields() const - { return _fields; } + const std::vector<vespalib::string> &getFields() const { + return _fields; + } bool operator==(const FieldSet &rhs) const; bool operator!=(const FieldSet &rhs) const; diff --git a/storage/src/vespa/storage/distributor/distributor_component.h b/storage/src/vespa/storage/distributor/distributor_component.h index 34fd08140e0..282f06981d3 100644 --- a/storage/src/vespa/storage/distributor/distributor_component.h +++ b/storage/src/vespa/storage/distributor/distributor_component.h @@ -23,7 +23,7 @@ class DistributorComponent : public storage::DistributorComponent, public DistributorOperationContext { private: DistributorInterface& _distributor; - BucketSpaceStateMap _bucket_space_states; + BucketSpaceStateMap _bucket_space_states; public: diff --git a/vespalib/src/tests/stllike/asciistream_test.cpp b/vespalib/src/tests/stllike/asciistream_test.cpp index be0bc1cb694..3042595e18c 100644 --- a/vespalib/src/tests/stllike/asciistream_test.cpp +++ b/vespalib/src/tests/stllike/asciistream_test.cpp @@ -409,27 +409,14 @@ AsciistreamTest::testWriteThenRead() void AsciistreamTest::testGetLine() { - asciistream is(""); - EXPECT_TRUE(is.getlines().empty()); - is = asciistream("line 1"); - std::vector<string> v = is.getlines(); - EXPECT_EQUAL(1u, v.size()); - EXPECT_EQUAL("line 1", v[0]); - is = asciistream("line 1\nline 2"); - v = is.getlines(); - EXPECT_EQUAL(2u, v.size()); - EXPECT_EQUAL("line 1", v[0]); - EXPECT_EQUAL("line 2", v[1]); - is = asciistream("line 1\nline 2\n\n"); - v = is.getlines(); - EXPECT_EQUAL(3u, v.size()); - EXPECT_EQUAL("line 1", v[0]); - EXPECT_EQUAL("line 2", v[1]); - EXPECT_EQUAL("", v[2]); - is = asciistream("line 1"); + asciistream is = asciistream("line 1\nline 2\nline 3"); string s; getline(is, s); EXPECT_EQUAL("line 1", s); + getline(is, s); + EXPECT_EQUAL("line 2", s); + getline(is, s); + EXPECT_EQUAL("line 3", s); } #define VERIFY_DOUBLE_SERIALIZATION(value, expected, format, precision) { \ diff --git a/vespalib/src/vespa/vespalib/stllike/asciistream.cpp b/vespalib/src/vespa/vespalib/stllike/asciistream.cpp index 3602cbe6151..eb127c7051a 100644 --- a/vespalib/src/vespa/vespalib/stllike/asciistream.cpp +++ b/vespalib/src/vespa/vespalib/stllike/asciistream.cpp @@ -8,10 +8,9 @@ #include <vespa/vespalib/locale/c.h> #include <vespa/fastos/file.h> #include <limits> -#include <stdexcept> #include <cassert> -#include <cmath> #include <charconv> +#include <vector> #include <vespa/log/log.h> LOG_SETUP(".vespalib.stllike.asciistream"); @@ -603,16 +602,6 @@ asciistream::write(const void * buf, size_t len) _rbuf = _wbuf; } -std::vector<string> -asciistream::getlines(char delim) -{ - std::vector<string> lines; - while (!eof()) { - lines.push_back(getline(delim)); - } - return lines; -} - string asciistream::getline(char delim) { diff --git a/vespalib/src/vespa/vespalib/stllike/asciistream.h b/vespalib/src/vespa/vespalib/stllike/asciistream.h index 6a4b4378634..b333be6cec2 100644 --- a/vespalib/src/vespa/vespalib/stllike/asciistream.h +++ b/vespalib/src/vespa/vespalib/stllike/asciistream.h @@ -2,7 +2,6 @@ #pragma once #include <vespa/vespalib/stllike/string.h> -#include <vector> namespace vespalib { @@ -153,7 +152,6 @@ public: static asciistream createFromFile(stringref fileName); static asciistream createFromDevice(stringref fileName); string getline(char delim='\n'); - std::vector<string> getlines(char delim='\n'); char getFill() const noexcept { return _fill; } size_t getWidth() const noexcept { return static_cast<size_t>(_width); } // match input type of setw Base getBase() const noexcept { return _base; } |