~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/srv/csm/shl/CartSet.cpp

  • Committer: Package Import Robot
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2015-07-11 02:00:35 UTC
  • mfrom: (10.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150711020035-2nhpztq7s15qyc0v
Tags: 2.5.1-1
* New upstream release. (Closes: #789968)
* Update debian/control.
  - Update Standards-Version to 3.9.6.
* Add support mips64(el) and ppc64el. (Closes: #741508, #748146)
* Add patches/support-gcc-5.x.patch. (Closes: #777767)
  - Fix build with gcc-5.x.
* Add patches/Disable-NET0001.als.patch.
  - Disable test of NET0001.als.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ---------------------------------------------------------------------------
 
2
// - CartSet.cpp                                                             -
 
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.                  -
 
7
// -                                                                         -
 
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
// ---------------------------------------------------------------------------
 
16
 
 
17
#include "Vector.hpp"
 
18
#include "Boolean.hpp"
 
19
#include "Integer.hpp"
 
20
#include "CartSet.hpp"
 
21
#include "Runnable.hpp"
 
22
#include "QuarkZone.hpp"
 
23
#include "Exception.hpp"
 
24
 
 
25
namespace afnix {
 
26
 
 
27
  // -------------------------------------------------------------------------
 
28
  // - class section                                                         -
 
29
  // -------------------------------------------------------------------------
 
30
 
 
31
  // create an empty cart set
 
32
 
 
33
  CartSet::CartSet (void) {
 
34
    d_acf = false;
 
35
    reset ();
 
36
  }
 
37
 
 
38
  // create an anonymous cart set
 
39
 
 
40
  CartSet::CartSet (const bool acf) {
 
41
    d_acf = acf;
 
42
    reset ();
 
43
  }
 
44
 
 
45
  // return the object class name
 
46
 
 
47
  String CartSet::repr (void) const {
 
48
    return "CartSet";
 
49
  }
 
50
 
 
51
  // reset this cart
 
52
  
 
53
  void CartSet::reset (void) {
 
54
    wrlock ();
 
55
    try {
 
56
      d_lht.reset ();
 
57
      d_aco.reset ();
 
58
      unlock ();
 
59
    } catch (...) {
 
60
      unlock ();
 
61
      throw;
 
62
    }
 
63
  }
 
64
 
 
65
  // get the number of cart in the cart set
 
66
 
 
67
  long CartSet::length (void) const {
 
68
    rdlock ();
 
69
    try {
 
70
      long result = d_lht.length ();
 
71
      unlock ();
 
72
      return result;
 
73
    } catch (...) {
 
74
      unlock ();
 
75
      throw;
 
76
    }
 
77
  }
 
78
        
 
79
  // return true if the cart set is empty
 
80
 
 
81
  bool CartSet::empty (void) const {
 
82
    rdlock ();
 
83
    try {
 
84
      bool result = d_lht.empty ();
 
85
      unlock ();
 
86
      return result;
 
87
    } catch (...) {
 
88
      unlock ();
 
89
      throw;
 
90
    }
 
91
  }
 
92
 
 
93
  // check if a cart exists by rid
 
94
 
 
95
  bool CartSet::exists (const String& rid) const {
 
96
    rdlock ();
 
97
    try {
 
98
      bool result = d_lht.exists (rid);
 
99
      unlock ();
 
100
      return result;
 
101
    } catch (...) {
 
102
      unlock ();
 
103
      throw;
 
104
    }
 
105
  }
 
106
 
 
107
  // get a cart by index
 
108
 
 
109
  Cart* CartSet::get (const long idx) const {
 
110
    rdlock ();
 
111
    try {
 
112
      Cart* lco = dynamic_cast <Cart*> (d_lht.getobj (idx));
 
113
      unlock ();
 
114
      return lco;
 
115
    } catch (...) {
 
116
      unlock ();
 
117
      throw;
 
118
    }
 
119
  }
 
120
 
 
121
  // get a cart by rid
 
122
 
 
123
  Cart* CartSet::lookup (const String& rid) const {
 
124
    rdlock ();
 
125
    try {
 
126
      Cart* lco = dynamic_cast <Cart*> (d_lht.lookup (rid));
 
127
      unlock ();
 
128
      return lco;
 
129
    } catch (...) {
 
130
      unlock ();
 
131
      throw;
 
132
    }
 
133
  }
 
134
 
 
135
  // add a rco in the cart set
 
136
 
 
137
  t_long CartSet::addrco (const Rco& rco) {
 
138
    wrlock ();
 
139
    try {
 
140
      // get the rco rid
 
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");
 
146
        }
 
147
        // add the rco and return
 
148
        t_long result = d_aco.addrco (rco);
 
149
        unlock ();
 
150
        return result;
 
151
      }
 
152
      // check if the cart exists in the hashtable
 
153
      Cart* lco = dynamic_cast <Cart*> (d_lht.get (rid));
 
154
      if (lco == nilp) {
 
155
        lco = new Cart (rid);
 
156
        d_lht.add (rid, lco);
 
157
      }
 
158
      // add the rco in the cart
 
159
      t_long result = lco->addrco (rco);
 
160
      unlock ();
 
161
      return result;
 
162
    } catch (...) {
 
163
      unlock ();
 
164
      throw;
 
165
    }
 
166
  }
 
167
 
 
168
  // get a rco by rid and index
 
169
 
 
170
  Rco CartSet::getrco (const String& rid, const t_long kid) const {
 
171
    rdlock ();
 
172
    try {
 
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");
 
178
        }
 
179
        Rco rco = d_aco.getrco (kid);
 
180
        unlock ();
 
181
        return rco;
 
182
      }
 
183
      // find the cart by rid
 
184
      Cart* lco = dynamic_cast <Cart*> (d_lht.lookup (rid));
 
185
      if (lco == nilp) {
 
186
        throw Exception ("internal-error", "invalid nil cart in lookup");
 
187
      }
 
188
      // get the rco by index
 
189
      Rco rco = lco->getrco (kid);
 
190
      unlock ();
 
191
      return rco;
 
192
    } catch (...) {
 
193
      unlock ();
 
194
      throw;
 
195
    }
 
196
  }
 
197
 
 
198
  // -------------------------------------------------------------------------
 
199
  // - object section                                                        -
 
200
  // -------------------------------------------------------------------------
 
201
 
 
202
  // the quark zone
 
203
  static const long QUARK_ZONE_LENGTH = 6;
 
204
  static QuarkZone  zone (QUARK_ZONE_LENGTH);
 
205
 
 
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");
 
214
 
 
215
  // create a new object in a generic way
 
216
 
 
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
 
222
    if (argc == 1) {
 
223
      bool acf = argv->getbool (0);
 
224
      return new CartSet (acf);
 
225
    }
 
226
    throw Exception ("argument-error",
 
227
                     "too many argument with cart set constructor");
 
228
  }
 
