--- parser3/src/classes/hash.C 2003/02/04 15:59:04 1.54.2.6 +++ parser3/src/classes/hash.C 2003/02/06 10:27:04 1.54.2.7 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_HASH_C="$Date: 2003/02/04 15:59:04 $"; +static const char* IDENT_HASH_C="$Date: 2003/02/06 10:27:04 $"; #include "classes.h" #include "pa_vmethod_frame.h" @@ -216,21 +216,21 @@ static void _sql(Request& r, StringPtr m if(params.count()>1) { ValuePtr voptions=params.as_no_junction(1, "options must be hash, not code"); if(!voptions->is_string()) - if(HashStringValuePtr options=voptions->get_hash(method_name)) { + if(HashStringValue* options=voptions->get_hash(method_name)) { int valid_options=0; - if(ValuePtr vlimit=(ValuePtr )options->get(*sql_limit_name)) { + if(ValuePtr vlimit=options->get(sql_limit_name)) { valid_options++; - limit=(ulong)r.process_to_value(*vlimit).as_double(); + limit=(ulong)r.process_to_value(vlimit)->as_double(); } - if(ValuePtr voffset=(ValuePtr )options->get(*sql_offset_name)) { + if(ValuePtr voffset=options->get(sql_offset_name)) { valid_options++; - offset=(ulong)r.process_to_value(*voffset).as_double(); + offset=(ulong)r.process_to_value(voffset)->as_double(); } - if(ValuePtr vdistinct=(ValuePtr )options->get(*sql_distinct_name)) { + if(ValuePtr vdistinct=options->get(sql_distinct_name)) { valid_options++; - distinct=r.process_to_value(*vdistinct).as_bool(); + distinct=r.process_to_value(vdistinct)->as_bool(); } - if(valid_options!=options->size()) + if(valid_options!=options->count()) throw Exception("parser.runtime", method_name, "called with invalid option"); @@ -241,104 +241,90 @@ static void _sql(Request& r, StringPtr m } Temp_lang temp_lang(r, String::UL_SQL); - const String& statement_string=r.process_to_string(statement); + StringPtr statement_string=r.process_to_string(statement); const char* statement_cstr= - statement_string.cstr(String::UL_UNSPECIFIED, r.connection(&method_name)); - Hash& hash=static_cast(r.get_self())->hash(&method_name); + statement_string->cstr(r.pool(), String::UL_UNSPECIFIED, r.connection(method_name)); + HashStringValue& hash=GET_SELF(r, VHash).hash(method_name); hash.clear(); - Hash_sql_event_handlers handlers(pool, method_name, + Hash_sql_event_handlers handlers(method_name, statement_string, statement_cstr, distinct, hash); - - r.connection(&method_name)->query( + r.connection(method_name)->query( statement_cstr, offset, limit, handlers, statement_string); } -static void keys_collector(const Hash::Key& key, Hash::Val *value, void *info) { - Table& table=*static_cast(info); - Pool& pool=table.pool(); - - Array& row=*new(pool) Array(pool); - row+=&key; - table+=&row; +static void keys_collector( + HashStringValue::key_type key, + HashStringValue::value_type value, + Table *table) { + Table::element_type row(new ArrayString); + *row+=key; + table+=row; } static void _keys(Request& r, StringPtr method_name, MethodParams& ) { - Pool& pool=r.pool(); + Table::columns_type columns(new ArrayString); + *columns+=StringPtr(new String("key")); + TablePtr table(new Table(method_name, columns)); - Array& columns=*new(pool) Array(pool); - columns+=new(pool) String(pool, "key"); - Table& table=*new(pool) Table(pool, method_name, &columns); + GET_SELF(r, VHash).hash(method_name).for_each(keys_collector, table.get()); - static_cast(r.get_self())->hash(&method_name).for_each(keys_collector, &table); - - r.write_no_lang(*new(pool) VTable(pool, &table)); + r.write_no_lang(ValuePtr(new VTable(table))); } static void _count(Request& r, StringPtr method_name, MethodParams& ) { - Pool& pool=r.pool(); - - r.write_no_lang( - *new(pool) VInt(pool, static_cast(r.get_self())->hash(&method_name).size())); + r.write_no_lang(ValuePtr(new VInt(GET_SELF(r, VHash).hash(method_name).count()))); } static void _delete(Request& r, StringPtr method_name, MethodParams& params) { Pool& pool=r.pool(); - static_cast(r.get_self())->hash(&method_name).remove(params.as_string(0, "key must be string")); + GET_SELF(r, VHash).hash(method_name).remove(params.as_string(0, "key must be string")); } #ifndef DOXYGEN struct Foreach_info { Request *r; - const String* key_var_name; - const String* value_var_name; + StringPtr key_var_name; + StringPtr value_var_name; ValuePtr body_code; ValuePtr delim_maybe_code; - VString *vkey; + VStringPtr vkey; bool need_delim; }; #endif - -static void one_foreach_cycle(const Hash::Key& akey, Hash::Val *avalue, - void *info) { - Foreach_info& i=*static_cast(info); - - i.vkey->set_string(akey); - Value& ncontext=*i.r->get_method_frame()->caller(); - ncontext.put_element(*i.key_var_name, i.vkey, false); - ncontext.put_element(*i.value_var_name, static_cast(avalue), false); - - StringOrValue sv_processed=i.r->process(*i.body_code); - const String *s_processed=sv_processed.get_string(); - if(i.delim_maybe_code && s_processed && s_processed->size()) { // delimiter set and we have body - if(i.need_delim) // need delim & iteration produced string? - i.r->write_pass_lang(i.r->process(*i.delim_maybe_code)); - i.need_delim=true; +static void one_foreach_cycle( + HashStringValue::key_type akey, + HashStringValue::value_type avalue, + Foreach_info *info) { + info->vkey->set_string(akey); + Value& ncontext=*info->r->get_method_frame()->caller(); + ncontext.put_element(info->key_var_name, info->vkey, false); + ncontext.put_element(info->value_var_name, avalue, false); + + StringOrValue sv_processed=info->r->process(info->body_code); + StringPtr s_processed=sv_processed.get_string(); + if(info->delim_maybe_code && s_processed && s_processed->size()) { // delimiter set and we have body + if(info->need_delim) // need delim & iteration produced string? + info->r->write_pass_lang(info->r->process(info->delim_maybe_code)); + info->need_delim=true; } - i.r->write_pass_lang(sv_processed); + info->r->write_pass_lang(sv_processed); } static void _foreach(Request& r, StringPtr method_name, MethodParams& params) { - Pool& pool=r.pool(); - const String& key_var_name=params.as_string(0, "key-var name must be string"); - const String& value_var_name=params.as_string(1, "value-var name must be string"); - Value& body_code=params.as_junction(2, "body must be code"); - ValuePtr delim_maybe_code=params.count()>3?¶ms.get(3):0; - - Foreach_info info={ - &r, - &key_var_name, &value_var_name, - &body_code, - delim_maybe_code, - - new(pool) VString(pool), - false - }; - VHash& self=*static_cast(r.get_self()); - Hash& hash=self.hash(&method_name); + Foreach_info info; + info.r=&r; + info.key_var_name=params.as_string(0, "key-var name must be string"); + info.value_var_name=params.as_string(1, "value-var name must be string"); + info.body_code=params.as_junction(2, "body must be code"); + info.delim_maybe_code=params.count()>3?params.get(3):ValuePtr(0); + info.vkey=VStringPtr(new VString); + + VHash& self=GET_SELF(r, VHash); + HashStringValue& hash=self.hash(method_name); VHash_lock lock(self); hash.for_each(one_foreach_cycle, &info); }