core/types, doc/conventions, builtin: NullptrCatch

This commit is contained in:
Perttu Ahola 2014-10-30 07:08:03 +02:00
parent 1c91de2ab1
commit 873213aae9
6 changed files with 82 additions and 70 deletions

View File

@ -272,6 +272,7 @@ struct CInstance: public ground_plane_lighting::Instance
void on_node_volume_updated(const voxelworld::NodeVolumeUpdated &event)
{
try {
if(!event.is_static_chunk)
return;
if(event.scene != m_scene_ref)
@ -327,6 +328,10 @@ struct CInstance: public ground_plane_lighting::Instance
}
//log_v(MODULE, "]");
});
} catch(NullptrCatch &e){
// Something was probably deleted or unloaded
log_v(MODULE, "NullptrCatch: %s", e.what());
}
}
void send_initial_sectors(int peer)

View File

@ -217,7 +217,7 @@ struct Module: public interface::Module, public main_context::Interface
{
Scene *scene = find_scene(ref);
if(!scene)
throw Exception("check_scene(): Scene not found");
throw NullptrCatch("check_scene(): Scene not found");
return scene;
}

View File

@ -94,6 +94,7 @@ struct CInstance: public worldgen::Instance
// queued but instead sectors get queued in the event queue.
void generate_next_section()
{
try {
if(!m_enabled)
throw Exception("generate_next_section(): Not enabled");
if(m_queued_sections.empty())
@ -110,6 +111,10 @@ struct CInstance: public worldgen::Instance
m_server->emit_event("worldgen:queue_modified",
new QueueModifiedEvent(m_scene_ref, m_queued_sections.size()));
} catch(NullptrCatch &e){
// Something was probably deleted or unloaded
log_v(MODULE, "NullptrCatch: %s", e.what());
}
}
// Interface

View File

@ -73,7 +73,7 @@ Non-exception throwing and exception-throwing methods
-----------------------------------------------------
- get_x: Returns nullptr or equivalent if not found
- find_x: Returns nullptr or equivalent if not found
- check_x: Throws exception if not found
- check_x: Throws NullptrCatch if not found
To check for nullptr returned by get_x() or find_x(), use the check() function
in core/types.h.

View File

@ -66,7 +66,9 @@ struct Exception: public std::exception {
}
};
#define DEFINE_EXCEPTION(name, base) struct name: public base \
{name(const ss_ &msg = ""): base(ss_()+#name+msg){}}
{name(const ss_ &msg): base(msg){}}
DEFINE_EXCEPTION(NullptrCatch, Exception);
static inline ss_ itos(int64_t i){
char buf[22];
@ -152,13 +154,13 @@ static inline cc_* cs(const T &v){
template<typename T>
static inline T* check(T *v){
if(v == nullptr)
throw Exception("check(): nullptr");
throw NullptrCatch("check(): nullptr");
return v;
}
template<typename T>
static inline const T* check(const T *v){
if(v == nullptr)
throw Exception("check(): nullptr");
throw NullptrCatch("check(): nullptr");
return v;
}

View File

@ -315,7 +315,7 @@ void ModuleThread::handle_direct_cb(
log_t(MODULE, "M[%s] ~direct_cb(): Executed",
cs(mc->info.name));
} catch(...){
log_v(MODULE, "M[%s] ~direct_cb() failed (exception)",
log_t(MODULE, "M[%s] ~direct_cb() failed (exception)",
cs(mc->info.name));
// direct_cb() exception should not directly shutdown the
// server; instead they are passed to the caller. Eventually