Visual Computing Library  devel
Loading...
Searching...
No Matches
const_correctness.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_BASE_CONST_CORRECTNESS_H
24#define VCL_BASE_CONST_CORRECTNESS_H
25
26#include <memory>
27#include <type_traits>
28
29namespace vcl {
30
42template<typename T>
44{
45 using type = T;
46};
47
53template<typename T>
55{
56 using type = const T*;
57};
58
64template<typename T>
65struct MakeConstPointer<std::shared_ptr<T>>
66{
67 using type = std::shared_ptr<const T>;
68};
69
77template<typename T>
78using MakeConstPointerT = typename MakeConstPointer<T>::type;
79
80/*
81 * Full deduction for the possibility to re-use same code for const and
82 * non-const member functions https://stackoverflow.com/a/47369227/5851101
83 */
84
102template<typename T>
103constexpr T& asConst(const T& value) noexcept
104{
105 return const_cast<T&>(value);
106}
107
113template<typename T>
114constexpr T* asConst(const T* value) noexcept
115{
116 return const_cast<T*>(value);
117}
118
124template<typename T>
125constexpr T* asConst(T* value) noexcept
126{
127 return value;
128}
129
130template<typename T>
131void asConst(const T&&) = delete;
132
133} // namespace vcl
134
135#endif // VCL_BASE_CONST_CORRECTNESS_H
constexpr T & asConst(const T &value) noexcept
Utility function that converts a const pointer/reference to a non-const pointer/reference.
Definition const_correctness.h:103
typename MakeConstPointer< T >::type MakeConstPointerT
Utility alias for the MakeConstPointer type.
Definition const_correctness.h:78
Utility type that makes possible to treat const pointers in a templated class that can treat a both c...
Definition const_correctness.h:44