value_size.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2008-2009: Joachim Faulhaber
  3. +------------------------------------------------------------------------------+
  4. Copyright (c) 1999-2006: Cortex Software GmbH, Kantstrasse 57, Berlin
  5. +------------------------------------------------------------------------------+
  6. Distributed under the Boost Software License, Version 1.0.
  7. (See accompanying file LICENCE.txt or copy at
  8. http://www.boost.org/LICENSE_1_0.txt)
  9. +-----------------------------------------------------------------------------*/
  10. #ifndef BOOST_ICL_VALUE_SIZE_HPP_JOFA_081004
  11. #define BOOST_ICL_VALUE_SIZE_HPP_JOFA_081004
  12. namespace boost{ namespace icl
  13. {
  14. template <typename Type>
  15. Type abs(Type val) { return val < 0 ? -val : val; }
  16. /// static class template for the size of a type's value
  17. /** This function is needed to be able to order values according
  18. to their size. This is used to e.g. prefer simple test
  19. instances and to express this simplicity independent of the
  20. type of the test case.
  21. @author Joachim Faulhaber
  22. */
  23. template <class Type>
  24. struct value_size
  25. {
  26. /** The size of a value is used to be able to order values according to
  27. their simplicity */
  28. static std::size_t apply(const Type& val);
  29. };
  30. template<> inline std::size_t value_size<int>::apply(const int& value)
  31. { return abs(value); }
  32. template<> inline std::size_t value_size<double>::apply(const double& value)
  33. { return static_cast<int>(abs(value)); }
  34. template <typename Type>
  35. inline std::size_t value_size<Type>::apply(const Type& value)
  36. { return icl::iterative_size(value); }
  37. }} // namespace boost icl
  38. #endif