|
|
| version 1.2, 2001/04/02 08:49:38 | version 1.9.2.1, 2001/04/27 15:19:23 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: random parser class. | Parser: @b random parser class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) |
| Line 8 | Line 8 |
| $Id$ | $Id$ |
| */ | */ |
| #include <process.h> | |
| #include <time.h> | |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| #include "pa_common.h" | #include "pa_common.h" |
| #include "_random.h" | |
| #include "pa_vint.h" | #include "pa_vint.h" |
| #include "pa_request.h" | #include "pa_request.h" |
| // global var | // defines |
| #define RANDOM_CLASS_NAME "random" | |
| // class | |
| VStateless_class *random_class; | class MRandom : public Methoded { |
| public: | |
| MRandom(Pool& pool); | |
| bool used_directly() { return true; } | |
| }; | |
| // methods | // methods |
| static void _generate(Request& r, const String& method_name, Array *params) { | /** ^random.generate[] |
| ^random.generate(range) | |
| */ | |
| static void _generate(Request& r, const String& method_name, MethodParams *params) { | |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| uint max=params->size()? | Value& range=params->get_junction(0, "range must be expression"); |
| (uint)(r.process(*static_cast<Value *>(params->get(0)))).as_double():0; | uint max=params->size()?(uint)r.process(range).as_double():0; |
| if(max<=1) | if(max<=1) |
| RTHROW(0, 0, | PTHROW(0, 0, |
| &method_name, | &method_name, |
| "bad range [0..%u]", max); | "bad range [0..%u]", max); |
| r.write_no_lang(*new(pool) VInt(pool, rand()%max)); | r.write_no_lang(*new(pool) VInt(pool, rand()%max)); |
| } | } |
| // initialize | // constructor |
| MRandom::MRandom(Pool& pool, VStateless_class& vclass) { | |
| set_name(NEW String(pool, RANDOM_CLASS_NAME)); | |
| void initialize_random_class(Pool& pool, VStateless_class& vclass) { | |
| // setting seed | // setting seed |
| srand(getpid()+time(NULL)); rand(); | srand(getpid()+time(NULL)); rand(); |
| // ^random.generate[] | // ^random.generate[] |
| // ^random.generate(range) | // ^random.generate(range) |
| vclass.add_native_method("generate", Method::CT_STATIC, _generate, 0, 1); | add_native_method("generate", Method::CT_STATIC, _generate, 1, 1); |
| } | |
| // global variable | |
| Methoded *random_class; | |
| // creator | |
| Methoded *MRandom_create(Pool& pool) { | |
| return random_class=new(pool) MRandom(pool); | |
| } | } |