// ---------------------------------------------------------------------------
// - Selector.cpp -
// - afnix:sio module - i/o select class implementation -
// ---------------------------------------------------------------------------
// - This program is free software; you can redistribute it and/or modify -
// - it provided that this copyright notice is kept intact. -
// - -
// - This program 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. In no event shall -
// - the copyright holder be liable for any direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software. -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2007 amaury darsch -
// ---------------------------------------------------------------------------
#include "Interp.hpp"
#include "Integer.hpp"
#include "QuarkZone.hpp"
#include "Selector.hpp"
#include "csio.hpp"
#include "cerr.hpp"
namespace afnix {
// -------------------------------------------------------------------------
// - class section -
// -------------------------------------------------------------------------
// create an empty selector
Selector::Selector (void) {
p_handle = c_shnew ();
}
// destroy this selector
Selector::~Selector (void) {
c_shfree (p_handle);
}
// return the class name
String Selector::repr (void) const{
return "Selector";
}
// add a new input stream
void Selector::add (Input* is) {
if (is == nilp) return;
wrlock ();
try {
if (d_isv.exists (is) == false) {
d_isv.append (is);
c_shiadd (p_handle, is->getsid ());
}
unlock ();
} catch (...) {
unlock ();
throw;
}
}
// add a new output stream
void Selector::add (Output* os) {
if (os == nilp) return;
wrlock ();
try {
if (d_osv.exists (os) == false) {
d_osv.append (os);
c_shoadd (p_handle, os->getsid ());
}
unlock ();
} catch (...) {
unlock ();
throw;
}
}
// return the number of input streams
long Selector::ilength (void) const {
rdlock ();
long result = d_isv.length ();
unlock ();
return result;
}
// return the number of output streams
long Selector::olength (void) const {
rdlock ();
long result = d_osv.length ();
unlock ();
return result;
}
// return an input stream by index
Input* Selector::iget (const long index) const {
rdlock ();
try {
Input* result = dynamic_cast (d_isv.get (index));
unlock ();
return result;
} catch (...) {
unlock ();
throw;
}
}
// return an output stream by index
Output* Selector::oget (const long index) const {
rdlock ();
try {
Output* result = dynamic_cast