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

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.12    ! parser      8:        $Id: random.C,v 1.11 2001/05/04 10:42:37 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);
1.11      paf        25: public: // Methoded
1.10      paf        26:        bool used_directly() { return true; }
                     27: };
1.1       paf        28: 
                     29: // methods
                     30: 
1.6       paf        31: static void _generate(Request& r, const String& method_name, MethodParams *params) {
1.1       paf        32:        Pool& pool=r.pool();
                     33: 
1.6       paf        34:        Value& range=params->get_junction(0, "range must be expression");
1.4       paf        35:     uint max=params->size()?(uint)r.process(range).as_double():0;
1.2       paf        36:     if(max<=1)
1.3       paf        37:                PTHROW(0, 0,
1.2       paf        38:                        &method_name,
                     39:                        "bad range [0..%u]", max);
1.1       paf        40:        
                     41:        r.write_no_lang(*new(pool) VInt(pool, rand()%max));
                     42: }
                     43: 
1.10      paf        44: // constructor
                     45: 
                     46: MRandom::MRandom(Pool& apool) : Methoded(apool) {
                     47:        set_name(*NEW String(pool(), RANDOM_CLASS_NAME));
                     48: 
1.1       paf        49: 
                     50:        // setting seed
                     51:        srand(getpid()+time(NULL));  rand();
                     52:        
1.10      paf        53: 
1.12    ! parser     54:        // ^random:generate[]
        !            55:        // ^random:generate(range)
1.10      paf        56:        add_native_method("generate", Method::CT_STATIC, _generate, 1, 1);
                     57: }
                     58: 
                     59: // creator
                     60: 
                     61: Methoded *MRandom_create(Pool& pool) {
                     62:        return new(pool) MRandom(pool);
1.1       paf        63: }

E-mail: