summaryrefslogtreecommitdiff
path: root/tiger-compiler/lib/misc/ref.hxx
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
commit967be9e750221ab2ab783f95df79bb26d290a45e (patch)
tree6802900a5e975f9f68b169f0f503f040056d6952 /tiger-compiler/lib/misc/ref.hxx
add: added projectsHEADmain
Diffstat (limited to 'tiger-compiler/lib/misc/ref.hxx')
-rw-r--r--tiger-compiler/lib/misc/ref.hxx71
1 files changed, 71 insertions, 0 deletions
diff --git a/tiger-compiler/lib/misc/ref.hxx b/tiger-compiler/lib/misc/ref.hxx
new file mode 100644
index 0000000..ae740fd
--- /dev/null
+++ b/tiger-compiler/lib/misc/ref.hxx
@@ -0,0 +1,71 @@
+/**
+ ** \file misc/ref.hxx
+ ** \brief Implementation of misc::ref.
+ **/
+
+#pragma once
+
+#include <typeinfo>
+
+#include <misc/ref.hh>
+
+namespace misc
+{
+ /*-----------------.
+ | Ctors and dtor. |
+ `-----------------*/
+
+ template <typename T>
+ template <typename U>
+ ref<T>::ref(const ref<U>& other)
+ : super_type(other)
+ {}
+
+ template <typename T>
+ ref<T>::ref(const ref<T>& other)
+ : super_type(other)
+ {}
+
+ template <typename T>
+ ref<T>::ref(T* p)
+ : super_type(p)
+ {}
+
+ /*---------------------.
+ | Equality operators. |
+ `---------------------*/
+
+ template <typename T> bool ref<T>::operator==(const T* other) const
+ {
+ return this->get() == other;
+ }
+
+ template <typename T> bool ref<T>::operator!=(const T* other) const
+ {
+ return !(*this == other);
+ }
+
+ /*--------.
+ | Casts. |
+ `--------*/
+
+ template <typename T> template <typename U> ref<U> ref<T>::unsafe_cast() const
+ {
+ ref<U> res;
+ (std::dynamic_pointer_cast<U, element_type>(*this)).swap(res);
+ return res;
+ }
+
+ template <typename T> template <typename U> ref<U> ref<T>::cast() const
+ {
+ if (!this->get() || !this->is_a<U>())
+ throw std::bad_cast();
+ return unsafe_cast<U>();
+ }
+
+ template <typename T> template <typename U> bool ref<T>::is_a() const
+ {
+ return dynamic_cast<U*>(this->get());
+ }
+
+} // namespace misc