C++11????????????????
???????????? ???????[ 2016/1/19 10:40:43 ] ??????????????????? .NET
?????????????????????
struct factory
{
template<typename T>
struct register_t
{
register_t(const std::string& key)
{
factory::get().map_.emplace(key?? []{ return new T; });
}
};
private:
inline static factory& get()
{
static factory instance;
return instance;
}
static std::map<std::string?? FunPtr> map_;
};
?????????????????register_t??????????????????????????????????????????????????е?????factory????б???map_??????map_?в?????key?????????????????????????C++11??????????????????????????????????????????г????????register_t??????????factory????б???map_??
??????????????
????#define REGISTER_MESSAGE_VNAME(T) reg_msg_##T##_
????#define REGISTER_MESSAGE(T?? key?? ...) static factory::register_t<T> REGISTER_MESSAGE_VNAME(T)(key?? __VA_ARGS__);
?????????????е??ú?????????
????class Message1 : public Message
????{
????//……
????};
????REGISTER_MESSAGE(Message1?? "message1");
???????????????????????????????????register_t??????????register_t???????????????????????????????????????????????????е????????????????????????????????????д???
?????????????????????????????????????????????????ù??????????????????????
????Factory??????
#include <map>
#include <string>
#include <functional>
#include <memory>
#include "Message.hpp"
struct factory
{
template<typename T>
struct register_t
{
register_t(const std::string& key)
{
factory::get().map_.emplace(key?? [] { return new T(); });
}
template<typename... Args>
register_t(const std::string& key?? Args... args)
{
factory::get().map_.emplace(key?? [&] { return new T(args...); });
}
};
static Message* produce(const std::string& key)
{
if (map_.find(key) == map_.end())
throw std::invalid_argument("the message key is not exist!");
return map_[key]();
}
static std::unique_ptr<Message> produce_unique(const std::string& key)
{
return std::unique_ptr<Message>(produce(key));
}
static std::shared_ptr<Message> produce_shared(const std::string& key)
{
return std::shared_ptr<Message>(produce(key));
}
private:
factory() {};
factory(const factory&) = delete;
factory(factory&&) = delete;
static factory& get()
{
static factory instance;
return instance;
}
static std::map<std::string?? std::function<Message*()>> map_;
};
std::map<std::string?? std::function<Message*()>> factory::map_;
#define REGISTER_MESSAGE_VNAME(T) reg_msg_##T##_
#define REGISTER_MESSAGE(T?? key?? ...) static factory::register_t<T> REGISTER_MESSAGE_VNAME(T)(key?? ##__VA_ARGS__);
???????
class Message
{
public:
virtual ~Message() {}
virtual void foo()
{
}
};
#include "MessageFactory.hpp"
#include "Message.hpp"
class Message1 : public Message
{
public:
Message1()
{
std::cout << "message1" << std::endl;
}
Message1(int a)
{
std::cout << "message1" << std::endl;
}
~Message1()
{
}
void foo() override
{
std::cout << "message1" << std::endl;
}
};
//REGISTER_MESSAGE(Message1?? "message1"?? 2);
REGISTER_MESSAGE(Message1?? "message1");
#include "Message1.hpp"
int main()
{
Message* p = factory::produce("message1");
p->foo(); //Message1
auto p2 = factory::produce_unique("message1");
p2->foo();
}
???????
???????C++11?????????????д?????????????????????????????????????swithc-case?????????????????????????
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
??C++????????????C++ lvalue??rvalueC++11????????C++???????????????C++?е?????????????????C++?????????C++???Windows????λ??C/C++???????????????????JAVA??C??C++??????????c++??python???????????????????????????????C++???????C++?е????????C++????????????????C++ ???????????????C++?????????????????????C++????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????