Annotation of parser3/src/classes/random.C, revision 1.9.2.1
1.1 paf 1: /** @file
1.9 paf 2: Parser: @b random parser class.
1.1 paf 3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5:
6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.9.2.1 ! paf 8: $Id: random.C,v 1.9 2001/04/26 15:01:48 paf Exp $
1.1 paf 9: */
10:
11: #include "pa_config_includes.h"
12: #include "pa_common.h"
13: #include "pa_vint.h"
14: #include "pa_request.h"
15:
1.9.2.1 ! paf 16: // defines
1.1 paf 17:
1.9.2.1 ! paf 18: #define RANDOM_CLASS_NAME "random"
! 19:
! 20: // class
! 21:
! 22: class MRandom : public Methoded {
! 23: public:
! 24: MRandom(Pool& pool);
! 25: bool used_directly() { return true; }
! 26: };
1.1 paf 27:
28: // methods
29:
1.9.2.1 ! paf 30: /** ^random.generate[]
! 31: ^random.generate(range)
! 32: */
1.6 paf 33: static void _generate(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 34: Pool& pool=r.pool();
35:
1.6 paf 36: Value& range=params->get_junction(0, "range must be expression");
1.4 paf 37: uint max=params->size()?(uint)r.process(range).as_double():0;
1.2 paf 38: if(max<=1)
1.3 paf 39: PTHROW(0, 0,
1.2 paf 40: &method_name,
41: "bad range [0..%u]", max);
1.1 paf 42:
43: r.write_no_lang(*new(pool) VInt(pool, rand()%max));
44: }
45:
1.9.2.1 ! paf 46: // constructor
! 47:
! 48: MRandom::MRandom(Pool& pool, VStateless_class& vclass) {
! 49: set_name(NEW String(pool, RANDOM_CLASS_NAME));
1.1 paf 50:
51: // setting seed
52: srand(getpid()+time(NULL)); rand();
53:
54: // ^random.generate[]
55: // ^random.generate(range)
1.9.2.1 ! paf 56: add_native_method("generate", Method::CT_STATIC, _generate, 1, 1);
! 57: }
! 58:
! 59: // global variable
! 60:
! 61: Methoded *random_class;
! 62:
! 63: // creator
! 64:
! 65: Methoded *MRandom_create(Pool& pool) {
! 66: return random_class=new(pool) MRandom(pool);
1.1 paf 67: }
E-mail: