|
|
2 anni fa | |
|---|---|---|
| .. | ||
| LICENSE | 3 anni fa | |
| PLOOC.pyi | 3 anni fa | |
| README.md | 3 anni fa | |
| plooc.h | 2 anni fa | |
| plooc_class.h | 2 anni fa | |
| plooc_class_black_box.h | 2 anni fa | |
| plooc_class_simple.h | 2 anni fa | |
| plooc_class_simple_c90.h | 2 anni fa | |
| plooc_class_strict.h | 2 anni fa | |
The Protected Low-overhead Object Oriented Programming with ANSI-C, a.k.a PLOOC ['plu:k] is a set of well-polished C macro templates which:
NOTE: The protection can be disabled by defining macro __OOC_DEBUG__ to facilitate debug.
Low-overhead
NOTE: Almost ZERO OVERHEAD. The template fully utilises the ANSI-C enforced compilation rules to deliver desired OO features with the the least necessary cost.
- Suitable for both bare-metal and RTOS.
- Suitable for both 8bit and 32bit MCU
The concept of OOC is not new. There are plenty of libraries, SDKs, templates providing objected-oriented programming extensions to ANSI-C language. Although PLOOC emphasises its low-overhead feature in both code size and performance, but a lot of macro template based ooc solutions are also low-overhead. PLOOC doesn't force you to use heap or pool for memory management, it doesn't provide GC feature. It simply leaves those options to users, so it is suitable for even 8bit system. Well, you can take this as draw-backs of PLOOC. I don't want to argue about this.
So what really sets PLOOC different from the others? Is it simply another re-invented wheel?
The answer is NO. Of course. PLOOC brings an unique feature most of others don't have. It lets private members of a class truly become private, i.e. protected. So users outside of the class source code are prevented from accessing the private members. What they see will be a solid memory, a mask created with byte array. Since class is mimicked by structure in C, the class in PLOOC is implemented with the masked-structure. As people expected, only class source code can access the private members, only class source code of a derived class can access the protected members of the base class and everyone can access the public members.
How could this be? You might already figure it out simply through the word "masked-structure". As you noticed, it could be nothing more than a fancy type-cheating trick in header files. The type-cheating trick works well until some strict-type-checking compiler is encountered. The most famous (notorious) one is IAR with multi-file compilation mode enabled. No type-cheating can survive from the bloody axe of IAR multi-file compilation mode.
//! the original structure in class source code
struct byte_queue_t {
uint8_t *pchBuffer;
uint16_t hwBufferSize;
uint16_t hwHead;
uint16_t hwTail;
uint16_t hwCount;
};
//! the masked structure: the class byte_queue_t in header file
typedef struct byte_queue_t {
uint8_t chMask [sizeof(struct {
uint8_t *pchBuffer;
uint16_t hwBufferSize;
uint16_t hwHead;
uint16_t hwTail;
uint16_t hwCount;
})];
} byte_queue_t;
In order to make it work, we have to make sure the class source codes don't include their own interface header file. you can even do this...if you are serious about the content
//! the masked structure: the class byte_queue_t in header file
typedef struct byte_queue_t {
uint8_t chMask [sizeof(struct {
uint32_t : 32;
uint16_t : 16;
uint16_t : 16;
uint16_t : 16;
uint16_t : 16;
})];
} byte_queue_t;
NOTE: Aforementioned code pieces are only used to show the underlying scheme but are not practical, as you can see, the alignment of the original structure is missing when creating the mask array. To solve the problem, you have to extract the alignment information via __alignof__() operator and set the mask array alignment attribute accordingly using __attribute__((aligned())).
PLOOC provides the "private-protection" feature with a different scheme other than type-cheating, so it support almost all C compilers with C99 feature enabled. As the author, I have to confess that it took me a lot of time to figure it out how to deal with strict-type-checking and the initial scheme was ugly and counter-intuition. Thanks to some inspiring contribution of SimonQian, it took me another 3 months to make PLOOC elegant and simple. The supports from HenryLong are also vital.
I hope you can enjoy this unique trying for the object-oriented programming challenge.
If you have any questions or suggestions, please feel free to let us know.
[12/05/2022] Improve compatibility with the latest C++ language, version 4.6.2
[02/01/2022] Add helper macros for heap, version 4.6.1
[30/12/2021] Improved CMSIS-Pac, version 4.6.0
[29/12/2021] Add CMSIS-Pack, version 4.5.9
[28/11/2020] Minor update, version 4.5.7
[05/08/2020] Add __PLOOC_CLASS_IMPLEMENT__ and __PLOOC_CLASS_INHERIT__ version 4.5.6
[18/05/2020] Introduce both short- and long- style of macro, version 4.5.5
[16/05/2020] Minor Update, version 4.5.4a
[11/05/2020] Minor Update, version 4.5.4
[15/04/2020] Update __PLOOC_EVAL, version 4.5.3
[19/02/2020] Minor update to enable RAM footprint optimisation, version 4.52
[21/01/2020] Misc update for C90, version 4.51
[09/06/2019] Added support for C89/90, version 4.50
[09/05/2019] Added support for C89/90, version 4.40
[08/15/2019] Updated plooc_class_strict.h to use more soften syntax, version 4.31
[08/14/2019] Introduced support for limited support for overload, version 4.30
[07/26/2019] Syntax update, version 4.21
[07/24/2019] Added new ooc class template, version 4.20
[07/12/2019] Minor Update, version 4.13
[05/30/2019] Minor Update, version 4.12
[05/02/2019] Efficiency improve, version 4.11
[05/01/2019] Compatibility Improving, version 4.04
[04/20/2019] Upload PLOOC to github, version 4.03
[04/17/2019] Uploaded PLOOC to github, version 4.01
The PLOOC library was written by GorgonMeducer(王卓然) and Simon Qian(钱晓晨)https://github.com/versaloon with support from Henry Long .
The PLOOC library is released under an open source license Apache 2.0 that allows both commercial and non-commercial use without restrictions. The only requirement is that credits is given in the source code and in the documentation for your product.
The full license text follows:
/*****************************************************************************
* Copyright(C)2009-2019 by GorgonMeducer<embedded_zhuoran@hotmail.com> *
* and SimonQian<simonqian@simonqian.com> *
* with support from HenryLong<henry_long@163.com> *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
****************************************************************************/
| module | Contrinutor |
|---|---|
| plooc.h | GorgonMeducer |
| plooc_class.h | GorgonMeducer, Simon Qian |
| plooc_class_strict.h | GorgonMeducer |
| plooc_class_back_box.h | GorgonMeducer |
| plooc_class_simple.h | Simon Qian |
| plooc_class_simple_c90.h | GorgonMeducer |
| module | Contrinutor |
|---|---|
| How to define a class | GorgonMeducer |
| How to access protected members | GorgonMeducer |
| How to implement Polymorphism | GorgonMeducer |
In order to show how PLOOC is easy and simple to use, examples are provided to demonstrate different aspects of the new OOPC method. Currently, the available examples are:
More examples will be added later...
This example shows
### Example 2: How to access protected members
### Example 3: How to implement Overload
How to implement overload using PLOOC
Require C11 support
LOG_OUT("\r\n-[Demo of overload]------------------------------\r\n");
LOG_OUT((uint32_t) 0x12345678);
LOG_OUT("\r\n");
LOG_OUT(0x12345678);
LOG_OUT("\r\n");
LOG_OUT("PI is ");
LOG_OUT(3.1415926f);
LOG_OUT("\r\n");
LOG_OUT("\r\nShow BYTE Array:\r\n");
LOG_OUT((uint8_t *)main, 100);
LOG_OUT("\r\nShow Half-WORD Array:\r\n");
LOG_OUT((uint16_t *)main, 100/sizeof(uint16_t));
LOG_OUT("\r\nShow WORD Array:\r\n");
LOG_OUT((uint32_t *)main, 100/sizeof(uint32_t));