Skip to content

Commit

Permalink
libcore|Range: Constructing a Range from a Value
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Aug 18, 2015
1 parent 81c89e0 commit b36d0e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
5 changes: 4 additions & 1 deletion doomsday/sdk/libcore/include/de/core/range.h
Expand Up @@ -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</small>
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDENG2_RANGE_H
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions doomsday/sdk/libcore/include/de/core/vector.h
Expand Up @@ -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</small>
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDENG2_VECTOR_H
Expand Down Expand Up @@ -71,7 +71,7 @@ template <typename VecType>
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;
}
Expand Down
20 changes: 19 additions & 1 deletion doomsday/sdk/libcore/include/de/data/value.h
Expand Up @@ -28,7 +28,6 @@ namespace de {

class Process;
class Record;
class NumberValue;

/**
* The base class for all runtime values. This is an abstract class.
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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 <typename RangeType>
RangeType rangeFromValue(Value const &value) {
return RangeType(value.element(0).asNumber(),
value.element(1).asNumber());
}

} // namespace de

#endif /* LIBDENG2_VALUE_H */
42 changes: 26 additions & 16 deletions doomsday/sdk/libcore/src/data/value.cpp
Expand Up @@ -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</small>
* http://www.gnu.org/licenses</small>
*/

#include "de/Value"
Expand Down Expand Up @@ -119,7 +119,7 @@ Value *Value::next()
}

bool Value::isFalse() const
{
{
// Default implementation -- some values may be neither true nor false.
return !isTrue();
}
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -186,34 +186,34 @@ Value *Value::constructFrom(Reader &reader)
reader.mark();
reader >> id;
reader.rewind();

std::auto_ptr<Value> 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;
Expand All @@ -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");
}
Expand All @@ -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));
}

0 comments on commit b36d0e1

Please sign in to comment.