// ---------------------------------------------------------------------------
// - Edge.cpp -
// - afnix:gfx module - graph edge 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 "Edge.hpp"
#include "Vertex.hpp"
#include "Vector.hpp"
#include "Integer.hpp"
#include "Runnable.hpp"
#include "QuarkZone.hpp"
#include "Exception.hpp"
namespace afnix {
// -------------------------------------------------------------------------
// - class section -
// -------------------------------------------------------------------------
// create a new edge
Edge::Edge (void) {
d_mrk = false;
p_clo = nilp;
}
// create a edge with a client object
Edge::Edge (Object* clo) {
d_mrk = false;
Object::iref (p_clo = clo);
}
// destroy this edge
Edge::~Edge (void) {
Object::dref (p_clo);
}
// return the edge class name
String Edge::repr (void) const {
return "Edge";
}
// make this edge a shared object
void Edge::mksho (void) {
if (p_shared != nilp) return;
Object::mksho ();
d_vos.mksho ();
if (p_clo != nilp) p_clo->mksho ();
}
// reset the edge
void Edge::reset (void) {
wrlock ();
d_mrk = false;
unlock ();
}
// return the edge cardinality
long Edge::cardinality (void) const {
rdlock ();
long result = d_vos.length ();
unlock ();
return result;
}
// add an vertex to this edge
void Edge::add (Vertex* vertex) {
if (vertex == nilp) return;
wrlock ();
if (d_vos.exists (vertex) == false) {
d_vos.add (vertex);
vertex->add (this);
}
unlock ();
}
// get an vertex by index
Vertex* Edge::get (const long index) const {
rdlock ();
try {
Vertex* result = dynamic_cast <Vertex*> (d_vos.get (index));
unlock ();
return result;
} catch (...) {
unlock ();
throw;
}
}
// set the edge client object
void Edge::setclo (Object* clo) {
wrlock ();
if (p_clo != clo) {
Object::dref (p_clo);
Object::iref (p_clo = clo);
if (p_shared != nilp) p_clo->mksho ();
}
unlock ();
}
// get the edge client object
Object* Edge::getclo (void) const {
rdlock ();
Object* clo = p_clo;
unlock ();
return clo;
}
// -------------------------------------------------------------------------
// - object section -
// -------------------------------------------------------------------------
// the quark zone
static const long QUARK_ZONE_LENGTH = 6;
static QuarkZone zone (QUARK_ZONE_LENGTH);
// the edge supported quarks
static const long QUARK_ADD = zone.intern ("add");
static const long QUARK_GET = zone.intern ("get");
static const long QUARK_CARD = zone.intern ("cardinality");
static const long QUARK_RESET = zone.intern ("reset");
static const long QUARK_GETCLO = zone.intern ("get-client");
static const long QUARK_SETCLO = zone.intern ("set-client");
// create a new object in a generic way
Object* Edge::mknew (Vector* argv) {
long argc = (argv == nilp) ? 0 : argv->length ();
if (argc == 0) return new Edge;
if (argc == 1) return new Edge (argv->get (0));
throw Exception ("argument-error", "too many arguments to create edge");
}
// return true if the given quark is defined
bool Edge::isquark (const long quark, const bool hflg) const {
rdlock ();
if (zone.exists (quark) == true) {
unlock ();
return true;
}
bool result = hflg ? Object::isquark (quark, hflg) : false;
unlock ();
return result;
}
// apply this object with a set of arguments and a quark
Object* Edge::apply (Runnable* robj, Nameset* nset, const long quark,
Vector* argv) {
// get the number of arguments
long argc = (argv == nilp) ? 0 : argv->length ();
// dispatch 0 argument
if (argc == 0) {
if (quark == QUARK_CARD) return new Integer (cardinality ());
if (quark == QUARK_GETCLO) {
rdlock ();
Object* result = getclo ();
robj->post (result);
unlock ();
return result;
}
if (quark == QUARK_RESET) {
reset ();
return nilp;
}
}
// dispatch 1 argument
if (argc == 1) {
if (quark == QUARK_ADD) {
Vertex* vertex = dynamic_cast <Vertex*> (argv->get (0));
if (vertex == nilp) throw Exception ("type-error", "invalid object");
add (vertex);
robj->post (vertex);
return vertex;
}
if (quark == QUARK_SETCLO) {
Object* result = argv->get (0);
setclo (result);
robj->post (result);
return result;
}
if (quark == QUARK_GET) {
long index = argv->getint (0);
rdlock ();
try {
Vertex* vertex = get (index);
robj->post (vertex);
unlock ();
return vertex;
} catch (...) {
unlock ();
throw;
}
}
}
// call the object method
return Object::apply (robj, nset, quark, argv);
}
}
syntax highlighted by Code2HTML, v. 0.9.1