From d2680f986baf7a7382e308a0bd98137b02019d92 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Fri, 22 Mar 2013 17:01:12 +0100 Subject: [PATCH] eoserial: traits for knowing whether a class is derived from another one at compile time. Thanks Herb Sutter --- eo/src/serial/Traits.h | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 eo/src/serial/Traits.h diff --git a/eo/src/serial/Traits.h b/eo/src/serial/Traits.h new file mode 100644 index 000000000..7cce0959e --- /dev/null +++ b/eo/src/serial/Traits.h @@ -0,0 +1,58 @@ +/* +(c) Benjamin Bouvier, 2013 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; + version 2 of the License. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +Contact: http://eodev.sourceforge.net + +Authors: + Benjamin Bouvier +*/ +# ifndef __EOSERIAL_TRAITS_H__ +# define __EOSERIAL_TRAITS_H__ + +/** + * @file Traits.h + * + * Traits used for serialization purposes. + * + * @author Benjamin Bouvier + */ + +namespace eoserial +{ + + /** + * @brief Trait to know whether Derived is derived from Base or not. + * + * To know whether A is derived from B, just test the boolean IsDerivedFrom::value. + * + * @see http://www.gotw.ca/publications/mxc++-item-4.htm + */ + template + class IsDerivedFrom + { + struct no{}; + struct yes{ no _[2]; }; + + static yes Test( Base* something ); + static no Test( ... ); + + public: + enum { value = sizeof( Test( static_cast(0) ) ) == sizeof(yes) }; + }; + +} + +# endif // __EOSERIAL_TRAITS_H__