diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2016-11-30 00:30:37 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2016-12-12 02:55:41 +0100 |
commit | 848672a85b9a553034e4ff46a52521e3ce681912 (patch) | |
tree | 5383b69bff5b732481689f3ec71625a348600431 /document | |
parent | 97cff7808649426865e0275f1c24058dbfd65940 (diff) |
Do not include the excetions everywhere.
Diffstat (limited to 'document')
22 files changed, 415 insertions, 368 deletions
diff --git a/document/src/vespa/document/fieldvalue/arrayfieldvalue.cpp b/document/src/vespa/document/fieldvalue/arrayfieldvalue.cpp index fa872a31870..bc507316e19 100644 --- a/document/src/vespa/document/fieldvalue/arrayfieldvalue.cpp +++ b/document/src/vespa/document/fieldvalue/arrayfieldvalue.cpp @@ -1,5 +1,4 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <vespa/fastos/fastos.h> #include <vespa/document/fieldvalue/arrayfieldvalue.h> #include <vespa/document/fieldvalue/intfieldvalue.h> #include <vespa/document/fieldvalue/floatfieldvalue.h> @@ -12,6 +11,8 @@ #include <vespa/document/util/bytebuffer.h> #include <vespa/document/util/serializable.h> +#include <vespa/document/util/serializableexceptions.h> +#include <vespa/vespalib/util/exceptions.h> #include <vespa/log/log.h> LOG_SETUP(".document.fieldvalue.array"); diff --git a/document/src/vespa/document/fieldvalue/collectionfieldvalue.cpp b/document/src/vespa/document/fieldvalue/collectionfieldvalue.cpp index 8d00540856b..e5e4bdb8c16 100644 --- a/document/src/vespa/document/fieldvalue/collectionfieldvalue.cpp +++ b/document/src/vespa/document/fieldvalue/collectionfieldvalue.cpp @@ -1,7 +1,7 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <vespa/fastos/fastos.h> #include <vespa/document/fieldvalue/collectionfieldvalue.h> +#include <vespa/vespalib/util/exceptions.h> namespace document { diff --git a/document/src/vespa/document/fieldvalue/document.cpp b/document/src/vespa/document/fieldvalue/document.cpp index d5210b4693c..d849cd6d513 100644 --- a/document/src/vespa/document/fieldvalue/document.cpp +++ b/document/src/vespa/document/fieldvalue/document.cpp @@ -13,6 +13,8 @@ #include <vespa/document/serialization/vespadocumentdeserializer.h> #include <vespa/document/serialization/vespadocumentserializer.h> #include <vespa/vespalib/objects/nbostream.h> +#include <vespa/document/util/serializableexceptions.h> +#include <sstream> using vespalib::nbostream; using vespalib::make_string; diff --git a/document/src/vespa/document/fieldvalue/fieldvalue.cpp b/document/src/vespa/document/fieldvalue/fieldvalue.cpp index d63c9e37ed7..3d3e0cd4358 100644 --- a/document/src/vespa/document/fieldvalue/fieldvalue.cpp +++ b/document/src/vespa/document/fieldvalue/fieldvalue.cpp @@ -20,6 +20,8 @@ #include <vespa/document/util/bytebuffer.h> #include <vespa/vespalib/objects/fieldbase.h> #include <vespa/vespalib/objects/nbostream.h> +#include <vespa/document/util/serializableexceptions.h> +#include <sstream> using vespalib::FieldBase; using vespalib::nbostream; diff --git a/document/src/vespa/document/fieldvalue/literalfieldvalue.cpp b/document/src/vespa/document/fieldvalue/literalfieldvalue.cpp index 3d492e1e1e8..0c7395b7d02 100644 --- a/document/src/vespa/document/fieldvalue/literalfieldvalue.cpp +++ b/document/src/vespa/document/fieldvalue/literalfieldvalue.cpp @@ -1,7 +1,8 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <vespa/fastos/fastos.h> #include <vespa/document/fieldvalue/literalfieldvalue.h> +#include <vespa/document/util/stringutil.h> +#include <sstream> namespace document { diff --git a/document/src/vespa/document/fieldvalue/literalfieldvalue.h b/document/src/vespa/document/fieldvalue/literalfieldvalue.h index b5c8ace6e9c..c601af88c20 100644 --- a/document/src/vespa/document/fieldvalue/literalfieldvalue.h +++ b/document/src/vespa/document/fieldvalue/literalfieldvalue.h @@ -28,14 +28,7 @@ public: typedef std::unique_ptr<LiteralFieldValueB> UP; typedef string value_type; - LiteralFieldValueB() : - FieldValue(), - _value(), - _backing(), - _altered(true) - { - _value = _backing; - } + LiteralFieldValueB(); LiteralFieldValueB(const LiteralFieldValueB &); LiteralFieldValueB(const string& value); @@ -60,26 +53,25 @@ public: _value = _backing; _altered = true; } - virtual size_t hash() const { return vespalib::hashValue(_value.c_str()); } + size_t hash() const override { return vespalib::hashValue(_value.c_str()); } void setValue(const char* val, size_t size) { setValue(stringref(val, size)); } - // FieldValue implementation. - virtual int compare(const FieldValue& other) const; + int compare(const FieldValue& other) const override; - virtual vespalib::string getAsString() const; - virtual std::pair<const char*, size_t> getAsRaw() const; + vespalib::string getAsString() const override; + std::pair<const char*, size_t> getAsRaw() const override; - virtual void printXml(XmlOutputStream& out) const; - virtual void print(std::ostream& out, bool verbose, - const std::string& indent) const; - virtual FieldValue& assign(const FieldValue&); - virtual bool hasChanged() const { return _altered; } + void printXml(XmlOutputStream& out) const override; + void print(std::ostream& out, bool verbose, + const std::string& indent) const override; + FieldValue& assign(const FieldValue&) override; + bool hasChanged() const override{ return _altered; } - virtual FieldValue& operator=(const vespalib::stringref &); - virtual FieldValue& operator=(int32_t); - virtual FieldValue& operator=(int64_t); - virtual FieldValue& operator=(float); - virtual FieldValue& operator=(double); + FieldValue& operator=(const vespalib::stringref &) override; + FieldValue& operator=(int32_t) override; + FieldValue& operator=(int64_t) override; + FieldValue& operator=(float) override; + FieldValue& operator=(double) override; protected: void syncBacking() const __attribute__((noinline)); void sync() const { @@ -97,28 +89,14 @@ private: template<typename SubClass, int type, bool addZeroTerm> class LiteralFieldValue : public LiteralFieldValueB { private: - virtual bool getAddZeroTerm() const { return addZeroTerm; } + bool getAddZeroTerm() const override{ return addZeroTerm; } public: typedef std::unique_ptr<SubClass> UP; LiteralFieldValue() : LiteralFieldValueB() { } LiteralFieldValue(const string& value) : LiteralFieldValueB(value) { } - virtual const DataType *getDataType() const; + const DataType *getDataType() const override; }; -template<typename SubClass, int type, bool addZeroTerm> -const DataType * -LiteralFieldValue<SubClass, type, addZeroTerm>::getDataType() const -{ - switch (type) { - case DataType::T_URI: return DataType::URI; - case DataType::T_STRING: return DataType::STRING; - case DataType::T_RAW: return DataType::RAW; - default: - throw vespalib::IllegalStateException(vespalib::make_string( - "Illegal literal type id %i", type), VESPA_STRLOC); - } -} - } // document diff --git a/document/src/vespa/document/fieldvalue/literalfieldvalue.hpp b/document/src/vespa/document/fieldvalue/literalfieldvalue.hpp new file mode 100644 index 00000000000..35e9dbb6a52 --- /dev/null +++ b/document/src/vespa/document/fieldvalue/literalfieldvalue.hpp @@ -0,0 +1,23 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +#include "literalfieldvalue.h" +#include <vespa/vespalib/util/exceptions.h> + +namespace document { + +template<typename SubClass, int type, bool addZeroTerm> +const DataType * +LiteralFieldValue<SubClass, type, addZeroTerm>::getDataType() const +{ + switch (type) { + case DataType::T_URI: return DataType::URI; + case DataType::T_STRING: return DataType::STRING; + case DataType::T_RAW: return DataType::RAW; + default: + throw vespalib::IllegalStateException(vespalib::make_string( + "Illegal literal type id %i", type), VESPA_STRLOC); + } +} + +} diff --git a/document/src/vespa/document/fieldvalue/numericfieldvalue.h b/document/src/vespa/document/fieldvalue/numericfieldvalue.h index 5464c064997..1d3b373fdfc 100644 --- a/document/src/vespa/document/fieldvalue/numericfieldvalue.h +++ b/document/src/vespa/document/fieldvalue/numericfieldvalue.h @@ -10,13 +10,7 @@ */ #pragma once -#include <boost/cast.hpp> -#include <boost/lexical_cast.hpp> -#include <boost/numeric/conversion/cast.hpp> #include <vespa/document/fieldvalue/fieldvalue.h> -#include <vespa/document/util/bytebuffer.h> -#include <vespa/vespalib/stllike/asciistream.h> -#include <vespa/vespalib/stllike/lexical_cast.h> #include <vespa/vespalib/stllike/hash_fun.h> namespace document { @@ -63,186 +57,5 @@ public: virtual bool hasChanged() const override { return _altered; } }; -template<typename Number> -FieldValue& -NumericFieldValue<Number>::assign(const FieldValue& value) -{ - if (value.getClass().id() == IDENTIFIABLE_CLASSID(ByteFieldValue)) { - _value = static_cast<Number>(value.getAsByte()); - } else if (value.getClass().id() == IDENTIFIABLE_CLASSID(ShortFieldValue)) { - _value = static_cast<Number>(value.getAsInt()); - } else if (value.getClass().id() == IDENTIFIABLE_CLASSID(IntFieldValue)) { - _value = static_cast<Number>(value.getAsInt()); - } else if (value.getClass().id() == IDENTIFIABLE_CLASSID(LongFieldValue)) { - _value = static_cast<Number>(value.getAsLong()); - } else if (value.getClass().id() == IDENTIFIABLE_CLASSID(FloatFieldValue)) { - _value = static_cast<Number>(value.getAsFloat()); - } else if (value.getClass().id() == IDENTIFIABLE_CLASSID(DoubleFieldValue)) - { - _value = static_cast<Number>(value.getAsDouble()); - } else { - return FieldValue::assign(value); - } - _altered = true; - return *this; -} - -template<typename Number> -int -NumericFieldValue<Number>::compare( const FieldValue& other) const -{ - int diff = FieldValue::compare(other); - if (diff != 0) return diff; - - const NumericFieldValue & otherNumber(static_cast<const NumericFieldValue &>(other)); - return (_value == otherNumber._value) - ? 0 - : (_value - otherNumber._value > 0) - ? 1 - : -1; -} - -template<typename Number> -void -NumericFieldValue<Number>::print( - std::ostream& out, bool, const std::string&) const -{ - if (sizeof(Number) == 1) { // Make sure char's are printed as numbers - out << (int) _value; - } else { - out << _value; - } -} - -template<typename Number> -FieldValue& -NumericFieldValue<Number>::operator=(const vespalib::stringref & value) -{ - // Lexical cast doesn't allow hex syntax we use in XML, - // so detect these in front. - if ((value.size() > 2) && (value[0] == '0') && ((value[1] | 0x20) == 'x')) { - char* endp; - // It is safe to assume that all hex numbers can be contained within - // 64 bit unsigned value. - unsigned long long val = strtoull(value.c_str(), &endp, 16); - if (*endp == '\0') { - // Allow numbers to be specified in range max signed to max - // unsigned. These become negative numbers. - _value = static_cast<Number>(val); - _altered = true; - return *this; - } - } - if (sizeof(Number) == sizeof(int8_t)) { - int val = vespalib::lexical_cast<int>(value); - if (val < -128 || val > 255) { - throw vespalib::IllegalArgumentException( - "Value of byte must be in the range -128 to 255", VESPA_STRLOC); - } - _value = static_cast<Number>(val); - } else { - try{ - _value = boost::lexical_cast<Number>(value); - } catch (boost::bad_lexical_cast& e) { - // If bad cast is thrown due to value being bigger than max positive - // signed value, but less than max positive unsigned value, - // use this workaround to try to convert it to signed. - if (sizeof(Number) == sizeof(uint32_t)) { - _value = boost::numeric_cast<Number>( - static_cast<int32_t>(boost::lexical_cast<uint32_t>(value))); - } else { - _value = boost::numeric_cast<Number>( - static_cast<int64_t>(boost::lexical_cast<uint64_t>(value))); - } - } - } - _altered = true; - return *this; -} - -template<typename Number> -FieldValue& -NumericFieldValue<Number>::operator=(int32_t value) -{ - _value = static_cast<Number>(value); - _altered = true; - return *this; -} - -template<typename Number> -FieldValue& -NumericFieldValue<Number>::operator=(int64_t value) -{ - _value = static_cast<Number>(value); - _altered = true; - return *this; -} - -template<typename Number> -FieldValue& -NumericFieldValue<Number>::operator=(float value) -{ - _value = static_cast<Number>(value); - _altered = true; - return *this; -} - -template<typename Number> -FieldValue& -NumericFieldValue<Number>::operator=(double value) -{ - _value = static_cast<Number>(value); - _altered = true; - return *this; -} - -template<typename Number> -char -NumericFieldValue<Number>::getAsByte() const -{ - return static_cast<char>(_value); -} - -template<typename Number> -int32_t -NumericFieldValue<Number>::getAsInt() const -{ - return static_cast<int32_t>(_value); -} - -template<typename Number> -int64_t -NumericFieldValue<Number>::getAsLong() const -{ - return static_cast<int64_t>(_value); -} - -template<typename Number> -float -NumericFieldValue<Number>::getAsFloat() const -{ - return static_cast<float>(_value); -} - -template<typename Number> -double -NumericFieldValue<Number>::getAsDouble() const -{ - return static_cast<double>(_value); -} - -template<typename Number> -vespalib::string -NumericFieldValue<Number>::getAsString() const -{ - vespalib::asciistream ost; - if (sizeof(Number) == sizeof(uint8_t)) { - ost << static_cast<uint32_t>(_value); - } else { - ost << _value; - } - return ost.str(); -} - } // document diff --git a/document/src/vespa/document/fieldvalue/numericfieldvalue.hpp b/document/src/vespa/document/fieldvalue/numericfieldvalue.hpp new file mode 100644 index 00000000000..ff4da5f4722 --- /dev/null +++ b/document/src/vespa/document/fieldvalue/numericfieldvalue.hpp @@ -0,0 +1,197 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <vespa/document/fieldvalue/fieldvalue.h> +#include <vespa/document/util/bytebuffer.h> +#include <vespa/vespalib/stllike/asciistream.h> +#include <vespa/vespalib/stllike/lexical_cast.h> +#include <boost/cast.hpp> +#include <boost/lexical_cast.hpp> +#include <boost/numeric/conversion/cast.hpp> + +namespace document { + +template<typename Number> +FieldValue& +NumericFieldValue<Number>::assign(const FieldValue& value) +{ + if (value.getClass().id() == IDENTIFIABLE_CLASSID(ByteFieldValue)) { + _value = static_cast<Number>(value.getAsByte()); + } else if (value.getClass().id() == IDENTIFIABLE_CLASSID(ShortFieldValue)) { + _value = static_cast<Number>(value.getAsInt()); + } else if (value.getClass().id() == IDENTIFIABLE_CLASSID(IntFieldValue)) { + _value = static_cast<Number>(value.getAsInt()); + } else if (value.getClass().id() == IDENTIFIABLE_CLASSID(LongFieldValue)) { + _value = static_cast<Number>(value.getAsLong()); + } else if (value.getClass().id() == IDENTIFIABLE_CLASSID(FloatFieldValue)) { + _value = static_cast<Number>(value.getAsFloat()); + } else if (value.getClass().id() == IDENTIFIABLE_CLASSID(DoubleFieldValue)) + { + _value = static_cast<Number>(value.getAsDouble()); + } else { + return FieldValue::assign(value); + } + _altered = true; + return *this; +} + +template<typename Number> +int +NumericFieldValue<Number>::compare( const FieldValue& other) const +{ + int diff = FieldValue::compare(other); + if (diff != 0) return diff; + + const NumericFieldValue & otherNumber(static_cast<const NumericFieldValue &>(other)); + return (_value == otherNumber._value) + ? 0 + : (_value - otherNumber._value > 0) + ? 1 + : -1; +} + +template<typename Number> +void +NumericFieldValue<Number>::print( + std::ostream& out, bool, const std::string&) const +{ + if (sizeof(Number) == 1) { // Make sure char's are printed as numbers + out << (int) _value; + } else { + out << _value; + } +} + +template<typename Number> +FieldValue& +NumericFieldValue<Number>::operator=(const vespalib::stringref & value) +{ + // Lexical cast doesn't allow hex syntax we use in XML, + // so detect these in front. + if ((value.size() > 2) && (value[0] == '0') && ((value[1] | 0x20) == 'x')) { + char* endp; + // It is safe to assume that all hex numbers can be contained within + // 64 bit unsigned value. + unsigned long long val = strtoull(value.c_str(), &endp, 16); + if (*endp == '\0') { + // Allow numbers to be specified in range max signed to max + // unsigned. These become negative numbers. + _value = static_cast<Number>(val); + _altered = true; + return *this; + } + } + if (sizeof(Number) == sizeof(int8_t)) { + int val = vespalib::lexical_cast<int>(value); + if (val < -128 || val > 255) { + throw vespalib::IllegalArgumentException( + "Value of byte must be in the range -128 to 255", VESPA_STRLOC); + } + _value = static_cast<Number>(val); + } else { + try{ + _value = boost::lexical_cast<Number>(value); + } catch (boost::bad_lexical_cast& e) { + // If bad cast is thrown due to value being bigger than max positive + // signed value, but less than max positive unsigned value, + // use this workaround to try to convert it to signed. + if (sizeof(Number) == sizeof(uint32_t)) { + _value = boost::numeric_cast<Number>( + static_cast<int32_t>(boost::lexical_cast<uint32_t>(value))); + } else { + _value = boost::numeric_cast<Number>( + static_cast<int64_t>(boost::lexical_cast<uint64_t>(value))); + } + } + } + _altered = true; + return *this; +} + +template<typename Number> +FieldValue& +NumericFieldValue<Number>::operator=(int32_t value) +{ + _value = static_cast<Number>(value); + _altered = true; + return *this; +} + +template<typename Number> +FieldValue& +NumericFieldValue<Number>::operator=(int64_t value) +{ + _value = static_cast<Number>(value); + _altered = true; + return *this; +} + +template<typename Number> +FieldValue& +NumericFieldValue<Number>::operator=(float value) +{ + _value = static_cast<Number>(value); + _altered = true; + return *this; +} + +template<typename Number> +FieldValue& +NumericFieldValue<Number>::operator=(double value) +{ + _value = static_cast<Number>(value); + _altered = true; + return *this; +} + +template<typename Number> +char +NumericFieldValue<Number>::getAsByte() const +{ + return static_cast<char>(_value); +} + +template<typename Number> +int32_t +NumericFieldValue<Number>::getAsInt() const +{ + return static_cast<int32_t>(_value); +} + +template<typename Number> +int64_t +NumericFieldValue<Number>::getAsLong() const +{ + return static_cast<int64_t>(_value); +} + +template<typename Number> +float +NumericFieldValue<Number>::getAsFloat() const +{ + return static_cast<float>(_value); +} + +template<typename Number> +double +NumericFieldValue<Number>::getAsDouble() const +{ + return static_cast<double>(_value); +} + +template<typename Number> +vespalib::string +NumericFieldValue<Number>::getAsString() const +{ + vespalib::asciistream ost; + if (sizeof(Number) == sizeof(uint8_t)) { + ost << static_cast<uint32_t>(_value); + } else { + ost << _value; + } + return ost.str(); +} + +} // document + diff --git a/document/src/vespa/document/fieldvalue/serializablearray.cpp b/document/src/vespa/document/fieldvalue/serializablearray.cpp index 6309338eb3b..95d2e871352 100644 --- a/document/src/vespa/document/fieldvalue/serializablearray.cpp +++ b/document/src/vespa/document/fieldvalue/serializablearray.cpp @@ -1,16 +1,14 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <vespa/fastos/fastos.h> -#include <vespa/log/log.h> -LOG_SETUP(".document.serializable-array"); - -#include <vespa/document/fieldvalue/serializablearray.h> - +#include "serializablearray.h" #include <vespa/document/util/bytebuffer.h> #include <vespa/document/util/compressor.h> +#include <vespa/document/util/serializableexceptions.h> #include <vespa/vespalib/util/stringfmt.h> #include <algorithm> -#include <string> -#include <vector> + +#include <vespa/log/log.h> +LOG_SETUP(".document.serializable-array"); + using std::vector; diff --git a/document/src/vespa/document/fieldvalue/serializablearray.h b/document/src/vespa/document/fieldvalue/serializablearray.h index f1872195f74..b92737b0c7c 100644 --- a/document/src/vespa/document/fieldvalue/serializablearray.h +++ b/document/src/vespa/document/fieldvalue/serializablearray.h @@ -16,14 +16,10 @@ #pragma once -#include <vespa/fastos/fastos.h> #include <vespa/document/util/bytebuffer.h> #include <vespa/document/util/compressionconfig.h> #include <vespa/document/util/compressor.h> #include <vespa/document/util/serializable.h> -#include <map> -#include <set> -#include <stdio.h> #include <vector> #include <vespa/vespalib/objects/cloneable.h> #include <vespa/vespalib/stllike/hash_map.h> @@ -107,7 +103,6 @@ private: public: static Statistics& getStatistics() { return _stats; } - typedef vespalib::LinkedPtr<SerializableArray> LP; typedef vespalib::CloneablePtr<SerializableArray> CP; typedef std::unique_ptr<SerializableArray> UP; diff --git a/document/src/vespa/document/fieldvalue/structfieldvalue.cpp b/document/src/vespa/document/fieldvalue/structfieldvalue.cpp index ec806dd17b0..ab5d42868a7 100644 --- a/document/src/vespa/document/fieldvalue/structfieldvalue.cpp +++ b/document/src/vespa/document/fieldvalue/structfieldvalue.cpp @@ -1,10 +1,6 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include <vespa/fastos/fastos.h> -#include <vespa/log/log.h> -LOG_SETUP(".document.structfieldvalue"); - #include <vespa/document/fieldvalue/structfieldvalue.h> - #include "fieldvaluewriter.h" #include <vespa/document/fieldvalue/document.h> #include <vespa/document/repo/fixedtyperepo.h> @@ -14,6 +10,10 @@ LOG_SETUP(".document.structfieldvalue"); #include <vespa/vespalib/util/crc.h> #include <vespa/document/datatype/positiondatatype.h> #include <vespa/vespalib/util/vstringfmt.h> +#include <vespa/document/util/serializableexceptions.h> + +#include <vespa/log/log.h> +LOG_SETUP(".document.structfieldvalue"); using std::vector; using vespalib::nbostream; diff --git a/document/src/vespa/document/fieldvalue/structuredfieldvalue.cpp b/document/src/vespa/document/fieldvalue/structuredfieldvalue.cpp index 2fb0b148140..e24819204a8 100644 --- a/document/src/vespa/document/fieldvalue/structuredfieldvalue.cpp +++ b/document/src/vespa/document/fieldvalue/structuredfieldvalue.cpp @@ -1,12 +1,11 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <vespa/fastos/fastos.h> #include <vespa/document/fieldvalue/structuredfieldvalue.h> - #include <vespa/document/base/field.h> #include <vespa/document/fieldvalue/fieldvalues.h> -#include <vespa/log/log.h> +#include <vespa/vespalib/util/exceptions.h> +#include <vespa/log/log.h> LOG_SETUP(".document.fieldvalue.structured"); namespace document { diff --git a/document/src/vespa/document/fieldvalue/structuredfieldvalue.h b/document/src/vespa/document/fieldvalue/structuredfieldvalue.h index 6ef6b553fae..2abeac9f1c0 100644 --- a/document/src/vespa/document/fieldvalue/structuredfieldvalue.h +++ b/document/src/vespa/document/fieldvalue/structuredfieldvalue.h @@ -172,34 +172,8 @@ public: } template <typename T> - std::unique_ptr<T> getAs(const Field &field) const { - FieldValue::UP val = getValue(field); - T *t = Identifiable::cast<T *>(val.get()); - if (val.get() && !t) { - throw vespalib::IllegalStateException("Field " + field.toString() - + " has unexpected type.", VESPA_STRLOC); - } - val.release(); - return std::unique_ptr<T>(t); - } + std::unique_ptr<T> getAs(const Field &field) const; }; -template<typename PrimitiveType> -void -StructuredFieldValue::set(const Field& field, const PrimitiveType& value) -{ - FieldValue::UP fval(field.getDataType().createFieldValue()); - *fval = value; - setFieldValue(field, std::move(fval)); -} - -template<typename PrimitiveType> -void -StructuredFieldValue::set(const vespalib::stringref & fieldName, - const PrimitiveType& value) -{ - set(getField(fieldName), value); -} - } // document diff --git a/document/src/vespa/document/fieldvalue/structuredfieldvalue.hpp b/document/src/vespa/document/fieldvalue/structuredfieldvalue.hpp new file mode 100644 index 00000000000..d02503b597a --- /dev/null +++ b/document/src/vespa/document/fieldvalue/structuredfieldvalue.hpp @@ -0,0 +1,41 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include "structuredfieldvalue.h" +#include <vespa/vespalib/util/exceptions.h> + +namespace document { + +template <typename T> +std::unique_ptr<T> +StructuredFieldValue::getAs(const Field &field) const { + FieldValue::UP val = getValue(field); + T *t = Identifiable::cast<T *>(val.get()); + if (val.get() && !t) { + throw vespalib::IllegalStateException("Field " + field.toString() + + " has unexpected type.", VESPA_STRLOC); + } + val.release(); + return std::unique_ptr<T>(t); +} + +template<typename PrimitiveType> +void +StructuredFieldValue::set(const Field& field, const PrimitiveType& value) +{ + FieldValue::UP fval(field.getDataType().createFieldValue()); + *fval = value; + setFieldValue(field, std::move(fval)); +} + +template<typename PrimitiveType> +void +StructuredFieldValue::set(const vespalib::stringref & fieldName, + const PrimitiveType& value) +{ + set(getField(fieldName), value); +} + +} // document + diff --git a/document/src/vespa/document/util/bufferexceptions.h b/document/src/vespa/document/util/bufferexceptions.h new file mode 100644 index 00000000000..8489f86f024 --- /dev/null +++ b/document/src/vespa/document/util/bufferexceptions.h @@ -0,0 +1,27 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +#include <vespa/vespalib/util/exceptions.h> +#include <memory> + +namespace document { + +class BufferOutOfBoundsException : public vespalib::IoException { + static vespalib::string createMessage(size_t pos, size_t len); +public: + BufferOutOfBoundsException(size_t pos, size_t len, + const vespalib::string& location = ""); + + VESPA_DEFINE_EXCEPTION_SPINE(BufferOutOfBoundsException) +}; + +class InputOutOfRangeException : public vespalib::IoException { +public: + InputOutOfRangeException(const vespalib::string& msg, + const vespalib::string& location = ""); + + VESPA_DEFINE_EXCEPTION_SPINE(InputOutOfRangeException) +}; + +} + diff --git a/document/src/vespa/document/util/bytebuffer.cpp b/document/src/vespa/document/util/bytebuffer.cpp index 1b0d9562bfd..4b27a7afa59 100644 --- a/document/src/vespa/document/util/bytebuffer.cpp +++ b/document/src/vespa/document/util/bytebuffer.cpp @@ -5,8 +5,9 @@ @date 2004-03-15 */ -#include <vespa/fastos/fastos.h> -#include <vespa/document/util/bytebuffer.h> +#include "bytebuffer.h" +#include "bufferexceptions.h" +#include "stringutil.h" #include <vespa/vespalib/stllike/asciistream.h> #include <sstream> diff --git a/document/src/vespa/document/util/bytebuffer.h b/document/src/vespa/document/util/bytebuffer.h index 9207d47350d..25e80250721 100644 --- a/document/src/vespa/document/util/bytebuffer.h +++ b/document/src/vespa/document/util/bytebuffer.h @@ -14,36 +14,14 @@ */ #pragma once -#include <vespa/fastos/types.h> +#include <vespa/vespalib/util/alloc.h> #include <vespa/vespalib/util/referencecounter.h> -#include <vespa/document/util/stringutil.h> -#include <vespa/vespalib/util/exception.h> -#include <vespa/vespalib/util/linkedptr.h> -#include <memory> namespace document { -class BufferOutOfBoundsException : public vespalib::IoException { - static vespalib::string createMessage(size_t pos, size_t len); -public: - BufferOutOfBoundsException(size_t pos, size_t len, - const vespalib::string& location = ""); - - VESPA_DEFINE_EXCEPTION_SPINE(BufferOutOfBoundsException) -}; - -class InputOutOfRangeException : public vespalib::IoException { -public: - InputOutOfRangeException(const vespalib::string& msg, - const vespalib::string& location = ""); - - VESPA_DEFINE_EXCEPTION_SPINE(InputOutOfRangeException) -}; - class ByteBuffer { public: - typedef vespalib::LinkedPtr<ByteBuffer> LP; typedef std::unique_ptr<ByteBuffer> UP; /** * Creates a byte buffer with no underlying buffer. @@ -243,8 +221,7 @@ public: void putDoubleLongNetwork(T val) { //TODO: Change this if we move to big-endian hardware if (__builtin_expect(getRemaining() < (int)sizeof(T), 0)) { - throw BufferOutOfBoundsException(getRemaining(), sizeof(T), - VESPA_STRLOC); + throwOutOfBounds(sizeof(T), getRemaining()); } unsigned char* data = reinterpret_cast<unsigned char*>(&val); for (int i=sizeof(T)-1; i>=0; --i) { @@ -256,8 +233,7 @@ public: void getDoubleLongNetwork(T &val) { //TODO: Change this if we move to big-endian hardware if (__builtin_expect(getRemaining() < (int)sizeof(T), 0)) { - throw BufferOutOfBoundsException(getRemaining(), sizeof(T), - VESPA_STRLOC); + throwOutOfBounds(sizeof(T), getRemaining()); } unsigned char* data = reinterpret_cast<unsigned char*>(&val); diff --git a/document/src/vespa/document/util/serializable.cpp b/document/src/vespa/document/util/serializable.cpp index c6738374223..331b62deeb7 100644 --- a/document/src/vespa/document/util/serializable.cpp +++ b/document/src/vespa/document/util/serializable.cpp @@ -1,10 +1,9 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <vespa/fastos/fastos.h> -#include <vespa/document/util/serializable.h> - -#include <stdio.h> -#include <vespa/document/util/bytebuffer.h> +#include "serializable.h" +#include "bufferexceptions.h" +#include "serializableexceptions.h" +#include "bytebuffer.h" namespace document { @@ -45,5 +44,42 @@ SerializeException::SerializeException( { } +void +Serializable::serialize(ByteBuffer& buffer) const { + int pos = buffer.getPos(); + try{ + onSerialize(buffer); + } catch (...) { + buffer.setPos(pos); + throw; + } +} + +void +Deserializable::deserialize(const DocumentTypeRepo &repo, ByteBuffer& buffer) { + int pos = buffer.getPos(); + try { + onDeserialize(repo, buffer); + } catch (const DeserializeException &) { + buffer.setPos(pos); + throw; + } catch (const BufferOutOfBoundsException &) { + buffer.setPos(pos); + throw; + } +} +void +VersionedDeserializable::deserialize(ByteBuffer& buffer, uint16_t version) { + int pos = buffer.getPos(); + try{ + onDeserialize(buffer, version); + } catch (const DeserializeException &) { + buffer.setPos(pos); + throw; + } catch (const BufferOutOfBoundsException &) { + buffer.setPos(pos); + throw; + } +} } diff --git a/document/src/vespa/document/util/serializable.h b/document/src/vespa/document/util/serializable.h index 55b2f1ce629..808bcdd6915 100644 --- a/document/src/vespa/document/util/serializable.h +++ b/document/src/vespa/document/util/serializable.h @@ -12,10 +12,6 @@ #pragma once -#include <map> -#include <vector> - -#include <vespa/vespalib/util/exception.h> #include <vespa/vespalib/objects/cloneable.h> #include <vespa/vespalib/objects/identifiable.h> @@ -25,22 +21,6 @@ namespace document { class DocumentTypeRepo; -class DeserializeException : public vespalib::IoException { -public: - DeserializeException(const vespalib::string& msg, const vespalib::string& location = ""); - DeserializeException(const vespalib::string& msg, const vespalib::Exception& cause, - const vespalib::string& location = ""); - VESPA_DEFINE_EXCEPTION_SPINE(DeserializeException) -}; - -class SerializeException : public vespalib::IoException { -public: - SerializeException(const vespalib::string& msg, const vespalib::string& location = ""); - SerializeException(const vespalib::string& msg, const vespalib::Exception& cause, - const vespalib::string& location = ""); - VESPA_DEFINE_EXCEPTION_SPINE(SerializeException) -}; - /** * Base class for classes that can be converted into a bytestream, * normally used later to create a similar instance. @@ -72,15 +52,7 @@ public: * serialized. * @throw BufferOutOfBoundsException If buffer does not have enough space. */ - void serialize(ByteBuffer& buffer) const { - int pos = buffer.getPos(); - try{ - onSerialize(buffer); - } catch (...) { - buffer.setPos(pos); - throw; - } - } + void serialize(ByteBuffer& buffer) const; /** * Creates a bytebuffer with enough space to serialize this instance @@ -102,8 +74,7 @@ public: class Deserializable : public vespalib::Cloneable, public Serializable { protected: - virtual void onDeserialize(const DocumentTypeRepo &repo, - ByteBuffer& buffer) = 0; + virtual void onDeserialize(const DocumentTypeRepo &repo, ByteBuffer& buffer) = 0; public: DECLARE_IDENTIFIABLE_ABSTRACT(Deserializable); @@ -120,18 +91,7 @@ public: * @throw BufferOutOfBoundsException If instance wants to read more data * than is available in the buffer. */ - void deserialize(const DocumentTypeRepo &repo, ByteBuffer& buffer) { - int pos = buffer.getPos(); - try { - onDeserialize(repo, buffer); - } catch (const DeserializeException &) { - buffer.setPos(pos); - throw; - } catch (const BufferOutOfBoundsException &) { - buffer.setPos(pos); - throw; - } - } + void deserialize(const DocumentTypeRepo &repo, ByteBuffer& buffer); }; /** @@ -163,18 +123,7 @@ public: * @throw BufferOutOfBoundsException If instance wants to read more data * than is available in the buffer. */ - void deserialize(ByteBuffer& buffer, uint16_t version) { - int pos = buffer.getPos(); - try{ - onDeserialize(buffer, version); - } catch (const DeserializeException &) { - buffer.setPos(pos); - throw; - } catch (const BufferOutOfBoundsException &) { - buffer.setPos(pos); - throw; - } - } + void deserialize(ByteBuffer& buffer, uint16_t version); }; } diff --git a/document/src/vespa/document/util/serializableexceptions.h b/document/src/vespa/document/util/serializableexceptions.h new file mode 100644 index 00000000000..ac32e13975a --- /dev/null +++ b/document/src/vespa/document/util/serializableexceptions.h @@ -0,0 +1,35 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +/** + * @file serializable.h + * @ingroup document + * + * @brief Interfaces to be used for serializing of objects. + * + * @author Thomas F. Gundersen, H�kon Humberset + * @date 2004-03-15 + * @version $Id$ + */ + +#pragma once + +#include <vespa/vespalib/util/exceptions.h> + +namespace document { + +class DeserializeException : public vespalib::IoException { +public: + DeserializeException(const vespalib::string& msg, const vespalib::string& location = ""); + DeserializeException(const vespalib::string& msg, const vespalib::Exception& cause, + const vespalib::string& location = ""); + VESPA_DEFINE_EXCEPTION_SPINE(DeserializeException) +}; + +class SerializeException : public vespalib::IoException { +public: + SerializeException(const vespalib::string& msg, const vespalib::string& location = ""); + SerializeException(const vespalib::string& msg, const vespalib::Exception& cause, + const vespalib::string& location = ""); + VESPA_DEFINE_EXCEPTION_SPINE(SerializeException) +}; + +} diff --git a/document/src/vespa/document/util/stringutil.cpp b/document/src/vespa/document/util/stringutil.cpp index f32c32ebd50..599dc0b13e1 100644 --- a/document/src/vespa/document/util/stringutil.cpp +++ b/document/src/vespa/document/util/stringutil.cpp @@ -8,9 +8,8 @@ * */ -#include <vespa/fastos/fastos.h> -#include <vespa/document/util/stringutil.h> -#include <vespa/vespalib/util/exception.h> +#include "stringutil.h" +#include <vespa/vespalib/util/exceptions.h> #include <vespa/vespalib/stllike/asciistream.h> #include <iomanip> |