Options.h Singleton Pattern - PowerPoint PPT Presentation

About This Presentation
Title:

Options.h Singleton Pattern

Description:

Sometimes we want to make sure that there is only one instance ... int field_offset (void); // Offset from BOL. //Returns the pivot strategy in this singleton ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 9
Provided by: cse6
Learn more at: http://www.cse.msu.edu
Category:

less

Transcript and Presenter's Notes

Title: Options.h Singleton Pattern


1
Options.h(Singleton Pattern)
  • By
  • Kirit Patel
  • James Vanderhyde

2
Why use Singleton Patterns?
  • Ensures a class has only one instance.
  • Providing a global point of access to it.
  • Sometimes we want to make sure that there is only
    one instance of a particular class/system. (i.e.
    file system, window manager,)
  • Singleton was a nice person.

3
Options.h
  • //This class is a Singleton. It contains exactly
  • // one instance that is statically referenced.
  • // It is useful for holding global information
  • // without the dangers of global variables.
  • class Options
  • public // interfaces available to other
    classes
  • //Returns a pointer to the singleton object
    of this class
  • static Options instance (void)
  • //Parses and stores options from main
  • void parse_args (int argc, char argv)

4
Options.h (Contd.)
  • //Four options that may be on or off
  • //These options are stored in octal order
  • // so that we can use them as bitmasks!
  • enum Option
  • FOLD 01, //compare strings case insensitively
  • NUMERIC 02, //compare string as if numbers
  • REVERSE 04, //sort in reverse order (?)
  • NORMAL 010 //compare strings case
    senstively
  • //Pivot-selection strategy for quicksort
  • enum Pivot_Strategy
  • MEDIAN, //median of first, middle, and last
  • RANDOM, //randomly choose a pivot
  • FIRST //use first element

5
Options.h (contd.)
  • //Returns whether Option o is set in this
    singleton
  • bool enabled (Option o)
  • //Returns value of a global variable for
  • // how far we have progressed through the
    input
  • int field_offset (void) // Offset from BOL.
  • //Returns the pivot strategy in this
    singleton
  • Pivot_Strategy pivot_strat (void)
  • //Function pointer for comparing two strings
  • int (compare) (const char l, const char
    r)

6
Options.h (Contd.)
  • protected //interfaces available to this class
    and classes derived from it
  • //The default constructor for this class
  • //I don't know how it ensures a singleton.
  • Options (void) // Ensure Singleton.
  • //The place the maskable options are stored
  • u_long options_ //Maintains options bitmask
    . . .
  • //How far we have progresses through the
    input
  • int field_offset_
  • //The single instance of the class
  • //Since it's static, it is referenced through
    the class
  • static Options instance_ // Singleton.

7
Options.H (Contd.)
  • //This operator is used to compare two Line_Ptrs
    objects.
  • int Line_Ptrsoperatorlt (const Line_Ptrs rhs)
  • //Get a handy reference to the global
    information.
  • Options options Optionsinstance ()
  • //If we are under normal operation,
  • if (options-gtenabled (OptionsNORMAL))
  • //we compare with strcmp.
  • return strcmp (this-gtbof_, rhs.bof_) lt 0
  • //Otherwise, if we are under case-insensitive
    operation,
  • else if (options-gtenabled (OptionsFOLD))
  • //we compare with strcasecmp.
  • return strcasecmp (this-gtbof_, rhs.bof_) lt 0

8
Options.H (Final)
  • //Otherwise, we must be under number comparing
    operation,
  • else
  • // assert (options-gtenabled (OptionsNUMERIC))
  • //so we use numcp.
  • return numcmp (this-gtbof_, rhs.bof_) lt 0
Write a Comment
User Comments (0)
About PowerShow.com