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