You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
2.9 KiB
138 lines
2.9 KiB
//
|
|
// Created by zwsd
|
|
//
|
|
|
|
#pragma once
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <list>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <set>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <typeindex>
|
|
#include <typeinfo>
|
|
#include <vector>
|
|
|
|
/**
|
|
* @brief
|
|
*
|
|
* service: ServiceContrainer
|
|
*
|
|
* 监听事件:
|
|
* 依赖状态:
|
|
* 依赖服务:
|
|
* 作用:
|
|
*
|
|
*/
|
|
// std::shared_ptr<Buffer> buf(new Buffer());
|
|
// Any any(buf);
|
|
// std::shared_ptr<Buffer> b = any.Get<std::shared_ptr<Buffer>>();
|
|
// assert(buf.get() == b.get());
|
|
namespace iflytop {
|
|
namespace core {
|
|
using namespace std;
|
|
|
|
class Any {
|
|
public:
|
|
Any() : content_(nullptr) {}
|
|
~Any() { delete content_; }
|
|
|
|
template <typename ValueType>
|
|
explicit Any(const ValueType& value) : content_(new AnyHolder<ValueType>(value)) {}
|
|
|
|
Any(const Any& other) : content_(other.content_ ? other.content_->clone() : nullptr) {}
|
|
|
|
public:
|
|
Any& swap(Any& rhs) {
|
|
std::swap(content_, rhs.content_);
|
|
return *this;
|
|
}
|
|
|
|
template <typename ValueType>
|
|
Any& operator=(const ValueType& rhs) {
|
|
Any(rhs).swap(*this);
|
|
return *this;
|
|
}
|
|
|
|
Any& operator=(const Any& rhs) {
|
|
Any(rhs).swap(*this);
|
|
return *this;
|
|
}
|
|
|
|
bool IsEmpty() const { return !content_; }
|
|
|
|
const std::type_info& GetType() const { return content_ ? content_->GetType() : typeid(void); }
|
|
|
|
template <typename ValueType>
|
|
ValueType operator()() const {
|
|
return Get<ValueType>();
|
|
}
|
|
|
|
template <typename ValueType>
|
|
ValueType Get() const {
|
|
if (GetType() == typeid(ValueType)) {
|
|
return static_cast<Any::AnyHolder<ValueType>*>(content_)->held_;
|
|
} else {
|
|
return ValueType();
|
|
}
|
|
}
|
|
|
|
protected:
|
|
class AnyPlaceHolder {
|
|
public:
|
|
virtual ~AnyPlaceHolder() {}
|
|
|
|
public:
|
|
virtual const std::type_info& GetType() const = 0;
|
|
virtual AnyPlaceHolder* clone() const = 0;
|
|
};
|
|
|
|
template <typename ValueType>
|
|
class AnyHolder : public AnyPlaceHolder {
|
|
public:
|
|
AnyHolder(const ValueType& value) : held_(value) {}
|
|
|
|
virtual const std::type_info& GetType() const { return typeid(ValueType); }
|
|
|
|
virtual AnyPlaceHolder* clone() const { return new AnyHolder(held_); }
|
|
|
|
ValueType held_;
|
|
};
|
|
|
|
protected:
|
|
AnyPlaceHolder* content_;
|
|
template <typename ValueType>
|
|
friend ValueType* any_cast(Any*);
|
|
};
|
|
|
|
template <typename ValueType>
|
|
ValueType* any_cast(Any* any) {
|
|
if (any && any->GetType() == typeid(ValueType)) {
|
|
return &(static_cast<Any::AnyHolder<ValueType>*>(any->content_)->held_);
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
template <typename ValueType>
|
|
const ValueType* any_cast(const Any* any) {
|
|
return any_cast<ValueType>(const_cast<Any*>(any));
|
|
}
|
|
|
|
template <typename ValueType>
|
|
ValueType any_cast(const Any& any) {
|
|
const ValueType* result = any_cast<ValueType>(&any);
|
|
assert(result);
|
|
|
|
if (!result) {
|
|
return ValueType();
|
|
}
|
|
|
|
return *result;
|
|
}
|
|
|
|
} // namespace core
|
|
} // namespace iflytop
|