Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Iterating question

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Robotex
Master Cheater
Reputation: 0

Joined: 05 Sep 2006
Posts: 378
Location: The pizza country!

PostPosted: Sat Sep 20, 2008 6:47 am    Post subject: [C++] Iterating question Reply with quote

I'm porting a project from C to C++
The original project uses a map-like rewrited database, thus I replaced it with a map object from STL
The old project uses a union type as key and returns a void* pointer wich is casted back to it's original structure (example: void* map_get(struct map_db* db, map_key key))
In C++ I made it a templated class, and until now I didn't have the need to use the object outside the same source file, but now I have to access all the map values (for saving/backup purposes)
The C project uses an another rewrited iterator type to do that, wich has functions like
Code:

struct map_iterator* map_begin(enum WICHDATABASE);
....
struct map_iterator* iter;
iter = map_begin(CHAR_DB);
for(sometype foo = (sometype*) map_first(iter); map_exist(iter); foo = (sometype*)map_next(iter)) //notice the map_iterator* implementation is not known, it's just a unknown pointer used as reference for map_....()
{
somesavefunction(foo);
}

but in C++, I don't have idea on how to do something like that with STL iterators without exposing the object (wich I don't want to do and plus, the map keys/values can be of different types and I don't think you can cast an STL iterator)

Any suggestion?

_________________

ASM/C++ Coder
Project Speranza lead developer
Back to top
View user's profile Send private message
jackyyll
Expert Cheater
Reputation: 0

Joined: 28 Jan 2008
Posts: 143
Location: here

PostPosted: Sat Sep 20, 2008 8:32 am    Post subject: Reply with quote

Code:

std::map< map_db, mapkey > map;
std::map< map_db, mapkey >::iterator it;

for ( it = map.begin(); it != map.end(); it++ )
{
    std::cout << "key: " << it->first << " value: " << it->second << std::endl;
    somesavefunction(foo)
}


Edit:
Hmm, didn't fully read what you posted.

So basically you have a map. They map's key is a union and the value is a void * to something that you will manually typecast later to get the information?

How about:

Code:

typedef union {
    int keyTypeOne;
    char *keyTypeTwo;
    double keyTypeThree;
    string keyTypeFour;
}Key;

std::map< Key, void * > theMap;
theMap::iterator it;

for ( it = theMap.begin(); it != theMap.end(); it++ )
{
      if ( it->first.keyTypeOne == 12 )
           int value = *( int * )it->second;
}


Not sure if this will work though..
Back to top
View user's profile Send private message AIM Address MSN Messenger
Robotex
Master Cheater
Reputation: 0

Joined: 05 Sep 2006
Posts: 378
Location: The pizza country!

PostPosted: Sat Sep 20, 2008 9:06 am    Post subject: Reply with quote

jackyyll wrote:
Code:

std::map< map_db, mapkey > map;
std::map< map_db, mapkey >::iterator it;

for ( it = map.begin(); it != map.end(); it++ )
{
    std::cout << "key: " << it->first << " value: " << it->second << std::endl;
    somesavefunction(foo)
}


Edit:
Hmm, didn't fully read what you posted.

So basically you have a map. They map's key is a union and the value is a void * to something that you will manually typecast later to get the information?

How about:

Code:

typedef union {
    int keyTypeOne;
    char *keyTypeTwo;
    double keyTypeThree;
    string keyTypeFour;
}Key;

std::map< Key, void * > theMap;
theMap::iterator it;

for ( it = theMap.begin(); it != theMap.end(); it++ )
{
      if ( it->first.keyTypeOne == 12 )
           int value = *( int * )it->second;
}


Not sure if this will work though..

no what you said is the C source wich I am porting to C++

Actually my class is like that
Code:

template <class TKey, class TValue>
class CDBMap
{
public:
   typedef TValue* (*DBCreateData)(TKey key, va_list args);
   typedef typename std::map<TKey, TValue*>::iterator Iterator;
   TValue* Put(TKey key, TValue* value);
   TValue* Get(TKey key);
   TValue* Remove(TKey key);
   TValue* Ensure(TKey key, DBCreateData create, ...);
   TValue* VEnsure(TKey key, DBCreateData create, va_list args);
   size_t Size();
   Iterator Begin() {return m_dbtable.begin();}
   Iterator End() {return m_dbtable.end();}

private:
   std::map<TKey, TValue*> m_dbtable;
};

CDBMap<int, class CSessionData> char_db; // int charid -> CSessionData*

and what I am trying to do is make something like an interface (get_first, is_next, etc) so I don't have to expose the database object itself (I thought about templated functions, but they are giving me linking errors)

//Edit: I guess I've found a way to do what I want
Code:

template <class TKey, class TValue>
class CMapIt
{
public:
   CMapIt(typename CDBMap<TKey, TValue>::Iterator begin, typename CDBMap<TKey, TValue>::Iterator end) :
     m_iter(begin), m_end(end)
     {}
   virtual ~CMapIt()
     {}
   bool Exist()
   {
      return (m_iter != m_end);
   }
   void* Value()
   {
      return (m_iter == m_end) ? NULL : m_iter->second;
   }
   void* NextValue()
   {
      return ((++m_iter) == m_end) ? NULL : m_iter->second;
   }
   static CMapIt* Make()
   //CMapIt<TKey, TValue>* CMapIt<TKey, TValue>::Make()
   {
      CMapIt* mapit;

      mapit = new CMapIt(pc_db.Begin(), pc_db.End());
      return mapit;
   }
private:
   typename CDBMap<TKey, TValue>::Iterator m_iter;
   typename CDBMap<TKey, TValue>::Iterator m_end;
};

_________________

ASM/C++ Coder
Project Speranza lead developer
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites