//
//  String.h
//
//  Qdos - string class and data object class
//
//  David Burgess
//  September 1999
//

#ifndef Q_STRING_H
#define Q_STRING_H

#include <string>
#include "QObject.h"
#include "Sequence.h"
#include "StringRep.h"

namespace QDOS {

typedef string String;

class QdString : public QdObject, public String
{
public:
  QdString(void) : QdObject(), String() {;}
  QdString( const string& s ) : QdObject(), String(s) {;}
  QdString( const QdString& qs ) : QdObject(), String(qs) {;}

  QdString& operator=( const string& s )
    { (string&)(*this) = s; return *this; }
  QdString& operator=( const QdString& qs )
    { (string&)(*this) = qs; return *this; }
    
  virtual QdObject_var create() const
  { return new QdString();}
    
  virtual QdObject_var copy() const
  { return new QdString(*this);}
  
  virtual string srep(const StringRep& stringrep,int sequence_index=0) const 
  { string delim(stringrep.get_token(StringRep::SR_TOKEN_STRING_DELIMITER));
    return delim+(*this)+delim;}
    
  virtual string object_srep( const StringRep& stringrep ) const
  { return "QdString";}

  virtual void get(Vistream& v_is)
  { string& s=*this; v_is >> s;}

  virtual void put(Vostream& v_os)
  { const string& s=*this; v_os << s;}
  
  QDOBJECT
};

typedef Var<QdString> QdString_var;

class StringSeq : public Sequence<String>
{
public:
  StringSeq(int num=0, const String& str=""):Sequence<String>(num,str){;}
  StringSeq( const vector<String>& s_seq ) : Sequence<String>(s_seq) {;}
  
  size_t maxlen() const
  { size_t mx=0; for (int i=0;i<size();i++) mx=std::max(mx,elt(i).size()); \
    return mx;}
};

class QdStringSeq : public QdObject, public StringSeq
{
public:
  QdStringSeq(int num=0, const String& str=""):StringSeq(num,str){;}
  //QdStringSeq( const StringSeq& s_seq ) : StringSeq(s_seq) {;}
  QdStringSeq( const QdStringSeq& s_seq):StringSeq(s_seq){;}
  QdStringSeq( const vector<String>& s_seq ):StringSeq(s_seq){;}

  virtual bool is_sequence(void) const { return true; }
  int sequence_size(void) const { return size(); }
  
  virtual QdObject_var create() const
  { return new QdStringSeq();}
  
  virtual QdObject_var copy() const
  { return new QdStringSeq(*this);}
  
  virtual string srep(const StringRep& stringrep,int sequence_index=0) const
  {
    string s;
    if(sequence_index<0 || sequence_index>=size())
        return s;
    string delim(stringrep.get_token(StringRep::SR_TOKEN_STRING_DELIMITER));
    return delim+elt(sequence_index)+delim ;
  }
 
  virtual string object_srep( const StringRep& stringrep ) const
  { return "QdStringSeq";}

  virtual void get(Vistream& v_is)
  { vector<string>& sv=*this; v_is >> sv;}
  
  virtual void put(Vostream& v_os)
  { const vector<string>& sv=*this; v_os << sv;}

  QDOBJECT
};

typedef SeqVar<QdStringSeq> QdStringSeq_var;

} // end namespace QDOS

#endif // #ifndef Q_STRING_H

