How do you make an object in C? Used in RTOs.

Source: Internet
Author: User

I went through the ee445m and found it's interesting since the goal of this class is to build a working RTOS. the first LEC note mentioned how to make an object in C. the example he used is the "FIFO ".

Http://stackoverflow.com/questions/2750270/c-c-struct-vs-class

There are 2 things to learn:

1. "#": this is called token paste operator, "\" is like "…" In Matlab, just means next line is connected to current line.

2. c doesn't support OOP (encapsulation, inheritance, polymorphism). In here, we are trying to mimic encapsulation in C.

// macro to create a pointer FIFO// Comment: NAME is the param to rename the var, functions inside the "class"#define AddPointerFifo(NAME,SIZE,TYPE,SUCCESS,FAIL) \TYPE volatile *NAME ## PutPt;    TYPE volatile *NAME ## GetPt;    TYPE static NAME ## Fifo [SIZE];        void NAME ## Fifo_Init(void){ long sr;    sr = StartCritical();                   NAME ## PutPt = NAME ## GetPt = &NAME ## Fifo[0];   EndCritical(sr);                      }                                       int NAME ## Fifo_Put (TYPE data){         TYPE volatile *nextPutPt;               nextPutPt = NAME ## PutPt + 1;          if(nextPutPt == &NAME ## Fifo[SIZE]){     nextPutPt = &NAME ## Fifo[0];         }                                       if(nextPutPt == NAME ## GetPt ){          return(FAIL);                         }                                       else{                                     *( NAME ## PutPt ) = data;              NAME ## PutPt = nextPutPt;              return(SUCCESS);                      }                                     }                                       int NAME ## Fifo_Get (TYPE *datapt){      if( NAME ## PutPt == NAME ## GetPt ){     return(FAIL);                         }                                       *datapt = *( NAME ## GetPt ## ++);      if( NAME ## GetPt == &NAME ## Fifo[SIZE]){     NAME ## GetPt = &NAME ## Fifo[0];     }                                       return(SUCCESS);                      }                                       unsigned short NAME ## Fifo_Size (void){  if( NAME ## PutPt < NAME ## GetPt ){      return ((unsigned short)( NAME ## PutPt - NAME ## GetPt + (SIZE*sizeof(TYPE)))/sizeof(TYPE));   }                                       return ((unsigned short)( NAME ## PutPt - NAME ## GetPt )/sizeof(TYPE)); }// e.g.,// AddPointerFifo(Rx,32,unsigned char, 1,0)// SIZE can be any size// creates RxFifo_Init() RxFifo_Get() and RxFifo_Put()

Ref:

1. LEC note of EE 445 m

2. http://www.cprogramming.com/reference/preprocessor/token-pasting-operator.html

3. http://complete-concrete-concise.com/programming/c/preprocessor-the-token-pasting-operator

4. http://c.learncodethehardway.org/book/ex19.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.