1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_UTILITIES_TRAITS_IS_BASE_OF_HPP
  26 #define SHARE_VM_UTILITIES_TRAITS_IS_BASE_OF_HPP
  27 
  28 #include "utilities/traits/isClassOrUnion.hpp"
  29 #include "utilities/traits/isSame.hpp"
  30 #include "utilities/traits/removeCv.hpp"
  31 #include "utilities/traits/isClassDefined.hpp"
  32 
  33 /**
  34  * IsBaseOfAndDerived<Base, Derived>::value is true if Derived is a derived class of Base, disgarding their CV qualifiers.
  35  * Returns false for non-class types, except unions which will generate a compiler error.
  36  */
  37 template<typename Base, typename Derived>
  38 class IsBaseAndDerived {
  39   typedef char yes[1];
  40   typedef char no[2];
  41 
  42   template<typename T>
  43   static yes &check(Derived*, T);
  44   static no &check(Base*, int);
  45 
  46   template<typename B, typename D>
  47   struct IsBaseOfHost {
  48     operator B*() const;
  49     operator D*();
  50   };
  51 
  52   template<typename U>
  53   struct Complete: U { typedef U type; };
  54 
  55   template<typename B, typename D>
  56   struct CheckInheritance {
  57     static const bool value = sizeof(check(IsBaseOfHost<typename Complete<B>::type, typename Complete<D>::type>(), int())) == sizeof(yes);
  58   };
  59 
  60   template<bool is_class, typename B, typename D>
  61   struct CheckIsClass {
  62     static const bool value = false;
  63   };
  64 
  65   // First check if it is a class before checking inheritance
  66   template<typename B, typename D>
  67   struct CheckIsClass<true, B, D> {
  68     static const bool value = CheckInheritance<B, D>::value;
  69   };
  70 
  71   typedef typename RemoveCv<Base>::type BaseNoCv;
  72   typedef typename RemoveCv<Derived>::type DerivedNoCv;
  73 
  74 public:
  75   static const bool value = CheckIsClass<IsClassOrUnion<BaseNoCv>::value && IsClassOrUnion<DerivedNoCv>::value, BaseNoCv, DerivedNoCv>::value &&
  76       !IsSame<BaseNoCv, DerivedNoCv>::value;
  77 };
  78 
  79 /**
  80  * IsBaseOf<Base, Derived>::value is true if either Derived is a derived class of Base
  81  * disregarding CV qualifiers or Derived and Base are the same type disregarding CV qualifiers.
  82  * Returns false for all non-class types, except unions which will generate a compiler error.
  83  */
  84 template<typename Base, typename Derived>
  85 class IsBaseOf {
  86   typedef typename RemoveCv<Base>::type BaseNoCv;
  87   typedef typename RemoveCv<Derived>::type DerivedNoCv;
  88 public:
  89   static const bool value = (IsBaseAndDerived<BaseNoCv, DerivedNoCv>::value ||
  90       IsSame<BaseNoCv, DerivedNoCv>::value) &&
  91       IsClassOrUnion<BaseNoCv>::value;
  92 };
  93 
  94 #endif // SHARE_VM_UTILITIES_TRAITS_IS_BASE_OF_HPP
  95