diff --git a/scribbles_and_notes/pin_interface_CRTP_style/PIN_modeling.cpp b/scribbles_and_notes/pin_interface_CRTP_style/PIN_modeling.cpp deleted file mode 100644 index 84c524a..0000000 --- a/scribbles_and_notes/pin_interface_CRTP_style/PIN_modeling.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include - -struct Pin -{ - virtual void set(bool logic); - - virtual void toggle(); - - virtual bool get(void); -}; - -// https://www.modernescpp.com/index.php/c-is-still-lazy - -// curiously recurring template pattern -template -struct PinCRTP : public Pin -{ - virtual override set(bool logic) - { - static_cast<> - } -}; - - -int main(void) -{ - return 0; -} \ No newline at end of file diff --git a/scribbles_and_notes/pin_interface_CRTP_style/pin_modleing_with_CRTP/Makefile b/scribbles_and_notes/pin_interface_CRTP_style/pin_modleing_with_CRTP/Makefile new file mode 100644 index 0000000..38e5ad5 --- /dev/null +++ b/scribbles_and_notes/pin_interface_CRTP_style/pin_modleing_with_CRTP/Makefile @@ -0,0 +1,24 @@ +cpp_src = $(wildcard *.cpp) +cpp_src += $(wildcard ./utils/*.cpp) +cpp_src += $(wildcard ./driver/*.cpp) + +cpp_obj = $(cpp_src:.cpp=.o) +c_obj = $(c_src:.c=.o) +CC = g++ +CFLAGS = -Wall -pedantic -li2c +LDFLAGS = +EXEC = runtest + + +all : $(EXEC) + +$(EXEC): $(cpp_obj) $(c_obj) + $(CC) -o $@ $^ $(LDFLAGS) + +clean: + rm -rf $(c_obj) $(cpp_obj) $(EXEC) + clear + +cleanall: + rm -rf $(c_obj) $(cpp_obj) $(EXEC) + clear diff --git a/scribbles_and_notes/pin_interface_CRTP_style/pin_modleing_with_CRTP/main.cpp b/scribbles_and_notes/pin_interface_CRTP_style/pin_modleing_with_CRTP/main.cpp new file mode 100644 index 0000000..0c05c7f --- /dev/null +++ b/scribbles_and_notes/pin_interface_CRTP_style/pin_modleing_with_CRTP/main.cpp @@ -0,0 +1,113 @@ +#include +#include + +// https://www.modernescpp.com/index.php/c-is-still-lazy + +// +// CRTP base class of pin +// + +/* + avoiding virtual call (static polymorphism) +*/ + +template +struct PinBase +{ + + void set(bool logic) + { + static_cast(this)->setImp(logic); + } + + void toggle() + { + static_cast(this)->toggleImp(); + } + + bool get(void) + { + return static_cast(this)->getImp(); + } + + private: + + // + // base implementations + // + + void setImp() + { + std::cout << "base implementation of set()!" << std::endl; + } + + void toggleImp() + { + std::cout << "base implementation of toggle()!" << std::endl; + } + + bool getImp(void) + { + std::cout << "base implementation of get()!" << std::endl; + return true; + } +}; + +// +// implementations +// + +struct STM32_Pin : PinBase +{ + void setImp(bool logic) + { + std::cout << "stm32 pin set to " << logic << std::endl; + } + + void toggleImp() + { + std::cout << "toggled stm32 pin" << std::endl; + } + + bool getImp() + { + return true; + } +}; + +struct AVR_Pin : PinBase +{ + void setImp(bool logic) + { + std::cout << "AVR pin set to " << logic << std::endl; + } + + void toggleImp() + { + std::cout << "toggled AVR pin" << std::endl; + } + + bool getImp() + { + return true; + } +}; + +template +void foo(T& base) +{ + base.set(true); + base.set(false); + base.toggle(); +} + +int main(void) +{ + STM32_Pin pin1; + AVR_Pin pin2; + + foo(pin1); + foo(pin2); + + return 0; +} \ No newline at end of file diff --git a/scribbles_and_notes/pin_interface_CRTP_style/pin_modleing_with_CRTP/runtest b/scribbles_and_notes/pin_interface_CRTP_style/pin_modleing_with_CRTP/runtest new file mode 100755 index 0000000..8f3b9a1 Binary files /dev/null and b/scribbles_and_notes/pin_interface_CRTP_style/pin_modleing_with_CRTP/runtest differ