1
// ---------------------------------------------------------------------------
3
// - afnix:csm service - cart set class implementation -
4
// ---------------------------------------------------------------------------
5
// - This program is free software; you can redistribute it and/or modify -
6
// - it provided that this copyright notice is kept intact. -
8
// - This program is distributed in the hope that it will be useful, but -
9
// - without any warranty; without even the implied warranty of -
10
// - merchantability or fitness for a particular purpose. In no event shall -
11
// - the copyright holder be liable for any direct, indirect, incidental or -
12
// - special damages arising in any way out of the use of this software. -
13
// ---------------------------------------------------------------------------
14
// - copyright (c) 1999-2015 amaury darsch -
15
// ---------------------------------------------------------------------------
18
#include "Boolean.hpp"
19
#include "Integer.hpp"
20
#include "CartSet.hpp"
21
#include "Runnable.hpp"
22
#include "QuarkZone.hpp"
23
#include "Exception.hpp"
27
// -------------------------------------------------------------------------
29
// -------------------------------------------------------------------------
31
// create an empty cart set
33
CartSet::CartSet (void) {
38
// create an anonymous cart set
40
CartSet::CartSet (const bool acf) {
45
// return the object class name
47
String CartSet::repr (void) const {
53
void CartSet::reset (void) {
65
// get the number of cart in the cart set
67
long CartSet::length (void) const {
70
long result = d_lht.length ();
79
// return true if the cart set is empty
81
bool CartSet::empty (void) const {
84
bool result = d_lht.empty ();
93
// check if a cart exists by rid
95
bool CartSet::exists (const String& rid) const {
98
bool result = d_lht.exists (rid);
107
// get a cart by index
109
Cart* CartSet::get (const long idx) const {
112
Cart* lco = dynamic_cast <Cart*> (d_lht.getobj (idx));
123
Cart* CartSet::lookup (const String& rid) const {
126
Cart* lco = dynamic_cast <Cart*> (d_lht.lookup (rid));
135
// add a rco in the cart set
137
t_long CartSet::addrco (const Rco& rco) {
141
String rid = rco.getrid ();
142
if (rid.isnil () == true) {
143
// check for valid anonymous
144
if (d_acf == false) {
145
throw Exception ("cart set-error", "invalid anonymous rco to add");
147
// add the rco and return
148
t_long result = d_aco.addrco (rco);
152
// check if the cart exists in the hashtable
153
Cart* lco = dynamic_cast <Cart*> (d_lht.get (rid));
155
lco = new Cart (rid);
156
d_lht.add (rid, lco);
158
// add the rco in the cart
159
t_long result = lco->addrco (rco);
168
// get a rco by rid and index
170
Rco CartSet::getrco (const String& rid, const t_long kid) const {
173
// check for anonymous
174
if (rid.isnil () == true) {
175
// check for valid anonymous
176
if (d_acf == false) {
177
throw Exception ("cart set-error", "invalid anonymous rco query");
179
Rco rco = d_aco.getrco (kid);
183
// find the cart by rid
184
Cart* lco = dynamic_cast <Cart*> (d_lht.lookup (rid));
186
throw Exception ("internal-error", "invalid nil cart in lookup");
188
// get the rco by index
189
Rco rco = lco->getrco (kid);
198
// -------------------------------------------------------------------------
199
// - object section -
200
// -------------------------------------------------------------------------
203
static const long QUARK_ZONE_LENGTH = 6;
204
static QuarkZone zone (QUARK_ZONE_LENGTH);
206
// the object supported quarks
207
static const long QUARK_GET = zone.intern ("get");
208
static const long QUARK_ADDRCO = zone.intern ("add-rco");
209
static const long QUARK_GETRCO = zone.intern ("get-rco");
210
static const long QUARK_LOOKUP = zone.intern ("lookup");
211
static const long QUARK_LENGTH = zone.intern ("length");
212
static const long QUARK_EMPTYP = zone.intern ("empty-p");
213
static const long QUARK_EXISTSP = zone.intern ("exists-p");
215
// create a new object in a generic way
217
Object* CartSet::mknew (Vector* argv) {
218
long argc = (argv == nilp) ? 0 : argv->length ();
219
// check for 0 argument
220
if (argc == 0) return new CartSet;
221
// check for 1 argument
223
bool acf = argv->getbool (0);
224
return new CartSet (acf);
226
throw Exception ("argument-error",
227
"too many argument with cart set constructor");
230
// return true if the given quark is defined
232
bool CartSet::isquark (const long quark, const bool hflg) const {
234
if (zone.exists (quark) == true) {
238
bool result = hflg ? Object::isquark (quark, hflg) : false;
243
// apply this object with a set of arguments and a quark
245
Object* CartSet::apply (Runnable* robj, Nameset* nset, const long quark,
247
// get the number of arguments
248
long argc = (argv == nilp) ? 0 : argv->length ();
250
// check for 0 argument
251
if (quark == QUARK_EMPTYP) return new Boolean (empty ());
252
if (quark == QUARK_LENGTH) return new Integer (length ());
253
// check for 1 argument
255
if (quark == QUARK_ADDRCO) {
256
Object* obj = argv->get (0);
257
Rco* rco = dynamic_cast <Rco*> (obj);
259
throw Exception ("type-error", "invalid object with cart set add",
262
t_long result = addrco (*rco);
263
return new Integer (result);
265
if (quark == QUARK_GET) {
268
long idx = argv->getlong (0);
269
Cart* lco = get (idx);
278
if (quark == QUARK_LOOKUP) {
281
String rid = argv->getstring (0);
282
Cart* lco = lookup (rid);
291
if (quark == QUARK_EXISTSP) {
292
String rid = argv->getstring (0);
293
return new Boolean (exists (rid));
296
// check for 2 arguments
298
if (quark == QUARK_GETRCO) {
299
String rid = argv->getstring (0);
300
t_long kid = argv->getlong (1);
301
return new Rco (getrco (rid, kid));
304
// call the object method
305
return Object::apply (robj, nset, quark, argv);