ClickHouse/libs/libcommon/include/ext/shared_ptr_helper.h
Ivan a502424c33
Implement support for insertion into Kafka tables (#6012)
* Add write-callback on each row for RowOutputStream
* Fix build of new rdkafka library
* Poll messages if Kafka outgoing queue is full
* Add test
* Add test producer-consumer
* Truncate delimiter from last row in message
2019-08-20 14:17:57 +03:00

40 lines
1.1 KiB
C++

#pragma once
#include <memory>
namespace ext
{
/** Allows to make std::shared_ptr from T with protected constructor.
*
* Derive your T class from shared_ptr_helper<T>
* and you will have static 'create' method in your class.
*
* Downsides:
* - your class cannot be final;
* - awful compilation error messages;
* - bad code navigation.
* - different dynamic type of created object, you cannot use typeid.
*/
template <typename T>
struct shared_ptr_helper
{
template <typename... TArgs>
static auto create(TArgs &&... args)
{
/** Local struct makes protected constructor to be accessible by std::make_shared function.
* This trick is suggested by Yurii Diachenko,
* inspired by https://habrahabr.ru/company/mailru/blog/341584/
* that is translation of http://videocortex.io/2017/Bestiary/#-voldemort-types
*/
struct Local : T
{
Local(TArgs &&... args) : T(std::forward<TArgs>(args)...) {}
};
return std::make_shared<Local>(std::forward<TArgs>(args)...);
}
};
}