test_icl_core_convert.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00023 //----------------------------------------------------------------------
00024 #include <icl_core/Convert.h>
00025 
00026 #include <iostream>
00027 
00028 struct Foo
00029 {
00030   Foo() : foo(42) { }
00031   int foo;
00032 };
00033 
00034 struct Bar
00035 {
00036   Bar() : bar(23) { }
00037   int bar;
00038 };
00039 
00040 std::ostream& operator << (std::ostream& os, const Foo& foo)
00041 {
00042   return os << "Foo:" << foo.foo;
00043 }
00044 
00045 std::ostream& operator << (std::ostream& os, const Bar& bar)
00046 {
00047   return os << "Bar:" << bar.bar;
00048 }
00049 
00050 namespace icl_core {
00051 
00052 template <>
00053 void convert<>(const Foo& from, Bar& to)
00054 {
00055   to.bar = from.foo;
00056 }
00057 
00058 template <>
00059 void convert<>(const Bar& from, Foo& to)
00060 {
00061   to.foo = from.bar;
00062 }
00063 
00064 }
00065 
00066 int main()
00067 {
00068   Foo foo;
00069   Bar bar;
00070 
00071   Bar bar2 = icl_core::convert<Bar>(foo);
00072   Foo foo2 = icl_core::convert<Foo>(bar);
00073 
00074   std::cout << "Original objects:  " << foo << " " << bar << "\n"
00075             << "Converted objects: " << foo2 << " " << bar2 << std::endl;
00076   return 0;
00077 }


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:04