Mercurial > hg > orthanc
annotate Core/Lua/LuaFunctionCall.cpp @ 1556:b8dc2f855a83
Preview of PDF files encapsulated in DICOM from Orthanc Explorer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 20 Aug 2015 17:05:05 +0200 |
parents | f967bdf8534e |
children | 9ea3d082b064 |
rev | line source |
---|---|
386 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1051
diff
changeset
|
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1051
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
386 | 5 * |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
386 | 34 #include "LuaFunctionCall.h" |
35 | |
996
cf52f3bcb2b3
clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
36 #include <cassert> |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
37 #include <stdio.h> |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
38 #include <boost/lexical_cast.hpp> |
386 | 39 |
40 namespace Orthanc | |
41 { | |
42 void LuaFunctionCall::CheckAlreadyExecuted() | |
43 { | |
44 if (isExecuted_) | |
45 { | |
46 throw LuaException("Arguments cannot be pushed after the function is executed"); | |
47 } | |
48 } | |
49 | |
50 LuaFunctionCall::LuaFunctionCall(LuaContext& context, | |
51 const char* functionName) : | |
52 context_(context), | |
53 isExecuted_(false) | |
54 { | |
55 // Clear the stack to fulfill the invariant | |
56 lua_settop(context_.lua_, 0); | |
57 lua_getglobal(context_.lua_, functionName); | |
58 } | |
59 | |
60 void LuaFunctionCall::PushString(const std::string& value) | |
61 { | |
62 CheckAlreadyExecuted(); | |
1465
905842836ad4
sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1448
diff
changeset
|
63 lua_pushlstring(context_.lua_, value.c_str(), value.size()); |
386 | 64 } |
65 | |
66 void LuaFunctionCall::PushBoolean(bool value) | |
67 { | |
68 CheckAlreadyExecuted(); | |
69 lua_pushboolean(context_.lua_, value); | |
70 } | |
71 | |
72 void LuaFunctionCall::PushInteger(int value) | |
73 { | |
74 CheckAlreadyExecuted(); | |
75 lua_pushinteger(context_.lua_, value); | |
76 } | |
77 | |
78 void LuaFunctionCall::PushDouble(double value) | |
79 { | |
80 CheckAlreadyExecuted(); | |
81 lua_pushnumber(context_.lua_, value); | |
82 } | |
83 | |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
84 void LuaFunctionCall::PushJson(const Json::Value& value) |
386 | 85 { |
86 CheckAlreadyExecuted(); | |
1051 | 87 context_.PushJson(value); |
386 | 88 } |
89 | |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
90 void LuaFunctionCall::ExecuteInternal(int numOutputs) |
386 | 91 { |
92 CheckAlreadyExecuted(); | |
93 | |
94 assert(lua_gettop(context_.lua_) >= 1); | |
95 int nargs = lua_gettop(context_.lua_) - 1; | |
96 int error = lua_pcall(context_.lua_, nargs, numOutputs, 0); | |
97 | |
98 if (error) | |
99 { | |
100 assert(lua_gettop(context_.lua_) >= 1); | |
101 | |
102 std::string description(lua_tostring(context_.lua_, -1)); | |
103 lua_pop(context_.lua_, 1); /* pop error message from the stack */ | |
104 throw LuaException(description); | |
105 } | |
106 | |
107 if (lua_gettop(context_.lua_) < numOutputs) | |
108 { | |
109 throw LuaException("The function does not give the expected number of outputs"); | |
110 } | |
111 | |
112 isExecuted_ = true; | |
113 } | |
114 | |
115 bool LuaFunctionCall::ExecutePredicate() | |
116 { | |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
117 ExecuteInternal(1); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
118 |
386 | 119 if (!lua_isboolean(context_.lua_, 1)) |
120 { | |
121 throw LuaException("The function is not a predicate (only true/false outputs allowed)"); | |
122 } | |
123 | |
124 return lua_toboolean(context_.lua_, 1) != 0; | |
125 } | |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
126 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
127 |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
128 void LuaFunctionCall::ExecuteToJson(Json::Value& result) |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
129 { |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
130 ExecuteInternal(1); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
131 context_.GetJson(result, lua_gettop(context_.lua_)); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
132 } |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
133 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
134 |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
135 void LuaFunctionCall::ExecuteToString(std::string& result) |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
136 { |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
137 ExecuteInternal(1); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
138 |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
139 int top = lua_gettop(context_.lua_); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
140 if (lua_isstring(context_.lua_, top)) |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
141 { |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
142 result = lua_tostring(context_.lua_, top); |
1010 | 143 } |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
144 else |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
145 { |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
146 throw LuaException("The function does not return a string"); |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
147 } |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
996
diff
changeset
|
148 } |
386 | 149 } |