changeset 328:25514c48e30e

demonstration of C++11 lambda functions
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 08 Jan 2013 14:55:55 +0100
parents 4564e908bba9
children f579d50fdf8f
files Resources/Samples/RestApiStandalone/CMakeLists.txt Resources/Samples/RestApiStandalone/README.txt Resources/Samples/RestApiStandalone/Sample.cpp
diffstat 3 files changed, 33 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/Resources/Samples/RestApiStandalone/CMakeLists.txt	Tue Jan 08 14:49:08 2013 +0100
+++ b/Resources/Samples/RestApiStandalone/CMakeLists.txt	Tue Jan 08 14:55:55 2013 +0100
@@ -2,6 +2,9 @@
 
 project(RestApiSample)
 
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall")
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -std=c++0x")
+
 file(DOWNLOAD 
   http://mongoose.googlecode.com/files/mongoose-3.1.tgz 
   ${CMAKE_BINARY_DIR}/mongoose-3.1.tar.gz
@@ -26,6 +29,12 @@
   WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
   )
 
+# Apply a patch to improve Mongoose shutdown
+execute_process(
+  COMMAND patch mongoose.c ${ORTHANC_DIR}/Resources/Patches/mongoose-patch.diff
+  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/mongoose
+  )
+
 execute_process(
   COMMAND ${CMAKE_COMMAND} -E tar xvfz ${CMAKE_BINARY_DIR}/jsoncpp-src-0.5.0.tar.gz
   WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
--- a/Resources/Samples/RestApiStandalone/README.txt	Tue Jan 08 14:49:08 2013 +0100
+++ b/Resources/Samples/RestApiStandalone/README.txt	Tue Jan 08 14:55:55 2013 +0100
@@ -3,9 +3,9 @@
 shown how a sample REST API can be created.
 
 This is the same sample than in folder "../RestApi", but with a
-different build script.
+different build script and the use of C++ lambda functions.
 
 The build script of this folder does not rely on the default CMake
 script from Orthanc. It dynamically links against the standard system
-libraries. This results in a simpler, standalone build
+Linux libraries. This results in a simpler, standalone build
 script. However, it will only work on Linux-based systems.
--- a/Resources/Samples/RestApiStandalone/Sample.cpp	Tue Jan 08 14:49:08 2013 +0100
+++ b/Resources/Samples/RestApiStandalone/Sample.cpp	Tue Jan 08 14:55:55 2013 +0100
@@ -49,31 +49,6 @@
  *  # curl http://localhost:8042 -X POST -d "PostBody"
  **/
 
-static void GetRoot(Orthanc::RestApi::GetCall& call)
-{
-  std::string answer = "Hello world\n";
-  answer += "Glad to meet you, Mr. " + call.GetArgument("name", "Nobody") + "\n";
-  call.GetOutput().AnswerBuffer(answer, "text/plain");
-}
- 
-static void DeleteRoot(Orthanc::RestApi::DeleteCall& call)
-{
-  call.GetOutput().AnswerBuffer("Hey, you have just deleted the server!\n",
-                                "text/plain");
-}
- 
-static void PostRoot(Orthanc::RestApi::PostCall& call)
-{
-  call.GetOutput().AnswerBuffer("I have received a POST with body: [" +
-                                call.GetPostBody() + "]\n", "text/plain");
-}
- 
-static void PutRoot(Orthanc::RestApi::PutCall& call)
-{
-  call.GetOutput().AnswerBuffer("I have received a PUT with body: [" +
-                                call.GetPutBody() + "]\n", "text/plain");
-}
- 
 int main()
 {
   // Initialize the logging mechanism
@@ -82,12 +57,29 @@
   FLAGS_minloglevel = 0;                      // Use the verbose mode
   FLAGS_v = 0;
   
-  // Define the callbacks of the REST API
+  // Define the callbacks of the REST API using C++11 lambda functions
   std::auto_ptr<Orthanc::RestApi> rest(new Orthanc::RestApi);
-  rest->Register("/", GetRoot);
-  rest->Register("/", PostRoot);
-  rest->Register("/", PutRoot);
-  rest->Register("/", DeleteRoot);
+
+  rest->Register("/", [] (Orthanc::RestApi::GetCall& call) {
+      std::string answer = "Hello world\n";
+      answer += "Glad to meet you, Mr. " + call.GetArgument("name", "Nobody") + "\n";
+      call.GetOutput().AnswerBuffer(answer, "text/plain");
+    });
+
+  rest->Register("/", [] (Orthanc::RestApi::DeleteCall& call) {
+      call.GetOutput().AnswerBuffer("Hey, you have just deleted the server!\n",
+                                    "text/plain");
+    });
+    
+  rest->Register("/", [] (Orthanc::RestApi::PostCall& call) {
+      call.GetOutput().AnswerBuffer("I have received a POST with body: [" +
+                                    call.GetPostBody() + "]\n", "text/plain");
+    });
+
+  rest->Register("/", [] (Orthanc::RestApi::PutCall& call) {
+      call.GetOutput().AnswerBuffer("I have received a PUT with body: [" +
+                                    call.GetPutBody() + "]\n", "text/plain");
+    });
 
   // Setup the embedded HTTP server
   Orthanc::MongooseServer httpServer;