Skip to content

Commit d32a888

Browse files
committed
GH-1141 Introduce StringPtr for static initialization
1 parent f7d6e78 commit d32a888

File tree

4 files changed

+93
-6
lines changed

4 files changed

+93
-6
lines changed

src/core/godot/string/string.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// This file is part of the Godot Orchestrator project.
2+
//
3+
// Copyright (c) 2023-present Crater Crash Studios LLC and its contributors.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
#include "core/godot/string/string.h"
18+
19+
using namespace godot;
20+
21+
StringPtr& StringPtr::operator=(const String& p_value) {
22+
if (!_data) {
23+
_data = memnew(String);
24+
}
25+
*_data = p_value;
26+
return *this;
27+
}
28+
29+
StringPtr& StringPtr::operator=(const char* p_value) {
30+
if (!_data) {
31+
_data = memnew(String);
32+
}
33+
*_data = p_value;
34+
return *this;
35+
}
36+
37+
const String& StringPtr::get() const {
38+
static String empty;
39+
return _data ? *_data : empty;
40+
}
41+
42+
StringPtr::~StringPtr() {
43+
if (_data) {
44+
memdelete(_data);
45+
_data = nullptr;
46+
}
47+
}

src/core/godot/string/string.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This file is part of the Godot Orchestrator project.
2+
//
3+
// Copyright (c) 2023-present Crater Crash Studios LLC and its contributors.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
#ifndef ORCHESTRATOR_CORE_GODOT_STRING_H
18+
#define ORCHESTRATOR_CORE_GODOT_STRING_H
19+
20+
#include <godot_cpp/variant/string.hpp>
21+
22+
namespace godot {
23+
24+
/// Provides a dynamic allocation wrapper for a String.
25+
/// This is useful when placing a Godot String in static initialization in GDExtension.
26+
class StringPtr {
27+
String* _data = nullptr;
28+
29+
public:
30+
StringPtr& operator=(const String& p_value);
31+
StringPtr& operator=(const char* p_value);
32+
33+
const String& get() const;
34+
35+
~StringPtr();
36+
};
37+
38+
}
39+
#endif // ORCHESTRATOR_CORE_GODOT_STRING_H

src/script/language.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ OScriptLanguage* OScriptLanguage::_singleton = nullptr;
4646

4747
thread_local OScriptLanguage::CallLevel* OScriptLanguage::_call_stack = nullptr;
4848
thread_local uint32_t OScriptLanguage::_call_stack_size = 0;
49-
thread_local String OScriptLanguage::_debug_parse_err_file = "";
49+
thread_local StringPtr OScriptLanguage::_debug_parse_err_file = StringPtr();
5050
thread_local int OScriptLanguage::_debug_parse_err_line = 0;
51-
thread_local String OScriptLanguage::_debug_error = "";
51+
thread_local StringPtr OScriptLanguage::_debug_error = StringPtr();
5252

5353
struct OScriptDepSort {
5454
//must support sorting so inheritance works properly (parent must be reloaded first)
@@ -304,7 +304,7 @@ void OScriptLanguage::_remove_named_global_constant(const StringName& p_name) {
304304
}
305305

306306
String OScriptLanguage::_debug_get_error() const {
307-
return _debug_error;
307+
return _debug_error.get();
308308
}
309309

310310
int32_t OScriptLanguage::_debug_get_stack_level_count() const {
@@ -347,7 +347,7 @@ String OScriptLanguage::_debug_get_stack_level_function(int32_t p_level) const {
347347
String OScriptLanguage::_debug_get_stack_level_source(int32_t p_level) const {
348348
#if GODOT_VERSION >= 0x040300
349349
if (_debug_parse_err_line >= 0) {
350-
return _debug_parse_err_file;
350+
return _debug_parse_err_file.get();
351351
}
352352

353353
ERR_FAIL_INDEX_V(p_level, _call_stack_size, {});

src/script/language.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef ORCHESTRATOR_SCRIPT_LANGUAGE_H
1818
#define ORCHESTRATOR_SCRIPT_LANGUAGE_H
1919

20+
#include "core/godot/string/string.h"
2021
#include "common/version.h"
2122
#include "script/compiler/compiled_function.h"
2223
#include "script/serialization/format_defs.h"
@@ -73,8 +74,8 @@ class OScriptLanguage : public ScriptLanguageExtension {
7374
#endif
7475

7576
static thread_local int _debug_parse_err_line;
76-
static thread_local String _debug_parse_err_file;
77-
static thread_local String _debug_error;
77+
static thread_local StringPtr _debug_parse_err_file;
78+
static thread_local StringPtr _debug_error;
7879
static thread_local CallLevel* _call_stack;
7980
static thread_local uint32_t _call_stack_size;
8081
uint32_t _debug_max_call_stack = 0;

0 commit comments

Comments
 (0)