Skip to content

Instantly share code, notes, and snippets.

@FONQRI
Last active July 28, 2021 07:38
Show Gist options
  • Save FONQRI/eafe465e1e51ec0cdf3828a49a5b86e5 to your computer and use it in GitHub Desktop.
Save FONQRI/eafe465e1e51ec0cdf3828a49a5b86e5 to your computer and use it in GitHub Desktop.
/***************************************************
* File add_static_make.h
* Copyright (C) 2021-6-19 ModernCpp.ir . All Rights Reserved. *
***************************************************/
#ifndef ADD_STATIC_MAKE_H
#define ADD_STATIC_MAKE_H
#include <memory>
template<class B, class T = std::nullptr_t>
class add_static_make
{
public:
add_static_make() = default;
template<class ... Args>
static std::shared_ptr<B> make(Args... agrs)
{
return std::make_shared<
typename std::conditional<std::is_same<T, std::nullptr_t>::value, B, T>::type>(std::forward<Args>(agrs)...);
}
};
#endif // ADD_STATIC_MAKE_H
// Example program
#include <iostream>
#include <string>
/***************************************************
* File empire_command_interface.h
* Copyright (C) 2021-6-5 ModernCpp.ir . All Rights Reserved. *
***************************************************/
#ifndef EMPIRE_COMMAND_INTERFACE_H
#define EMPIRE_COMMAND_INTERFACE_H
#include <string>
class empire_command_interface
{
public:
empire_command_interface(std::string* msg){}
virtual std::string execute() = 0;
virtual std::string name() = 0;
};
#endif // EMPIRE_COMMAND_INTERFACE_H
/***************************************************
* File add_static_make.h
* Copyright (C) 2021-6-19 ModernCpp.ir . All Rights Reserved. *
***************************************************/
#ifndef ADD_STATIC_MAKE_H
#define ADD_STATIC_MAKE_H
#include <memory>
template<class B, class T = std::nullptr_t>
class add_static_make
{
public:
add_static_make() = default;
template<class ... Args>
static std::shared_ptr<B> make(Args... agrs)
{
return std::make_shared<
typename std::conditional<std::is_same<T, std::nullptr_t>::value, B, T>::type>(std::forward<Args>(agrs)...);
}
};
#endif // ADD_STATIC_MAKE_H
/***************************************************
* File get_kingdom_connections_info_command.h
* Copyright (C) 2021-6-10 ModernCpp.ir . All Rights Reserved. *
***************************************************/
#ifndef GET_KINGDOM_CONNECTIONS_INFO_COMMAND_H
#define GET_KINGDOM_CONNECTIONS_INFO_COMMAND_H
#include <memory>
class get_kingdom_connections_info_command
: public empire_command_interface,
public add_static_make<empire_command_interface, get_kingdom_connections_info_command>
{
public:
get_kingdom_connections_info_command(std::string* msg):empire_command_interface(msg) {}
// empire_command_interface interface
public:
std::string execute() override
{
return "";
}
std::string name() override
{
return "get_kingdom_connections_info_command";
}
};
#endif // GET_KINGDOM_CONNECTIONS_INFO_COMMAND_H
/***************************************************
* File restrict_gate_command.h
* Copyright (C) 2021-6-10 ModernCpp.ir . All Rights Reserved. *
***************************************************/
#ifndef RESTRICT_GATE_COMMAND_H
#define RESTRICT_GATE_COMMAND_H
#include <memory>
class restrict_gate_command
: public empire_command_interface,
public add_static_make<empire_command_interface, restrict_gate_command>
{
public:
explicit restrict_gate_command(std::string* msg):empire_command_interface(msg) {}
// static std::shared_ptr<restrict_gate_command> make();
// empire_command_interface interface
public:
std::string execute() override
{
return "";
}
std::string name() override
{
return "restrict_gate_command";
}
};
#endif // RESTRICT_GATE_COMMAND_H
int main()
{
std::string msg("test command");
auto rgc = restrict_gate_command::make(&msg);
auto gkcic = get_kingdom_connections_info_command::make(&msg);
std::cout << rgc->name() << std::endl;
std::cout << gkcic->name() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment