Skip to content

Commit

Permalink
✨ Proper align.hpp header using templates!
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePhD committed Sep 20, 2022
1 parent dd305d2 commit 8c47f80
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 106 deletions.
18 changes: 0 additions & 18 deletions include/ztd/idk/align.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,6 @@ ZTD_C_LANGUAGE_LINKAGE_I_ ZTD_IDK_API_LINKAGE_I_ inline ztdc_aligned_mutable_poi
/// @param _SPACE The amount of available space within which this alignment pay be performed, in bytes.
#define ztdc_align(_ALIGN, _SIZE, _PTR, _SPACE) \
_Generic(_PTR, const void* : ztdc_align_const, void* : ztdc_align_mutable)(_ALIGN, _SIZE, _PTR, _SPACE)
#else

//////
/// @brief Aligns a pointer according to the given `alignment` and `size`, within the available `space` bytes.
///
/// @param _ALIGN The desired alignment for object that will be put at the (new aligned) pointer's location.
/// @param _SIZE The size of the object that will be put at the (newly aligned) pointer's location, in bytes.
/// @param _PTR The pointer to align.
/// @param _SPACE The amount of available space within which this alignment pay be performed, in bytes.
template <typename _Type>
auto ztdc_align(::std::size_t __alignment, ::std::size_t __size, _Type* __ptr, ::std::size_t __space) {
if constexpr (::std::is_const_v<_Type>) {
return ztdc_align_const(__alignment, __size, __ptr, __space);
}
else {
return ztdc_align_mutable(__alignment, __size, __ptr, __space);
}
}
#endif


Expand Down
89 changes: 89 additions & 0 deletions include/ztd/idk/align.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// =============================================================================
//
// ztd.idk
// Copyright © 2022 JeanHeyd "ThePhD" Meneide and Shepherd's Oasis, LLC
// Contact: opensource@soasis.org
//
// Commercial License Usage
// Licensees holding valid commercial ztd.idk licenses may use this file in
// accordance with the commercial license agreement provided with the
// Software or, alternatively, in accordance with the terms contained in
// a written agreement between you and Shepherd's Oasis, LLC.
// For licensing terms and conditions see your agreement. For
// further information contact opensource@soasis.org.
//
// Apache License Version 2 Usage
// Alternatively, this file may be used under the terms of Apache License
// Version 2.0 (the "License") for non-commercial use; you may not use this
// file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================ //

#pragma once

#ifndef ZTD_IDK_ALIGN_HPP
#define ZTD_IDK_ALIGN_HPP

#include <ztd/idk/version.hpp>

#include <ztd/idk/align.h>

#if ZTD_IS_ON(ZTD_C)
#include <stddef.h>
#include <stdint.h>
#else
#include <cstddef>
#include <cstdint>
#endif

namespace ztd {
ZTD_IDK_INLINE_ABI_NAMESPACE_OPEN_I_

//////
/// @addtogroup ztd_idk_align Align
/// @{

//////
/// @brief Aligns a pointer according to the given `alignment` and `size`, within the available `space` bytes.
///
/// @param __alignment The desired alignment for object that will be put at the (new aligned) pointer's location.
/// @param __size The size of the object that will be put at the (newly aligned) pointer's location, in bytes.
/// @param __ptr The pointer to align.
/// @param __space The amount of available space within which this alignment pay be performed, in bytes.
template <typename _Type>
auto align(::std::size_t __alignment, ::std::size_t __size, _Type* __ptr, ::std::size_t __space) noexcept {
if constexpr (::std::is_const_v<_Type>) {
return ztdc_align_const(__alignment, __size, __ptr, __space);
}
else {
return ztdc_align_mutable(__alignment, __size, __ptr, __space);
}
}

//////
/// @}

ZTD_IDK_INLINE_ABI_NAMESPACE_CLOSE_I_
} // namespace ztd

// redefine for C++
#undef ztdc_align
//////
/// @brief Aligns a pointer according to the given `alignment` and `size`, within the available `space` bytes.
///
/// @param _ALIGN The desired alignment for object that will be put at the (new aligned) pointer's location.
/// @param _SIZE The size of the object that will be put at the (newly aligned) pointer's location, in bytes.
/// @param _PTR The pointer to align.
/// @param _SPACE The amount of available space within which this alignment pay be performed, in bytes.
#define ztdc_align(_ALIGN, _SIZE, _PTR, _SPACE) ::ztd::align(_ALIGN, _SIZE, _PTR, _SPACE)

#endif // ZTD_IDK_ALIGN_HPP
55 changes: 0 additions & 55 deletions include/ztd/idk/ckd_int.h

This file was deleted.

2 changes: 1 addition & 1 deletion include/ztd/idk/unreachable.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#ifndef ZTD_IDK_UNREACHABLE_H
#define ZTD_IDK_UNREACHABLE_H

#include <ztd/version.h>
#include <ztd/idk/version.h>

#if ZTD_HAS_BUILTIN_I_(__builtin_unreachable)
#define ZTD_UNREACHABLE() __builtin_unreachable();
Expand Down
2 changes: 2 additions & 0 deletions include/ztd/idk/unreachable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@

#include <ztd/idk/unreachable.h>

// Nothing to do here, yet, since there's no C++-specific tech for unreachable that is needed.

#endif // ZTD_IDK_UNREACHABLE_HPP
2 changes: 1 addition & 1 deletion tests/idk/inclusion/source/ztd/idk/align.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
//
// ============================================================================ //

#include <ztd/idk/align.h>
#include <ztd/idk/align.hpp>
31 changes: 0 additions & 31 deletions tests/idk/inclusion/source/ztd/idk/ckd_int.c

This file was deleted.

0 comments on commit 8c47f80

Please sign in to comment.