1 /*
2  * Copyright (c) 2020, 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_UTILITIES_VALUEOBJARRAY_HPP
26 #define SHARE_UTILITIES_VALUEOBJARRAY_HPP
27 
28 #include "utilities/debug.hpp"
29 
30 // Stamps out Count instances of Type using a recursive template.
31 template <typename Type, int Count>
32 class ValueObjBlock {
33   typedef ValueObjBlock<Type, Count - 1> Next;
34 
35   Type _instance;
36   Next _next;
37 
38 public:
39   template <typename Generator>
ValueObjBlock(Generator g,Type ** save_to)40   ValueObjBlock(Generator g, Type** save_to) :
41       _instance(*g),
42       _next(++g, save_to + 1) {
43     *save_to = &_instance;
44   }
45 };
46 
47 // Specialization for the recursion base case.
48 template <typename Type>
49 class ValueObjBlock<Type, 0> {
50 public:
51   template <typename Generator>
ValueObjBlock(Generator,Type **)52   ValueObjBlock(Generator, Type**) {}
53 };
54 
55 // Maps an array of size Count over stamped-out instances of Type.
56 template <typename Type, int Count>
57 struct ValueObjArray {
58   Type*                      _ptrs[Count];
59   ValueObjBlock<Type, Count> _block;
60 
61   template <typename Generator>
ValueObjArrayValueObjArray62   ValueObjArray(Generator g) : _ptrs(), _block(g, _ptrs) {}
63 
atValueObjArray64   Type* at(int index) const {
65     assert(0 <= index && index < Count, "index out-of-bounds: %d", index);
66     return _ptrs[index];
67   }
68 
countValueObjArray69   static int count() {
70     return Count;
71   }
72 };
73 
74 #endif // SHARE_UTILITIES_VALUEOBJARRAY_HPP
75