Annotation of parser3/src/classes/random.C, revision 1.4
1.1 paf 1: /** @file
2: Parser: random parser class.
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.4 ! paf 8: $Id: random.C,v 1.3 2001/04/04 06:16:18 paf Exp $
1.1 paf 9: */
10:
11: #include <process.h>
12: #include <time.h>
13:
14: #include "pa_config_includes.h"
15: #include "pa_common.h"
16: #include "_random.h"
17: #include "pa_vint.h"
18: #include "pa_request.h"
19:
20: // global var
21:
22: VStateless_class *random_class;
23:
24: // methods
25:
26: static void _generate(Request& r, const String& method_name, Array *params) {
27: Pool& pool=r.pool();
28:
1.4 ! paf 29: Value& range=*static_cast<Value *>(params->get(0));
! 30: // (this body type)
! 31: r.fail_if_junction_(false, range, method_name, "range must be expression");
! 32:
! 33: uint max=params->size()?(uint)r.process(range).as_double():0;
1.2 paf 34: if(max<=1)
1.3 paf 35: PTHROW(0, 0,
1.2 paf 36: &method_name,
37: "bad range [0..%u]", max);
1.1 paf 38:
39: r.write_no_lang(*new(pool) VInt(pool, rand()%max));
40: }
41:
42: // initialize
43:
44: void initialize_random_class(Pool& pool, VStateless_class& vclass) {
45: // setting seed
46: srand(getpid()+time(NULL)); rand();
47:
48: // ^random.generate[]
49: // ^random.generate(range)
1.4 ! paf 50: vclass.add_native_method("generate", Method::CT_STATIC, _generate, 1, 1);
1.1 paf 51: }
E-mail: