Boost logo

Boost-Commit :

From: steven_at_[hidden]
Date: 2008-05-14 16:18:27


Author: steven_watanabe
Date: 2008-05-14 16:18:26 EDT (Wed, 14 May 2008)
New Revision: 45361
URL: http://svn.boost.org/trac/boost/changeset/45361

Log:
Made use of a base unit as a unit fail early
Added:
   sandbox/units/libs/units/test/fail_quantity_non_unit.cpp (contents, props changed)
Text files modified:
   sandbox/units/boost/units/base_unit.hpp | 2 ++
   sandbox/units/boost/units/quantity.hpp | 9 +++++++++
   sandbox/units/boost/units/scaled_base_unit.hpp | 2 ++
   sandbox/units/libs/units/test/Jamfile.v2 | 1 +
   4 files changed, 14 insertions(+), 0 deletions(-)

Modified: sandbox/units/boost/units/base_unit.hpp
==============================================================================
--- sandbox/units/boost/units/base_unit.hpp (original)
+++ sandbox/units/boost/units/base_unit.hpp 2008-05-14 16:18:26 EDT (Wed, 14 May 2008)
@@ -65,6 +65,8 @@
 {
     public:
         /// INTERNAL ONLY
+ typedef void boost_units_is_base_unit_type;
+ /// INTERNAL ONLY
         typedef base_unit this_type;
         /// The dimensions of this base unit.
         typedef Dim dimension_type;

Modified: sandbox/units/boost/units/quantity.hpp
==============================================================================
--- sandbox/units/boost/units/quantity.hpp (original)
+++ sandbox/units/boost/units/quantity.hpp 2008-05-14 16:18:26 EDT (Wed, 14 May 2008)
@@ -19,6 +19,7 @@
 #include <boost/mpl/and.hpp>
 #include <boost/mpl/not.hpp>
 #include <boost/mpl/or.hpp>
+#include <boost/mpl/assert.hpp>
 #include <boost/utility/enable_if.hpp>
 #include <boost/type_traits/is_arithmetic.hpp>
 #include <boost/type_traits/is_convertible.hpp>
@@ -39,6 +40,12 @@
 
 namespace detail {
 
+template<class T, class Enable = void>
+struct is_base_unit : mpl::false_ {};
+
+template<class T>
+struct is_base_unit<T, typename T::boost_units_is_base_unit_type> : mpl::true_ {};
+
 template<class Source, class Destination>
 struct is_narrowing_conversion_impl : mpl::bool_<(sizeof(Source) > sizeof(Destination))> {};
 
@@ -81,6 +88,8 @@
 template<class Unit,class Y = double>
 class quantity
 {
+ // base units are not the same as units.
+ BOOST_MPL_ASSERT_NOT((detail::is_base_unit<Unit>));
         enum { force_instantiation_of_unit = sizeof(Unit) };
     public:
         typedef quantity<Unit,Y> this_type;

Modified: sandbox/units/boost/units/scaled_base_unit.hpp
==============================================================================
--- sandbox/units/boost/units/scaled_base_unit.hpp (original)
+++ sandbox/units/boost/units/scaled_base_unit.hpp 2008-05-14 16:18:26 EDT (Wed, 14 May 2008)
@@ -50,6 +50,8 @@
 template<class S, class Scale>
 struct scaled_base_unit
 {
+ /// INTERNAL ONLY
+ typedef void boost_units_is_base_unit_type;
     typedef scaled_base_unit type;
     typedef scaled_base_unit_tag tag;
     typedef S system_type;

Modified: sandbox/units/libs/units/test/Jamfile.v2
==============================================================================
--- sandbox/units/libs/units/test/Jamfile.v2 (original)
+++ sandbox/units/libs/units/test/Jamfile.v2 2008-05-14 16:18:26 EDT (Wed, 14 May 2008)
@@ -50,6 +50,7 @@
     [ compile-fail fail_heterogeneous_unit.cpp : : ]
     [ compile-fail fail_base_dimension.cpp : : ]
     [ compile-fail fail_add_temperature.cpp : : ]
+ [ compile-fail fail_quantity_non_unit.cpp : : ]
    ;
 
 }

Added: sandbox/units/libs/units/test/fail_quantity_non_unit.cpp
==============================================================================
--- (empty file)
+++ sandbox/units/libs/units/test/fail_quantity_non_unit.cpp 2008-05-14 16:18:26 EDT (Wed, 14 May 2008)
@@ -0,0 +1,36 @@
+// mcs::units - A C++ library for zero-overhead dimensional analysis and
+// unit/quantity manipulation and conversion
+//
+// Copyright (C) 2008 Steven Watanabe
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+/**
+\file
+
+\brief fail_quantity_non_unit.cpp
+
+\detailed
+
+Make sure that trying to use a base_unit as though
+it were a unit fails.
+
+Output:
+@verbatim
+@endverbatim
+**/
+
+#include <boost/units/quantity.hpp>
+#include <boost/units/systems/base_units/meter.hpp>
+
+namespace bu = boost::units;
+
+int main(int,char *[])
+{
+
+ bu::quantity<bu::meter_base_unit> q;
+
+ return 0;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk