AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
IStringable.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12//
   13// Created by alex2772 on 7/7/21.
   14//
   15
   16#pragma once
   17
   18
   19#include <AUI/Common/SharedPtrTypes.h>
   20#include <AUI/Reflect/AReflect.h>
   21#include <AUI/Common/AString.h>
   22
   30public:
   31    virtual ~IStringable() = default;
   32
   36    virtual AString toString() const = 0;
   37
   42    template<typename T>
   43    static AString toString(const T* t) {
   44        /*
   45         * since dynamic_cast is an expensive operation, we should do the compile-time check if we can directly call
   46         * the toString() function
   47         */
   48        if constexpr (std::is_base_of_v<IStringable, std::decay_t<T>>) {
   49            return t->toString();
   50        } else {
   51            if (auto stringable = dynamic_cast<const IStringable*>(t)) {
   52                return stringable->toString();
   53            } else {
   54                return AReflect::name(t);
   55            }
   56        }
   57    }
   58
   63    template<typename T>
   64    static AString toString(const _<T>& t) {
   65        return toString(t.get());
   66    }
   67};
Represents a Unicode character string.
Definition AString.h:38
Object that can be converted to string.
Definition IStringable.h:29
static AString toString(const T *t)
Definition IStringable.h:43
virtual AString toString() const =0
static AString toString(const _< T > &t)
Definition IStringable.h:64
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
AString name(T *v)
Runtime reflection based on typeid.
Definition AReflect.h:29