Am I re-inventing the wheel?

D

Devon Null

Am I re-inventing the wheel here? The wheel I am referring to is the Bag
class. This encompasses just about everything I need to do AFAIK for now.

Am I also right in assuming that if I do something like pSword1.reset()
that it will clean up after itself?

Code:
#include "items.h"
#include <iostream>
#include <boost/shared_ptr.hpp>

using namespace std;
using boost::shared_ptr;

class Bag
{
    public:
        Bag( int index_ = 4, float weight = 0.0 );

        void set_bag_name( string bag_name_ ) { bag_name = bag_name_; }
        string get_bag_name() { return bag_name; }

        void set_bag_weight( float weight ) { bag_weight = weight; }
        float get_bag_weight() { return bag_weight; }

        void add_item( shared_ptr<Item> &item, int slot = -1);
        void remove_item( shared_ptr<Item> &item );
        void swap_items( shared_ptr<Item> &item1, shared_ptr<Item> &item2 );
        void list_items();

    private:
        string bag_name;
        float bag_weight;
        vector<shared_ptr<Item> > test_vector;
        vector<shared_ptr<Item> >::iterator bag_iter;
        shared_ptr<Item> null_ptr;
        int index;
        int find_item( shared_ptr<Item> &item );
};

Bag::Bag( int index_, float weight ): bag_weight( weight ), index( index_ )
{
    bag_name = "You need to set a bag name.";
    test_vector.assign( index, null_ptr );
}

int Bag::find_item( shared_ptr<Item> &item )
{
    int slot = 0;
    for( bag_iter = test_vector.begin(); bag_iter < test_vector.end() &&
*bag_iter != item; bag_iter++ )
    {
        slot++;
    }
    if( bag_iter == test_vector.end() )
    {
        slot = -1;
    }
    return slot;
}

void Bag::add_item( shared_ptr<Item> &item, int slot )
{
    if( slot == -1 )
    {
        int slot_count = 0;
        bag_iter = test_vector.begin();
        while( *bag_iter != null_ptr )
        {
            bag_iter++;
            slot_count++;
        }
        slot = slot_count;
    }
    bag_iter = ( test_vector.begin() + slot );
    if( slot > index )
    {
        cout << "Assignment not allowed, slot exceeds index." << endl;
    }
    else if( slot <= index && *bag_iter != null_ptr )
    {
        cout << "Slot already has an item in it. Use swap_items() or
replace_item()." << endl;
    }
    else if( slot <= index && *bag_iter == null_ptr )
    {
        test_vector[slot] = item;
        cout << item->get_item_name() << endl;
    }
    else
    {
        cout << "All of the test_vector tests failed." << endl;
    }
}

void Bag::remove_item( shared_ptr<Item> &item )
{
    int slot = find_item( item );
    test_vector[slot] = null_ptr;
}

void Bag::swap_items( shared_ptr<Item> &item1, shared_ptr<Item> &item2 )
{
    int slot1 = 0;
    int slot2 = 0;
    if( &item1 == &item2 )
    { }
    else
    {
        slot1 = find_item( item1 );
        slot2 = find_item( item2 );
        test_vector[slot1] = item2;
        if( slot2 != -1 )
        {
            test_vector[slot2] = item1;
        }
    }
}

void Bag::list_items()
{
    cout << endl;
    int slot = 0;
    for( bag_iter = test_vector.begin(); bag_iter < test_vector.end();
bag_iter++ )
    {
        cout << "Slot " << slot + 1<< ": ";
        if( *bag_iter == null_ptr )
        {
            cout << "Empty" << endl;
        }
        else
        {
            cout << test_vector[slot]->get_item_name() << endl;
        }
        slot++;
    }
    cout << endl;
}


int main()
{
    shared_ptr<Item> pSword1( new Weapon );
    shared_ptr<Item> pItem1( new Item );
    pSword1->set_item_name( "Wooden Sword" );
    pItem1->set_item_name( "Potion" );

    Bag some_bag;
    cout << "\nAdding..." << endl;
    some_bag.add_item( pSword1 );
    cout << "\nAdding on top of pSword1..." << endl;
    some_bag.add_item( pItem1, 0 );
    cout << "\nAdding..." << endl;
    some_bag.add_item( pItem1 );
    some_bag.list_items();
    cout << "\nRemoving..." << endl;
    some_bag.remove_item( pSword1 );
    some_bag.list_items();
    cout << "\nAdding..." << endl;
    some_bag.add_item( pSword1, 2 );
    some_bag.list_items();
    cout << "\nSwapping..." << endl;
    some_bag.swap_items( pSword1, pItem1 );
    some_bag.list_items();
    some_bag.set_bag_name( "Hip Pouch" );
    cout << "Bag name is " << some_bag.get_bag_name() << endl;

    return 0;
}

Here is the output:

[output]

Adding...
Wooden Sword

Adding on top of pSword1...
Slot already has an item in it. Use swap_items() or replace_item().

Adding...
Potion

Slot 1: Wooden Sword
Slot 2: Potion
Slot 3: Empty
Slot 4: Empty


Removing...

Slot 1: Empty
Slot 2: Potion
Slot 3: Empty
Slot 4: Empty


Adding...
Wooden Sword

Slot 1: Empty
Slot 2: Potion
Slot 3: Wooden Sword
Slot 4: Empty


Swapping...

Slot 1: Empty
Slot 2: Wooden Sword
Slot 3: Potion
Slot 4: Empty

Bag name is Hip Pouch

[/output]
--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
 
B

BobR

