parent
0a12b12eb9
commit
51b7b7c448
@ -1,28 +0,0 @@
|
|||||||
#include <memory>
|
|
||||||
|
|
||||||
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 <typename T>
|
|
||||||
struct PinCRTP : public Pin
|
|
||||||
{
|
|
||||||
virtual override set(bool logic)
|
|
||||||
{
|
|
||||||
static_cast<>
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -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
|
@ -0,0 +1,113 @@
|
|||||||
|
#include <memory>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// https://www.modernescpp.com/index.php/c-is-still-lazy
|
||||||
|
|
||||||
|
//
|
||||||
|
// CRTP base class of pin
|
||||||
|
//
|
||||||
|
|
||||||
|
/*
|
||||||
|
avoiding virtual call (static polymorphism)
|
||||||
|
*/
|
||||||
|
|
||||||
|
template <typename Derived>
|
||||||
|
struct PinBase
|
||||||
|
{
|
||||||
|
|
||||||
|
void set(bool logic)
|
||||||
|
{
|
||||||
|
static_cast<Derived*>(this)->setImp(logic);
|
||||||
|
}
|
||||||
|
|
||||||
|
void toggle()
|
||||||
|
{
|
||||||
|
static_cast<Derived*>(this)->toggleImp();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get(void)
|
||||||
|
{
|
||||||
|
return static_cast<Derived*>(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<STM32_Pin>
|
||||||
|
{
|
||||||
|
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<AVR_Pin>
|
||||||
|
{
|
||||||
|
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<typename T>
|
||||||
|
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;
|
||||||
|
}
|
Binary file not shown.
Loading…
Reference in new issue