Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/newyaroslav/unique_class_id
unique_class_id is a header-only C++ library that allows giving classes a unique ID and then comparing classes based on their unique IDs.
https://github.com/newyaroslav/unique_class_id
class-identifier cpp cpp11 guid header-only unique-id unique-identifier
Last synced: about 1 month ago
JSON representation
unique_class_id is a header-only C++ library that allows giving classes a unique ID and then comparing classes based on their unique IDs.
- Host: GitHub
- URL: https://github.com/newyaroslav/unique_class_id
- Owner: NewYaroslav
- License: mit
- Created: 2023-02-12T06:03:44.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-14T03:51:21.000Z (7 months ago)
- Last Synced: 2024-05-14T04:41:39.119Z (7 months ago)
- Topics: class-identifier, cpp, cpp11, guid, header-only, unique-id, unique-identifier
- Language: C++
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# unique_class_id
unique_class_id is a header-only C++ library that allows giving classes a unique ID and then comparing classes based on their unique IDs.## Description
Unique class id library is a header-only library that allows classes to have unique IDs and then compare classes based on their unique IDs.To use the library, simply include the header "unique_class_id.hpp" in your code.
An example of using the library is shown below:
```cpp
#include
#include "unique_class_id.hpp"class A {
DEFINE_UNIQUE_CLASS_ID(A)
};class B : public A {
DEFINE_UNIQUE_CLASS_ID(B)
};class Base {
public:
virtual unique_class_id::class_id_t id() const = 0;
};class DerivedA : public Base {
DEFINE_FINAL_UNIQUE_CLASS_ID(DerivedA)
};class DerivedB : public Base {
DEFINE_FINAL_UNIQUE_CLASS_ID(DerivedB)
};int main() {
A *a = new A();
B *b = new B();if (unique_class_id::is_same_class(b)) {
std::cout << "Same class" << std::endl;
} else {
std::cout << "Different class" << std::endl;
}Base *base_ptr = new DerivedA();
unique_class_id::class_id_t base_id = base_ptr->id();
unique_class_id::class_id_t derived_a_id = DerivedA().id();
unique_class_id::class_id_t derived_b_id = DerivedB().id();
if (base_id == derived_a_id) {
std::cout << "Base pointer is pointing to DerivedA\n";
} else
if (base_id == derived_b_id) {
std::cout << "Base pointer is pointing to DerivedB\n";
}delete a;
delete b;
delete base_ptr;
return 0;
}
```