Devon Null wrote in message...
Code:
#include "items.h"
#include <iostream>
#include <boost/shared_ptr.hpp>[/QUOTE]

You should take this to a boost NG. 

[ yeah, I gots me flame suit on! <G> ]
 
O

Old Wolf

Devon Null wrote in message...
Code:
#include "items.h"
#include <iostream>
#include <boost/shared_ptr.hpp>[/QUOTE]

You should take this to a boost NG.

[ yeah, I gots me flame suit on! <G> ][/QUOTE]

shared_ptr is in TR1 which means it's on-topic for
this NG, as far as I'm concerned :)

Also, even if it weren't, I think the original
question is still pertinent, the OP wants to know
if there is already some standard feature that does
what he's doing.
 
A

Alan Johnson

Devon said:
Am I re-inventing the wheel here? The wheel I am referring to is the Bag
class. This encompasses just about everything I need to do AFAIK for now.

Am I also right in assuming that if I do something like pSword1.reset()
that it will clean up after itself?

Code:
#include "items.h"
#include <iostream>
#include <boost/shared_ptr.hpp>

using namespace std;
using boost::shared_ptr;

class Bag
{
public:
Bag( int index_ = 4, float weight = 0.0 );

void set_bag_name( string bag_name_ ) { bag_name = bag_name_; }
string get_bag_name() { return bag_name; }

void set_bag_weight( float weight ) { bag_weight = weight; }
float get_bag_weight() { return bag_weight; }

void add_item( shared_ptr<Item> &item, int slot = -1);
void remove_item( shared_ptr<Item> &item );
void swap_items( shared_ptr<Item> &item1, shared_ptr<Item> &item2 );
void list_items();

private:
string bag_name;
float bag_weight;
vector<shared_ptr<Item> > test_vector;
vector<shared_ptr<Item> >::iterator bag_iter;
shared_ptr<Item> null_ptr;
int index;
int find_item( shared_ptr<Item> &item );
};

Bag::Bag( int index_, float weight ): bag_weight( weight ), index( index_ )
{
bag_name = "You need to set a bag name.";
test_vector.assign( index, null_ptr );
}

int Bag::find_item( shared_ptr<Item> &item )
{
int slot = 0;
for( bag_iter = test_vector.begin(); bag_iter < test_vector.end() &&
*bag_iter != item; bag_iter++ )
{
slot++;
}
if( bag_iter == test_vector.end() )
{
slot = -1;
}
return slot;
}

void Bag::add_item( shared_ptr<Item> &item, int slot )
{
if( slot == -1 )
{
int slot_count = 0;
bag_iter = test_vector.begin();
while( *bag_iter != null_ptr )
{
bag_iter++;
slot_count++;
}
slot = slot_count;
}
bag_iter = ( test_vector.begin() + slot );
if( slot > index )
{
cout << "Assignment not allowed, slot exceeds index." << endl;
}
else if( slot <= index && *bag_iter != null_ptr )
{
cout << "Slot already has an item in it. Use swap_items() or
replace_item()." << endl;
}
else if( slot <= index && *bag_iter == null_ptr )
{
test_vector[slot] = item;
cout << item->get_item_name() << endl;
}
else
{
cout << "All of the test_vector tests failed." << endl;
}
}

void Bag::remove_item( shared_ptr<Item> &item )
{
int slot = find_item( item );
test_vector[slot] = null_ptr;
}

void Bag::swap_items( shared_ptr<Item> &item1, shared_ptr<Item> &item2 )
{
int slot1 = 0;
int slot2 = 0;
if( &item1 == &item2 )
{ }
else
{
slot1 = find_item( item1 );
slot2 = find_item( item2 );
test_vector[slot1] = item2;
if( slot2 != -1 )
{
test_vector[slot2] = item1;
}
}
}

void Bag::list_items()
{
cout << endl;
int slot = 0;
for( bag_iter = test_vector.begin(); bag_iter < test_vector.end();
bag_iter++ )
{
cout << "Slot " << slot + 1<< ": ";
if( *bag_iter == null_ptr )
{
cout << "Empty" << endl;
}
else
{
cout << test_vector[slot]->get_item_name() << endl;
}
slot++;
}
cout << endl;
}


int main()
{
shared_ptr<Item> pSword1( new Weapon );
shared_ptr<Item> pItem1( new Item );
pSword1->set_item_name( "Wooden Sword" );
pItem1->set_item_name( "Potion" );

Bag some_bag;
cout << "\nAdding..." << endl;
some_bag.add_item( pSword1 );
cout << "\nAdding on top of pSword1..." << endl;
some_bag.add_item( pItem1, 0 );
cout << "\nAdding..." << endl;
some_bag.add_item( pItem1 );
some_bag.list_items();
cout << "\nRemoving..." << endl;
some_bag.remove_item( pSword1 );
some_bag.list_items();
cout << "\nAdding..." << endl;
some_bag.add_item( pSword1, 2 );
some_bag.list_items();
cout << "\nSwapping..." << endl;
some_bag.swap_items( pSword1, pItem1 );
some_bag.list_items();
some_bag.set_bag_name( "Hip Pouch" );
cout << "Bag name is " << some_bag.get_bag_name() << endl;

return 0;
}

You are doing some work that std::set could do for you. For example,
finding an item, or ensuring that an item is only added to a bag once.
 
D

Devon Null

Old said:
Also, even if it weren't, I think the original
question is still pertinent, the OP wants to know
if there is already some standard feature that does
what he's doing.

Aye - that was my intent. Lots of work if there is something that does
this already. Great learning experience, but something I learned before
this - if it's been done before, it's probably been done better/more
efficient.
--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top