Program Listing for File ClientRegistry.h

Program Listing for File ClientRegistry.h#

Return to documentation for file (include/Karana/ProxyScene/ClientRegistry.h)

/*
 * Copyright (c) 2024-2026 Karana Dynamics Pty Ltd. All rights reserved.
 *
 * NOTICE TO USER:
 *
 * This source code and/or documentation (the "Licensed Materials") is
 * the confidential and proprietary information of Karana Dynamics Inc.
 * Use of these Licensed Materials is governed by the terms and conditions
 * of a separate software license agreement between Karana Dynamics and the
 * Licensee ("License Agreement"). Unless expressly permitted under that
 * agreement, any reproduction, modification, distribution, or disclosure
 * of the Licensed Materials, in whole or in part, to any third party
 * without the prior written consent of Karana Dynamics is strictly prohibited.
 *
 * THE LICENSED MATERIALS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
 * KARANA DYNAMICS DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, AND
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL KARANA DYNAMICS BE LIABLE FOR ANY DAMAGES WHATSOEVER,
 * INCLUDING BUT NOT LIMITED TO LOSS OF PROFITS, DATA, OR USE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, WHETHER IN CONTRACT, TORT,
 * OR OTHERWISE ARISING OUT OF OR IN CONNECTION WITH THE LICENSED MATERIALS.
 *
 * U.S. Government End Users: The Licensed Materials are a "commercial item"
 * as defined at 48 C.F.R. 2.101, and are provided to the U.S. Government
 * only as a commercial end item under the terms of this license.
 *
 * Any use of the Licensed Materials in individual or commercial software must
 * include, in the user documentation and internal source code comments,
 * this Notice, Disclaimer, and U.S. Government Use Provision.
 */

/**
 * @file
 * @brief Contains the declarations for the ClientRegistry class.
 */

#pragma once

#include "Karana/Frame/Frame.h"
#include "Karana/KCore/SharedPointer.h"
#include "Karana/Scene/Scene.h"
#include <type_traits>
#include <unordered_map>

namespace Karana::Scene {
    /// Struct for data associated with a registered client Scene
    struct ClientSceneData {
        /**
         * @brief ClientSceneData constructor.
         * @param scene - The client Scene
         * @param origin_frame - The Frame used as origin for the client Scene
         * @param layers - Bitmask for layers to implement in this client
         */
        ClientSceneData(const Karana::Core::ks_ptr<Scene> &scene,
                        const Karana::Core::ks_ptr<Karana::Frame::Frame> &origin_frame,
                        layer_t layers);

        /// The client Scene
        Karana::Core::ks_ptr<Scene> scene;

        /// The Frame used as origin for the client Scene
        Karana::Core::ks_ptr<Karana::Frame::Frame> origin_frame;

        /// Bitmask for layers to implement in this client
        layer_t layers;
    };

    /// Container to track information about registered client Scenes
    class ClientRegistry {
      public:
        /**
         * @brief ClientRegistry constructor
         * @param root_frame - The default Frame to attach nodes to.
         */
        ClientRegistry(const Karana::Core::ks_ptr<Karana::Frame::Frame> &root_frame);

        /**
         * @brief Add a new ClientSceneData to the registry
         * @param client_data - The new ClientSceneData
         */
        void insert(ClientSceneData &&client_data);

        /**
         * @brief Remove a client scene from the registry
         * @param scene - The client Scene to remove
         */
        void erase(const Scene &scene);

        /**
         * @brief Remove a client scene from the registry by id
         * @param id - The id of the client Scene to remove
         */
        void erase(const Karana::Core::id_t &id);

        /**
         * @brief Remove all client scenes from the registry
         */
        void clear();

        /**
         * @brief Check whether a client belongs to the registry
         * @param scene - The client Scene to check for
         * @return Whether the client belongs to the registry
         */
        bool contains(const Scene &scene) const;
        /**
         * @brief Check whether a client belongs to the registry by id
         * @param id - The id of the client Scene to check for
         * @return Whether the client belongs to the registry
         */
        bool contains(const Karana::Core::id_t &id) const;

        /**
         * @brief Lookup the ClientSceneData for a client Scene
         * @param scene - The client Scene to get data for
         * @return ClientSceneData for the given client Scene.
         */
        const ClientSceneData &at(const Scene &scene) const;

        /**
         * @brief Lookup the ClientSceneData for a client Scene by id
         * @param id - The id of the client Scene to get data for
         * @return ClientSceneData for the client Scene with the given id.
         */
        const ClientSceneData &at(const Karana::Core::id_t &id) const;

        /// The default Frame to attach nodes to.
        const Karana::Core::ks_ptr<Karana::Frame::Frame> root_frame;

        /**
         * @brief Helper to invoke a callback for each registered client Scene
         * @param func - The callback to invoke.
         */
        template <typename Func> void forEach(Func &&func) const {
            static_assert(std::is_invocable_v<Func, const ClientSceneData &>,
                          "Func must be callable with (const ClientSceneData&)");

            for (const auto &[id, client_data] : _map) {
                func(client_data);
            }
        }

      private:
        std::unordered_map<Karana::Core::id_t, ClientSceneData> _map;
    };
} // namespace Karana::Scene