iterators for bz_API-STLWrapper classes

Make suggestions for improving one of the best games on the net!
Post Reply
Enigma
Private First Class
Private First Class
Posts: 212
Joined: Sat Apr 23, 2005 3:13 am

iterators for bz_API-STLWrapper classes

Post by Enigma »

I think it would be nice if the STL-wrapper classes in bzfsAPI supported iterators. What I mean is to add the functions begin(), end(), rbegin(), and rend() to each class, as well as the necessary typedefs to make code a little cleaner.

Currently, to use std::for_each with bz_APIIntList, code has to be written like this...

(Note that operator[] has to be used because it is the only function that returns a reference to const, and the dataBlob member is protected.)

Code: Select all

bz_APIIntList *player_list = bz_newIntList();
bz_getPlayerIndexList(player_list);

 std::for_each(&(*player_list)[0],
               &(*player_list)[player_list->size()],
               std::bind2nd(kill_team_player(), team));
 
or

Code: Select all

 std::for_each(&player_list->operator[](0),
               &player_list->operator[](player_list->size()),
               std::bind2nd(kill_team_player(), team));
Whereas code written with begin(), end(), etc... looks much cleaner.

Code: Select all

 std::for_each(player_list->begin(), 
               player_list->end(), 
               std::bind2nd(kill_team_player(), team));
Added iterator typedefs seem necessary if the functions to get iterators are added to the bz_API classes.

Code: Select all

class bzAPIIntList {
typedef std::vector<int>::iterator iterator;
...
};

bzAPIIntList::iterator begin = blah.begin();

Anyway, I hope I did not overlook anything, and thanks for taking the time to read this.
User avatar
DTRemenak
General
General
Posts: 625
Joined: Thu Jan 16, 2003 4:54 am
Location: U.S.
Contact:

Post by DTRemenak »

You're welcome to implement iterators for the API parameter classes. Such a patch would likely be accepted.

You may not, however, use the STL to do so ;) (so no typedef'ing to std::vector<int>::iterator, for instance). There was a reason we didn't use the STL in the API itself (not that I remember what it was).
User avatar
JeffM
Staff Sergeant
Staff Sergeant
Posts: 5196
Joined: Fri Dec 13, 2002 4:11 am

Post by JeffM »

windows can't use STL across the DLL/APP barrier.

The actual code for the implementation has to be done inside the bzfAPI.cpp file, and can't be done in the header.
ImageJeffM
Enigma
Private First Class
Private First Class
Posts: 212
Joined: Sat Apr 23, 2005 3:13 am

Post by Enigma »

Here is a small patch with begin() and end() added to the container classes.

I wrote a reverse_iterator class and a iterator_traits class (not in the patch), but I implemented the reverse_iterator as a template class, which I don't think will work, as jeff mentioned that the definitions need to be in the cxx file.

In the end() functions I subtracted 1 and added 1 so that a out_of_range exception isn't thrown.

Code: Select all

-bz_APIIntList::iterator bz_APIIntList::end()
-{
-  int* ret = &data->list.at(data->list.size()-1);
-  return ret+1;
-}
As a side note, I've never used DLLs.

I need to sleep :cry:
Attachments
iterators.patch.txt
(4.62 KiB) Downloaded 59 times
Post Reply