/* * InspIRCd -- Internet Relay Chat Daemon * * Copyright (C) 2026 Sadie Powell * * This file is part of InspIRCd. InspIRCd is free software: you can * redistribute it and/or modify it under the terms of the GNU General Public * License as published by the Free Software Foundation, version 2. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once namespace insp { /** A pointer to a FILE that closes when it goes out of scope. */ using file_ptr = std::unique_ptr; /** Assigns a pointer type to a target if it is not null; otherwise, does nothing. * This is mainly intended for use with const char* to std::string assignment. */ template void assign_ptr(Target& target, const Source* source) { if (source) target = source; } /** Deletes all elements in a container using operator delete * @param cont The container containing the elements to delete */ template >> void delete_all(const Cont& cont) { std::for_each(cont.begin(), cont.end(), Del()); } /** Deletes a object and zeroes the memory location that pointed to it. * @param pr A reference to the pointer that contains the object to delete. */ template> void delete_zero(T*& pr) { auto* p = pr; pr = nullptr; Del()(p); } /** Determines if the pointer in \p ptr1 is empty. * @param ptr Either a shared_ptr<> or a weak_ptr<>. */ template bool empty_ptr(const Ptr& ptr) { return same_ptr(ptr, {}); } /** Determines if the pointer in \p ptr1 points to the same value as the pointer in \p ptr2. * @param ptr1 Either a shared_ptr<> or a weak_ptr<>. * @param ptr2 Either a shared_ptr<> or a weak_ptr<>. */ template bool same_ptr(const Ptr1& ptr1, const Ptr2& ptr2) { return !ptr1.owner_before(ptr2) && !ptr2.owner_before(ptr1); } /** Returns the raw pointer of the shared pointer passed to it. * @param value The shared pointer to return the raw pointer of. */ template inline Value* to_ptr(std::shared_ptr& value) { return value.get(); } /** Returns the raw pointer of the reference passed to it. * @param value The reference to return the raw pointer of. */ template inline Value* to_ptr(Value& value) { return &value; } /** Returns the value passed to it. * @param value The value to return. */ template inline Value* to_ptr(Value* value) { return value; } }