Skip to content
Permalink
Browse files

Eliminate static scope from C++

The static keyword is heavily overloaded in C++, so the standard
committee has long recommended using annonymous namespaces instead for
indicating that code only applies to the current translation unit.

As an advantage, anonymous namespaces can also apply to types and
typedefs.

In the case of headers I removed static without replacing it - static
does not make sense in the context of header files.
  • Loading branch information...
TheCycoONE committed Apr 5, 2019
1 parent 8ec5921 commit 1ee6c904dc1d29423b2fe519cd834122ecdf5578
@@ -40,8 +40,10 @@ font data and Lua script are hardcoded into this file (the font itself is a
homemade bitmap font, as we cannot rely on TH fonts being present).
*/

static constexpr int first_bootstrap_code_line_number = __LINE__ + 2;
static constexpr const char* bootstrap_code[] = {
namespace {

constexpr int first_bootstrap_code_line_number = __LINE__ + 2;
constexpr const char* bootstrap_code[] = {
"local lines, dat, tab, pal, err = {}, ...",
"local function t(s) return s:gsub('\\t', ' ') end",
"for s in tostring(err):gmatch'[^\\r\\n]+' do lines[#lines+1] = t(s) end",
@@ -91,7 +93,7 @@ static constexpr const char* bootstrap_code[] = {

/* Start autogenerated content */
/* Data from bootstrap_font.tab inserted by mkbootstrap.lua: */
static constexpr std::array<uint8_t, 499> bootstrap_font_tab {
constexpr std::array<uint8_t, 499> bootstrap_font_tab {
0x52, 0x4E, 0x43, 0x01, 0x00, 0x00, 0x05, 0x46, 0x00, 0x00, 0x01, 0xE1, 0xFB,
0xF2, 0x66, 0x51, 0xBE, 0xEF, 0x0C, 0x09, 0x59, 0x60, 0x10, 0x34, 0x43, 0x54,
0xA6, 0x46, 0xA2, 0x08, 0x00, 0xA0, 0xC4, 0x01, 0xF6, 0x3D, 0x00, 0x00, 0xAF,
@@ -134,7 +136,7 @@ static constexpr std::array<uint8_t, 499> bootstrap_font_tab {
};

/* Data from bootstrap_font.dat inserted by mkbootstrap.lua: */
static constexpr std::array<uint8_t, 1534> bootstrap_font_dat {
constexpr std::array<uint8_t, 1534> bootstrap_font_dat {
0x52, 0x4E, 0x43, 0x01, 0x00, 0x00, 0x13, 0x68, 0x00, 0x00, 0x05, 0xEC, 0xD3,
0x6C, 0x7E, 0xAB, 0xBE, 0xEF, 0x94, 0x90, 0x81, 0x61, 0x50, 0x34, 0x44, 0x33,
0x33, 0x53, 0x66, 0x64, 0x64, 0x26, 0x20, 0x60, 0x47, 0x1E, 0x01, 0xFF, 0x84,
@@ -255,7 +257,7 @@ static constexpr std::array<uint8_t, 1534> bootstrap_font_dat {
0x02, 0xAC, 0x9A, 0x43, 0x10, 0xB5, 0x0A, 0xA0, 0x17, 0x1C, 0xA6, 0x00, 0x9B
};
/* Data from bootstrap_font.pal inserted by mkbootstrap.lua: */
static constexpr std::array<uint8_t, 47> bootstrap_font_pal {
constexpr std::array<uint8_t, 47> bootstrap_font_pal {
0x52, 0x4E, 0x43, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x1D, 0xB7,
0xFF, 0x76, 0x79, 0xBE, 0xEF, 0x90, 0x10, 0x90, 0x05, 0x01, 0x02, 0x00, 0x00,
0x20, 0x2B, 0x04, 0x00, 0x00, 0x00, 0x40, 0x08, 0x00, 0xB6, 0x00, 0x00, 0x00,
@@ -264,7 +266,7 @@ static constexpr std::array<uint8_t, 47> bootstrap_font_pal {
/* End autogenerated content */

// Lua reader function for loading bootstrap_code
static const char* read_bootstrap_line(lua_State *L, void *data, size_t *size)
const char* read_bootstrap_line(lua_State *L, void *data, size_t *size)
{
int& iLine = *reinterpret_cast<int*>(data);
++iLine;
@@ -290,11 +292,13 @@ static const char* read_bootstrap_line(lua_State *L, void *data, size_t *size)
}

template<typename Array>
static inline void push(lua_State *L, Array data)
inline void push(lua_State *L, Array data)
{
lua_pushlstring(L, reinterpret_cast<const char*>(data.data()), data.size());
}

} // namespace

int bootstrap_lua_resources(lua_State *L)
{
push(L, bootstrap_font_dat);
@@ -3,7 +3,7 @@

#include <array>

static constexpr std::array<uint16_t, 0x80> cp437_to_unicode_table {
constexpr std::array<uint16_t, 0x80> cp437_to_unicode_table {
/* 0x00 through 0x7F need no translation */
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,
0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
@@ -8,7 +8,7 @@
// 1st byte is between 0x81 and 0xFE (subtract 0x81 for table lookup)
// 2nd byte is between 0x40 and 0xFE (subtract 0x40 for table lookup)
// Unspecified codes are mapped to 0x003F
static constexpr std::array<std::array<uint16_t, 191>, 126> cp936_to_unicode_table {{
constexpr std::array<std::array<uint16_t, 191>, 126> cp936_to_unicode_table {{
{
0x4E02, 0x4E04, 0x4E05, 0x4E06, 0x4E0F, 0x4E12, 0x4E17, 0x4E1F,
0x4E20, 0x4E21, 0x4E23, 0x4E26, 0x4E29, 0x4E2E, 0x4E2F, 0x4E31,
@@ -28,6 +28,8 @@ SOFTWARE.
#include <algorithm>
#include <array>

namespace {

enum iso_volume_descriptor_type : uint8_t
{
vdt_privary_volume = 0x01,
@@ -74,6 +76,17 @@ constexpr std::array<const char, 10> vblk_0_filename {'V','B','L','K','-','0','.
/// size is 2048.
constexpr size_t min_sector_size = 2048;

template <class T> inline T read_native_int(const uint8_t *p)
{
// ISO 9660 commonly encodes multi-byte integers as little endian followed
// by big endian. Note that the first byte of iEndianness will be a zero on
// little endian systems, and a one on big endian.
static const uint16_t iEndianness = 0x0100;
return reinterpret_cast<const T*>(p)[*reinterpret_cast<const uint8_t*>(&iEndianness)];
}

} // namespace

iso_filesystem::iso_filesystem()
{
raw_file = nullptr;
@@ -110,15 +123,6 @@ void iso_filesystem::set_path_separator(char cSeparator)
path_seperator = cSeparator;
}

template <class T> static inline T read_native_int(const uint8_t *p)
{
// ISO 9660 commonly encodes multi-byte integers as little endian followed
// by big endian. Note that the first byte of iEndianness will be a zero on
// little endian systems, and a one on big endian.
static const uint16_t iEndianness = 0x0100;
return reinterpret_cast<const T*>(p)[*reinterpret_cast<const uint8_t*>(&iEndianness)];
}

bool iso_filesystem::initialise(FILE* fRawFile)
{
raw_file = fRawFile;
@@ -479,21 +483,23 @@ void iso_filesystem::set_error(const char* sFormat, ...)
va_end(a);
}

static int l_isofs_new(lua_State *L)
namespace {

int l_isofs_new(lua_State *L)
{
luaT_stdnew<iso_filesystem>(L, luaT_environindex, true);
return 1;
}

static int l_isofs_set_path_separator(lua_State *L)
int l_isofs_set_path_separator(lua_State *L)
{
iso_filesystem *pSelf = luaT_testuserdata<iso_filesystem>(L);
pSelf->set_path_separator(luaL_checkstring(L, 2)[0]);
lua_settop(L, 1);
return 1;
}

static int l_isofs_set_root(lua_State *L)
int l_isofs_set_root(lua_State *L)
{
iso_filesystem *pSelf = luaT_testuserdata<iso_filesystem>(L);
FILE *fIso = *luaT_testuserdata<FILE*>(L, 2);
@@ -512,7 +518,7 @@ static int l_isofs_set_root(lua_State *L)
}
}

static int l_isofs_read_contents(lua_State *L)
int l_isofs_read_contents(lua_State *L)
{
iso_filesystem *pSelf = luaT_testuserdata<iso_filesystem>(L);
const char* sFilename = luaL_checkstring(L, 2);
@@ -534,15 +540,15 @@ static int l_isofs_read_contents(lua_State *L)
return 1;
}

static void l_isofs_list_files_callback(void *p, const char* s)
void l_isofs_list_files_callback(void *p, const char* s)
{
lua_State *L = reinterpret_cast<lua_State*>(p);
lua_pushstring(L, s);
lua_pushboolean(L, 1);
lua_settable(L, 3);
}

static int l_isofs_list_files(lua_State *L)
int l_isofs_list_files(lua_State *L)
{
iso_filesystem *pSelf = luaT_testuserdata<iso_filesystem>(L);
const char* sPath = luaL_checkstring(L, 2);
@@ -552,6 +558,8 @@ static int l_isofs_list_files(lua_State *L)
return 1;
}

} // namespace

int luaopen_iso_fs(lua_State *L)
{
lua_settop(L, 1);
@@ -8,7 +8,10 @@
to this function has one parameter which is the RNC compressed data.
The return value is the decompressed data.
*/
static int l_decompress(lua_State *L)

namespace {

int l_decompress(lua_State *L)
{
size_t inlen;
const uint8_t* in = reinterpret_cast<const uint8_t*>(luaL_checklstring(L, 1, &inlen));
@@ -63,11 +66,13 @@ static int l_decompress(lua_State *L)
return 2;
}

static const std::vector<luaL_Reg> rnclib = {
const std::vector<luaL_Reg> rnclib = {
{"decompress", l_decompress},
{nullptr, nullptr}
};

} // namespace

int luaopen_rnc(lua_State *L)
{
luaT_register(L, "rnc", rnclib);
@@ -40,11 +40,15 @@ int luaopen_random(lua_State *L);
#endif
// End of config file checking

static inline void preload_lua_package(lua_State *L, const char* name, lua_CFunction fn)
namespace {

inline void preload_lua_package(lua_State *L, const char* name, lua_CFunction fn)
{
luaT_execute(L, std::string("package.preload.").append(name).append(" = ...").c_str(), fn);
}

} // namespace

int lua_main_no_eval(lua_State *L)
{
// assert(_VERSION == LUA_VERSION)
@@ -32,6 +32,8 @@ SOFTWARE.
#pragma warning(disable: 4996) // Disable "std::strcpy unsafe" warnings under MSVC
#endif

namespace {

enum persist_type
{
// LUA_TNIL = 0,
@@ -53,16 +55,19 @@ enum persist_type
PERSIST_TCOUNT, // must equal 16 (for compatibility)
};

static int l_writer_mt_index(lua_State *L);
int l_writer_mt_index(lua_State *L);

template <class T> static int l_crude_gc(lua_State *L)
template <class T>
int l_crude_gc(lua_State *L)
{
// This __gc metamethod does not verify that the given value is the correct
// type of userdata, or that the value is userdata at all.
reinterpret_cast<T*>(lua_touserdata(L, 1))->~T();
return 0;
};

} // namespace

//! Structure for loading multiple strings as a Lua chunk, avoiding concatenation
/*!
luaL_loadbuffer() is a good way to load a string as a Lua chunk. If there are
@@ -688,12 +693,16 @@ class lua_persist_basic_writer : public lua_persist_writer
bool had_error;
};

static int l_writer_mt_index(lua_State *L)
namespace {

int l_writer_mt_index(lua_State *L)
{
return reinterpret_cast<lua_persist_basic_writer*>(
lua_touserdata(L, luaT_upvalueindex(2)))->write_object_raw();
}

} // namespace

//! Basic implementation of depersistance interface
/*!
self - Instance of lua_persist_basic_reader allocated as a Lua userdata
@@ -1198,7 +1207,9 @@ class lua_persist_basic_reader : public lua_persist_reader
bool had_error;
};

static int l_dump_toplevel(lua_State *L)
namespace {

int l_dump_toplevel(lua_State *L)
{
luaL_checktype(L, 2, LUA_TTABLE);
lua_settop(L, 2);
@@ -1210,7 +1221,7 @@ static int l_dump_toplevel(lua_State *L)
return pWriter->finish();
}

static int l_load_toplevel(lua_State *L)
int l_load_toplevel(lua_State *L)
{
size_t iDataLength;
const uint8_t *pData = luaT_checkfile(L, 1, &iDataLength);
@@ -1236,7 +1247,7 @@ static int l_load_toplevel(lua_State *L)
}
}

static int calculate_line_number(const char *sStart, const char *sPosition)
int calculate_line_number(const char *sStart, const char *sPosition)
{
int iLine = 1;
for(; sStart != sPosition; ++sStart)
@@ -1260,7 +1271,7 @@ static int calculate_line_number(const char *sStart, const char *sPosition)
return iLine;
}

static const char* find_function_end(lua_State *L, const char* sStart)
const char* find_function_end(lua_State *L, const char* sStart)
{
const char* sEnd = sStart;
while(sEnd)
@@ -1285,7 +1296,7 @@ static const char* find_function_end(lua_State *L, const char* sStart)
return nullptr;
}

static int l_persist_dofile(lua_State *L)
int l_persist_dofile(lua_State *L)
{
const char *sFilename = luaL_checkstring(L, 1);
lua_settop(L, 1);
@@ -1428,21 +1439,23 @@ static int l_persist_dofile(lua_State *L)
return lua_gettop(L) - 1;
}

static int l_errcatch(lua_State *L)
int l_errcatch(lua_State *L)
{
// Dummy function for debugging - place a breakpoint on the following
// return statement to inspect the full C call stack when a Lua error
// occurs (assuming that l_errcatch is used as the error catch handler).
return 1;
}

static const std::vector<luaL_Reg> persist_lib = {
const std::vector<luaL_Reg> persist_lib = {
// Due to the various required upvalues, functions are registered
// manually, but we still need a dummy to pass to luaL_register.
{"errcatch", l_errcatch},
{nullptr, nullptr}
};

} // namespace

int luaopen_persist(lua_State *L)
{
luaT_register(L, "persist", persist_lib);

0 comments on commit 1ee6c90

Please sign in to comment.
You can’t perform that action at this time.