src/hotspot/share/metaprogramming/primitiveConversions.hpp
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 46958 hotspot/src/share/vm/metaprogramming/primitiveConversions.hpp@a13bd8c6b7a2
child 47609 a1f68e415b48
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     1
/*
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     4
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     8
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    13
 * accompanied this code).
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    14
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    18
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    21
 * questions.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    22
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    23
 */
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    24
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    25
#ifndef SHARE_VM_METAPROGRAMMING_PRIMITIVECONVERSIONS_HPP
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    26
#define SHARE_VM_METAPROGRAMMING_PRIMITIVECONVERSIONS_HPP
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    27
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    28
#include "memory/allocation.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    29
#include "metaprogramming/enableIf.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    30
#include "metaprogramming/integralConstant.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    31
#include "metaprogramming/isFloatingPoint.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    32
#include "metaprogramming/isIntegral.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    33
#include "metaprogramming/isRegisteredEnum.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    34
#include "utilities/debug.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    35
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    36
class PrimitiveConversions : public AllStatic {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    37
public:
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    38
  // Return a value of type T with the same representation as x.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    39
  //
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    40
  // T and U must be of the same size.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    41
  //
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    42
  // At least one of T or U must be an integral type.  The other must
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    43
  // be an integral, floating point, or pointer type.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    44
  template<typename T, typename U> static T cast(U x);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    45
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    46
  // Support thin wrappers over primitive types.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    47
  // If derived from TrueType, provides representational conversion
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    48
  // from T to some other type.  When true, must provide
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    49
  // - Value: typedef for T.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    50
  // - Decayed: typedef for decayed type.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    51
  // - static Decayed decay(T x): return value of type Decayed with
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    52
  //   the same representation as x.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    53
  // - static T recover(Decayed x): return a value of type T with the
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    54
  //   same representation as x.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    55
  template<typename T> struct Translate : public FalseType {};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    56
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    57
private:
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    58
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    59
  template<typename T,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    60
           typename U,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    61
           bool same_size = sizeof(T) == sizeof(U),
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    62
           typename Enable = void>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    63
  struct Cast;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    64
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    65
  template<typename T, typename U> static T cast_using_union(U x);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    66
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    67
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    68
// Return an object of type T with the same value representation as x.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    69
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    70
// T and U must be of the same size.  It is expected that one of T and
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    71
// U is an integral type, and the other is an integral type, a
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    72
// (registered) enum type, or a floating point type
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    73
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    74
// This implementation uses the "union trick", which seems to be the
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    75
// best of a bad set of options.  Though technically undefined
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    76
// behavior, it is widely and well supported, producing good code.  In
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    77
// some cases, such as gcc, that support is explicitly documented.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    78
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    79
// Using memcpy is the correct method, but some compilers produce
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    80
// wretched code for that method, even at maximal optimization levels.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    81
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    82
// Using static_cast is only possible for integral and enum types, not
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    83
// for floating point types.  And for integral and enum conversions,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    84
// static_cast has unspecified or implementation-defined behavior for
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    85
// some cases.  C++11 <type_traits> can be used to avoid most or all
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    86
// of those unspecified or implementation-defined issues, though that
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    87
// may require multi-step conversions.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    88
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    89
// Using reinterpret_cast of references has undefined behavior for
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    90
// many cases, and there is much less empirical basis for its use, as
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    91
// compared to the union trick.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    92
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    93
inline T PrimitiveConversions::cast_using_union(U x) {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    94
  STATIC_ASSERT(sizeof(T) == sizeof(U));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    95
  union { T t; U u; };
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    96
  u = x;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    97
  return t;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    98
}
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    99
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   100
//////////////////////////////////////////////////////////////////////////////
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   101
// cast<T>(x)
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   102
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   103
// Cast<T, U, same_size, Enable>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   104
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   105
// Give an informative error if the sizes differ.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   106
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   107
struct PrimitiveConversions::Cast<T, U, false> VALUE_OBJ_CLASS_SPEC {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   108
  STATIC_ASSERT(sizeof(T) == sizeof(U));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   109
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   110
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   111
// Conversion between integral types.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   112
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   113
struct PrimitiveConversions::Cast<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   114
  T, U, true,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   115
  typename EnableIf<IsIntegral<T>::value && IsIntegral<U>::value>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   116
  VALUE_OBJ_CLASS_SPEC
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   117
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   118
  T operator()(U x) const { return cast_using_union<T>(x); }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   119
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   120
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   121
// Convert an enum or floating point value to an integer value.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   122
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   123
struct PrimitiveConversions::Cast<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   124
  T, U, true,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   125
  typename EnableIf<IsIntegral<T>::value &&
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   126
                    (IsRegisteredEnum<U>::value ||
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   127
                     IsFloatingPoint<U>::value)>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   128
  VALUE_OBJ_CLASS_SPEC
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   129
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   130
  T operator()(U x) const { return cast_using_union<T>(x); }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   131
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   132
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   133
// Convert an integer to an enum or floating point value.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   134
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   135
struct PrimitiveConversions::Cast<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   136
  T, U, true,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   137
  typename EnableIf<IsIntegral<U>::value &&
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   138
                    (IsRegisteredEnum<T>::value ||
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   139
                     IsFloatingPoint<T>::value)>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   140
  VALUE_OBJ_CLASS_SPEC
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   141
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   142
  T operator()(U x) const { return cast_using_union<T>(x); }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   143
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   144
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   145
// Convert a pointer to an integral value.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   146
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   147
struct PrimitiveConversions::Cast<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   148
  T, U*, true,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   149
  typename EnableIf<IsIntegral<T>::value>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   150
  VALUE_OBJ_CLASS_SPEC
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   151
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   152
  T operator()(U* x) const { return reinterpret_cast<T>(x); }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   153
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   154
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   155
// Convert an integral value to a pointer.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   156
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   157
struct PrimitiveConversions::Cast<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   158
  T*, U, true,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   159
  typename EnableIf<IsIntegral<U>::value>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   160
  VALUE_OBJ_CLASS_SPEC
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   161
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   162
  T* operator()(U x) const { return reinterpret_cast<T*>(x); }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   163
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   164
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   165
template<typename T, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   166
inline T PrimitiveConversions::cast(U x) {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   167
  return Cast<T, U>()(x);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   168
}
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   169
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   170
#endif // SHARE_VM_METAPROGRAMMING_PRIMITIVECONVERSIONS_HPP