crazy macro problem

C

Chris Ellis

Hi,

I'm trying to write a macro that will generate managed c++ wrappers of
map. I want a managed iterator and a managed map. I successfully built a
pair of classes (iterator and map) that work fine. I called it IntToIntMap
and IntToIntIterator. When I was finished, I realized that if I ever wanted
to map two other data types, I would have to do just as much work, so I
decided to make a macro to fake templating. So, I modeled the macro after
the working classes, but when it was finished it kept getting errors. Next,
I actually copied and pasted the working classes over the macro and re-did
the macro from known working code, but it still kept getting one error, and
for the life of me I can't understand why. Below is the code. I would be
very grateful if anyone could give me a pointer, or point me to a reference
that says why this is happening.

Thanks for any help in advance,
Chris

first, because it's simpler and more concise, here is how I use the macro:

#include "MapMacros.h"

namespace HelperDataStructures
{
MAKE_GC_MAP_AND_ITERATOR( int, int, IntToInt);
}

next... because it is also concise, this is the error it gives me:

C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\map(36) :
error C3602: 'Int_To_Int_Iterator': a __gc type object cannot be passed by
value

lastly, here is the macro definition:

#include <map>

using namespace System;



#define MAKE_GC_MAP_AND_ITERATOR( t1, t2, name )\
public __gc class name##Iterator: public IDisposable\
{\
public private:\
typedef std::map<t1, t2, std::less<t1> > mapType;\
typedef mapType::iterator itType;\
public private:\
itType * m_pIt;\
name##Iterator( itType it ): m_pIt( new itType( it ) ) {}\
public:\
typedef name##Iterator myType;\
name##Iterator( myType *it ): m_pIt( new itType( *it->m_pIt ) ) {}\
~name##Iterator() { Dispose(); }\
__property t1 get_First() { return (*m_pIt)->first; }\
__property t2 get_Second() { return (*m_pIt)->second; }\
__property void set_Second( t2 val ) { (*m_pIt)->second = val; }\
public:\
static bool op_Equality( myType *a, myType *b ) { return
(*a->m_pIt)==(*b->m_pIt); }\
static bool op_Inequality( myType *a, myType *b ) { return
(*a->m_pIt)!=(*b->m_pIt); }\
void Dispose() { delete m_pIt; m_pIt = NULL; }\
};\
[System::Reflection::DefaultMember(S"Item")]\
public __gc class name##map: public IDisposable\
{\
public private:\
typedef std::map<t1, t2, std::less<t1> > mapType;\
typedef name##Iterator gcItType;\
private:\
mapType * m_pMap;\
public:\
name##map(): m_pMap( new mapType ) {}\
~name##map() { Dispose(); }\
public:\
__property int get_Item( int i ) { return (*m_pMap)[ i ]; }\
__property void set_Item( int i, int val ) { (*m_pMap)[ i ] = val; }\
__property gcItType* get_Begin() { return new gcItType(
m_pMap->begin() ); }\
__property gcItType* get_End() { return new gcItType(
m_pMap->end() ); }\
__property int get_Size() { return m_pMap->size(); }\
__property bool get_Empty() { return m_pMap->empty(); }\
public:\
void Clear() { m_pMap->clear(); }\
gcItType* Erase( gcItType* i ) { return new gcItType( m_pMap->erase(
*i->m_pIt ) ); }\
gcItType* Find( int i ) { return new gcItType( m_pMap->find(
i ) ); }\
void Dispose() { delete m_pMap; m_pMap = NULL; }\
}
 
A

Arjun Bijanki [VCPP MSFT]

--------------------
From: "Chris Ellis" <dumpThis@praiseGod777@[email protected]>
Subject: crazy macro problem
Date: Sat, 29 May 2004 10:54:26 -0400
Hi,

I'm trying to write a macro that will generate managed c++ wrappers of
map. I want a managed iterator and a managed map. I successfully built a
pair of classes (iterator and map) that work fine. I called it IntToIntMap
and IntToIntIterator. When I was finished, I realized that if I ever wanted
to map two other data types, I would have to do just as much work, so I
decided to make a macro to fake templating. So, I modeled the macro after
the working classes, but when it was finished it kept getting errors. Next,
I actually copied and pasted the working classes over the macro and re-did
the macro from known working code, but it still kept getting one error, and
for the life of me I can't understand why. Below is the code. I would be
very grateful if anyone could give me a pointer, or point me to a reference
that says why this is happening.

Thanks for any help in advance,
Chris

Generating a preprocessed file is often useful when debugging macros. Try
that, and see if it helps:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html
/_core_.2f.P.asp
 

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

Top