Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make code more explicit and safe #13

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
109c701
Replace NULL with nullptr
fel1x-developer Mar 11, 2024
2ff507c
Use default destructors instead of empty ones
fel1x-developer Mar 11, 2024
88a23e6
Add include guard
fel1x-developer Mar 11, 2024
ee54feb
Make member function const if possible
fel1x-developer Mar 11, 2024
03146c8
Replace throw with noexcept
fel1x-developer Mar 11, 2024
cbf4e05
Mark single-argument constructors explicit
fel1x-developer Mar 11, 2024
7a5b4af
Remove const-qualified parameters in the function declaration
fel1x-developer Mar 11, 2024
38f2488
Prefer using 'override' instead of 'virtual'
fel1x-developer Mar 11, 2024
d15b98a
Use range-based loops
fel1x-developer Mar 11, 2024
c582111
Use static_cast to avoid implicit conversion
fel1x-developer Mar 11, 2024
664a031
Use a braced initializer list for return type
fel1x-developer Mar 11, 2024
412bbe4
Use auto when initializing with a cast
fel1x-developer Mar 11, 2024
410b14c
Don't access static member through instance
fel1x-developer Mar 11, 2024
8414b69
Use make_shared()
fel1x-developer Mar 11, 2024
ccd96c5
Do not include unused headers
fel1x-developer Mar 11, 2024
74466f9
Remove redundant static keyword
fel1x-developer Mar 11, 2024
d790993
Use final when possible
fel1x-developer Mar 11, 2024
366b7a8
Make function parameters const
fel1x-developer Mar 11, 2024
ba2c74a
Remove redundant static_cast
fel1x-developer Mar 11, 2024
5494f7a
Use explicit constructor
fel1x-developer Mar 11, 2024
72f2a93
Don't access static member through instance
fel1x-developer Mar 11, 2024
e4c499f
Add [[nodiscard]] to some functions
fel1x-developer Mar 11, 2024
1cd22fc
Throw with runtime_err
fel1x-developer Mar 11, 2024
c241993
Correct return type
fel1x-developer Mar 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions c_gate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ lutok::state_c_gate::state_c_gate(state& state_) :
/// Destroying this object has no implications on the life cycle of the Lua
/// state. Only the corresponding state object controls when the Lua state is
/// closed.
lutok::state_c_gate::~state_c_gate(void)
{
}
lutok::state_c_gate::~state_c_gate(void) = default;


/// Creates a C++ state for a C Lua state.
Expand All @@ -62,15 +60,15 @@ lutok::state_c_gate::~state_c_gate(void)
lutok::state
lutok::state_c_gate::connect(lua_State* raw_state)
{
return state(static_cast< void* >(raw_state));
return state(raw_state);
}


/// Returns the C native Lua state.
///
/// \return A native lua_State object holding the Lua C API state.
lua_State*
lutok::state_c_gate::c_state(void)
lutok::state_c_gate::c_state(void) const
{
return static_cast< lua_State* >(_state.raw_state());
}
4 changes: 2 additions & 2 deletions c_gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ class state_c_gate {
state& _state;

public:
state_c_gate(state&);
explicit state_c_gate(state&);
~state_c_gate(void);

static state connect(lua_State*);

lua_State* c_state(void);
[[nodiscard]] lua_State* c_state(void) const;
};


Expand Down
18 changes: 7 additions & 11 deletions debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include <lutok/c_gate.hpp>
#include <lutok/debug.hpp>
#include <lutok/exceptions.hpp>
#include <lutok/state.ipp>


/// Internal implementation for lutok::debug.
Expand All @@ -51,9 +50,7 @@ lutok::debug::debug(void) :


/// Destructor.
lutok::debug::~debug(void)
{
}
lutok::debug::~debug(void) = default;


