1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_COMMON_BUFFER_PARSER_IMPL_H_
16 : #define SYZYGY_COMMON_BUFFER_PARSER_IMPL_H_
17 :
18 : #ifndef SYZYGY_COMMON_BUFFER_PARSER_H_
19 : #error This file is only meant to be included from buffer_parser.h.
20 : #endif
21 :
22 : #include <stddef.h>
23 :
24 : #include "syzygy/common/align.h"
25 :
26 : namespace common {
27 :
28 : namespace detail {
29 :
30 : // Utility template class for determining the alignment of a given type.
31 : template <typename DataType>
32 : struct GetAlignment {
33 : struct Helper {
34 : uint8_t foo;
35 : DataType bar;
36 : };
37 : static const size_t kAlignment = offsetof(Helper, bar);
38 : };
39 :
40 : } // namespace detail
41 :
42 : template <class DataType>
43 : bool BinaryBufferParser::GetAtImplicitAlignment(
44 E : size_t pos, size_t size, const DataType** data_ptr) const {
45 E : const size_t kAlign = detail::GetAlignment<DataType>::kAlignment;
46 E : return GetAtExplicitAlignment(pos, size, kAlign, data_ptr);
47 E : }
48 :
49 : template <class DataType>
50 : bool BinaryBufferParser::GetAtExplicitAlignment(
51 E : size_t pos, size_t size, size_t align, const DataType** data_ptr) const {
52 E : if (!common::IsAligned(data_ + pos, align))
53 E : return false;
54 E : return GetAt(pos, size, reinterpret_cast<const void**>(data_ptr));
55 E : }
56 :
57 : } // namespace common
58 :
59 : #endif // SYZYGY_COMMON_BUFFER_PARSER_IMPL_H_
|