[ROOT] Re: [Carrot] Using TMap for GET / POST / COOKIE vars

From: Ashley Cambrell (ash@freaky-namuh.com)
Date: Fri Apr 26 2002 - 16:37:20 MEST


Valeriy Onuchin wrote:

> Hi Ashley,
>yes. Using TMap is more effective than TList for "large" collections ( thousands objects).
>Since  POST,GET,COOKIES are "not numerous" ( no more than hundred ), using TList is quite effective.
>  
>
You still have to interact through a tlist though. [?] Where as a TMap
you can just do:

TMap tm;
tm.Add(new TObjString("bob"), new TObjString("smith"));
cout << ((TObjString *)tm.GetValue(new TObjString("bob")))->GetString()
<< endl; // apart from the leak in the second new bob.

and it's even easier once I wrap it in a char friendly manner. (below)

>Regards.	Valeriy
>  
>
//Still trying to work out the operator overloading as per the root list

#ifndef THTTPDATA_H
#define THTTPDATA_H

#include <TMap.h>
#include <TObjString.h>
#include <TString.h>
class THTTPData : public TMap
{
    public:
    THTTPData(Int_t capacity = TCollection::kInitHashTableCapacity,
Int_t rehash = 0)
    : TMap(capacity, rehash)
    {
    }
   
    const TString& operator[]( const char* key ) const
    {
        TObjString *tosKey = new TObjString(key);
        TString value;
       
        if( this->TMap::GetValue(tosKey) )
            value = ((TObjString
*)(this->TMap::GetValue(tosKey)))->String();
       
        delete tosKey;
       
        return value;
    }

    void operator[]( const char* key , const char* value)
    {
        Add(key, value);
    }
       
    void Add(const char* key, const char* value)
    {
        TObjString *tosKey = new TObjString(key);
        TObjString *tosValue = new TObjString(value);
   
        if( !this->TMap::GetValue(tosKey) )
        {
            this->TMap::Add(tosKey, tosValue);
        }
        else
        {
            TObjString *oldVal = this->TMap::Remove(tosKey);
            delete oldVal;
            this->TMap::Add(tosKey, tosValue);
        }
    }
};

#endif //THTTPDATA_H

Is there much memory overhead with TMap?  I was also thinking of
implementing a handy PHP construct:

<input type="text" name="user[username]" >
<input type="text" name="user[password]" >

//in php
<?php
    print_r($user); //PHP converts user[*] to array
    //prints out user[username] => (value of user[username] input from form)
    //user[password] => (value of user[password] input from form)
?>

So in Carrot this could come across as:  (not syntactically complete)

if( ap.GetVars("GET")->IsMap("user") )
{
    TMap *user = ap.GetVars("GET")->GetValue("user");
    cout << user.GetValue("username") << endl;
    cout << user.GetValue("password") << endl;
}

The details of what ap.GetVars returns would have to be worked out.
 What ever object is returned would have to be aware of what type of
object it was storing

Just a thought anyway  :-)

Thanks  Valeriy

Ashley Cambrell

BTW, did you manage to get TApacheUpload working from the ALLVar.C script?



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:51 MET