InfiniTensor/include/core/object.h

71 lines
1.4 KiB
C
Raw Normal View History

2022-07-31 21:43:26 +08:00
#pragma once
#include "core/common.h"
2022-08-05 12:50:34 +08:00
#include "ref.h"
2022-07-31 21:43:26 +08:00
2022-08-07 21:12:17 +08:00
namespace infini {
2022-07-31 21:43:26 +08:00
using UidBaseType = int;
2022-07-31 21:43:26 +08:00
class Uid {
2022-07-31 21:43:26 +08:00
private:
UidBaseType uid;
2022-07-31 21:43:26 +08:00
public:
Uid(UidBaseType uid) : uid(uid) {}
Uid &operator=(const Uid &rhs) = delete;
operator UidBaseType() const { return uid; }
};
class Guid : public Uid {
2022-07-31 21:43:26 +08:00
private:
UidBaseType generateGuid() {
static UidBaseType guidCnt = 0;
2022-07-31 21:43:26 +08:00
return ++guidCnt;
}
public:
Guid() : Uid(generateGuid()) {}
Guid(const Guid &rhs) : Uid(generateGuid()) {}
};
/**
* @brief Family unique ID. Cloned tensors shared the same FUID.
*/
class Fuid : public Uid {
private:
UidBaseType generateFuid() {
static UidBaseType fuidCnt = 0;
return ++fuidCnt;
2022-07-31 21:43:26 +08:00
}
public:
Fuid() : Uid(generateFuid()) {}
Fuid(const Fuid &fuid) : Uid(fuid) {}
2022-07-31 21:43:26 +08:00
};
class Object {
protected:
Guid guid;
public:
virtual ~Object(){};
virtual string toString() const = 0;
void print() { std::cout << toString() << std::endl; }
UidBaseType getGuid() const { return guid; }
2022-07-31 21:43:26 +08:00
};
inline std::ostream &operator<<(std::ostream &os, const Object &obj) {
os << obj.toString();
return os;
}
2022-08-05 12:50:34 +08:00
// Overload for Ref-wrapped Object
template <typename T,
typename std::enable_if_t<std::is_base_of_v<Object, T>> * = nullptr>
inline std::ostream &operator<<(std::ostream &os, const Ref<T> &obj) {
os << obj->toString();
return os;
}
2022-08-07 21:12:17 +08:00
} // namespace infini