229
 
 
230
  // return true if the given quark is defined
 
231
 
 
232
  bool CartSet::isquark (const long quark, const bool hflg) const {
 
233
    rdlock ();
 
234
    if (zone.exists (quark) == true) {
 
235
      unlock ();
 
236
      return true;
 
237
    }
 
238
    bool result = hflg ? Object::isquark (quark, hflg) : false;
 
239
    unlock ();
 
240
    return result;
 
241
  }
 
242
  
 
243
  // apply this object with a set of arguments and a quark
 
244
  
 
245
  Object* CartSet::apply (Runnable* robj, Nameset* nset, const long quark,
 
246
                          Vector* argv) {
 
247
    // get the number of arguments
 
248
    long argc = (argv == nilp) ? 0 : argv->length ();
 
249
    
 
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
 
254
    if (argc == 1) {
 
255
      if (quark == QUARK_ADDRCO) {
 
256
        Object* obj = argv->get (0);
 
257
        Rco*    rco = dynamic_cast <Rco*> (obj);
 
258
        if (rco == nilp) {
 
259
          throw Exception ("type-error", "invalid object with cart set add",
 
260
                           Object::repr (obj));
 
261
        }
 
262
        t_long result = addrco (*rco);
 
263
        return new Integer (result);
 
264
      }
 
265
      if (quark == QUARK_GET) {
 
266
        rdlock ();
 
267
        try {
 
268
          long idx = argv->getlong (0);
 
269
          Cart* lco = get (idx);
 
270
          robj->post (lco);
 
271
          unlock ();
 
272
          return lco;
 
273
        } catch (...) {
 
274
          unlock ();
 
275
          throw;
 
276
        }
 
277
      }
 
278
      if (quark == QUARK_LOOKUP) {
 
279
        rdlock ();
 
280
        try {
 
281
          String rid = argv->getstring (0);
 
282
          Cart* lco = lookup (rid);
 
283
          robj->post (lco);
 
284
          unlock ();
 
285
          return lco;
 
286
        } catch (...) {
 
287
          unlock ();
 
288
          throw;
 
289
        }
 
290
      }
 
291
      if (quark == QUARK_EXISTSP) {
 
292
        String rid = argv->getstring (0);
 
293
        return new Boolean (exists (rid));
 
294
      }
 
295
    }
 
296
    // check for 2 arguments
 
297
    if (argc == 2) {
 
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));
 
302
      }
 
303
    }
 
304
    // call the object method
 
305
    return Object::apply (robj, nset, quark, argv);
 
306
  }
 
307
}