×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    1

    How to make singleton

    Hi all
    I am doing a game program in series 60 platform. I need to make a Singleton class. But Symbian does not support global static variable.
    But probably there is a way by extending CCoeStatic class.I tried but cannot find out any way. Please give me a solution. I need it very urgently Thank u in advance
    Rownak

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    68

    RE: How to make singleton

    Actually, you get 4 words of static data, per DLL, per thread. Look in the documentation for TLS.

    Using the Dll::SetTls() and Dll::Tls() commands it is possible to create a singleton.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    14

    RE: How to make singleton

    Rownak,

    My solution for Singletons is below. I would be appreciate your constructive criticism for the approach. I probably should add in better support for leaving etc.

    Regards,
    Phil.
    StaticSim

    Usage:
    1. In an early class have the variable included.
    CStaticSimulator aStaticSimulator;

    2. Make static declarations as per the second file.

    File: StaticSim.h

    #ifndef STATICSIM_H
    #define STATICSIM_H
    /*
    * @(#)StaticSim.h 2003/01/19
    *
    * Copyright (c) 2003 Virtual Nation Pty Ltd,
    * 15/18 Ridge St, North Sydney, NSW, 2060, Australia.
    * All rights reserved.
    *
    * This software is the confidential and proprietary information of Virtual
    * Nation Pty Ltd. ("Confidential Information"). You shall not disclose such
    * Confidential Information and shall use it only in accordance with the terms
    * of the license agreement you entered into with Virtual Nation Pty Ltd.
    *
    *
    * $Header: /7650/iserv/include/cs/util/StaticSim.h 2 29/01/03 3:18p Phaynes $
    *
    */

    #ifndef STATICSIMCONST_H
    #include <cs/util/StaticSimConst.h>
    #endif

    /**
    * DLL's in Symbian may not contain writeable static data as it is designed
    * for a ROM environment.
    *
    * This provides a mechanism for simulating static variables and provides
    * replacement methods for the ThreadSpecific Data that it relies upon.
    */
    class BaseStaticSim
    {
    public:
    /**
    * Must be called by the owning thread to start statics simulation.
    * Returns false if it fails to correctly initialise.
    */
    static bool init();
    /**
    * Register instance to simulate the static.
    * @param anInstance The instance that it is the static equivalent.
    * @param ID The ID of that instance.
    * @return false if failed to register.
    */
    static bool registerInstance(BaseStaticSim* anInstance, int ID);
    /**
    * Must called by the owning thread to stop simulation.
    * Memory allocated will be deleted here.
    */
    static void shutdown();
    /**
    * Returns the base / root simulation instance from the thread specific
    * word available.
    */
    static BaseStaticSim* getBaseInst();
    /**
    * Returns an ID.
    */
    static BaseStaticSim* getInst(int ID);
    /**
    * Thread specific data member replacement.
    * @return 0 if all ok.
    */
    static int setTls(void* data);
    static void* Tls();

    virtual ~BaseStaticSim();
    protected:
    BaseStaticSim();
    bool baseRegisterInstance(BaseStaticSim* anInstance, int ID);
    BaseStaticSim* baseGetInst(int ID);
    void baseInit();

    private:
    void expandSize();

    private:
    BaseStaticSim** mBaseInstances;
    int mCurrentSize;

    };

    template <class T, long ID>
    class StaticSim : public BaseStaticSim
    {
    public:
    static T* getInst() {
    return (T*)BaseStaticSim::getInst(ID);
    }
    virtual ~StaticSim() {}
    };
    //-----------------------------------------------------------------------------
    /**
    * Starts and stops the simulator.
    */
    class CStaticSimulator
    {
    public:
    CStaticSimulator() {
    BaseStaticSim::init();
    }
    virtual ~CStaticSimulator() {
    BaseStaticSim::shutdown();
    }
    };

    /**
    * This member is called at the start of the process to create the
    * simulated statics.
    *
    * This must be edited in the <cs/util/StaticSimConst.h> file
    * every time a new file that requires statics is added.
    */
    extern bool libStaticSimInits();

    //-----------------------------------------------------------------------------
    /**
    * A test class to demonstrate the principle of usage.
    */
    class TestStaticSim : public StaticSim<TestStaticSim, TEST_STATIC_SIM>
    {
    public:
    TestStaticSim()
    : aStatic(0)
    {}

    int aStatic;

    public:
    static TestStaticSim& inst() {
    return *getInst();
    }
    };

    #endif


    File: StaticSimConst.h

    #ifndef STATICSIMCONST_H
    #define STATICSIMCONST_H
    /*
    * @(#)StaticSimConst.h 2003/01/21
    *
    * Copyright (c) 2003 Virtual Nation Pty Ltd,
    * 15/18 Ridge St, North Sydney, NSW, 2060, Australia.
    * All rights reserved.
    *
    * This software is the confidential and proprietary information of Virtual
    * Nation Pty Ltd. ("Confidential Information"). You shall not disclose such
    * Confidential Information and shall use it only in accordance with the terms
    * of the license agreement you entered into with Virtual Nation Pty Ltd.
    *
    *
    * $Header: /7650/iserv/include/cs/util/StaticSimConst.h 1 28/01/03 11:55p Phaynes $
    *
    */

    /** The unique constants for each server file. */
    const int STATIC_SIM_S = 0;
    const int TEST_STATIC_SIM = 1;
    const int DATE_STATIC_SIM = 2;
    const int ABSTRACT_ENTITY_STATIC_SIM = 3;
    const int ASYNCHRONOUS_STREAM_STATIC_SIM = 4;
    const int HTTP_SERVERMGR_STATIC_SIM = 5;
    const int REMOTE_CMD_SPACE_STATIC_SIM = 6;
    const int STREAM_REGISTRY_STATIC_SIM = 7;
    const int XML_SERIALISE_STATIC_SIM = 8;



    /** Constant application level statics. */
    const int APP_STATIC = 25;

    bool testStaticSim();
    bool dtStaticSim();
    bool aeStaticSim();
    bool asStaticSim();
    bool httpServerMgrStaticSim();
    bool rcStaticSim();
    bool srStaticSim();
    bool xmlSerialiseStaticSim();

    /**
    * Put in references to the inits here.
    * The res &= yourInit(); convention ensures that if any method fails to
    * return true, the function fails to return true.
    */
    inline bool libStaticSimInits()
    {
    bool res = true;

    res &= testStaticSim();
    res &= dtStaticSim();
    res &= aeStaticSim();
    res &= asStaticSim();
    res &= httpServerMgrStaticSim();
    res &= rcStaticSim();
    res &= srStaticSim();
    res &= xmlSerialiseStaticSim();


    return res;
    }

    #endif

    File: StaticSim.cpp
    /*
    * @(#)StaticSim.cpp 2002/12/22
    *
    * Copyright (c) 2002 Virtual Nation Pty Ltd,
    * 15/18 Ridge St, North Sydney, NSW, 2060, Australia.
    * All rights reserved.
    *
    * This software is the confidential and proprietary information of Virtual
    * Nation Pty Ltd. ("Confidential Information"). You shall not disclose such
    * Confidential Information and shall use it only in accordance with the terms
    * of the license agreement you entered into with Virtual Nation Pty Ltd.
    *
    *
    * $Header: /7650/iserv/src/cs/util/StaticSim.cpp 1 28/01/03 11:55p Phaynes $
    *
    */

    #include <cs/util/StaticSim.h>
    #include <e32std.h>

    #include <cs/util/StaticSimConst.h>

    // Minimum default size.
    static const int DEFAULT_BASE_SIZE = 20;

    //--------------------------------------------------------------PAH-22/01/03---
    BaseStaticSim::BaseStaticSim()
    : mBaseInstances(0), mCurrentSize(0)
    {
    }
    //--------------------------------------------------------------PAH-22/01/03---
    void BaseStaticSim::baseInit()
    {
    mCurrentSize = DEFAULT_BASE_SIZE;
    mBaseInstances = new BaseStaticSim*[mCurrentSize];
    for (int i=0; i < mCurrentSize; i++) {
    mBaseInstances[i] = 0;
    }
    }
    //--------------------------------------------------------------PAH-22/01/03---
    BaseStaticSim::~BaseStaticSim()
    {
    for (int i=0; i < mCurrentSize; i++) {
    if (mBaseInstances[i] != 0) {
    delete mBaseInstances[i];
    mBaseInstances[i] = 0;
    }
    }
    if (mBaseInstances != 0) delete [] mBaseInstances;
    }
    //--------------------------------------------------------------PAH-22/01/03---
    void BaseStaticSim::expandSize()
    {
    int newSize = mCurrentSize * 2;
    BaseStaticSim** newInstances = new BaseStaticSim*[newSize];
    int i;
    for (i = 0; i < mCurrentSize; i++) {
    newInstances[i] = mBaseInstances[i];
    }

    delete [] mBaseInstances;
    mBaseInstances = newInstances;

    for (i = mCurrentSize; i < newSize; i++) {
    mBaseInstances[i] = 0;
    }
    mCurrentSize = newSize;
    }
    //--------------------------------------------------------------PAH-22/01/03---
    bool BaseStaticSim::init()
    {
    BaseStaticSim* instances = new BaseStaticSim();
    if (instances == 0) return false;
    instances->baseInit();
    if (Dll::SetTls((TAny*)instances) != KErrNone) {
    delete instances;
    return false;
    }
    return libStaticSimInits();
    }
    //--------------------------------------------------------------PAH-22/01/03---
    bool BaseStaticSim::baseRegisterInstance(BaseStaticSim* anInstance, int ID)
    {
    if (ID >= mCurrentSize) {
    expandSize();
    // Ensure that we don't get into an infinite loop of expansion.
    if (ID >= mCurrentSize) return false;
    }
    if (mBaseInstances[ID] != 0) return false;
    mBaseInstances[ID] = anInstance;
    return true;
    }
    //--------------------------------------------------------------PAH-22/01/03---
    bool BaseStaticSim::registerInstance(BaseStaticSim* anInstance, int ID)
    {
    BaseStaticSim* inst = getBaseInst();
    return (inst == 0) ? false : inst->baseRegisterInstance(anInstance, ID);
    }
    //--------------------------------------------------------------PAH-22/01/03---
    void BaseStaticSim::shutdown()
    {
    BaseStaticSim* inst = getBaseInst();
    if (inst != 0) delete inst;
    Dll::SetTls(0);
    }
    //--------------------------------------------------------------PAH-22/01/03---
    BaseStaticSim* BaseStaticSim::getBaseInst()
    {
    return (BaseStaticSim*)Dll::Tls();
    }
    //--------------------------------------------------------------PAH-22/01/03---
    int BaseStaticSim::setTls(void* data)
    {
    BaseStaticSim* inst = getBaseInst();
    if (inst == 0) return 1;
    inst->mBaseInstances[0] = (BaseStaticSim*)data;
    return 0;
    }
    //--------------------------------------------------------------PAH-22/01/03---
    void* BaseStaticSim::Tls() {
    BaseStaticSim* inst = getBaseInst();
    return (inst == 0) ? 0 : (void*)inst->mBaseInstances[0];
    }
    //--------------------------------------------------------------PAH-22/01/03---
    BaseStaticSim* BaseStaticSim::baseGetInst(int ID)
    {
    if ((ID < 0) | (ID >= mCurrentSize)) return 0;
    return mBaseInstances[ID];
    }
    //--------------------------------------------------------------PAH-22/01/03---
    BaseStaticSim* BaseStaticSim::getInst(int ID)
    {
    BaseStaticSim* inst = getBaseInst();
    return (inst == 0) ? 0 : inst->baseGetInst(ID);
    }

    //-----------------------------------------------------------------------------
    bool testStaticSim()
    {
    return BaseStaticSim::registerInstance(new TestStaticSim(), TEST_STATIC_SIM);;
    }

    #if !defined(__DLL__) && defined (__WINS__)

    static void* gPtr = 0;
    TInt Dll::SetTls(TAny* aPtr)
    {
    gPtr = aPtr;
    return KErrNone;
    }
    TAny* Dll::Tls()
    {
    return gPtr;
    }
    #endif

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×