/*
 * SPDX-FileCopyrightText: 2004 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#ifndef TANGO_CLIENT_DBSERVERCACHE_H
#define TANGO_CLIENT_DBSERVERCACHE_H

#include <tango/common/tango_const.h>

#include <omniORB4/CORBA.h>

#include <string>
#include <vector>

/** @defgroup DBase Database Client classes
 * @ingroup Client
 */

namespace Tango
{
class Database;

/****************************************************************************************
 *                                                                                         *
 *                     The DbServerCache class                                                *
 *                     ------------------                                                    *
 *                                                                                         *
 ***************************************************************************************/

//
// DbServerCache data object to implement a DB cache
// used during the DS startup sequence
//

class DbServerCache
{
  public:
    typedef struct
    {
        int first_idx;
        int last_idx;
    } EltIdx;

    typedef struct
    {
        int first_idx;
        int last_idx;
        int prop_nb;
        int *props_idx;
    } PropEltIdx;

    typedef struct
    {
        int first_idx;
        int last_idx;
        int att_nb;
        int *atts_idx;
    } AttPropEltIdx;

    typedef struct
    {
        PropEltIdx dev_prop;
        AttPropEltIdx dev_att_prop;
        AttPropEltIdx dev_pipe_prop;
    } DevEltIdx;

    typedef struct
    {
        PropEltIdx class_prop;
        AttPropEltIdx class_att_prop;
        AttPropEltIdx class_pipe_prop;
        EltIdx dev_list;
        int dev_nb;
        DevEltIdx *devs_idx;
    } ClassEltIdx;

    DbServerCache(Database *, const std::string &, const std::string &);
    ~DbServerCache();

    const DevVarLongStringArray *import_adm_dev();
    const DevVarLongStringArray *import_notifd_event();
    const DevVarLongStringArray *import_adm_event();
    const DevVarStringArray *get_class_property(DevVarStringArray *);
    const DevVarStringArray *get_dev_property(DevVarStringArray *);
    const DevVarStringArray *get_dev_list(DevVarStringArray *);
    const DevVarStringArray *get_class_att_property(DevVarStringArray *);
    const DevVarStringArray *get_dev_att_property(DevVarStringArray *);
    const DevVarStringArray *get_obj_property(DevVarStringArray *);
    const DevVarStringArray *get_device_property_list(DevVarStringArray *);
    const DevVarStringArray *get_class_pipe_property(DevVarStringArray *);
    const DevVarStringArray *get_dev_pipe_property(DevVarStringArray *);
    const DevVarLongStringArray *import_tac_dev(const std::string &);

    const EltIdx &get_imp_dat()
    {
        return imp_adm;
    }

    const EltIdx &get_imp_notifd_event()
    {
        return imp_notifd_event;
    }

    const EltIdx &get_imp_adm_event()
    {
        return imp_adm_event;
    }

    const PropEltIdx &get_DServer_class_prop()
    {
        return DServer_class_prop;
    }

    const PropEltIdx &get_Default_prop()
    {
        return Default_prop;
    }

    const PropEltIdx &get_adm_dev_prop()
    {
        return adm_dev_prop;
    }

    const PropEltIdx &get_ctrl_serv_prop()
    {
        return ctrl_serv_prop;
    }

    int get_class_nb()
    {
        return class_nb;
    }

    const ClassEltIdx *get_classes_elt()
    {
        return classes_idx;
    }

    int get_data_nb()
    {
        return n_data;
    }

  private:
    void prop_indexes(int &, int &, PropEltIdx &, const DevVarStringArray *);
    void prop_att_indexes(int &, int &, AttPropEltIdx &, const DevVarStringArray *);
    void prop_pipe_indexes(int &, int &, AttPropEltIdx &, const DevVarStringArray *);
    void get_obj_prop(DevVarStringArray *, PropEltIdx &, bool dev_prop = false);
    int find_class(DevString);
    int find_dev_att(DevString, int &, int &);
    int find_obj(DevString obj_name, int &);
    void get_obj_prop_list(DevVarStringArray *, PropEltIdx &);

    CORBA::Any_var received;
    const DevVarStringArray *data_list;
    int n_data;
    int proc_release;

    EltIdx imp_adm;
    EltIdx imp_notifd_event;
    EltIdx imp_adm_event;
    EltIdx imp_tac;
    PropEltIdx ctrl_serv_prop;
    PropEltIdx DServer_class_prop;
    PropEltIdx Default_prop;
    PropEltIdx adm_dev_prop;
    int class_nb;
    ClassEltIdx *classes_idx;

    DevVarLongStringArray imp_adm_data;
    DevVarLongStringArray imp_notifd_event_data;
    DevVarLongStringArray imp_adm_event_data;
    DevVarLongStringArray imp_tac_data;
    DevVarStringArray ret_obj_prop;
    DevVarStringArray ret_dev_list;
    DevVarStringArray ret_obj_att_prop;
    DevVarStringArray ret_obj_pipe_prop;
    DevVarStringArray ret_prop_list;
};
} // namespace Tango

#endif // TANGO_CLIENT_DBSERVERCACHE_H
