Singleton without static : possible??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eminemence

    Singleton without static : possible??

    Hi,
    Is it possible to implement a Singleton without using static variables
    or global variables.
    Thanks.
    --eminemence.

  • Steve Pope

    #2
    Re: Singleton without static : possible??

    eminemence <eminemence@gma il.comwrote:
    >Is it possible to implement a Singleton without using static variables
    >or global variables.
    Maybe by writing to a file. But it would be a major kludge.

    Steve

    Comment

    • eminemence

      #3
      Re: Singleton without static : possible??

      Still any other way??
      --eminemence.

      Steve Pope wrote:
      eminemence <eminemence@gma il.comwrote:
      >
      Is it possible to implement a Singleton without using static variables
      or global variables.
      >
      Maybe by writing to a file. But it would be a major kludge.
      >
      Steve

      Comment

      • Gianni Mariani

        #4
        Re: Singleton without static : possible??

        eminemence wrote:
        Hi,
        Is it possible to implement a Singleton without using static variables
        or global variables.
        Thanks.
        --eminemence.
        >
        No.

        Comment

        • eminemence

          #5
          Re: Singleton without static : possible??

          Thats quite sad as many mobile platforms need this singleton facility
          but don't support global and static vaiables.
          --eminemence.

          Gianni Mariani wrote:
          eminemence wrote:
          Hi,
          Is it possible to implement a Singleton without using static variables
          or global variables.
          Thanks.
          --eminemence.
          >
          No.

          Comment

          • Kirit Sælensminde

            #6
            Re: Singleton without static : possible??


            eminemence wrote:
            Hi,
            Is it possible to implement a Singleton without using static variables
            or global variables.
            Thanks.
            --eminemence.
            That depends on exactly what you want to do and why. Why don't you want
            to use statics or global variables?

            Here is a simple singleton that stores one int. Whether it is of much
            use or not depends on whether or not you can pass 'singleton' around to
            every function that requires it.

            int main() {
            int singleton;
            // use singleton here
            return 0;
            }


            K

            Comment

            • Zara

              #7
              Re: Singleton without static : possible??

              On 2 Nov 2006 01:50:07 -0800, "eminemence " <eminemence@gma il.com>
              wrote:
              >Gianni Mariani wrote:
              >eminemence wrote:
              Hi,
              Is it possible to implement a Singleton without using static variables
              or global variables.
              Thanks.
              --eminemence.
              >
              >>
              >No.
              >Thats quite sad as many mobile platforms need this singleton facility
              >but don't support global and static vaiables.
              >--eminemence.
              >
              [Please, do not top-post]

              Really? A C++ compiler with no possibility to define a static or
              global variable? I have never found one, and I work in the embedded
              field. May you tell me which one is it, to avoid it?

              Zara

              Comment

              • benben

                #8
                Re: Singleton without static : possible??

                Gianni Mariani wrote:
                eminemence wrote:
                >Hi,
                >Is it possible to implement a Singleton without using static variables
                >or global variables.
                >Thanks.
                >--eminemence.
                >>
                >
                No.
                Why the hell not? As long as you have a storage you can do it. For example,

                Single* obtain_single()
                {
                void* p = get_system_pool ();
                byte_t* i = static_cast<byt e_t*>(p);
                if (*(i-1) == 0)
                {
                Single* s = new(p) Single;
                return s;
                }

                return static_cast<Sin gle*>(p);
                }

                Regards,
                Ben

                Comment

                • eminemence

                  #9
                  Re: Singleton without static : possible??


                  Zara wrote:
                  On 2 Nov 2006 01:50:07 -0800, "eminemence " <eminemence@gma il.com>
                  wrote:
                  >
                  Gianni Mariani wrote:
                  eminemence wrote:
                  Hi,
                  Is it possible to implement a Singleton without using static variables
                  or global variables.
                  Thanks.
                  --eminemence.

                  >
                  No.
                  Thats quite sad as many mobile platforms need this singleton facility
                  but don't support global and static vaiables.
                  --eminemence.
                  [Please, do not top-post]
                  >
                  Really? A C++ compiler with no possibility to define a static or
                  global variable? I have never found one, and I work in the embedded
                  field. May you tell me which one is it, to avoid it?
                  >
                  Zara
                  Symbian does not allow.
                  So if you compile for the emulator it will allow the static and global
                  declarations but when the app is finally linked for the platform it
                  throws an error.
                  So such a facility is needed just to accommodate all platforms nothing
                  else.
                  --eminemence.

                  Comment

                  • Alf P. Steinbach

                    #10
                    Re: Singleton without static : possible??

                    * eminemence:
                    Zara wrote:
                    >On 2 Nov 2006 01:50:07 -0800, "eminemence " <eminemence@gma il.com>
                    >wrote:
                    >>
                    >>Gianni Mariani wrote:
                    >>>eminemence wrote:
                    >>>>Hi,
                    >>>>Is it possible to implement a Singleton without using static variables
                    >>>>or global variables.
                    >>>>Thanks.
                    >>>>--eminemence.
                    >>>>>
                    >>>No.
                    >>Thats quite sad as many mobile platforms need this singleton facility
                    >>but don't support global and static vaiables.
                    >>--eminemence.
                    >>>
                    >[Please, do not top-post]
                    >>
                    >Really? A C++ compiler with no possibility to define a static or
                    >global variable? I have never found one, and I work in the embedded
                    >field. May you tell me which one is it, to avoid it?
                    >>
                    >Zara
                    >
                    Symbian does not allow.
                    So if you compile for the emulator it will allow the static and global
                    declarations but when the app is finally linked for the platform it
                    throws an error.
                    So such a facility is needed just to accommodate all platforms nothing
                    else.
                    That's truly Evil(TM). I read up on it. And there seems to be no good
                    reason -- a lot of technical mumbo-jumbo talk about possible costs of
                    static data given the quite baffling decisions the Symbian OS designers
                    have landed on, but no discussion of the rationale of those decisions,
                    e.g. why application DLLs are treated the same as ordinary DLLs.

                    But then, I've never programmed the Symbian OS.

                    It seems you have three choices to get the equivalent of standard C++
                    functionality for static data on this OS:

                    * do not target the old combination EKA1 DLL (whatever "EKA1" means),
                    which is where this problem exists, or

                    * place all needed singletons or support for them in an EXE server,
                    or

                    * use thread local storage (in Symbian, Dll::SetTls() and Dll::Tls()).

                    <url:
                    http://developer.symbi an.com/main/downloads/papers/static_data/SupportForWrite ableStaticDataI nDLLsv1.1.pdf>.

                    Hope this helps,

                    - Alf

                    --
                    A: Because it messes up the order in which people normally read text.
                    Q: Why is it such a bad thing?
                    A: Top-posting.
                    Q: What is the most annoying thing on usenet and in e-mail?

                    Comment

                    • eminemence

                      #11
                      Re: Singleton without static : possible??


                      Alf P. Steinbach wrote:
                      * eminemence:
                      Zara wrote:
                      On 2 Nov 2006 01:50:07 -0800, "eminemence " <eminemence@gma il.com>
                      wrote:
                      >
                      >Gianni Mariani wrote:
                      >>eminemence wrote:
                      >>>Hi,
                      >>>Is it possible to implement a Singleton without using static variables
                      >>>or global variables.
                      >>>Thanks.
                      >>>--eminemence.
                      >>>>
                      >>No.
                      >Thats quite sad as many mobile platforms need this singleton facility
                      >but don't support global and static vaiables.
                      >--eminemence.
                      >>
                      [Please, do not top-post]
                      >
                      Really? A C++ compiler with no possibility to define a static or
                      global variable? I have never found one, and I work in the embedded
                      field. May you tell me which one is it, to avoid it?
                      >
                      Zara
                      Symbian does not allow.
                      So if you compile for the emulator it will allow the static and global
                      declarations but when the app is finally linked for the platform it
                      throws an error.
                      So such a facility is needed just to accommodate all platforms nothing
                      else.
                      >
                      That's truly Evil(TM). I read up on it. And there seems to be no good
                      reason -- a lot of technical mumbo-jumbo talk about possible costs of
                      static data given the quite baffling decisions the Symbian OS designers
                      have landed on, but no discussion of the rationale of those decisions,
                      e.g. why application DLLs are treated the same as ordinary DLLs.
                      >
                      But then, I've never programmed the Symbian OS.
                      >
                      It seems you have three choices to get the equivalent of standard C++
                      functionality for static data on this OS:
                      >
                      * do not target the old combination EKA1 DLL (whatever "EKA1" means),
                      which is where this problem exists, or
                      >
                      * place all needed singletons or support for them in an EXE server,
                      or
                      >
                      * use thread local storage (in Symbian, Dll::SetTls() and Dll::Tls()).
                      >
                      <url:
                      http://developer.symbi an.com/main/downloads/papers/static_data/SupportForWrite ableStaticDataI nDLLsv1.1.pdf>.
                      >
                      Hope this helps,
                      >
                      - Alf
                      >
                      --
                      A: Because it messes up the order in which people normally read text.
                      Q: Why is it such a bad thing?
                      A: Top-posting.
                      Q: What is the most annoying thing on usenet and in e-mail?
                      Hi Alf,
                      As I have been programming for Symbian for the past 2 and half years
                      and was aware of the solution when its only for Symbian.
                      But what do we do if we want to have a single design and make that work
                      on most of the mobile platforms?
                      We cannot ignore the previous version of Symbian as lots of devices are
                      in the market which have the older version.
                      So nothing evil but just trying to have a generic design which can
                      work.
                      Best Regards
                      --eminemence.

                      Comment

                      • Roland Pibinger

                        #12
                        Re: Singleton without static : possible??

                        On 2 Nov 2006 01:50:07 -0800, "eminemence " <eminemence@gma il.com>
                        wrote:
                        >Thats quite sad as many mobile platforms need this singleton facility
                        >but don't support global and static vaiables.
                        Create a context object and pass it to the places where the context is
                        needed. BTW, global and static variables cause a lot of well known
                        problems and reduce reusability and testability of code. It's better
                        to avoid them even if you are not forced by the environment.

                        Best wishes,
                        Roland Pibinger

                        Comment

                        • Gianni Mariani

                          #13
                          Re: Singleton without static : possible??

                          benben wrote:
                          Gianni Mariani wrote:
                          >eminemence wrote:
                          >>Hi,
                          >>Is it possible to implement a Singleton without using static variables
                          >>or global variables.
                          >>Thanks.
                          >>--eminemence.
                          >>>
                          >>
                          >No.
                          >
                          Why the hell not? As long as you have a storage you can do it. For example,
                          >
                          Single* obtain_single()
                          {
                          void* p = get_system_pool ();
                          byte_t* i = static_cast<byt e_t*>(p);
                          if (*(i-1) == 0)
                          {
                          Single* s = new(p) Single;
                          return s;
                          }
                          >
                          return static_cast<Sin gle*>(p);
                          }
                          Is get_system_pool () standard C++ ?

                          I assumed that the OP meant "using standard c++" since this NG is
                          specific on "standard" C++.

                          Comment

                          • Alf P. Steinbach

                            #14
                            Re: Singleton without static : possible??

                            * eminemence:
                            As I have been programming for Symbian for the past 2 and half years
                            and was aware of the solution when its only for Symbian.
                            But what do we do if we want to have a single design and make that work
                            on most of the mobile platforms?
                            First, please don't quote signatures.

                            Well, the language standard can't help you when the problem is a
                            non-standard implementation of the language.

                            However, abstraction can. E.g. a static singleton manager library,
                            implemented differently for different platforms. But then, Symbian's
                            lack of support for exceptions and insistence on evil two-phase
                            construction makes things complicated, so some innovation required.

                            --
                            A: Because it messes up the order in which people normally read text.
                            Q: Why is it such a bad thing?
                            A: Top-posting.
                            Q: What is the most annoying thing on usenet and in e-mail?

                            Comment

                            • Steve Pope

                              #15
                              Re: Singleton without static : possible??

                              eminemence <eminemence@gma il.comwrote:
                              >Symbian does not allow.
                              >So if you compile for the emulator it will allow the static and global
                              >declarations but when the app is finally linked for the platform it
                              >throws an error.
                              >So such a facility is needed just to accommodate all platforms nothing
                              >else.
                              Consider devoting a thread or process to the functionality
                              you would use a singleton for.

                              Steve

                              Comment

                              Working...