transform into templates

M

Mosfet

Hi,

How can I transform the following functions into templated version :

class foo
{
public:

Ret_t GetProfileCount( UShort_t& a_nCount )
{
return Get( SM_PRIV_CMD_GET_PROFILE_COUNT,
0,
0,
( ULong_t ) sizeof( UShort_t ),
( MemPtr_t ) &a_nCount );
}
};



I tried this :

class foo
{
public:

template <class Type>
Ret_t GetProp1(Short_t a_cmdId, Type& a_argType)
{
return Get( a_cmdId,
0,
0,
( a_argType ) sizeof( a_argType ),
( MemPtr_t ) &a_argType );
}

typedef GetProp1<UShort_t> GetProfileCount;
};


but of course it doesn't work like that.

Some help is needed.
 
V

Victor Bazarov

Mosfet said:
Hi,

How can I transform the following functions into templated version :

class foo
{
public:

Ret_t GetProfileCount( UShort_t& a_nCount )
{
return Get( SM_PRIV_CMD_GET_PROFILE_COUNT,
0,
0,
( ULong_t ) sizeof( UShort_t ),
( MemPtr_t ) &a_nCount );
}
};



I tried this :

class foo
{
public:

template <class Type>
Ret_t GetProp1(Short_t a_cmdId, Type& a_argType)
{
return Get( a_cmdId,
0,
0,
( a_argType ) sizeof( a_argType ),

Should probably be

(ULong_t) sizeof( a_argType ),

....
( MemPtr_t ) &a_argType );
}

typedef GetProp1<UShort_t> GetProfileCount;
};


but of course it doesn't work like that.

Not sure what you mean by "doesn't work".

V
 
M

Mosfet

Victor Bazarov a écrit :
Should probably be

(ULong_t) sizeof( a_argType ),

...


Not sure what you mean by "doesn't work".

V
error C2146: syntax error : missing ';' before identifier 'GetProfileCount'
error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int
error C2365: 'CSyncClientSession::GetProp1' : redefinition; previous
definition was 'member function'
error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int
 
V

Victor Bazarov

Mosfet said:
Victor Bazarov a écrit :
error C2146: syntax error : missing ';' before identifier
'GetProfileCount' error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
error C2365: 'CSyncClientSession::GetProp1' : redefinition; previous
definition was 'member function'
error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int

FAQ 5.8.

V
 
V

Victor Bazarov

Mosfet said:
Victor Bazarov a écrit :
I am not sure it's MS specific. I think I made a mistake ...

Who said anything about MS specificity? I don't want to assume anything
about your 'ULong_t', 'Ret_t' and so on. Do you have them defined? Do
not make me guess. If you want help, provide enough information. And
do read the FAQ.

V
 
M

Mosfet

Victor Bazarov a écrit :
Who said anything about MS specificity? I don't want to assume anything
about your 'ULong_t', 'Ret_t' and so on. Do you have them defined? Do
not make me guess. If you want help, provide enough information. And
do read the FAQ.

V
Ok sorry.
Yes it's define like that

typedef short Short_t; // short integer, 16 Bytes
typedef long Long_t; // long integer, 32 Bytes
typedef char Char_t;
typedef unsigned short UShort_t; // unsigned short integer, 16 Bytes
typedef unsigned long ULong_t; // unsigned long integer, 32 Bytes
typedef char* String_t; // String pointer,
typedef unsigned char Byte_t; // a single byte
typedef Byte_t Boolean_t; // a boolean
typedef Short_t Ret_t; // Return Type of API Commands
typedef Long_t Length_t; // System dependent string length
typedef Short_t MemHandle_t; // Memory object Handle
typedef unsigned char* MemPtr_t; // Memory object Pointer
typedef void* VoidPtr_t; // Void Pointer
typedef Long_t MemSize_t; // System dependent memory object size
typedef unsigned char MemByte_t; // Memory element
typedef unsigned int Flag_t; // A generic flag type. This type is
used to
// declare variables in structures
wherever
// flags are used.

