forked from mpusz/mp-units
units building and packaging redesigned + Travis CI support
This commit is contained in:
+1
-15
@@ -20,23 +20,9 @@
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
cmake_minimum_required(VERSION 3.8)
|
||||
project(units_example)
|
||||
|
||||
# set path to custom cmake modules
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../cmake/common/cmake")
|
||||
|
||||
# include common tools and workarounds
|
||||
include(tools)
|
||||
|
||||
# add dependencies
|
||||
if(NOT TARGET mp::units)
|
||||
find_package(units CONFIG REQUIRED)
|
||||
endif()
|
||||
|
||||
# example app
|
||||
add_executable(example example.cpp)
|
||||
target_link_libraries(example
|
||||
PRIVATE
|
||||
mp::units
|
||||
units::units
|
||||
)
|
||||
|
||||
+24
-10
@@ -23,26 +23,40 @@
|
||||
#include "units/si/velocity.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace units;
|
||||
namespace {
|
||||
|
||||
template<Velocity V, Time T>
|
||||
void foo(V v, T t)
|
||||
using namespace units::literals;
|
||||
|
||||
template<units::Length D, units::Time T>
|
||||
constexpr units::Velocity avg_speed(D d, T t)
|
||||
{
|
||||
const Length distance = v * t;
|
||||
std::cout << "A car driving " << v.count() << " km/h in a time of " << t.count() << " minutes will pass "
|
||||
<< quantity_cast<length<meter, double>>(distance).count() << " meters.\n";
|
||||
return d / t;
|
||||
}
|
||||
|
||||
void foo()
|
||||
template<units::Velocity V, units::Time T>
|
||||
void example_1(V v, T t)
|
||||
{
|
||||
using namespace units::literals;
|
||||
foo(60_kmph, 10.0_min);
|
||||
const units::Length distance = v * t;
|
||||
std::cout << "A car driving " << v.count() << " km/h in a time of " << t.count() << " minutes will pass "
|
||||
<< units::quantity_cast<units::length<units::meter, double>>(distance).count() << " meters.\n";
|
||||
}
|
||||
|
||||
void example_2(double distance_v, double duration_v)
|
||||
{
|
||||
units::length<units::kilometer> distance(distance_v);
|
||||
units::time<units::hour> duration(duration_v);
|
||||
const auto kmph = avg_speed(distance, duration);
|
||||
std::cout << "Average speed of a car that makes " << distance.count() << " km in "
|
||||
<< duration.count() << " hours is " << kmph.count() << " km/h.\n";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
try {
|
||||
foo();
|
||||
example_1(60_kmph, 10.0_min);
|
||||
example_2(220, 2);
|
||||
}
|
||||
catch (const std::exception& ex) {
|
||||
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
|
||||
|
||||
Reference in New Issue
Block a user