1/*
2 * \file       ocsd_lib_dcd_register.h
3 * \brief      OpenCSD : Library decoder registration and management.
4 *
5 * \copyright  Copyright (c) 2016, ARM Limited. All Rights Reserved.
6 */
7
8#ifndef ARM_OCSD_LIB_DCD_REGISTER_H_INCLUDED
9#define ARM_OCSD_LIB_DCD_REGISTER_H_INCLUDED
10
11
12/*
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright notice,
20 * this list of conditions and the following disclaimer in the documentation
21 * and/or other materials provided with the distribution.
22 *
23 * 3. Neither the name of the copyright holder nor the names of its contributors
24 * may be used to endorse or promote products derived from this software without
25 * specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <map>
40
41#include "opencsd/ocsd_if_types.h"
42#include "common/ocsd_dcd_mngr_i.h"
43
44/*!
45 * @class OcsdLibDcdRegister : Registers decoders with the library
46 *
47 * library decoder register class allows decoders to be registered by name, and the register allows clients to access
48 * the list of names of registerd decoders.
49 *
50 * The decoders in the library are accessed through the decoder manager interface. This provides a set of functions to allow
51 * the creation, manipulation and destruction of registered decoders
52 *
53 */
54class OcsdLibDcdRegister
55{
56public:
57    static OcsdLibDcdRegister *getDecoderRegister();
58
59    static void deregisterAllDecoders();    //!< library cleanup - deregisters decoder managers and destroys the register object.
60    static const ocsd_trace_protocol_t getNextCustomProtocolID();
61    static void releaseLastCustomProtocolID();
62
63    const ocsd_err_t registerDecoderTypeByName(const std::string &name, IDecoderMngr *p_decoder_fact);  //!< register a decoder manager interface
64    const ocsd_err_t getDecoderMngrByName(const std::string &name, IDecoderMngr **p_decoder_mngr);
65    const ocsd_err_t getDecoderMngrByType(const ocsd_trace_protocol_t decoderType, IDecoderMngr **p_decoder_mngr);
66
67    const bool isRegisteredDecoder(const std::string &name);
68    const bool getFirstNamedDecoder(std::string &name);
69    const bool getNextNamedDecoder(std::string &name);
70
71    const bool isRegisteredDecoderType(const ocsd_trace_protocol_t decoderType);
72
73private:
74    void registerBuiltInDecoders();         //!< register the list of build in decoder managers on first access of getDecoderMngrByName.
75    void deRegisterCustomDecoders();        //!< delete all custom decoders registered with the library.
76
77    std::map<const std::string, IDecoderMngr *> m_decoder_mngrs;                    //!< map linking names to decoder manager interfaces.
78    std::map<const std::string, IDecoderMngr *>::const_iterator m_iter;             //!< iterator for name search.
79
80    std::map<const ocsd_trace_protocol_t, IDecoderMngr *> m_typed_decoder_mngrs;    //!< map linking decoder managers to protocol type ID
81
82    // cache last found by type to speed up repeated quries on same object.
83    IDecoderMngr *m_pLastTypedDecoderMngr;      //!< last manager we found by type
84
85
86
87    // singleton pattern - need just one of these in the library - ensure all default constructors are private.
88    OcsdLibDcdRegister();
89    OcsdLibDcdRegister(OcsdLibDcdRegister const &) {};
90    OcsdLibDcdRegister& operator=(OcsdLibDcdRegister const &){ return *this; };
91    ~OcsdLibDcdRegister();
92
93    static OcsdLibDcdRegister *m_p_libMngr;
94    static bool m_b_registeredBuiltins;
95    static ocsd_trace_protocol_t m_nextCustomProtocolID;
96};
97
98/*!
99 * Typedef of function signature to create a decoder manager.
100 *
101 * @param *name : Registered name of the decoder.
102 */
103typedef IDecoderMngr *(*CreateMngr)(const std::string &name);
104
105/*!
106 * Template function to create a specific decoder manager class object.
107 *
108 * @param &name : Registered name of the decoder.
109 *
110 * @return IDecoderMngr *  : pointer to the decoder manager base class interface.
111 */
112template <typename T> IDecoderMngr *createManagerInst(const std::string &name)
113{
114    return new (std::nothrow)T(name);
115}
116
117/*! Structure to contain the information needed to create and register a builtin decoder
118 *  manager with the library
119 */
120typedef struct built_in_decoder_info {
121    IDecoderMngr *pMngr;    //!< pointer to created decoder manager
122    CreateMngr PFn;         //!< function to create the decoder manager.
123    const char *name;       //!< registered name of the decoder.
124} built_in_decoder_info_t;
125
126//! Define to use to fill in an array of built_in_decoder_info_t structures.
127#define CREATE_BUILTIN_ENTRY(C,N) { 0, createManagerInst<C>, N }
128
129#endif // ARM_OCSD_LIB_DCD_REGISTER_H_INCLUDED
130
131/* End of File ocsd_lib_dcd_register.h */
132