Data Access Library (DAL)
/home/amesfoort/DAL/dal/hdf5/types/implicitdowncast.h
00001 /* Copyright 2011-2012  ASTRON, Netherlands Institute for Radio Astronomy
00002  * This file is part of the Data Access Library (DAL).
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either 
00007  * version 3 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public 
00015  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef DAL_IMPLICITDOWNCAST_H
00018 #define DAL_IMPLICITDOWNCAST_H
00019 
00020 #include "isderivedfrom.h"
00021 
00022 namespace dal {
00023 
00048 template<typename B>
00049 class ImplicitDowncast {
00050 public:
00051   ImplicitDowncast( B &base ): base(base) {}
00052 
00053   // the cast-operator can be templated based on return-type: just what we need!
00054   template<typename D> operator D&() {
00055     // trigger a compile error if an incompatible cast is requested
00056     IsDerivedFrom<D,B> type_safety;
00057     (void)type_safety;
00058 
00059     return dynamic_cast<D&>(base);
00060   }
00061 
00062 private:
00063   B &base;
00064 };
00065 
00066 }
00067 
00068 #endif
 All Classes Functions Variables Typedefs Friends