sgpemv2/plugins/pyloader/src/sgpem.i

245 lines
6.7 KiB
OpenEdge ABL
Raw Normal View History

%include "std_vector.i"
%module sgpem
%{
#include "policy.hh"
#include "policy_parameters.hh"
#include "process.hh"
#include "ready_queue.hh"
#include "schedulable.hh"
#include "scheduler.hh"
#include "thread.hh"
%}
/* NOTE : passing Unicode strings to C++ methods calling them
* from Python results in a SIGSEGV. You've been warned!
* (Investigate if this can be fixed, else please report it in
* the sgpem user manual)
*/
/* Due to the relatively new support for namespaces in SWIG,
* make sure to include the full visibility signature when
* returning / passing parameters from / to functions with
* objects different to the one you're declaring.
*/
/* FIXME : look up into Swig manual the management of
* STL exceptions and write wrappers for them.
*/
namespace std {
class exception {
public:
virtual const char* what() const throw();
private:
exception();
};
class runtime_error : public std::exception {
public:
virtual const char* what() const throw();
private:
runtime_error();
};
}
// (See last comment): Wrap the above exceptions in the SWIG way.
namespace sgpem {
/** Don't get worried, order is not important! */
enum policy_sorts_type { policy_sorts_threads, policy_sorts_processes };
class Policy {
public:
virtual ~Policy() = 0;
sgpem::PolicyParameters& get_parameters();
};
// --------------------------------------------
class PolicyParametersException : public std::runtime_error {
public:
PolicyParametersException(char* msg);
%rename (__str__) what;
virtual const char* what();
}; //~ class PolicyParametersException
// --------------------------------------------
class PolicyParameters
{
public:
//methods to CREATE PARAMETERS
// (rewrapped correctly for SWIG usage)
%ignore register_int(const Glib::ustring&, const int&,
const int&, const bool&, const int&);
%ignore register_float(const Glib::ustring&, const float&,
const float&, const bool&, const float&);
%ignore register_string(const Glib::ustring&, const bool&,
const char*);
%extend {
void register_int(const char* name,
const int& lower_bound,
const int& upper_bound,
const bool& required,
const int& default_value = 0)
{
self->register_int(name, lower_bound, upper_bound,
required, default_value);
}
void register_float(const char* name,
const float& lower_bound,
const float& upper_bound,
const bool& required,
const float& default_value = 0.0f)
{
self->register_float(name, lower_bound, upper_bound,
required, default_value);
}
void register_string(const char* name,
const bool& required,
const char* default_value = "")
{
self->register_string(name, required, default_value);
}
}
//methods to SET the VALUE of PARAMETERS
// (rewrapped correctly for SWIG usage)
%ignore set_int(const Glib::ustring&, const int&);
%ignore set_float(const Glib::ustring&, const float&);
%ignore set_string(const Glib::ustring&, const Glib::ustring&);
%extend {
bool set_int(const char* name, const int& value)
{ return self->set_int(name, value); }
bool set_float(const char* name, const float& value)
{ return self->set_float(name, value); }
bool set_string(const char* name, const char* value)
{ return self->set_string(name, value); }
}
//methods to GET the VALUE of PARAMETERS
// (rewrapped correctly for SWIG usage)
%ignore get_int(const Glib::ustring&) const;
%ignore get_float(const Glib::ustring&) const;
%ignore get_string(const Glib::ustring&) const;
%extend {
int get_int(const char* name) const
{ return self->get_int(name); }
float get_float(const char* name) const
{ return self->get_float(name); }
const char* get_string(const char* name) const
{ return self->get_string(name).c_str(); }
}
}; //~ class PolicyParameters
// --------------------------------------------
class Schedulable
{
public:
virtual ~Schedulable() = 0;
enum state
{
state_running = 1<<0,
state_ready = 1<<1,
state_blocked = 1<<2,
state_future = 1<<3,
state_terminated = 1<<4
};
virtual unsigned int get_arrival_time() const = 0;
virtual unsigned int get_remaining_time() const = 0;
virtual int get_base_priority() const = 0;
virtual int get_current_priority() const = 0;
virtual unsigned int get_total_cpu_time() const = 0;
virtual state get_state() const = 0;
%ignore Schedulable::get_name() const;
%extend {
const char* get_name() const
{ return self->get_name().c_str(); }
}
}; //~ class Schedulable
// --------------------------------------------
// Instantiate a Thread* vector for usage with Process
// std::out_of_range should be automatically defined,
// see Swig manual at the STL Vector <20>.
namespace std {
%template(ThreadVector) vector<Thread*>;
}
class Process : public virtual Schedulable
{
public:
virtual std::vector<Thread*> get_threads();
} //~ class Process
class Thread : public virtual Schedulable
{
public:
virtual Process& get_process();
} //~ class Thread
// --------------------------------------------
class ReadyQueue
{
public:
typedef unsigned int position;
typedef unsigned int size_t;
size_t size() const;
// Dynamic cast to Process or to Thread so
// that Python code sees the extra-methods
%typename(out) sgpem::Schedulable*;
{
// OMG, Ponies!!
Process* proc;
Thread* thread;
if((proc = dynamic_cast<Process*>($1)) != NULL)
$result = SWIG_NewPointerObj(SWIG_as_voidptr(proc),
SWIGTYPE_p_sgpem__Process, 0 | 0 );
else if((thread = dynamic_cast<Thread*>($1)) != NULL)
$result = SWIG_NewPointerObj(SWIG_as_voidptr(thread),
SWIGTYPE_p_sgpem__Thread, 0 | 0 );
else // Fall back to Schedulable* if no dynamic_cast went well:
$result = SWIG_NewPointerObj(SWIG_as_voidptr(thread),
$1_descriptor, 0 | 0 );
}
sgpem::Schedulable* get_item_at(position index);
%typename(out) sgpem::Schedulable*;
void swap(position a, position b) throw(std::out_of_range);
private:
// Avoid instantiation and copy
ReadyQueue();
ReadyQueue(const ReadyQueue&);
ReadyQueue& operator=(const ReadyQueue&);
~ReadyQueue();
}; //~ class ReadyQueue
// ---------------------------------------------
class Scheduler {
public:
sgpem::Policy& get_policy();
static sgpem::Scheduler& get_instance();
sgpem::ReadyQueue* get_ready_queue();
private:
Scheduler();
~Scheduler();
};
} //~ namespace sgpem