/// Wrapper around lua_getinfo.
Expand All @@ -64,7 +61,7 @@ lutok::debug::~debug(void)
/// \warning Terminates execution if there is not enough memory to manipulate
/// the Lua stack.
void
lutok::debug::get_info(state& s, const std::string& what_)
lutok::debug::get_info(state& s, const std::string& what_) const
{
lua_State* raw_state = state_c_gate(s).c_state();

Expand All @@ -78,7 +75,7 @@ lutok::debug::get_info(state& s, const std::string& what_)
/// \param s The Lua state.
/// \param level The second parameter to lua_getstack.
void
lutok::debug::get_stack(state& s, const int level)
lutok::debug::get_stack(state& s, const int level) const
{
lua_State* raw_state = state_c_gate(s).c_state();

Expand All @@ -102,7 +99,7 @@ lutok::debug::event(void) const
std::string
lutok::debug::name(void) const
{
assert(_pimpl->lua_debug.name != NULL);
assert(_pimpl->lua_debug.name != nullptr);
return _pimpl->lua_debug.name;
}

Expand All @@ -113,7 +110,7 @@ lutok::debug::name(void) const
std::string
lutok::debug::name_what(void) const
{
assert(_pimpl->lua_debug.namewhat != NULL);
assert(_pimpl->lua_debug.namewhat != nullptr);
return _pimpl->lua_debug.namewhat;
}

Expand All @@ -124,7 +121,7 @@ lutok::debug::name_what(void) const
std::string
lutok::debug::what(void) const
{
assert(_pimpl->lua_debug.what != NULL);
assert(_pimpl->lua_debug.what != nullptr);
return _pimpl->lua_debug.what;
}

Expand All @@ -135,7 +132,7 @@ lutok::debug::what(void) const
std::string
lutok::debug::source(void) const
{
assert(_pimpl->lua_debug.source != NULL);
assert(_pimpl->lua_debug.source != nullptr);
return _pimpl->lua_debug.source;
}

Expand Down Expand Up @@ -187,6 +184,5 @@ lutok::debug::last_line_defined(void) const
std::string
lutok::debug::short_src(void) const
{
assert(_pimpl->lua_debug.short_src != NULL);
return _pimpl->lua_debug.short_src;
}
24 changes: 12 additions & 12 deletions debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ class debug {
debug(void);
~debug(void);

void get_info(state&, const std::string&);
void get_stack(state&, const int);
void get_info(state&, const std::string&) const;
void get_stack(state&, int) const;

int event(void) const;
std::string name(void) const;
std::string name_what(void) const;
std::string what(void) const;
std::string source(void) const;
int current_line(void) const;
int n_ups(void) const;
int line_defined(void) const;
int last_line_defined(void) const;
std::string short_src(void) const;
[[nodiscard]] int event(void) const;
[[nodiscard]] std::string name(void) const;
[[nodiscard]] std::string name_what(void) const;
[[nodiscard]] std::string what(void) const;
[[nodiscard]] std::string source(void) const;
[[nodiscard]] int current_line(void) const;
[[nodiscard]] int n_ups(void) const;
[[nodiscard]] int line_defined(void) const;
[[nodiscard]] int last_line_defined(void) const;
[[nodiscard]] std::string short_src(void) const;
};


Expand Down
12 changes: 3 additions & 9 deletions exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ lutok::error::error(const std::string& message) :


/// Destructor for the error.
lutok::error::~error(void) throw()
{
}
lutok::error::~error(void) noexcept = default;


/// Constructs a new error.
Expand All @@ -63,9 +61,7 @@ lutok::api_error::api_error(const std::string& api_function_,


/// Destructor for the error.
lutok::api_error::~api_error(void) throw()
{
}
lutok::api_error::~api_error(void) noexcept = default;


/// Constructs a new api_error with the message on the top of the Lua stack.
Expand Down Expand Up @@ -111,9 +107,7 @@ lutok::file_not_found_error::file_not_found_error(


/// Destructor for the error.
lutok::file_not_found_error::~file_not_found_error(void) throw()
{
}
lutok::file_not_found_error::~file_not_found_error(void) noexcept = default;


/// Gets the name of the file that could not be found.
Expand Down
14 changes: 7 additions & 7 deletions exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,35 @@ class state;
class error : public std::runtime_error {
public:
explicit error(const std::string&);
virtual ~error(void) throw();
~error(void) noexcept override;
};


/// Exception for errors raised by the Lua API library.
class api_error : public error {
class api_error final : public error {
/// Name of the Lua C API function that caused the error.
std::string _api_function;

public:
explicit api_error(const std::string&, const std::string&);
virtual ~api_error(void) throw();
~api_error(void) noexcept override;

static api_error from_stack(state&, const std::string&);

const std::string& api_function(void) const;
[[nodiscard]] const std::string& api_function(void) const;
};


/// File not found error.
class file_not_found_error : public error {
class file_not_found_error final : public error {
/// Name of the not-found file.
std::string _filename;

public:
explicit file_not_found_error(const std::string&);
virtual ~file_not_found_error(void) throw();
~file_not_found_error(void) noexcept override;

const std::string& filename(void) const;
[[nodiscard]] const std::string& filename(void) const;
};


Expand Down
5 changes: 5 additions & 0 deletions include/lutok/c_gate.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#ifndef C_GATE_HPP
#define C_GATE_HPP

#include "../../c_gate.hpp"

#endif // C_GATE_HPP
5 changes: 5 additions & 0 deletions include/lutok/debug.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#ifndef DEBUG_HPP
#define DEBUG_HPP

#include "../../debug.hpp"

#endif // DEBUG_HPP
5 changes: 5 additions & 0 deletions include/lutok/exceptions.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#ifndef EXCEPTIONS_HPP
#define EXCEPTIONS_HPP

#include "../../exceptions.hpp"

#endif // EXCEPTIONS_HPP
5 changes: 5 additions & 0 deletions include/lutok/state.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#ifndef STATE_HPP
#define STATE_HPP

#include "../../state.hpp"

#endif // STATE_HPP
5 changes: 5 additions & 0 deletions include/lutok/state.ipp
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#ifndef STATE_IPP
#define STATE_IPP

#include "../../state.ipp"

#endif // STATE_IPP
7 changes: 3 additions & 4 deletions operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ lutok::create_module(state& s, const std::string& name,
{
stack_cleaner cleaner(s);
s.new_table();
for (std::map< std::string, cxx_function >::const_iterator
iter = members.begin(); iter != members.end(); iter++) {
s.push_string((*iter).first);
s.push_cxx_function((*iter).second);
for (const auto & member : members) {
s.push_string(member.first);
s.push_cxx_function(member.second);
s.set_table(-3);
}
s.set_global(name);
Expand Down
10 changes: 5 additions & 5 deletions operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ namespace lutok {

void create_module(state&, const std::string&,
const std::map< std::string, cxx_function >&);
unsigned int do_file(state&, const std::string&, const int, const int,
const int);
unsigned int do_string(state&, const std::string&, const int, const int,
const int);
void eval(state&, const std::string&, const int);
unsigned int do_file(state&, const std::string&, int, int,
int);
unsigned int do_string(state&, const std::string&, int, int,
int);
void eval(state&, const std::string&, int);


} // namespace lutok
Expand Down
5 changes: 2 additions & 3 deletions stack_cleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <cassert>
#include <cstring>

#include "stack_cleaner.hpp"
#include "state.ipp"
Expand Down Expand Up @@ -75,7 +74,7 @@ lutok::stack_cleaner::~stack_cleaner(void)
assert(current_depth >= _pimpl->original_depth);
const unsigned int diff = current_depth - _pimpl->original_depth;
if (diff > 0)
_pimpl->state_ref.pop(diff);
_pimpl->state_ref.pop(static_cast<int>(diff));
}


Expand All @@ -85,7 +84,7 @@ lutok::stack_cleaner::~stack_cleaner(void)
/// elements that are currently in the stack will not be touched during
/// destruction when the function is called.
void
lutok::stack_cleaner::forget(void)
lutok::stack_cleaner::forget(void) const
{
_pimpl->original_depth = _pimpl->state_ref.get_top();
}
4 changes: 2 additions & 2 deletions stack_cleaner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ class stack_cleaner {
stack_cleaner& operator=(const stack_cleaner&);

public:
stack_cleaner(state&);
explicit stack_cleaner(state&);
~stack_cleaner(void);

void forget(void);
void forget(void) const;
};


Expand Down
Loading