diff --git a/doomsday/sdk/libcore/include/de/core/range.h b/doomsday/sdk/libcore/include/de/core/range.h index 5fa6be0028..0af431baf0 100644 --- a/doomsday/sdk/libcore/include/de/core/range.h +++ b/doomsday/sdk/libcore/include/de/core/range.h @@ -13,7 +13,7 @@ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser * General Public License for more details. You should have received a copy of * the GNU Lesser General Public License along with this program; if not, see: - * http://www.gnu.org/licenses + * http://www.gnu.org/licenses */ #ifndef LIBDENG2_RANGE_H @@ -50,6 +50,9 @@ struct Range if(i > end) return end; return i; } + inline Type wrap(Type const &i) const { + return de::wrap(i, start, end); + } inline Range &operator |= (Type const &value) { start = de::min(start, value); end = de::max(end, value); diff --git a/doomsday/sdk/libcore/include/de/core/vector.h b/doomsday/sdk/libcore/include/de/core/vector.h index f71b1427b2..c6ca0d0d32 100644 --- a/doomsday/sdk/libcore/include/de/core/vector.h +++ b/doomsday/sdk/libcore/include/de/core/vector.h @@ -20,7 +20,7 @@ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser * General Public License for more details. You should have received a copy of * the GNU Lesser General Public License along with this program; if not, see: - * http://www.gnu.org/licenses + * http://www.gnu.org/licenses */ #ifndef LIBDENG2_VECTOR_H @@ -71,7 +71,7 @@ template VecType vectorFromValue(Value const &value) { VecType converted; for(int i = 0; i < converted.size(); ++i) { - converted[i] = typename VecType::ValueType(value.element(NumberValue(i)).asNumber()); + converted[i] = typename VecType::ValueType(value.element(i).asNumber()); } return converted; } diff --git a/doomsday/sdk/libcore/include/de/data/value.h b/doomsday/sdk/libcore/include/de/data/value.h index 50c4a06f28..947eaed498 100644 --- a/doomsday/sdk/libcore/include/de/data/value.h +++ b/doomsday/sdk/libcore/include/de/data/value.h @@ -28,7 +28,6 @@ namespace de { class Process; class Record; -class NumberValue; /** * The base class for all runtime values. This is an abstract class. @@ -164,6 +163,8 @@ class DENG2_PUBLIC Value : public String::IPatternArg, public ISerializable */ virtual Value const &element(Value const &index) const; + Value const &element(dint index) const; + /** * Get a specific element of the value. This is meaningful with * arrays and dictionaries. @@ -174,6 +175,8 @@ class DENG2_PUBLIC Value : public String::IPatternArg, public ISerializable */ virtual Value &element(Value const &index); + Value &element(dint index); + /** * Duplicates an element of the value. This is necessary when the * value is immutable: one can take copies of the contained @@ -323,6 +326,21 @@ class DENG2_PUBLIC Value : public String::IPatternArg, public ISerializable }; }; +/** + * Utility for converting a Value with two elements into a Range. This + * only works if the value contains a sufficient number of elements and they + * can be converted to numbers. + * + * @param value Value with multiple elements. + * + * @return Vector. + */ +template +RangeType rangeFromValue(Value const &value) { + return RangeType(value.element(0).asNumber(), + value.element(1).asNumber()); +} + } // namespace de #endif /* LIBDENG2_VALUE_H */ diff --git a/doomsday/sdk/libcore/src/data/value.cpp b/doomsday/sdk/libcore/src/data/value.cpp index 8b25928320..271468b2d7 100644 --- a/doomsday/sdk/libcore/src/data/value.cpp +++ b/doomsday/sdk/libcore/src/data/value.cpp @@ -14,7 +14,7 @@ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser * General Public License for more details. You should have received a copy of * the GNU Lesser General Public License along with this program; if not, see: - * http://www.gnu.org/licenses + * http://www.gnu.org/licenses */ #include "de/Value" @@ -119,7 +119,7 @@ Value *Value::next() } bool Value::isFalse() const -{ +{ // Default implementation -- some values may be neither true nor false. return !isTrue(); } @@ -136,7 +136,7 @@ void Value::negate() /// @throw ArithmeticError Value cannot be negated. throw ArithmeticError("Value::negate", "Value cannot be negated"); } - + void Value::sum(Value const &/*value*/) { /// @throw ArithmeticError Value cannot be summed. @@ -146,21 +146,21 @@ void Value::sum(Value const &/*value*/) void Value::subtract(Value const &/*subtrahend*/) { /// @throw ArithmeticError Value cannot be subtracted from. - throw ArithmeticError("Value::subtract", "Value cannot be subtracted from"); + throw ArithmeticError("Value::subtract", "Value cannot be subtracted from"); } - + void Value::divide(Value const &/*divisor*/) { /// @throw ArithmeticError Value cannot be divided. throw ArithmeticError("Value::divide", "Value cannot be divided"); } - + void Value::multiply(Value const &/*value*/) { /// @throw ArithmeticError Value cannot be multiplied. throw ArithmeticError("Value::multiply", "Value cannot be multiplied"); } - + void Value::modulo(Value const &/*divisor*/) { /// @throw ArithmeticError Module operation is not defined for the value. @@ -186,34 +186,34 @@ Value *Value::constructFrom(Reader &reader) reader.mark(); reader >> id; reader.rewind(); - + std::auto_ptr result; switch(id) { case NONE: result.reset(new NoneValue); break; - + case NUMBER: result.reset(new NumberValue); break; - + case TEXT: result.reset(new TextValue); break; - + case ARRAY: result.reset(new ArrayValue); break; - + case DICTIONARY: result.reset(new DictionaryValue); break; - + case BLOCK: result.reset(new BlockValue); break; - + case FUNCTION: result.reset(new FunctionValue); break; @@ -225,9 +225,9 @@ Value *Value::constructFrom(Reader &reader) case TIME: result.reset(new TimeValue); break; - + default: - /// @throw DeserializationError The identifier that species the type of the + /// @throw DeserializationError The identifier that species the type of the /// serialized value was invalid. throw DeserializationError("Value::constructFrom", "Invalid value identifier"); } @@ -236,3 +236,13 @@ Value *Value::constructFrom(Reader &reader) reader >> *result.get(); return result.release(); } + +Value const &Value::element(dint index) const +{ + return element(NumberValue(index)); +} + +Value &Value::element(dint index) +{ + return element(NumberValue(index)); +}