- Add down_cast<> to safely checking dynamic_casts when in development. Be sure to read its documentation before use\!

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@1188 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-09-16 11:21:06 +00:00
parent f1954c9f71
commit deaf0702e5
3 changed files with 178 additions and 16 deletions

View File

@ -436,6 +436,7 @@ EXTRA_DIST += $(glade_DATA)
templates_HEADERS += \ templates_HEADERS += \
src/backend/sgpemv2/templates/deletor.tcc \ src/backend/sgpemv2/templates/deletor.tcc \
src/backend/sgpemv2/templates/down_cast.tcc \
src/backend/sgpemv2/templates/parameter.tcc \ src/backend/sgpemv2/templates/parameter.tcc \
src/backend/sgpemv2/templates/sequences.tcc \ src/backend/sgpemv2/templates/sequences.tcc \
src/backend/sgpemv2/templates/singleton.hh \ src/backend/sgpemv2/templates/singleton.hh \

View File

@ -32,9 +32,11 @@
#include "static_request.hh" #include "static_request.hh"
#include "static_sub_request.hh" #include "static_sub_request.hh"
#include <sgpemv2/history_observer.hh>
#include "concrete_history.hh" #include "concrete_history.hh"
#include <sgpemv2/history_observer.hh>
#include <sgpemv2/templates/down_cast.tcc>
#include <sgpemv2/templates/deletor.tcc> #include <sgpemv2/templates/deletor.tcc>
#include <sgpemv2/templates/smartp.tcc> #include <sgpemv2/templates/smartp.tcc>
@ -170,7 +172,7 @@ ConcreteHistory::remove(resource_key_t resource_key)
typedef ConcreteEnvironment::Processes::iterator ProcIt; typedef ConcreteEnvironment::Processes::iterator ProcIt;
for (ProcIt it1 = processes.begin(); it1 != processes.end(); it1++) for (ProcIt it1 = processes.begin(); it1 != processes.end(); it1++)
{ {
Threads& threads = dynamic_cast<DynamicProcess&>(**it1).get_dynamic_threads(); Threads& threads = down_cast<DynamicProcess&>(**it1).get_dynamic_threads();
for (Threads::iterator it2 = threads.begin(); it2 != threads.end(); it2++) for (Threads::iterator it2 = threads.begin(); it2 != threads.end(); it2++)
{ {
Requests& reqs = (*it2)->get_dynamic_requests(); Requests& reqs = (*it2)->get_dynamic_requests();
@ -212,7 +214,7 @@ ConcreteHistory::remove(Process& process)
void void
ConcreteHistory::remove(Thread& thread) ConcreteHistory::remove(Thread& thread)
{ {
DynamicThread& dyn_thr = dynamic_cast<DynamicThread&>(thread); DynamicThread& dyn_thr = down_cast<DynamicThread&>(thread);
// Pay attention that initial isn't deleted by reset() // Pay attention that initial isn't deleted by reset()
ConcreteEnvironment& initial = *_snapshots.front(); ConcreteEnvironment& initial = *_snapshots.front();
@ -223,7 +225,7 @@ ConcreteHistory::remove(Thread& thread)
if (found == NULL) if (found == NULL)
return; // not found, just return. return; // not found, just return.
DynamicProcess& dynamic_found = dynamic_cast<DynamicProcess&>(*found); DynamicProcess& dynamic_found = down_cast<DynamicProcess&>(*found);
bool removed = deep_remove<DynamicThread>(dynamic_found.get_dynamic_threads(), dyn_thr); bool removed = deep_remove<DynamicThread>(dynamic_found.get_dynamic_threads(), dyn_thr);
if (removed) if (removed)
@ -234,7 +236,7 @@ ConcreteHistory::remove(Thread& thread)
void void
ConcreteHistory::remove(Request& request) ConcreteHistory::remove(Request& request)
{ {
DynamicRequest& dyn_req = dynamic_cast<DynamicRequest&>(request); DynamicRequest& dyn_req = down_cast<DynamicRequest&>(request);
DynamicThread& dyn_thr = dyn_req.get_thread(); DynamicThread& dyn_thr = dyn_req.get_thread();
DynamicProcess& dyn_proc = dyn_thr.get_process(); DynamicProcess& dyn_proc = dyn_thr.get_process();
@ -243,7 +245,7 @@ ConcreteHistory::remove(Request& request)
ConcreteEnvironment::Processes& processes = initial.get_processes(); ConcreteEnvironment::Processes& processes = initial.get_processes();
Process* proc_ref = deep_find<Process>(processes, dyn_proc); Process* proc_ref = deep_find<Process>(processes, dyn_proc);
DynamicProcess* dyn_proc_ref = dynamic_cast<DynamicProcess*>(proc_ref); DynamicProcess* dyn_proc_ref = down_cast<DynamicProcess*>(proc_ref);
if (dyn_proc_ref == NULL) if (dyn_proc_ref == NULL)
return; // not found, just return. return; // not found, just return.
DynamicThread* thr_ref = deep_find<DynamicThread>(dyn_proc_ref->get_dynamic_threads(), dyn_thr); DynamicThread* thr_ref = deep_find<DynamicThread>(dyn_proc_ref->get_dynamic_threads(), dyn_thr);
@ -263,7 +265,7 @@ ConcreteHistory::remove(SubRequest& subrequest)
// this function makes one relevant assumption: // this function makes one relevant assumption:
// the initial environment does contain empty request queues only. // the initial environment does contain empty request queues only.
DynamicSubRequest& dyn_sub = dynamic_cast<DynamicSubRequest&>(subrequest); DynamicSubRequest& dyn_sub = down_cast<DynamicSubRequest&>(subrequest);
DynamicRequest& dyn_req = dyn_sub.get_request(); DynamicRequest& dyn_req = dyn_sub.get_request();
DynamicThread& dyn_thr = dyn_req.get_thread(); DynamicThread& dyn_thr = dyn_req.get_thread();
DynamicProcess& dyn_proc = dyn_thr.get_process(); DynamicProcess& dyn_proc = dyn_thr.get_process();
@ -273,7 +275,7 @@ ConcreteHistory::remove(SubRequest& subrequest)
ConcreteEnvironment::Processes& processes = initial.get_processes(); ConcreteEnvironment::Processes& processes = initial.get_processes();
Process* proc_ref = deep_find<Process>(processes, dyn_proc); Process* proc_ref = deep_find<Process>(processes, dyn_proc);
DynamicProcess* dyn_proc_ref = dynamic_cast<DynamicProcess*>(proc_ref); DynamicProcess* dyn_proc_ref = down_cast<DynamicProcess*>(proc_ref);
if (dyn_proc_ref == NULL) if (dyn_proc_ref == NULL)
return; // not found, just return. return; // not found, just return.
DynamicThread* thr_ref = deep_find<DynamicThread>(dyn_proc_ref->get_dynamic_threads(), dyn_thr); DynamicThread* thr_ref = deep_find<DynamicThread>(dyn_proc_ref->get_dynamic_threads(), dyn_thr);
@ -346,7 +348,7 @@ ConcreteHistory::edit_resource(Resource& resource,
{ {
// And preemptable and availability?? FIXME! // And preemptable and availability?? FIXME!
DynamicResource* res = dynamic_cast<DynamicResource*>(&resource); DynamicResource* res = down_cast<DynamicResource*>(&resource);
StaticResource& core = res->get_core(); StaticResource& core = res->get_core();
core.set_name(name); core.set_name(name);
core.set_places(places); core.set_places(places);
@ -379,7 +381,7 @@ ConcreteHistory::edit_process(Process& process,
time_t arrival_time, time_t arrival_time,
prio_t base_priority) prio_t base_priority)
{ {
DynamicProcess* proc = dynamic_cast<DynamicProcess*>(&process); DynamicProcess* proc = down_cast<DynamicProcess*>(&process);
StaticProcess& core = proc->get_core(); StaticProcess& core = proc->get_core();
core.set_name(name); core.set_name(name);
core.set_arrival_time(arrival_time); core.set_arrival_time(arrival_time);
@ -399,7 +401,7 @@ ConcreteHistory::add_thread(const Glib::ustring& name,
{ {
reset(false); reset(false);
DynamicProcess& parent_process = dynamic_cast<DynamicProcess&>(parent); DynamicProcess& parent_process = down_cast<DynamicProcess&>(parent);
StaticProcess& parent_core = parent_process.get_core(); StaticProcess& parent_core = parent_process.get_core();
StaticThread* core = new StaticThread(name, parent_core, cpu_time, arrival_time, base_priority); StaticThread* core = new StaticThread(name, parent_core, cpu_time, arrival_time, base_priority);
DynamicThread* thread = new DynamicThread(core, &parent_process); DynamicThread* thread = new DynamicThread(core, &parent_process);
@ -415,7 +417,7 @@ ConcreteHistory::edit_thread(Thread& thread,
time_t arrival_time, time_t arrival_time,
prio_t base_priority) prio_t base_priority)
{ {
DynamicThread* thre = dynamic_cast<DynamicThread*>(&thread); DynamicThread* thre = down_cast<DynamicThread*>(&thread);
StaticThread& core = thre->get_core(); StaticThread& core = thre->get_core();
core.set_name(name); core.set_name(name);
core.set_total_cpu_time(cpu_time); core.set_total_cpu_time(cpu_time);
@ -431,7 +433,7 @@ ConcreteHistory::add_request(Thread& owner,
{ {
reset(false); reset(false);
DynamicThread& dyn_owner = dynamic_cast<DynamicThread&>(owner); DynamicThread& dyn_owner = down_cast<DynamicThread&>(owner);
StaticThread& owner_core = dyn_owner.get_core(); StaticThread& owner_core = dyn_owner.get_core();
StaticRequest* core = new StaticRequest(&owner_core, instant); StaticRequest* core = new StaticRequest(&owner_core, instant);
@ -447,7 +449,7 @@ void
ConcreteHistory::edit_request(Request& request, ConcreteHistory::edit_request(Request& request,
time_t instant) time_t instant)
{ {
DynamicRequest* req = dynamic_cast<DynamicRequest*>(&request); DynamicRequest* req = down_cast<DynamicRequest*>(&request);
StaticRequest& core = req->get_core(); StaticRequest& core = req->get_core();
core.set_instant(instant); core.set_instant(instant);
@ -462,7 +464,7 @@ ConcreteHistory::add_subrequest(Request& request,
{ {
reset(false); reset(false);
DynamicRequest& dyn_request = dynamic_cast<DynamicRequest&>(request); DynamicRequest& dyn_request = down_cast<DynamicRequest&>(request);
StaticSubRequest* core = new StaticSubRequest(resource_key, duration); StaticSubRequest* core = new StaticSubRequest(resource_key, duration);
DynamicSubRequest* subreq = new DynamicSubRequest(core, &dyn_request); DynamicSubRequest* subreq = new DynamicSubRequest(core, &dyn_request);
@ -478,7 +480,7 @@ ConcreteHistory::edit_subrequest(SubRequest& subrequest,
resource_key_t resource_key, resource_key_t resource_key,
time_t duration) time_t duration)
{ {
DynamicSubRequest* sreq = dynamic_cast<DynamicSubRequest*>(&subrequest); DynamicSubRequest* sreq = down_cast<DynamicSubRequest*>(&subrequest);
StaticSubRequest& core = sreq->get_core(); StaticSubRequest& core = sreq->get_core();
core.set_resource_key(resource_key); core.set_resource_key(resource_key);
core.set_length(duration); core.set_length(duration);

View File

@ -0,0 +1,159 @@
// sgpemv2/templates/down_cast.tcc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef DOWN_CAST_TCC
#define DOWN_CAST_TCC 1
#include <functional>
#ifndef NDEBUG
#include <sys/types.h>
#include <iostream>
#include <signal.h>
#include <unistd.h>
#endif
namespace sgpem
{
/** \brief A debug functor that wraps C++ dynamic_cast
*
* Since we may want to debug exceptions easily before
* the stack becomes unwinded, this functor wraps
* the normal C++ dynamic_cast<> for references and:
*
* - if the macro NDEBUG isn't defined, and the
* dynamic_cast<> throws a std::bad_cast exception,
* suspend the process so that the debugger can
* immediately get a backtrace;
* - else, use a static_cast. This is meant for
* release code that has been \strong extensively
* tested.
*
* This template comes in two partial specializations:
* one for references, and one for pointers.
*
* Note that you shouldn't use \strong always this
* template for all your downcast, but only for those
* you don't want to test their return value because
* you assume it's correct.
*/
template<typename To>
struct _down_cast
{
// Uninstantiable for normal objects.
// Partial specializations for references and pointers
// will follow.
// We use a functor, and not a normal template function,
// because in this way the parameter of operator()
// is automatically deduced by the compiler (else we
// would have to provide two template parameters), and
// we've an extra type-check that we aren't using
// normal objects as template parameters, but
// just pointers or references.
};
// --------------------------------------
// Specialization for references:
template<typename To>
struct _down_cast<To&>
{
template<typename From>
inline To& operator()(From& obj) const
{
#ifndef NDEBUG
try
{
return dynamic_cast<To&>(obj);
}
catch(const std::bad_cast& e)
{
pid_t me = getpid();
std::clog << "DEBUG: st8ad_cast exception. "
<< "Guru Meditation #81310005.48454C50." << std::endl
<< " ** " << e.what() << std::endl
<< "Process [" << me << "] stopped." << std::endl;
kill(me, SIGSTOP);
throw;
}
#else
return static_cast<To&>(obj);
#endif
}
};
// --------------------------------------
// Specialization for pointers:
template<typename To>
struct _down_cast<To*>
{
template<typename From>
inline To* operator()(From* obj) const
{
#ifndef NDEBUG
To* check_ptr = dynamic_cast<To*>(obj);
if(check_ptr == NULL)
{
pid_t me = getpid();
std::clog << "DEBUG: dynamic_cast<> returned null pointer. "
<< "Guru Meditation #81310005.48454C50." << std::endl
<< "Process [" << me << "] stopped." << std::endl;
kill(me, SIGSTOP);
throw;
}
return check_ptr;
#else
return dynamic_cast<To*>(obj);
#endif
}
};
/** \brief Helper function to check a downcast for references
*
* See ::_down_cast for a more extensive documentation. This
* is just a wrapper to provide a more natural interface to
* the user
*/
template<typename To, typename From>
inline To down_cast(From& obj)
{
return _down_cast<To>()(obj);
}
/** \brief Helper function to check a downcast for pointers
*
* See ::_down_cast for a more extensive documentation. This
* is just a wrapper to provide a more natural interface to
* the user
*/
template<typename To, typename From>
inline To down_cast(From* const obj)
{
return _down_cast<To>()(obj);
}
} //~ namespace sgpem
#endif //~ DOWN_CAST_TCC