/*
 *    SCCS ID: %Z%%M% %I% %E% SST 
 *
 *    Copyright (c) 1992-1993 Mark Soloway.  All rights reserved.
 *    See SST_LEGAL_NOTICE file for terms of the license.
 */

#ifndef _TT_PATTERN_H
#define _TT_PATTERN_H

#include <desktop/tt_c.h>
#include <rpc/types.h>
#include <rpc/xdr.h>

#include "TTBoolean.h" 
#include "TTGeneric.h" 

class TTMessage;
class TTObjectSpec;
class TTPattern;
class TTSession;

typedef Tt_callback_action (*TTPatternCallback) (TTPattern *, TTMessage *);

class TTPattern : public TTGeneric {
public:
  TTPattern (Tt_status&);
  TTPattern (Tt_category, Tt_status&);
  TTPattern (Tt_category,
	     const char *op,
	     Tt_scope,
	     Tt_address,
	     Tt_status&);
  TTPattern (Tt_category,
	     const char *op,
	     Tt_scope,
	     Tt_status&);
  TTPattern (Tt_category,
             const char *objid,
             const char *op,
             Tt_scope,
             Tt_status&);
  TTPattern (Tt_category,
	     const TTObjectSpec&,
	     const char *op,
	     Tt_scope,
	     Tt_status&);
  TTPattern (const TTPattern&);
  TTPattern &operator= (const TTPattern&);
  ~TTPattern ();

  Tt_status   setCategory (Tt_category);
  Tt_category getCategory (Tt_status&) const;

  Tt_status addAddress (Tt_address);
  Tt_status addClass (Tt_class);
  Tt_status addDisposition (Tt_disposition);
  Tt_status addFile (const char *);
  Tt_status addObject (const char *);
  Tt_status addObjectSpec (const TTObjectSpec&);
  Tt_status addOp (const char *);
  Tt_status addOType (const char *);
  Tt_status addScope (Tt_scope);
  Tt_status addSender (const char *);
  Tt_status addSenderPType (const char *);
  Tt_status addSession (const char *);
  Tt_status addState (Tt_state);

  Tt_status addArg (Tt_mode, const char *, int);
  Tt_status addArg (Tt_mode, const char *, const char *);
  Tt_status addArg (Tt_mode, const char *, const unsigned char *, int);
  Tt_status addArg (Tt_mode, const char *, xdrproc_t, void *);

  Tt_status addContext (const char *, int);
  Tt_status addContext (const char *, const char *);
  Tt_status addContext (const char *, const unsigned char *, int);
  Tt_status addContext (const char *, xdrproc_t, void *);

  // The message object received by a pattern callback is deleted
  // automatically by the pattern object.  The pattern callback should
  // NOT delete the message object or pattern object that has been
  // received.

  // NOTE: Pattern callbacks are executed unless the TTSession object
  //       the pattern is registered with has received a message either
  //       through one of the "waitFor..." member functions or by a
  //       call to receiveAsynchronousMessage.
  Tt_status setCallback (TTPatternCallback);

protected:
  // If called with TT_TRUE (by a derived class), the defaultPatternCallback  
  // function is called when the pattern is matched by a message that has
  // been received.  The defaultPatternCallback can be overridden by the
  // derived class.
  Tt_status                  useDefaultPatternCallback (TTBoolean);

private:
  TTPatternCallback patternCallback;
  Tt_pattern        ttPattern;
  TTBoolean         useDefaultPatternCallbackFlag;

  TTPattern () { setTTPatternDefaults (); }
  TTPattern (Tt_pattern pattern)
    {
      setTTPatternDefaults ();
      setPattern (pattern);
    }

  void       setTTPatternDefaults ();
  Tt_pattern getPattern () const;
  void       setPattern (Tt_pattern);
  void       checkNewPattern (Tt_status);
  void       initializeOrIncreaseReferenceCount ();
  void       increaseReferenceCount ();
  void       decreaseReferenceCount ();

  virtual Tt_callback_action defaultPatternCallback (TTMessage *);

#ifdef __GNUC__
public:
#endif
  static Tt_callback_action internalPatternCallback (Tt_message,
						     Tt_pattern);

friend class TTMessage;
friend class TTSession;
};

#endif
