"Gernot Frisch" <Me@[EMAIL PROTECTED]
> wrote in message
news:5kncjiF4maonU1@[EMAIL PROTECTED]
>
>> Actually, if you look at the C++ class I was defining I was actually
>> doing
>> that. The parts class didn't predefine anything. That would be up to
>> whatever called it.
>
>
> Nathan's solution is best. If you want an "arm" pointer, you might have
> each body assigned with a unique description string, and make an
operator:
>
> body* operator[] (const std::string& bodyname)
> {
> if(this->m_name == bodyname) return this;
> for(std::container<body>::iterator it=m_bodies.begin();
> it!=m_bodies.end(); ++it)
> return it->operator[](bodyname);
> return NULL;
> }
This is what I wound up coming up with.
class PartIndex
{
public:
PartIndex( const std::string& Name = "Torso" ): Name_( Name ) {}
std::string Name() const { return Name_; }
bool Add( const std::string& Name, size_t Index )
{
if ( Name_ == Name )
{
Indicies_.insert( Index );
return true;
}
else
{
for ( std::vector<PartIndex>::iterator it = SubParts_.begin();
it != SubParts_.end(); ++it )
if ( (*it).Add( Name, Index ) )
return true;
return false;
}
}
bool HasSubpart( const std::string& Name ) const
{
for ( std::vector<PartIndex>::const_iterator it =
SubParts_.begin();
it != SubParts_.end(); ++it )
if ( (*it).Name_ == Name )
return true;
return false;
}
bool AddSubpart( const std::string& Part, const std::string& Name )
{
if ( Name_ == Part )
{
if ( ! HasSubpart( Name ) )
SubParts_.push_back( PartIndex( Name ) );
return true;
// else do nothing, part already exists so we don't care
}
else
{
for ( std::vector<PartIndex>::iterator it = SubParts_.begin();
it != SubParts_.end(); ++it )
if ( (*it).AddSubpart( Part, Name ) )
return true;
return false;
}
}
void PartIndicies( std::set<size_t>& Index ) const
{
std::copy( Indicies_.begin(), Indicies_.end(),
std::inserter(Index,
Index.end()) );
}
bool PartIndicies( const std::string& Name, std::set<size_t>& Index )
{
if ( Name_ == Name )
{
PartIndicies( Index );
return true;
}
else
{
for ( std::vector<PartIndex>::iterator it = SubParts_.begin();
it != SubParts_.end(); ++it )
if ( (*it).PartIndicies ( Name, Index ) )
return true;
return false;
}
}
void Indicies( std::set<size_t>& Index ) const
{
PartIndicies( Index );
for ( std::vector<PartIndex>::const_iterator it =
SubParts_.begin();
it != SubParts_.end(); ++it )
(*it).Indicies( Index );
}
bool Indicies( const std::string& Name, std::set<size_t>& Index )
{
if ( Name_ == Name )
{
Indicies( Index );
return true;
}
else
{
for ( std::vector<PartIndex>::iterator it = SubParts_.begin();
it != SubParts_.end(); ++it )
if ( (*it).Indicies ( Name, Index ) )
return true;
return false;
}
}
void Dump( std::ostream& os )
{
Dump( os, "" );
}
void Dump( std::ostream& os, const std::string& Prefix )
{
os << Prefix << Name_ << " ";
std::copy( Indicies_.begin(), Indicies_.end(),
std::ostream_iterator<int>(os, " "));
os << "\n";
for ( std::vector<PartIndex>::iterator it = SubParts_.begin(); it
!=
SubParts_.end(); ++it )
(*it).Dump( os, Prefix + " " );
}
private:
std::string Name_;
std::set<size_t> Indicies_;
std::vector<PartIndex> SubParts_;
};


|