Annotation of parser3/src/classes/random.C, revision 1.10

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.10    ! paf         8:        $Id: random.C,v 1.9.2.3 2001/04/28 07:27:32 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.10    ! paf        16: // defines
1.1       paf        17: 
1.10    ! 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.10    ! 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.10    ! paf        46: // constructor
        !            47: 
        !            48: MRandom::MRandom(Pool& apool) : Methoded(apool) {
        !            49:        set_name(*NEW String(pool(), RANDOM_CLASS_NAME));
        !            50: 
1.1       paf        51: 
                     52:        // setting seed
                     53:        srand(getpid()+time(NULL));  rand();
                     54:        
1.10    ! paf        55: 
1.1       paf        56:        // ^random.generate[]
                     57:        // ^random.generate(range)
1.10    ! paf        58:        add_native_method("generate", Method::CT_STATIC, _generate, 1, 1);
        !            59: }
        !            60: 
        !            61: // creator
        !            62: 
        !            63: Methoded *MRandom_create(Pool& pool) {
        !            64:        return new(pool) MRandom(pool);
1.1       paf        65: }

E-mail: