How do you make an object in C? Used in RTOS.
2014-07-17 14:32
337 查看
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.
Ref:
1. lec note of EE 445M
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
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 445M
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
相关文章推荐
- How do you check if a variable is an array in JavaScript? [duplicate]
- How do I list all fields of an object in Objective-C?
- How do I get the name of an object's type in JavaScript
- In C how do you redirect stdin/stdout/stderr to files when making an execvp() or similar call?
- How do I make my GUI plot into an axes within the GUI figure rather than inside of a new figure in M
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- In an iOS 5 Storyboard, how do you push a new scene to the original view controller from a Popover?
- How do you make an awesome team?(来考验一下自己的英语能力吧)
- In C++, which of the following syntax can be used for instantiating an object named arr of the vecto
- How do you turn a Mongoose document into a plain object?
- How can I get an object's absolute position on the page in Javascript?
- How to make an image move in a circular path using jquery?
- How do I remove a particular element from an array in JavaScript?
- 如何在Visual Studio的工程中共享代码 l How do you share code between projects/solutions in Visual Studio
- How do you get the length of a string in jQuery?
- How do you select a particular option in a SELECT element in jQuery? - Stack Overflow
- What is Logical Volume Management and How Do You Enable It in Ubuntu?
- 如何在Visual Studio的工程中共享代码 l How do you share code between projects/solutions in Visual Studio
- JQuery怎么知道一个元素是否隐藏或显示How do you test if something is hidden in jQuery?
- Bubble Gum, Bubble Gum, in the dish, how many pieces do you wish?”