32.8 Accessing parameter values

Previous sections in this chapter have used the OEWriteSettings function to list the settings of various parameter to standard out. Parameter values however, are most commonly accessed directly using the Has and Get member functions of OEInterface. These functions are described in detail in the API documentation, but briefly the Has function returns true if a parameter has either a default value or a user specified setting. The Get returns the user specified setting of the parameter, or the default value if the user did not specify the setting (if the user did not specify a setting and there is no default value a default constructed object is returned an an error is issued). The following example illustrates the use of the Has and Get member functions.

Chapter 32 - Example 10 : cpp file

#include "oeplatform.h"
#include "oesystem.h"

//Defines static const unsigned char* array InterfaceData
#include "ch32-10.itf"

//Defines static const unsigned char* array RequirementData
#include "ch32-10r.req"

using namespace OEPlatform;
using namespace OESystem;
using namespace std;
int main(int argc, char** argv)
{
  OEInterface itf;
  OEConfigure(itf,InterfaceData);
  if (OECheckHelp(itf,argc,argv)) return 0;
  OEParseCommandLine(itf,argc,argv,RequirementData);

  if (!itf.Has<string>("-op"))
  {
    OEThrow.Error("You must specify -op");
    return 1;
  }

  string op = itf.Get<string>("-op");
  float x = itf.Get<float>("-x");
  float y = itf.Get<float>("-y");

  if (op == "add")
  {
    oeout << x << " plus " << y << " is " << x+y << oeendl;
  }
  else if (op == "subtract")
  {
    oeout << x << " minus " << y << " is " << x-y << oeendl;
  }
  else if (op == "multiply")
  {
    oeout << x << " times " << y << " is " << x*y << oeendl;
  }
  else if (op == "divide")
  {
    oeout << x << " divided by " << y << " is " << x/y << oeendl;
  }
  else
  {
    OEThrow.Error("%s is not a valid operation",op.c_str());
    return 1;
  }

  return 0;
}

Chapter 32 - Example 10 : OEConfigure txt configuration file

!CATEGORY Variables
  !BRIEF Category for variables

  !PARAMETER -x
    !TYPE float
    !DEFAULT 0.0
    !BRIEF First variable
  !END

  !PARAMETER -y
    !TYPE float
    !DEFAULT 0.0
    !BRIEF Second variable
  !END

!END

!CATEGORY operator

  !PARAMETER -op
    !TYPE string
    !BRIEF Operation to perform on x and y
    !DETAIL
      The operation performed is

        x <-op> y

      So if -op is 'add' the operation is

        x + y
  !END

!END

Chapter 32 - Example 10 : Requirement txt configuration file

!REQUIREMENT
  !OPTION -y 0.0 !-op divide
  !OPTION !-y 0.0
  !ERROR_MSG
    Cannot divide by zero
    result would be undefined
!END

Not that there is no need to check to see if the itf object Has -x or -y because these variables have a default value, Has will therefore always return true. The Get function with therefore always succeed (if the user doesn't specify a value the default will be returned).

Output of this program looks like the following.

> ch32-10 -x 1 -y 2 -op add
1.000000 plus 2.000000 is 3.000000

> ch32-10 -x 1 -y 2 -op multiply
1.000000 times 2.000000 is 2.000000

> ch32-10 -x 1 -y 2 -op divide
1.000000 divided by 2.000000 is 0.500000

> ch32-10 -x 1 -y 2 -op subtract
1.000000 minus 2.000000 is -1.000000

> ch32-10 -x 1 -y 2 -op square
Fatal: square is not a valid operation

> ch32-10 -op divide
Fatal: Missing requirements:
    Cannot divide by zero
    result would be undefined
>

Some things to know about the Has and Get member functions of OEInterface.

  1. If Has is templated incorrectly (i.e., the parameter with the given name is not the same type as the Has functions template) it returns false.

  2. If Get is templated incorrectly (i.e., the parameter with the given name is not the same type as the Get functions template) it returns a default constructed object of the type Get is templated upon, and issued an error.

  3. If Has returns true, Get will succeed (for the same input).

  4. The Has and Get automatically search all child interfaces for parameters.