The Battle for Wesnoth  1.19.0-dev
lua_ptr.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include <memory>
18 template<typename T>
19 class lua_ptr;
20 
21 template<typename T>
23 {
24 public:
25  enable_lua_ptr(T* tp) : self_(std::make_shared<T*>(tp)) {}
26 private:
27  friend class lua_ptr<T>;
28  std::shared_ptr<T*> self_;
29 };
30 
31 /** Tmust inherit enable_lua_ptr<T> */
32 template<typename T>
33 class lua_ptr
34 {
35 public:
37  T* get_ptr()
38  {
39  if(auto pp = self_.lock()) {
40  return *pp;
41  }
42  return nullptr;
43  }
44  std::weak_ptr<T*> self_;
45 };
std::shared_ptr< T * > self_
Definition: lua_ptr.hpp:28
enable_lua_ptr(T *tp)
Definition: lua_ptr.hpp:25
Tmust inherit enable_lua_ptr<T>
Definition: lua_ptr.hpp:34
lua_ptr(enable_lua_ptr< T > &o)
Definition: lua_ptr.hpp:36
std::weak_ptr< T * > self_
Definition: lua_ptr.hpp:44
T * get_ptr()
Definition: lua_ptr.hpp:37