вторник, 9 января 2024 г.

Статический полиморфизм в С++

 #include <iostream>  
   
 // ------------------------ interface ------------------------  
 template<typename T>  
 struct IMeta  
 {  
   inline void foo() { return static_cast<T*>(this)->_foo(); }  
 };  
   
 template<typename T>  
 struct IPayload  
 {  
   inline void bar() { return static_cast<T*>(this)->_bar(); }  
 };  
   
 template<typename T>  
 struct IStream : public T, public IPayload<IStream<T>>, public IMeta<IStream<T>>  
 {  
   inline void baz() { return static_cast<T*>(this)->_baz(); }  
 };  
   
 // ------------------------ implementation ------------------------  
 template<typename T>  
 class Meta  
 {  
   friend class IMeta<IStream<T>>;  
   
   inline void _foo()  
      {  
           std::cout << "Meta::_foo::a = " << static_cast<T*>(this)->a << std::endl;  
      }  
 };  
   
 template<typename T>  
 class Payload  
 {  
   friend class IPayload<IStream<T>>;  
   
   inline void _bar()  
      {  
           std::cout << "Payload::_bar::a = " << static_cast<T*>(this)->a << std::endl;  
      }  
 };  
   
 class Stream : public Meta<Stream>, public Payload<Stream>  
 {  
   friend class IStream<Stream>;  
   friend class Meta<Stream>;  
   friend class Payload<Stream>;  
     
   uint64_t a = 3;  
   
   inline void _baz()  
      {  
           std::cout << "Stream::_baz::a = " << a << std::endl;  
      }  
 };  
   
 // ------------------------ usage ------------------------  
 int main()  
 {  
   IStream<Stream> stream;  
   
   stream.foo();  
   stream.bar();  
   stream.baz();  
   
   return 0;  
 }  
   

вторник, 2 января 2024 г.

С++17: автоопределение в runtime потока вызова для функции

 #include <iostream>  
 #include <thread>  
   
 void foo(unsigned char *data)  
 {  
   thread_local std::thread::id currentId(std::this_thread::get_id());  
   
   static std::thread::id id1(std::this_thread::get_id());  
   if (currentId == id1)  
   {   
     std::cout << "Thread1" << std::endl;  
     return;  
   }  
   
   static std::thread::id id2(std::this_thread::get_id());  
   if (currentId == id2)  
   {   
     std::cout << "Thread2" << std::endl;  
     return;  
   }  
   
   throw std::runtime_error("Thread is not processed!");  
 }  
   
 int main()  
 {  
   unsigned char *ptr;  
     
   std::thread thread1([&] { foo(ptr); });  
   std::thread thread2([&] { foo(ptr); });  
   //std::thread thread3([&] { foo(ptr); });  // will be an exception
   
   thread1.join();  
   thread2.join();  
   //thread3.join();  
   
   return 0;  
 }  

Перегруженный макрос

https://ideone.com/Kp59hv
 #include <stdio.h>  
 #include <stdint.h>  
 #include <stdlib.h>  
    
 /*   
  "Variadic macros tricks"  
    https://w...content-available-to-author-only...t.co/2014/11/25/variadic-macros-tricks/  
  "Is it possible to iterate over arguments in variadic macros?"  
    https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros/  
 */  
    
 #define _GET_OVERRIDE(ARG_1, ARG_2, ARG_3, ACTION, ...) ACTION  
    
 #define MALLOC(pMem)         \  
 (                  \  
  {                 \  
   pMem = malloc(sizeof(*pMem));  \  
   pMem ? 0 : 1;          \  
  }                 \  
 )  
    
 #define MALLOC_1(ARG_1, ...) MALLOC(ARG_1)  
 #define MALLOC_2(ARG_1, ...) MALLOC(ARG_1) | MALLOC_1(__VA_ARGS__)  
 #define MALLOC_3(ARG_1, ...) MALLOC(ARG_1) | MALLOC_2(__VA_ARGS__)  
    
 /* with return value */  
 #define MULTI_MALLOC(...) _GET_OVERRIDE(__VA_ARGS__, MALLOC_3, MALLOC_2, MALLOC_1)(__VA_ARGS__)  
    
 #define FREE(pMem)  \  
 {           \  
  free(pMem);     \  
  pMem = NULL;    \  
 }  
    
 #define FREE_1(ARG_1, ...) FREE(ARG_1)  
 #define FREE_2(ARG_1, ...) FREE(ARG_1) FREE_1(__VA_ARGS__)  
 #define FREE_3(ARG_1, ...) FREE(ARG_1) FREE_2(__VA_ARGS__)  
    
 /* without return value */  
 #define MULTI_FREE(...) _GET_OVERRIDE(__VA_ARGS__, FREE_3, FREE_2, FREE_1)(__VA_ARGS__)  
    
 int main(void)   
 {  
  uint16_t *pnShort = NULL;  
  uint32_t *pnInt  = NULL;  
  uint64_t *pnLong = NULL;  
    
  printf("start: %p, %p, %p\n", pnShort, pnInt, pnLong);  
    
  if ( MULTI_MALLOC(pnShort, pnInt, pnLong) ) { return 1; }  
  printf("3 args: %p, %p, %p\n", pnShort, pnInt, pnLong);  
  MULTI_FREE(pnShort, pnInt, pnLong);  
    
  if ( MULTI_MALLOC(pnShort, pnInt) ) { return 1; }  
  printf("2 args: %p, %p, %p\n", pnShort, pnInt, pnLong);  
  MULTI_FREE(pnShort, pnInt);  
    
  if ( MULTI_MALLOC(pnShort) ) { return 1; }  
  printf("1 arg: %p, %p, %p\n", pnShort, pnInt, pnLong);  
  MULTI_FREE(pnShort);  
    
  printf("finish: %p, %p, %p\n", pnShort, pnInt, pnLong);  
    
  return 0;  
Бен Клеменс "Язык С в XXI веке" гл.10 "Улучшенная структура" параграф "Векторизация функции" стр.218.