Boost logo

Boost-Commit :

From: steven_at_[hidden]
Date: 2007-08-23 14:03:38


Author: steven_watanabe
Date: 2007-08-23 14:03:38 EDT (Thu, 23 Aug 2007)
New Revision: 38866
URL: http://svn.boost.org/trac/boost/changeset/38866

Log:
Finish runtime_unit.cpp example
Added:
   sandbox/units/libs/units/example/runtime_unit_input.txt (contents, props changed)
Text files modified:
   sandbox/units/libs/units/doc/units.qbk | 8 +++++++-
   sandbox/units/libs/units/example/Jamfile.v2 | 3 ++-
   sandbox/units/libs/units/example/runtime_unit.cpp | 21 +++++++++++++++------
   3 files changed, 24 insertions(+), 8 deletions(-)

Modified: sandbox/units/libs/units/doc/units.qbk
==============================================================================
--- sandbox/units/libs/units/doc/units.qbk (original)
+++ sandbox/units/libs/units/doc/units.qbk 2007-08-23 14:03:38 EDT (Thu, 23 Aug 2007)
@@ -887,7 +887,13 @@
 
 ([@../../libs/units/example/runtime_unit.cpp runtime_unit.cpp])
 
-TODO: finish this example.
+[import ../example/runtime_unit.cpp]
+
+This example shows how to implement an interface that
+allow different units at runtime while still maintaining
+type safety for internal calculations.
+
+[runtime_unit_snippet_1]
 
 [endsect]
 

Modified: sandbox/units/libs/units/example/Jamfile.v2
==============================================================================
--- sandbox/units/libs/units/example/Jamfile.v2 (original)
+++ sandbox/units/libs/units/example/Jamfile.v2 2007-08-23 14:03:38 EDT (Thu, 23 Aug 2007)
@@ -24,8 +24,9 @@
 {
   test-suite units
    :
- [ make_tests [ path.glob . : *.cpp : performance.* ] ]
+ [ make_tests [ path.glob . : *.cpp : performance.* runtime_unit.* ] ]
     [ compile performance.cpp ]
+ [ run runtime_unit.cpp : <runtime_unit_input.txt : : ]
    ;
 
 }

Modified: sandbox/units/libs/units/example/runtime_unit.cpp
==============================================================================
--- sandbox/units/libs/units/example/runtime_unit.cpp (original)
+++ sandbox/units/libs/units/example/runtime_unit.cpp 2007-08-23 14:03:38 EDT (Thu, 23 Aug 2007)
@@ -12,19 +12,26 @@
 #include <iostream>
 #include <boost/lexical_cast.hpp>
 #include <boost/units/quantity.hpp>
+#include <boost/units/cmath.hpp>
 #include <boost/units/systems/si/length.hpp>
 #include <boost/units/systems/base_units.hpp>
 
+//[runtime_unit_snippet_1
+
 namespace {
 
-std::map<std::string, double> known_units;
+std::map<std::string, boost::units::quantity<boost::units::SI::length> > known_units;
+
+}
 
+boost::units::quantity<boost::units::SI::length> calculate(const boost::units::quantity<boost::units::SI::length>& t) {
+ return(boost::units::hypot(t, 2.0 * boost::units::SI::meters));
 }
 
 int main() {
- known_units["meter"] = 1.0;
- known_units["centimeter"] = .01;
- known_units["foot"] = conversion_factor(boost::units::foot_base_unit::unit_type(), boost::units::SI::meter);
+ known_units["meter"] = 1.0 * boost::units::SI::meters;
+ known_units["centimeter"] = .01 * boost::units::SI::meters;;
+ known_units["foot"] = conversion_factor(boost::units::foot_base_unit::unit_type(), boost::units::SI::meter) * boost::units::SI::meters;;
     std::string output_type("meter");
     std::string input;
     while(std::cin >> input) {
@@ -32,7 +39,7 @@
         else if(input == "help") {
             std::cout << "type \"exit\" to exit\n"
                 "type \"return 'unit'\" to set the return units\n"
- "type \"'number' 'unit'\" to convert a quantity to the return units" << std::endl;
+ "type \"'number' 'unit'\" to do a simple calculation" << std::endl;
         } else if(input == "return") {
             if(std::cin >> input) {
                 if(known_units.find(input) != known_units.end()) {
@@ -47,7 +54,7 @@
                 double value = boost::lexical_cast<double>(input);
                 if(std::cin >> input) {
                     if(known_units.find(input) != known_units.end()) {
- std::cout << (value * known_units[input] / known_units[output_type]) << ' ' << output_type << std::endl;
+ std::cout << static_cast<double>(calculate(value * known_units[input]) / known_units[output_type]) << ' ' << output_type << std::endl;
                     } else {
                         std::cout << "Unknown unit \"" << input << "\"" << std::endl;
                     }
@@ -58,3 +65,5 @@
         }
     }
 }
+
+//]

Added: sandbox/units/libs/units/example/runtime_unit_input.txt
==============================================================================
--- (empty file)
+++ sandbox/units/libs/units/example/runtime_unit_input.txt 2007-08-23 14:03:38 EDT (Thu, 23 Aug 2007)
@@ -0,0 +1,5 @@
+return foot
+2.0 centimeter
+return centimeter
+3.0 meter
+exit


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