* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
+// EXTERNAL INCLUDES
#include <cstdint>
+#include <cmath>
#include <iostream>
namespace Dali
{
+
namespace Toolkit
{
/**
* @brief A type that represents a layout length.
*
- * Currently, this implies pixels, but could be extended to handle device dependant sizes, etc.
+ * Currently, this implies pixels, but could be extended to handle device dependent sizes, etc.
*/
class LayoutLength
{
public:
- using IntType = int;
+ /**
+ * @brief default constructor, initialises to 0
+ */
LayoutLength()
: mValue( 0 )
{
}
- LayoutLength( IntType value )
+ /**
+ * @brief constructor
+ *
+ * @param value the value to initialise with
+ */
+ LayoutLength( int value )
: mValue( value )
{
}
+ /**
+ * @brief constructor from float
+ *
+ * @param value the value to initialise with
+ */
+ LayoutLength( float value )
+ : mValue( value )
+ {
+ }
+
+ /**
+ * @brief constructor
+ *
+ * @param layoutLength the value to initialise with
+ */
LayoutLength( const LayoutLength& layoutLength )
: mValue( layoutLength.mValue )
{
}
- LayoutLength& operator=(const LayoutLength& rhs)
+ /**
+ * @brief assignment operator
+ *
+ * @param rhs LayoutLength to assign
+ * @return reference to itself
+ */
+ LayoutLength& operator=( const LayoutLength& rhs )
{
if( this != &rhs )
{
return *this;
}
- bool operator==( const LayoutLength& rhs )
+ /**
+ * @brief assignment operator from int
+ *
+ * @param value the value to assign
+ * @return reference to itself
+ */
+ LayoutLength& operator=( int value )
{
- return mValue == rhs.mValue;
+ mValue = value;
+ return *this;
}
- bool operator==( LayoutLength::IntType rhs )
+ /**
+ * @brief assignment operator from float
+ *
+ * @param value the value to assign
+ * @return reference to itself
+ */
+ LayoutLength& operator=( float value )
{
- return mValue == rhs;
+ mValue = value;
+ return *this;
}
- bool operator!=( const LayoutLength& rhs )
+ /**
+ * @brief plus equals operator
+ *
+ * @param rhs to add to this
+ * @return reference to itself
+ */
+ LayoutLength& operator+=( LayoutLength rhs )
{
- return !operator==(rhs);
+ mValue += rhs.mValue;
+ return *this;
}
- bool operator<( const LayoutLength& rhs )
+ /**
+ * @brief minus equals operator
+ *
+ * @param rhs to subtract from this
+ * @return reference to itself
+ */
+ LayoutLength& operator-=( LayoutLength rhs )
{
- return mValue < rhs.mValue;
+ mValue -= rhs.mValue;
+ return *this;
}
- bool operator<( const LayoutLength rhs ) const
+ /**
+ * @brief return value as decimal value (full raw value)
+ *
+ * @return the LayoutLength as full decimal value
+ */
+ float AsDecimal() const
{
- return mValue < rhs.mValue;
+ return mValue;
}
- bool operator<=( const LayoutLength& rhs )
- {
- return mValue <= rhs.mValue;
- }
- bool operator<=( LayoutLength rhs )
- {
- return mValue <= rhs.mValue;
- }
- bool operator>( const LayoutLength& rhs )
- {
- return mValue > rhs.mValue;
- }
- bool operator>( LayoutLength rhs )
- {
- return mValue > rhs.mValue;
- }
- bool operator>=( const LayoutLength& rhs )
- {
- return mValue >= rhs.mValue;
- }
- bool operator>=( LayoutLength rhs )
+ /**
+ * @brief return value as rounded integer value (whole number)
+ *
+ * @return the LayoutLength rounded to next integer value
+ */
+ float AsInteger() const
{
- return mValue >= rhs.mValue;
+ return round( mValue );
}
- LayoutLength operator+( const LayoutLength& rhs )
+ /**
+ * @brief return value as truncated integral value (whole number)
+ *
+ * @return the LayoutLength rounded to next integral value
+ */
+ float AsTruncated() const
{
- return mValue + rhs.mValue;
+ return trunc( mValue );
}
- LayoutLength operator+( LayoutLength::IntType rhs )
- {
- return mValue + rhs;
- }
+private:
- LayoutLength operator-( const LayoutLength& rhs )
- {
- return mValue - rhs.mValue;
- }
- LayoutLength operator-( LayoutLength::IntType rhs )
- {
- return mValue - rhs;
- }
+ float mValue;
- LayoutLength& operator+=( const LayoutLength& rhs )
- {
- mValue += rhs.mValue;
- return *this;
- }
- LayoutLength& operator+=( LayoutLength::IntType rhs )
- {
- mValue += rhs;
- return *this;
- }
+};
- LayoutLength& operator-=( const LayoutLength& rhs )
- {
- mValue -= rhs.mValue;
- return *this;
- }
+/**
+ * @brief equal to operator
+ *
+ * @param lhs value to compare
+ * @param rhs value to compare against
+ * @note Using value instead of reference to allow conversions from int and float
+ * @return true if identical, false otherwise
+ */
+inline bool operator==( LayoutLength lhs, LayoutLength rhs )
+{
+ return lhs.AsDecimal() == rhs.AsDecimal();
+}
- LayoutLength& operator-=( LayoutLength::IntType rhs )
- {
- mValue -= rhs;
- return *this;
- }
+/**
+ * @brief not equal to operator
+ *
+ * @param lhs value to compare
+ * @param rhs value to compare against
+ * @note Using value instead of reference to allow conversions from int and float
+ * @return true if value is not identical, false otherwise
+ */
+inline bool operator!=( LayoutLength lhs, LayoutLength rhs )
+{
+ return !operator==( lhs, rhs );
+}
- LayoutLength operator/( const LayoutLength& rhs )
- {
- return mValue / rhs.mValue;
- }
- LayoutLength operator/( LayoutLength::IntType rhs )
- {
- return mValue / rhs;
- }
+/**
+ * @brief less than operator
+ *
+ * @param lhs value to compare
+ * @param rhs value to compare against
+ * @note Using value instead of reference to allow conversions from int and float
+ * @return true if lhs value is less, false otherwise
+ */
+inline bool operator<( LayoutLength lhs, LayoutLength rhs )
+{
+ return lhs.AsDecimal() < rhs.AsDecimal();
+}
- LayoutLength operator*( const LayoutLength& rhs )
- {
- return mValue * rhs.mValue;
- }
- LayoutLength operator*( LayoutLength::IntType rhs )
- {
- return mValue * rhs;
- }
- LayoutLength operator*( float rhs )
- {
- return LayoutLength(LayoutLength::IntType(AsFloat() * rhs));
- }
+/**
+ * @brief greater than operator
+ *
+ * @param lhs value to compare
+ * @param rhs value to compare against
+ * @note Using value instead of reference to allow conversions from int and float
+ * @return true if lhs value is greater, false otherwise
+ */
+inline bool operator>( LayoutLength lhs, LayoutLength rhs )
+{
+ return rhs < lhs;
+}
- /**
- * @brief explicit method to return the value as float
- * @return the LayoutLength as float
- */
- float AsFloat() const
- {
- return static_cast<float>( mValue );
- }
+/**
+ * @brief less than or equal to operator
+ *
+ * @param lhs value to compare
+ * @param rhs value to compare against
+ * @note Using value instead of reference to allow conversions from int and float
+ * @return true if lhs value is less than or equal to, false otherwise
+ */
+inline bool operator<=( LayoutLength lhs, LayoutLength rhs )
+{
+ return !(lhs > rhs);
+}
- IntType mValue;
-};
+/**
+ * @brief greater than or equal to operator
+ *
+ * @param lhs value to compare
+ * @param rhs value to compare against
+ * @note Using value instead of reference to allow conversions from int and float
+ * @return true if lhs value is greater than or equal to, false otherwise
+ */
+inline bool operator>=( LayoutLength lhs, LayoutLength rhs )
+{
+ return !(lhs < rhs);
+}
+
+/**
+ * @brief add two LayoutLengths
+ *
+ * @param lhs The LayoutLength to add
+ * @param rhs The LayoutLength to add
+ * @note Using value instead of reference to allow conversions from int and float
+ * @return the resulting LayoutLength
+ */
+inline LayoutLength operator+( LayoutLength lhs, LayoutLength rhs )
+{
+ return lhs.AsDecimal() + rhs.AsDecimal();
+}
+
+/**
+ * @brief subtract two LayoutLengths
+ *
+ * @param lhs The LayoutLength to subtract from
+ * @param rhs The LayoutLength to subtract
+ * @note Using value instead of reference to allow conversions from int and float
+ * @return the resulting LayoutLength
+ */
+inline LayoutLength operator-( LayoutLength lhs, LayoutLength rhs )
+{
+ return lhs.AsDecimal() - rhs.AsDecimal();
+}
+
+/**
+ * @brief multiply two LayoutLengths
+ *
+ * @param lhs The LayoutLength to multiply
+ * @param rhs The LayoutLength to multiply
+ * @note Using value instead of reference to allow conversions from int and float
+ * @return the resulting LayoutLength
+ */
+inline LayoutLength operator*( LayoutLength lhs, LayoutLength rhs )
+{
+ return lhs.AsDecimal() * rhs.AsDecimal();
+}
+
+/**
+ * @brief divide LayoutLength by a LayoutLength
+ *
+ * @param lhs The LayoutLength to divide
+ * @param rhs The LayoutLength to divide by
+ * @note Using value instead of reference to allow conversions from int and float
+ * @return the resulting value as float
+ */
+inline LayoutLength operator/( LayoutLength lhs, LayoutLength rhs )
+{
+ return lhs.AsDecimal() / rhs.AsDecimal();
+}
/**
* @brief Prints a LayoutLength
*/
inline std::ostream& operator<<( std::ostream& o, const LayoutLength& layoutLength )
{
- return o<<layoutLength.mValue;
+ return o << layoutLength.AsDecimal();
}
} // namespace Toolkit