Source: OmInputDevice.h


Annotated List
Files
Globals
Hierarchy
Index
#ifndef OM_INPUT_DEVICE_H
#define OM_INPUT_DEVICE_H

/**
 * Low level input handling for the Standard OpenMath protocol.
 *
 * This class provides a basic light encapsulation of the C library, concerning the input operations.
 * The built-in types in use are strongly compatible with those used in the C library.
 * This layer can be used independently from the object-oriented approach provided by the OmNode hierarchy,
 * and it may be more simple to integrate it in C++ code than directly calling the C library.
 * Any call to readX() may throw an exception, which should be caught as an OmException & object.
 * All readX() methods return the current (this) input device.
 */
class OmInputDevice
{
public:
  /**
   * Read the type of the current available token.
   */
  virtual OmInputDevice & readType(OmType & type_out) {
    OMtokenType type;
    OmException::checkUnderlying(OMgetType(device_, &type));
    type_out = OmType(type);
    return *this; }

  /**
   * Read the length of the current available token, if applicable.
   */
  virtual OmInputDevice & readLength(unsigned int & length_out) {
    int length=0;
    OmException::checkUnderlying(OMgetLength(device_, &length));
    length_out = length;
    return *this; }

  /**
   * Read the length of the current available Symbol token, if applicable.
   */
  virtual OmInputDevice & readSymbolLength(unsigned int & cdLength_out, unsigned int & nameLength_out) {
    int cdLength=0, nameLength=0;
    OmException::checkUnderlying(OMgetSymbolLength(device_, &cdLength, &nameLength));
    cdLength_out = cdLength;
    nameLength_out = nameLength;
    return *this; }

  /**
   * Read a built-in 32-bits signed integer value.
   */
  virtual OmInputDevice & readInteger(int & value_out) {
    OmException::checkUnderlying(OMgetInt32(device_, &value_out));
    return *this; }

  /**
   * Read a big integer number.
   * @li Precondition: ~ NullPointer(digits_in)
   */
  virtual OmInputDevice & readBigInteger(char * digits_in, unsigned int length_in, int & sign_out, unsigned int & base_out) {
    OmException::checkNullPointer(digits_in);
    OMbigIntType format;
    OmException::checkUnderlying(OMgetBigIntN(device_, digits_in, length_in, &sign_out, &format));
    base_out = (format==OMbigIntBase10 ? 10 : (format==OMbigIntBase16 ? 16 : 0));
    return *this; }

  /**
   * Read a built-in 64bits float value.
   */
  virtual OmInputDevice & readFloat(double & value_out) {
    OmException::checkUnderlying(OMgetFloat64(device_, &value_out));
    return *this; }

  /**
   * Read a byte array.
   * @li Precondition: ~ NullPointer(buffer_in)
   */
  virtual OmInputDevice & readByteArray(char * buffer_in, unsigned int length_in) {
    OmException::checkNullPointer(buffer_in);
    OmException::checkUnderlying(OMgetByteArrayN(device_, buffer_in, length_in));
    return *this; }
  
  /**
   * Read a zero-terminated C string of 8 bits characters.
   * @li Precondition: ~ NullPointer(buffer_in)
   */
  virtual OmInputDevice & readString(char * buffer_in, unsigned int length_in) {
    OmException::checkNullPointer(buffer_in);
    OmException::checkUnderlying(OMgetStringN(device_, buffer_in, length_in));
    return *this; }

  /**
   * Read a zero-terminated C string of wide characters.
   * @li Precondition: ~ NullPointer(buffer_in)
   */
  virtual OmInputDevice & readWString(wchar_t * buffer_in, unsigned int length_in) {
    OmException::checkNullPointer(buffer_in);
    OmException::checkUnderlying(OMgetWCStringN(device_, (OMUCS2 *)buffer_in, length_in));
    return *this; }

  /**
   * Read a variable as a zero-terminated C string of characters.
   * @li Precondition: ~ NullPointer(name_in)
   */
  virtual OmInputDevice & readVariable(char * name_in, unsigned int length_in) {
    OmException::checkNullPointer(name_in);
    OmException::checkUnderlying(OMgetVarN(device_, name_in, length_in));
    return *this; }

  /**
   * Read a symbol as a couple (cd, name) of two zero-terminated strings of characters.
   * @li Precondition: ~ Nullpointer(cd_in) and ~ NullPointer(name_in)
   */
  virtual OmInputDevice & readSymbol(char * cd_in, unsigned int cdLength_in, char * name_in, unsigned int nameLength_in) {
    OmException::checkNullPointer(cd_in);
    OmException::checkNullPointer(name_in);
    OmException::checkUnderlying(OMgetSymbolN(device_, cd_in, cdLength_in, name_in, nameLength_in));
    return *this; }

