15 #include "../clib/passert.h" 16 #include "../clib/stlutil.h" 34 for (
const auto& elem : other.
contents_ )
36 const std::string& key = elem.first;
46 for (
unsigned i = 0; i < size; ++i )
50 if ( keyimp != NULL && valimp != NULL && keyimp->
isa(
OTString ) )
97 if ( !( is >> size >> colon ) )
99 return new BError(
"Unable to unpack struct elemcount" );
104 "Unable to unpack struct elemcount. Length given must be positive integer!" );
108 return new BError(
"Unable to unpack struct elemcount. Bad format. Colon not found!" );
116 os << key <<
" = " << bvalref->impref().getFormattedStringRep();
133 : m_StructObj( pStruct ),
134 m_pStruct( pStruct ),
135 m_IterVal( pIterVal ),
150 key = ( *itr ).first;
165 key = ( *itr ).first;
180 size_t size =
sizeof(
BStruct );
181 for (
const auto& elem : contents_ )
183 const std::string& bkey = elem.first;
185 size += bkey.capacity() + bvalref.
sizeEstimate() + (
sizeof(
void* ) * 3 + 1 ) / 2;
192 return contents_.size();
198 std::string
key( membername );
200 auto itr = contents_.find( key );
201 if ( itr != contents_.end() )
204 oref->setimp( target );
210 contents_[
key] = ref;
218 std::string
key( name );
220 auto itr = contents_.find( key );
221 if ( itr != contents_.end() )
223 return ( *itr ).second->impptr();
233 std::string
key( membername );
235 auto itr = contents_.find( key );
236 if ( itr != contents_.end() )
238 return ( *itr ).second;
252 auto itr = contents_.
find( keystr->
value() );
253 if ( itr != contents_.end() )
265 throw std::runtime_error(
"some fool used operator[] on a struct, with an Integer index" );
269 return BObjectRef(
new BError(
"Struct members can only be accessed by name" ) );
281 auto itr = contents_.
find( keystr->
value() );
282 if ( itr != contents_.end() )
285 oref->setimp( new_target );
290 contents_[keystr->
value()].set(
new BObject( new_target ) );
296 throw std::runtime_error(
297 "some fool tried to use operator[] := on a struct, with an Integer index" );
301 return new BError(
"Struct members can only be accessed by name" );
307 std::string
key( name );
308 contents_[
key] = val;
313 std::string
key( name );
325 return new BLong( static_cast<int>( contents_.size() ) );
327 return new BError(
"struct.size() doesn't take parameters." );
333 return new BError(
"Struct keys must be strings" );
334 String* strkey =
static_cast<String*
>( keyobj->impptr() );
335 int nremove =
static_cast<int>( contents_.erase( strkey->
value() ) );
336 return new BLong( nremove );
340 return new BError(
"struct.erase(key) requires a parameter." );
348 return new BError(
"Struct keys must be strings" );
349 String* strkey =
static_cast<String*
>( keyobj->impptr() );
351 return new BLong( static_cast<int>( contents_.size() ) );
355 return new BError(
"struct.insert(key,value) requires two parameters." );
362 return new BError(
"Struct keys must be strings" );
363 String* strkey =
static_cast<String*
>( keyobj->impptr() );
364 int count =
static_cast<int>( contents_.count( strkey->
value() ) );
365 return new BLong( count );
369 return new BError(
"struct.exists(key) requires a parameter." );
375 std::unique_ptr<ObjArray> arr(
new ObjArray );
376 for (
const auto& content : contents_ )
378 arr->addElement(
new String( content.first ) );
380 return arr.release();
383 return new BError(
"struct.keys() doesn't take parameters." );
393 if ( objmethod != NULL )
401 os << packtype() << contents_.size() <<
":";
402 for (
const auto& content : contents_ )
404 const std::string&
key = content.first;
408 bvalref->impref().packonto( os );
415 os << typetag() <<
"{ ";
418 for (
const auto& content : contents_ )
420 const std::string&
key = content.first;
428 FormatForStringRep( os, key, bvalref );
439 std::string
key( name );
440 if ( contents_.count( key ) == 0 )
454 std::string
key( name );
455 contents_.erase( key );
461 std::string
key( name );
462 int count =
static_cast<int>( contents_.count( key ) );
virtual void FormatForStringRep(std::ostream &os, const std::string &key, const BObjectRef &bvalref) const
virtual BObjectRef set_member(const char *membername, BObjectImp *value, bool copy) POL_OVERRIDE
virtual BObjectRef operDotMinus(const char *name) POL_OVERRIDE
const std::string & value() const
virtual BObjectRef operDotQMark(const char *name) POL_OVERRIDE
BObjectImp * copy(void) const
int find(int begin, const char *target)
bool isa(BObjectType type) const
virtual char packtype() const
std::map< std::string, BObjectRef, Clib::ci_cmp_pred > Contents
size_t sizeEstimate() const
virtual void packonto(std::ostream &os) const POL_OVERRIDE
const Contents & contents() const
virtual BObjectImp * call_method_id(const int id, Executor &ex, bool forcebuiltin=false) POL_OVERRIDE
virtual BObject * step() POL_OVERRIDE
virtual BObjectImp * copy() const =0
#define OSTRINGSTREAM_STR(x)
void addMember(const char *name, BObjectRef val)
ObjMethod * getKnownObjMethod(const char *token)
static BObjectImp * unpack(std::istream &is)
virtual BObjectRef get_member(const char *membername) POL_OVERRIDE
virtual BObjectRef operDotPlus(const char *name) POL_OVERRIDE
virtual void packonto(std::ostream &os) const POL_OVERRIDE
virtual std::string getStringRep() const POL_OVERRIDE
BObject * getParamObj(unsigned param)
virtual const char * typetag() const
virtual BObjectImp * copy() const POL_OVERRIDE
static UninitObject * create()
static BObjectImp * unpack(const char *pstr)
virtual BObjectImp * call_method(const char *methodname, Executor &ex) POL_OVERRIDE
virtual BObjectImp * call_method_id(const int id, Executor &ex, bool forcebuiltin=false)
unsigned int count() const
virtual u8 typeOfInt() const POL_OVERRIDE
virtual const char * typeOf() const POL_OVERRIDE
virtual BObjectImp * array_assign(BObjectImp *idx, BObjectImp *target, bool copy) POL_OVERRIDE
friend class BStructIterator
BStructIterator(BStruct *pDict, BObject *pIterVal)
virtual BObjectRef OperSubscript(const BObject &obj) POL_OVERRIDE
const BObjectImp * FindMember(const char *name)
virtual size_t sizeEstimate() const POL_OVERRIDE
virtual ContIterator * createIterator(BObject *pIterVal) POL_OVERRIDE