class foo
{
public:

foo();
virtual ~foo();

template <class Type>
Ret_t GetProp1(Short_t a_cmdId, Type& a_argType)
{
return Get( a_cmdId,
0,
0,
( a_argType ) sizeof( a_argType ),
( MemPtr_t ) &a_argType );
}


typedef GetProp1<SM_PRIV_CMD_GET_PROFILE_COUNT, UShort_t> GetProfileCount;
};
 
G

Gianni Mariani

Hi,

How can I transform the following functions into templated version :

class foo
{
public:

Ret_t GetProfileCount( UShort_t& a_nCount ) typename Traits::> {
return Get( SM_PRIV_CMD_GET_PROFILE_COUNT,
0,
0,
( ULong_t ) sizeof( UShort_t ),
( MemPtr_t ) &a_nCount );

}
};

I tried this :

class foo
{
public:

template <class Type>
Ret_t GetProp1(Short_t a_cmdId, Type& a_argType)
{
return Get( a_cmdId,
0,
0,
( a_argType ) sizeof( a_argType ),
( MemPtr_t ) &a_argType );

}

typedef GetProp1<UShort_t> GetProfileCount;

};

but of course it doesn't work like that.

Some help is needed.


Is the correlation between SM_PRIV_CMD_GET_PROFILE_COUNT (and other
types) and the argument ?

Then, you need to couple these things somehow:

i.e.

struct ProfileCount
{
enum { cmd_id = SM_PRIV_CMD_GET_PROFILE_COUNT };
typedef ULong_t & param_t;
};

Now define a Get method

template <typename Traits>
Ret_t Get( typename Traits::param_t param )
{
return Get(
Traits::cmd_id, 0, 0, sizeof( param ),
reinterpret_cast<MemPtr_t>( & param )
);
}

Now you can write

if ( Get<ProfileCount>( val ) == OK )

So, now you need to ask yourself, did it make a difference to you -
probably not much.

val = Get<ProfileCount>();

That would be a bit better - maybe you can define another Get like
(and keep them both)

.... first add a few things here
struct ProfileCount
{
enum { cmd_id = SM_PRIV_CMD_GET_PROFILE_COUNT };
typedef ULong_t result_t;
typedef result_t & param_t;
};

template <typename Traits>
typename Traits::result_t Get()
{
typename Traits::result_t val;
Ret_t ret = Get(
Traits::cmd_id, 0, 0, sizeof( val ),
reinterpret_cast<MemPtr_t>( & val )
);

if ( ret != OK )
{
abort(); // if it is a programming error
throw Exception<ProfileCount>();
}

return val;
}

OK - now you have two ways to call these GET methods and one lump of
code.

Without knowing more about what it is exactly that you need to do, I
have no idea how to help.

As a guess, you can add some methods in your traits class depending on
the type you're dealing with.

Example: You wanted to return a vector and the Get called required an
array, then you need to provide a method of converting a reference to
a vector to a reference to an array which would work also for non
array types.

Do you need to do somthing like:

std::list<std::string> val = Get<Names>();

If so, you need to add some more stuff to your traits class so that it
works for all types.
 
V

Victor Bazarov

Mosfet said:
[..some senseless type renaming snipped..]
class foo
{
public:

foo();
virtual ~foo();

template <class Type>
Ret_t GetProp1(Short_t a_cmdId, Type& a_argType)
{
return Get( a_cmdId,
0,
0,
( a_argType ) sizeof( a_argType ),
( MemPtr_t ) &a_argType );
}


typedef GetProp1<SM_PRIV_CMD_GET_PROFILE_COUNT, UShort_t>

'GetProp1' has only _one_ template argument.
GetProfileCount; };

'GetProp1' is a function template. I don't think you can use that
template-id to form a typedef-id. If you're trying to do what I
think you're trying to do, you'd be much better off with a simple
function that calls the template instance:

Ret_t GetProfileCount(UShort_t & arg) {
return GetProp1(SM_PRIV_CMD..., arg);
}

V
 

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

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,211
Latest member
ChloeK3656

Latest Threads

Top