Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix NS3 and lua builds
[simgrid.git] / src / bindings / lua / lua_host.cpp
index 804b672..71484a4 100644 (file)
@@ -1,5 +1,4 @@
-/* Copyright (c) 2010, 2012-2015. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -7,10 +6,12 @@
 /* SimGrid Lua bindings                                                     */
 
 #include "lua_private.h"
+#include "simgrid/s4u/Host.hpp"
+extern "C" {
 #include <lauxlib.h>
-#include <simgrid/host.h>
+}
 
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_host, bindings, "Lua bindings (host module)");
+XBT_LOG_NEW_DEFAULT_CATEGORY(lua_host, "Lua Host module");
 
 #define HOST_MODULE_NAME "simgrid.host"
 #define HOST_FIELDNAME   "__simgrid_host"
@@ -18,11 +19,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_host, bindings, "Lua bindings (host module)"
 /*                                simgrid.host API                                   */
 /* ********************************************************************************* */
 
-/**
- * \brief Ensures that a value in the stack is a host and returns it.
- *
- * This function is called from C and helps to verify that a specific
- * userdatum on the stack is in fact a sg_host_t.
+/** @brief Ensures that the pointed stack value is an host userdatum and returns it.
  *
  * \param L a Lua state
  * \param index an index in the Lua stack
@@ -30,16 +27,15 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_host, bindings, "Lua bindings (host module)"
  */
 sg_host_t sglua_check_host(lua_State * L, int index)
 {
-  sg_host_t *pi, ht;
   luaL_checktype(L, index, LUA_TTABLE);
   lua_getfield(L, index, HOST_FIELDNAME);
-  pi = (sg_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME);
-  if (pi == NULL)
-    XBT_ERROR("luaL_checkudata() returned NULL");
-  ht = *pi;
+  sg_host_t *pi = (sg_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME);
+  lua_pop(L, 1);
+  if (pi == nullptr)
+    XBT_ERROR("luaL_checkudata() returned nullptr");
+  sg_host_t ht = *pi;
   if (!ht)
     luaL_error(L, "null Host");
-  lua_pop(L, 1);
   return ht;
 }
 
@@ -55,21 +51,18 @@ sg_host_t sglua_check_host(lua_State * L, int index)
 static int l_host_get_by_name(lua_State * L)
 {
   const char *name = luaL_checkstring(L, 1);
-  XBT_DEBUG("Getting host by name...");
+  lua_remove(L, 1); /* remove the args from the stack */
+
   sg_host_t host = sg_host_by_name(name);
-  if (!host) {
-    XBT_ERROR("sg_get_host_by_name failed, requested hostname: %s", name);
-  }
+  lua_ensure(host, "No host name '%s' found.", name);
+
   lua_newtable(L);                        /* table */
-  sg_host_t *lua_host = (sg_host_t *) lua_newuserdata(L, sizeof(sg_host_t));
-                                          /* table userdatum */
+  sg_host_t *lua_host = (sg_host_t *) lua_newuserdata(L, sizeof(sg_host_t)); /* table userdatum */
   *lua_host = host;
   luaL_getmetatable(L, HOST_MODULE_NAME); /* table userdatum metatable */
   lua_setmetatable(L, -2);                /* table userdatum */
-  lua_setfield(L, -2, HOST_FIELDNAME);  /* table -- put the userdata as field of the table */
+  lua_setfield(L, -2, HOST_FIELDNAME);    /* table -- put the userdata as field of the table */
 
-  /* remove the args from the stack */
-  lua_remove(L, 1);
   return 1;
 }
 
@@ -84,7 +77,7 @@ static int l_host_get_by_name(lua_State * L)
 static int l_host_get_name(lua_State * L)
 {
   sg_host_t ht = sglua_check_host(L, 1);
-  lua_pushstring(L, sg_host_get_name(ht));
+  lua_pushstring(L, ht->cname());
   return 1;
 }
 
@@ -166,7 +159,7 @@ static const luaL_Reg host_functions[] = {
   {"destroy", l_host_destroy},
   // Bypass XML Methods
   {"set_property", console_host_set_property},
-  {NULL, NULL}
+  {nullptr, nullptr}
 };
 
 /**