Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #ifndef MUSE_GLOBAL_PTRUTILS_H
23 : #define MUSE_GLOBAL_PTRUTILS_H
24 :
25 : #include "stringutils.h"
26 : #include "log.h"
27 :
28 : namespace muse::ptr {
29 0 : template<typename T, typename E> T* checked_cast(E* source)
30 : {
31 : #ifndef NDEBUG
32 0 : T* casted = dynamic_cast<T*>(source);
33 0 : if (source && !casted) {
34 0 : assert(false && "checked_cast: bad cast");
35 : }
36 0 : return casted;
37 : #else
38 : return static_cast<T*>(source);
39 : #endif
40 : }
41 :
42 : template<typename T, typename E> const T* checked_cast(const E* source)
43 : {
44 : #ifndef NDEBUG
45 : T* casted = dynamic_cast<T*>(source);
46 : if (source && !casted) {
47 : assert(false && "checked_cast: bad cast");
48 : }
49 : return casted;
50 : #else
51 : return static_cast<T*>(source);
52 : #endif
53 : }
54 :
55 : inline bool string_is_hex(const std::string& str)
56 : {
57 : return strings::startsWith(str, "0x");
58 : }
59 :
60 : inline std::string ptr_to_string(void* p)
61 : {
62 : std::stringstream ss;
63 : ss << p;
64 : return ss.str();
65 : }
66 :
67 : inline void* ptr_from_string(const std::string& str)
68 : {
69 : uintptr_t num = 0;
70 : std::stringstream ss;
71 : ss << std::hex << str;
72 : ss >> num;
73 : return reinterpret_cast<void*>(num);
74 : }
75 : }
76 :
77 : #endif // MUSE_GLOBAL_PTRUTILS_H
|