ivl 679
ivl/details/core/safety_check/safe_loop.hpp
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>.
00002    A C++ template library extending syntax towards mathematical notation.
00003 
00004    Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr>
00005    Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr>
00006 
00007    ivl is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU Lesser General Public License 
00009    version 3 as published by the Free Software Foundation.
00010 
00011    Alternatively, you can redistribute it and/or modify it under the terms 
00012    of the GNU General Public License version 2 as published by the Free 
00013    Software Foundation.
00014 
00015    ivl is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00018    See the GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License 
00021    and a copy of the GNU Lesser General Public License along 
00022    with ivl. If not, see <http://www.gnu.org/licenses/>. */
00023 
00024 #ifndef IVL_CORE_DETAILS_SAFETY_CHECK_SAFE_LOOP_HPP
00025 #define IVL_CORE_DETAILS_SAFETY_CHECK_SAFE_LOOP_HPP
00026 
00027 namespace ivl {
00028 
00029 template<template <typename, typename> class F, class A, class B>
00030 inline
00031 void safe_loop_on(A& a, const B& b) 
00032 {
00033         if(a.overlap(b)) {
00034                  // is this derived_type redundancy?
00035                 typename A::derived_type::create_new tmp;
00036                 tmp.assign(a.derived());
00037                 ivl::loop_on<F>(tmp, b);
00038                 //ivl::copy(a, tmp);
00039                 //loop_on<ivl::loops::assign_copy_class>(a, tmp);
00040                 a.derived().assign(tmp);
00041         } else {
00042                 ivl::loop_on<F>(a, b);
00043         }
00044 }
00045 
00046 template <class A, class B>
00047 inline
00048 void safe_assign(A& a, const B& b)
00049 {
00050         using namespace types;
00051         // this is complementary to resolve_assign so that
00052         // this_type = scalar<derived_type> is allowed.
00053         // TODO: minor. it would be preferred this to be done somehow only once.
00054         typedef typename t_and<
00055                 is_base<types::scalar_identifier, B>,
00056                 is_base<A, B> >::type descalarize_t;
00057 
00058         if(a.overlap(types::r_descalarize<descalarize_t>(b))) {
00059                 typename A::create_new tmp;
00060                 tmp.assign(types::r_descalarize<descalarize_t>(b));
00061                 a.assign(tmp);
00062         } else {
00063                 a.assign(b);
00064         }
00065 }
00066 
00067 } /* namespace ivl */
00068 
00069 #endif // IVL_CORE_DETAILS_SAFETY_CHECK_SAFE_LOOP_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations