Options h Singleton Pattern By Kirit Patel James
Options. h (Singleton Pattern) By: Kirit Patel James Vanderhyde
Why use Singleton Patterns? n n 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.
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[]);
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
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);
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. };
Options. H (Contd. ) //This operator is used to compare two Line_Ptrs objects. int Line_Ptrs: : operator< (const Line_Ptrs &rhs) { //Get a handy reference to the global information. Options* options = Options: : instance (); //If we are under normal operation, if (options->enabled (Options: : NORMAL)) //we compare with strcmp. return strcmp (this->bof_, rhs. bof_) < 0; //Otherwise, if we are under case-insensitive operation, else if (options->enabled (Options: : FOLD)) //we compare with strcasecmp. return strcasecmp (this->bof_, rhs. bof_) < 0;
Options. H (Final) //Otherwise, we must be under number comparing operation, else // assert (options->enabled (Options: : NUMERIC)); //so we use numcp. return numcmp (this->bof_, rhs. bof_) < 0; }
- Slides: 8