#include <stdio.h>
#include <iostream>
#include <functional>
template <typename MsgT>
class Msg {
public:
Msg(int size) : size_(size) { element_ = new MsgT[size]; }
~Msg() { delete []element_; };
const MsgT& element(int index) const;
void set_element(int index, const MsgT& value);
int size() const { return size_; };
void PrintElement(int index) const { std::cout << element(index) << std::endl; } //怎么去打印每个数组呢?count?
private:
MsgT *element_;
int size_;
};
template <typename MsgT>
const MsgT& Msg<MsgT>::element(int index) const {
return element_[index];
}
template <typename MsgT>
void Msg<MsgT>::set_element(int index, const MsgT& value) {
element_[index] = value;
}
template <typename T>
int PublishMsg(const Msg<T> &msg) {
//由于参数msg是const的,所以只能调用其const的成员函数,所以size()和PrintElement()要加const后缀(声明和定义都要加)。
for (int i = 0; i < msg.size(); i++) {
msg.PrintElement(i);
}
return 0;
}
template <typename T>
int PublishMsgWrapper(int cmd, Msg<T> *msg, int len) {
std::cout << "cmd = " << cmd << ", len = " << len << std::endl;
return PublishMsg(*msg);
}
template <typename T>
//std::function<int (const Msg<T> &msg)> SubHandler;
typedef int (*SubHandler)(const Msg<T> &msg); //typedef不能定义模板,报错:“int (__cdecl *__cdecl SubHandler)(const Msg<MsgT> &)”: 不能是模板定义
int Subscribe(const std::string& name, SubHandler *handler) {
return 0;
}
int IntHandler(const Msg<int> &msg) {
return 0;
}
int main() {
Msg<int> a_msg(8); //或者 Msg<int> a_msg = Msg<int>(8);
for (int i = 0; i < 8; i++) {
a_msg.set_element(i, i * 2);
}
PublishMsgWrapper(59, &a_msg, 8);
return 0;
}
#include <stdio.h>
#include <iostream>
#include <functional>
template <typename MsgT>
class Msg {
public:
Msg(int size) : size_(size) {element_ = new MsgT[size]; }
~Msg() { delete []element_; };
const MsgT& element(int index) const;
void set_element(int index, const MsgT& value);
int size() const { return size_; };
void PrintElement(int index) const { std::cout << element(index) << std::endl; }
private:
MsgT *element_;
int size_;
};
template <typename MsgT>
const MsgT& Msg<MsgT>::element(int index) const {
return element_[index];
}
template <typename MsgT>
void Msg<MsgT>::set_element(int index, const MsgT& value) {
element_[index] = value;
}
//template <typename T>
//typedef std::function<int (const Msg<T> &msg)> SubHandler;
template <typename T>
int PublishMsg(const Msg<T> &msg) {
for (int i = 0; i < msg.size(); i++) {
msg.PrintElement(i);
}
return 0;
}
/* class或者struct都不允许只给数据成员设置模板,而只能给整个类或者某个函数成员。
而如果是整个类,则下面RouteMsgList的数据定义在一开始就要指定模板类型 */
class RouteMsgList {
public:
template <typename T>
RouteMsgList(int (*sub_handler)(const Msg<T> &msg), int set, int id) {
cmdset_ = set;
cmdid_ = id;
Msg<int> a_msg(8);
for (int i = 0; i < 8; i++) {
a_msg.set_element(i, i * 2);
}
PublishMsg(a_msg);
SetSubHandler(a_msg, sub_handler);
}
int cmdset_;
int cmdid_;
//template <typename T>
//std::function<int (const Msg<T> &msg)> sub_handler_;
///int SubHandler(const Msg<T> &msg);
};
template <typename T>
int SetSubHandler(const Msg<T> &msg, int (*sub_handler)(const Msg<T> &msg)) {
printf("=>=>=>\n");
return 0;
}
int IntHandler(const Msg<int> &msg) {
return 0;
}
static RouteMsgList route_list[] = {
RouteMsgList(IntHandler, 3, 1),
};
int main() {
return 0;
}