Jump to content
Sign in to follow this  
Graeme

Templates For Prototype In Header

Recommended Posts

Hi,

 

My understanding in C is that the header file is not meant for code, only for prototypes and const etc... am i wrong? When i look at the samples suppled i see the drivers (like the LCD driver) has ALL its code in the header.

 

My main question is :

 

How do I define a prototype for a fuction that uses a template in a header file. I have tried in the example below, but the compiler does not give any decent error. It either just says : 'Removing target xxxx.obj' or it just says : 'Failure' with no explination or reason why.

 

If I take the code out of the C file and put it all in the header then it compiles fine, but as I mentioned ealier, i though the header file was not meant for code.

 

 

Many thanks in advance

Graeme

 

Im my header file i have : (keyboard.h)

 

#define kbTemplate template <unsigned short DataPort, unsigned char DataBit>

//Prototypes

kbTemplate // This does not help

unsigned short kbGetDataBits() ; //(**** This is the bit, how do i define this prototype with the template ****)

 

 

 

In My C file I have : (keyboard.c)

#include "Keyboard.h"

kbTemplate

unsigned short kbGetDataBits(void)

{

......... Do all my processing .......

}

 

 

In my main C file (test.C) I have

 

#include <system.h>

#include <stdio.h>

#include "Keyboard.h"

 

#define kbARGS PORTB,1

 

void interrupt(void)

{

int inBit = 0;

unsigned short res;

char *chrout="0000";

if (intcon.1)

{

clear_bit(intcon,INTE); // disable Interrupt

trisb.0 = 0; // make clock an output

portb.0 = 0; // make it low

trisb.1 = 0;

 

res = kbGetDataBits<kbARGS>();

if (res>>8 == kbDataByteOK)

{

res &=0xFF;

sprintf(chrout,"%X",(res));

writeString(chrout);

}

 

portb.1 = 0;

trisb.0 = 1; // make clock an input

trisb.1 = 1;

set_bit(intcon,INTE); // Enable Interrupt

intcon.1=0;

}

 

}

Share this post


Link to post
Share on other sites
Hi,

 

My understanding in C is that the header file is not meant for code, only for prototypes and const etc... am i wrong? When i look at the samples suppled i see the drivers (like the LCD driver) has ALL its code in the header...

 

Your understanding is quite correct. Standard practice is when prototypes go to headers and implementation to sources. The only exceptions are inline functions and templates. The reason for this is that for both compiler needs to know their implementation every time such functions get called.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
Hi,

 

My understanding in C is that the header file is not meant for code, only for prototypes and const etc... am i wrong? When i look at the samples suppled i see the drivers (like the LCD driver) has ALL its code in the header...

 

Your understanding is quite correct. Standard practice is when prototypes go to headers and implementation to sources. The only exceptions are inline functions and templates. The reason for this is that for both compiler needs to know their implementation every time such functions get called.

 

Regards,

Pavel

 

Thanks for the quick reply,

 

Thats sad - cant keep my code as clean then.

 

Is it better to use templates rather than pass in the Port and Pin Id in an INIT finction and store them in local variables?

 

void initKeyboard (portb,1) sort of thing ?

{

 

}

Share this post


Link to post
Share on other sites
Hi,

 

My understanding in C is that the header file is not meant for code, only for prototypes and const etc... am i wrong? When i look at the samples suppled i see the drivers (like the LCD driver) has ALL its code in the header...

 

Your understanding is quite correct. Standard practice is when prototypes go to headers and implementation to sources. The only exceptions are inline functions and templates. The reason for this is that for both compiler needs to know their implementation every time such functions get called.

 

Regards,

Pavel

 

Thanks for the quick reply,

 

Thats sad - cant keep my code as clean then.

 

Is it better to use templates rather than pass in the Port and Pin Id in an INIT finction and store them in local variables?

 

void initKeyboard (portb,1) sort of thing ?

{

 

}

 

You can pass port and pin as function arguments but this will involve pointer math what in turn will produce larger code compared to template approach.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...