[OPUS]

Num_in_str - A helper class that aids encapsulation of numbers in string objects.


Availability:

This class is available for the OPUS Blackboard API. The initial OPUS 2.0 release is described here

Constructors:
Num_in_str( )
~Num_in_str( )

Methods:
operator+=( ) appends a value to the string
set_width( ) sets the field width
set_fill( ) sets the fill character
reset( ) resets the object to an uninitialized state
str( ) gets the current string value

Description

This class aids the insertion of numbers into strings. It overloads the += operator, allowing various numeric types to be appended to an initial string value (if supplied). A width and fill character can be specified for number formatting purposes.

Example

  
    #include <iostream>
    #include "num_in_str.h"
  
    using namespace std;
  
    // The following example uses a Num_in_str object to embed a
    // number in a string, then print it out
    int main(int argc, char* argv[])
    {
       Num_in_str ns("OSF_TRIGGER:");
       ns.set_width(2);
       ns.set_fill('0');
       ns += 2;
       cout << ns.str() << endl; // outputs "OSF_TRIGGER:02"
       return(0);
    }
  


Num_in_str::Num_in_str - The Num_in_str object constructor.

Synopsis


Num_in_str::Num_in_str(
                       const string& s) // I - string to initialize from

Num_in_str::Num_in_str(
                       const char* s) // I - string to initialize from

Description

The constructor initializes the object's private members. No default width or fill character are set.

Exceptions Thrown

none


Num_in_str::~Num_in_str - The Num_in_str object destructor.

Synopsis


Num_in_str::~Num_in_str()

Description

This method destroys the object.

Exceptions Thrown

none


Num_in_str::operator+= - Append argument to the string maintained by the object.

Synopsis


Num_in_str& Num_in_str::operator+=(
                                   const int i) // I - add int to string

Num_in_str& Num_in_str::operator+=(
                                   const unsigned int i) // I - add int to
                                                         //     string

Num_in_str& Num_in_str::operator+=(
                                   const long i) // I - add long to string

Num_in_str& Num_in_str::operator+=(
                                   const unsigned long i) // I - add int to
                                                          //     string

Num_in_str& Num_in_str::operator+=(
                                   const double& i) // I - add dbl to string

Num_in_str& Num_in_str::operator+=(
                                   const string& s) // I - concat. to string

Description

This method appends the argument to the string maintained by the object using the current formatting that is in effect.

Returns

    A reference to this object.

Exceptions Thrown

none

Example

  
    Num_in_str ns("FOO");
    ns += 10.5;
    string s("BAR");
    ns += s;
    cout << ns.str() << endl;  // outputs "FOO10.5BAR"
  


Num_in_str::set_width - Set the field width for future += operations.

Synopsis


void Num_in_str::set_width(
                           const int i) // I - width

Description

The field width to apply during subsequent += operations is set by this method. If the length of the argument to the += operator is less than the width set by this method, pad characters (as set by the set_fill method) are applied until the width is achieved. If a width is specified, all += operations with numbers are right-justified whereas += operations with strings are left-justified.

Exceptions Thrown

Bad_val<int> - if width is <= 0; Bad_val.arg contains argument

Example

  
    Num_in_str ns("FOO");
    ns.set_width(5);
    ns+=2;
    cout << ns.str() // output is "FOO    2"
  


Num_in_str::set_fill - Set the character to use for field padding.

Synopsis


void Num_in_str::set_fill(
                          const char c) // I - fill character

Description

This method sets the character to use as padding when a += operation is attempted and the field width is greater than the concatenated string. The default fill character is a space.

Exceptions Thrown

none

Example

  
    Num_in_str ns("FOO");
    ns.set_width(5);
    ns.set_fill('#');
    ns+=1;
    cout << ns.str() // outputs "FOO#####1"
  


Num_in_str::reset - Reset the object to its defaults.

Synopsis


void Num_in_str::reset()

Description

This method unsets any width and fill character, and erases the current string value.

Exceptions Thrown

none


Num_in_str::str - Get a copy of the current string.

Synopsis


string Num_in_str::str() const

Description

This method returns the value of the internally maintained string.

Returns

    string containing the current value of this object

Exceptions Thrown

none

Example

  
    Num_in_str ns("FOO");
    cout << ns.str() << endl;  // outputs "FOO"
  

OPUS API index · STScI Home Page · Search · Topics · Index

Copyright © 1997-2000 The Association of Universities for Research in Astronomy, Inc. All Rights Reserved.


For more information, contact opushelp@stsci.edu

Last modified: 25 April 2000