HoverRace  2.0
WrapperFactory.h
Go to the documentation of this file.
1 
2 // WrapperFactory.h
3 //
4 // Copyright (c) 2014, 2016 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include "../Util/Log.h"
25 
26 #include "Core.h"
27 #include "RegistryRef.h"
28 #include "ScriptExn.h"
29 
30 namespace HoverRace {
31  namespace Script {
32  class Core;
33  }
34 }
35 
36 namespace HoverRace {
37 namespace Script {
38 
43 template<class Inside, class Outside>
45 {
46 public:
48  scripting(&scripting), ref(scripting) { }
49  WrapperFactory(const WrapperFactory&) = default;
50  WrapperFactory(WrapperFactory&&) = default;
51 
52  WrapperFactory &operator=(const WrapperFactory&) = default;
53  WrapperFactory &operator=(WrapperFactory&&) = default;
54 
55  WrapperFactory &operator=(const luabind::object &obj)
56  {
57  Set(obj);
58  return *this;
59  }
60 
61  void Set(const luabind::object &obj)
62  {
63  using namespace luabind;
64 
65  switch (type(obj)) {
66  case LUA_TUSERDATA:
67  case LUA_TFUNCTION:
68  ref = obj;
69  break;
70  case LUA_TNIL:
71  ref.Clear();
72  break;
73  default:
74  throw ScriptExn("Expected a constructor or function.");
75  }
76  }
77 
85  std::shared_ptr<Outside> operator()(std::shared_ptr<Inside> inside) const
86  {
87  using namespace luabind;
88  using namespace Util;
89 
90  if (!ref) {
91  return std::make_shared<Outside>(inside);
92  }
93 
94  lua_State *L = scripting->GetState();
95 
96  object obj(L, inside);
97  std::shared_ptr<Outside> retv;
98 
99  ref.Push();
100  obj.push(L);
101  scripting->Invoke(1, nullptr, [&](lua_State *L, int num) {
102  if (num < 1) {
103  HR_LOG(error) << "Expected a return value.";
104  }
105  else {
106  object retObj(from_stack(L, -num));
107  try {
108  retv = object_cast<std::shared_ptr<Outside>>(retObj);
109  }
110  catch (cast_failed &ex)
111  {
112  HR_LOG(error) << "Unexpected return value: " << ex.what();
113  }
114  }
115  lua_pop(L, num);
116  return 0;
117  });
118 
119  return retv ? retv : std::make_shared<Outside>(inside);
120  }
121 
122 private:
125 };
126 
127 } // namespace Script
128 } // namespace HoverRace
WrapperFactory & operator=(const luabind::object &obj)
Definition: WrapperFactory.h:55
Generic script engine exception.
Definition: ScriptExn.h:41
WrapperFactory(Script::Core &scripting)
Definition: WrapperFactory.h:47
Encapsulates a ref to the registry.
Definition: RegistryRef.h:50
RegistryRef ref
Definition: WrapperFactory.h:124
Script::Core * scripting
Definition: WrapperFactory.h:123
Definition: StyleEnv.cpp:32
A reference to a Lua function or userdata that wraps a native object.
Definition: WrapperFactory.h:44
#define HR_LOG(lvl)
Alias for BOOST_LOG_TRIVIAL.
Definition: Log.h:45
void Set(const luabind::object &obj)
Definition: WrapperFactory.h:61
std::shared_ptr< Outside > operator()(std::shared_ptr< Inside > inside) const
Wrap a native object in the wrapper.
Definition: WrapperFactory.h:85
Definition: Announcement.h:24
A script environment.
Definition: Core.h:66