  /**
   * Read an application begin tag.
   */
  virtual OmInputDevice & readApplication() {
    OmException::checkUnderlying(OMgetApp(device_));
    return *this; }

  /**
   * Read an application end tag.
   */
  virtual OmInputDevice & readEndApplication() {
    OmException::checkUnderlying(OMgetEndApp(device_));
    return *this; }

  /**
   * Read an attribute begin tag.
   */
  virtual OmInputDevice & readAttribute() {
    OmException::checkUnderlying(OMgetAttr(device_));
    return *this; }

  /**
   * Read an attribute end tag.
   */
  virtual OmInputDevice & readEndAttribute() {
    OmException::checkUnderlying(OMgetEndAttr(device_));
    return *this; }

  /**
   * Read an attribute parameter begin tag.
   */  
  virtual OmInputDevice & readAttributeParameter() {
    OmException::checkUnderlying(OMgetAtp(device_));
    return *this; }
  
  /**
   * Read an attribute parameter end tag.
   */
  virtual OmInputDevice & readEndAttributeParameter() {
    OmException::checkUnderlying(OMgetEndAtp(device_));
    return *this; }

  /**
   * Read a binding begin tag.
   */
  virtual OmInputDevice &  readBinding() {
    OmException::checkUnderlying(OMgetBind(device_));
    return *this; }

  /**
   * Read a binding end tag.
   */
  virtual OmInputDevice & readEndBinding() {
    OmException::checkUnderlying(OMgetEndBind(device_));
    return *this; }

  /**
   * Read a binding variable begin tag.
   */
  virtual OmInputDevice & readBindingVariable() {
    OmException::checkUnderlying(OMgetBVar(device_));
    return *this; }

  /**
   * Read a binding variable end tag.
   */
  virtual OmInputDevice & readEndBindingVariable() {
    OmException::checkUnderlying(OMgetEndBVar(device_));
    return *this; }

  /**
   * Read an object begin tag.
   */
  virtual OmInputDevice & readObject() {
    OmException::checkUnderlying(OMgetObject(device_));
    return *this; }

  /**
   * Read an object end tag.
   */
  virtual OmInputDevice & readEndObject() {
    OmException::checkUnderlying(OMgetEndObject(device_));
    return *this; }

  /**
   * Read an error begin tag.
   */
  virtual OmInputDevice & readError() {
    OmException::checkUnderlying(OMgetError(device_));
    return *this; }

  /**
   * Read an error end tag.
   */
  virtual OmInputDevice & readEndError() {
    OmException::checkUnderlying(OMgetEndError(device_));
    return *this; }

  /**
   * Read a processing instruction.
   * @li Precondition: ~ NullPointer(buffer_in)
   */
  virtual OmInputDevice & readPInstruction(char * buffer_in, unsigned int length_in) {
    OmException::checkNullPointer(buffer_in);
    OmException::checkUnderlying(OMgetPInstructionN(device_, buffer_in, length_in));
    return *this; } 

  /**
   * Read a comment.
   * @li Precondition: ~ NullPointer(buffer_in)
   */
  virtual OmInputDevice & readComment(char * buffer_in, unsigned int length_in) {
    OmException::checkNullPointer(buffer_in);
    OmException::checkUnderlying(OMgetCommentN(device_, buffer_in, length_in));
    return *this; }

public:
  /**
   * Constructor. The input stream and the encoding must be given in arguments, they cannot change later.
   * Also it is strongly recommended not to share an input stream with another input device (undefined behavior).
   */
  OmInputDevice(OmInputStream & stream_in, OmEncoding encoding_in, bool ignoreComment_in=true) {
    device_ = OMmakeDevice((OMencodingType)encoding_in, stream_in.getIO());
    OMignoreComment(device_, ignoreComment_in);
    OMsetVerbosityLevel(0); }

  /**
   * Destructor.
   */
  virtual ~OmInputDevice() {
    OMcloseDevice(device_); }

  /**
   * Get the encoding.
   */
  virtual OmEncoding getEncoding() const {
    return OmEncoding(OMgetDeviceEncoding(device_)); }

private:
  OmInputDevice(const OmInputDevice &) {}
  void operator =(const OmInputDevice &) {}

private:
  OMdev device_;
};

#endif // OM_INPUT_DEVICE_H

Generated by: root@localhost.localdomain on Tue Oct 12 21:02:30 199.