Changeset 1986


Ignore:
Timestamp:
Jun 15, 2006, 2:13:49 AM (15 years ago)
Author:
orlov
Message:
  • A lot of stuff for easing symbol creation.
  • iter, lsplit, and rsplit are renamed to RF_iter, RF_lsplit, and RF_rsplit.
Location:
to-imperative/trunk/runtime
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/runtime/rf_expr.hh

    r1778 r1986  
    2828class Result;
    2929
     30class True;
     31
    3032using namespace rftype ;
     33
     34using pxx::WString;
    3135
    3236///
     
    204208  /// whether a result expression was already constructed and delete it.
    205209  static inline void clear (Expr* _ptr) ;
     210  ///
     211  /// Try to get a reference to the object underlying the first term.
     212  /// If the term has a type other then RF_SYMBOL(ObjClass) then throw
     213  /// InvalidParam().
     214  template <class ObjClass>
     215  inline ObjClass const& get_object () const ;
     216  ///
     217  /// Try to cast the first term to the concrete term type
     218  /// (RF_SYMBOL(ObjClass)) and return a pointer to the underlying object.
     219  /// If not success return null.
     220  template <class ObjClass>
     221  inline ObjClass* try_get_ptr () const ;
     222  ///
     223  /// If a type of the first term is RF_SYMBOL(ObjClass1) then convert the
     224  /// underlying object to ObjClass2.
     225  /// Else throw InvalidParam().
     226  template <class ObjClass1, class ObjClass2>
     227  inline ObjClass2 convert_object() const ;
     228  ///
     229  /// Same as convert_object() but before converting check that Cond::check()
     230  /// is true for the object.
     231  template <class ObjClass1, class ObjClass2, class Cond>
     232  inline ObjClass2 check_convert_object() const ;
    206233
    207234  template<class TermClass, typename CppType>
     
    211238  static inline Expr create_seq (CppType const* _array) ;
    212239
     240  template<class ObjClass, typename CppType1>
     241  static inline Expr create_terms (CppType1 const* _array) ;
     242
     243  template<class ObjClass, typename CppType1, typename CppType2>
     244  static inline Expr create_terms (CppType1 const* _array, CppType2 const& _arg2) ;
     245
    213246  template<class ObjClass>
    214247  static inline Expr create () ;
     248
     249  template<class ObjClass1, class ObjClass2>
     250  inline Expr convert () const ;
     251
     252  template<class ObjClass1, class ObjClass2>
     253  inline Expr (Expr const& _expr) ;
    215254
    216255  template<class ObjClass, class CppType1>
     
    238277  static inline Expr create_sym (const CppType1& _arg1, const CppType2& _arg2) ;
    239278 
     279  template <class ObjClass, class CppType1, class CppType2, class CppType3>
     280  static inline Expr create_sym (
     281      const CppType1& _arg1, const CppType2& _arg2, const CppType3& _arg3) ;
     282 
    240283  template <class ObjClass>
    241284  static inline Expr create_static_sym (wchar_t const* _name) ;
     285
     286  template <class TypeCond, class ObjCond>
     287  inline bool check () const ;
     288
     289  template <class TypeCond>
     290  inline bool check_type () const ;
    242291
    243292private:
     
    321370  /// Check whether we can copy another expression to the left from our
    322371  /// in the same memory block.
    323   inline bool lt_alloc( uintptr_t _len ) const ;
    324 
     372  inline bool lt_alloc (uintptr_t _len) const ;
     373
     374protected:
     375
     376  ///
     377  /// Set term at _p as a border.  To use in case of expression construction
     378  /// failure.
     379  static inline void set_border (Term* _p) ;
    325380
    326381public:
    327382
    328383  class TooLargeArgument :
    329     public Exception
     384    public pxx::Exception
    330385  {
    331386  public:
     
    333388      { fprintf(stderr, "Argument too large for conversion\n"); }
    334389  };
     390
     391  class InvalidParam :
     392    public pxx::Exception
     393  {
     394  public:
     395    void print()
     396      { fprintf(stderr, "Invalid parameter\n"); }
     397  };
     398
     399  template <class C>
     400  class Iterator
     401  {
     402    Term* curr;
     403    Term* last;
     404  public:
     405    Iterator (Expr const& _expr) :
     406      curr (_expr.get_first()),
     407      last (_expr.get_last())
     408    {}
     409    ///
     410    inline Iterator& operator ++ ()
     411    {
     412      curr++;
     413      return self;
     414    }
     415    ///
     416    inline Iterator& operator ++ (int)
     417    {
     418      return operator++();
     419    }
     420    ///
     421    inline operator bool () const
     422    {
     423      return curr < last;
     424    }
     425  };
     426
     427  template <class TypeCond, class Conv, class Cond = True>
     428  class Convert;
    335429
    336430  inline Expr const& operator , (Expr const& _expr) const ;
     
    405499///
    406500/// A macro naming iterator for expression variable
    407 #define iter(e) e##_iter
     501#define RF_iter(e) e##_iter
    408502///
    409503/// A macro splitting an expression variable
    410 #define lsplit(e, minlen, le, re) \
    411   SplitIterator<d_lt> iter(e)(e, minlen); \
    412   Expr const& le = iter(e).get_left(); \
    413   Expr const& re = iter(e).get_right()
     504#define RF_lsplit(e, minlen, le, re) \
     505  SplitIterator<d_lt> RF_iter(e)(e, minlen); \
     506  Expr const& le = RF_iter(e).get_left(); \
     507  Expr const& re = RF_iter(e).get_right()
    414508 
    415 #define rsplit(e, minlen, le, re) \
    416   SplitIterator<d_rt> iter(e)(e, minlen); \
    417   Expr const& le = iter(e).get_left(); \
    418   Expr const& re = iter(e).get_right()
     509#define RF_rsplit(e, minlen, le, re) \
     510  SplitIterator<d_rt> RF_iter(e)(e, minlen); \
     511  Expr const& le = RF_iter(e).get_left(); \
     512  Expr const& re = RF_iter(e).get_right()
    419513
    420514
  • to-imperative/trunk/runtime/rf_expr.ih

    r1772 r1986  
    1717#include "rf_object_ref.ih"
    1818#include "rf_static_object.ih"
    19 #include "rf_integer.ih"
    20 #include "rf_short_int.ih"
    2119#include "rf_common.ih"
    2220#include "rf_expr_constr.hh"
     
    463461}
    464462
     463template <class ObjClass>
     464inline ObjClass const& Expr::get_object () const
     465{
     466  assert(get_len() > 0);
     467  if (first->is_instance_of<ObjClass>())
     468    return first->get_object<ObjClass>();
     469  throw InvalidParam();;
     470}
     471
     472template <class ObjClass>
     473inline ObjClass* Expr::try_get_ptr () const
     474{
     475  assert(get_len() > 0);
     476  if (first->is_instance_of<ObjClass>())
     477    return first->get_obj_ptr<ObjClass>();
     478  return null;
     479}
     480
     481template <class ObjClass>
     482inline Expr Expr::create ()
     483{
     484  return CreateExpr0<ObjClass::term_class, ObjClass>();
     485}
     486
     487template <class ObjClass, class CppType1>
     488inline Expr Expr::create (const CppType1& _arg1)
     489{
     490  return CreateExpr1<ObjClass::term_class, ObjClass, CppType1>(_arg1);
     491}
     492
     493template <class ObjClass, class CppType1, class CppType2>
     494inline Expr Expr::create (const CppType1& _arg1, const CppType2& _arg2)
     495{
     496  return CreateExpr2<ObjClass::term_class, ObjClass, CppType1, CppType2>(
     497      _arg1, _arg2);
     498}
     499
     500template <class ObjClass>
     501inline Expr Expr::create_sym ()
     502{
     503  Expr e(1);
     504  new (e.first) Symbol<ObjClass, typename SymbolType<ObjClass>::Type>();
     505  e.set_flat_bit<typename SymbolType<ObjClass>::Type>();
     506  return e;
     507}
     508
     509template <class ObjClass>
     510inline Expr Expr::create_static_sym (wchar_t const* _name)
     511{
     512  Expr e(1);
     513  new (e.first) Symbol<NamedObject<ObjClass>, ObjectTerm>(_name);
     514  e.set_flat_bit<ObjectTerm>();
     515  return e;
     516}
     517
     518
     519template <class ObjClass, class CppType1>
     520inline Expr Expr::create_sym (const CppType1& _arg1)
     521{
     522  Expr e(1);
     523  new (e.first) Symbol<ObjClass, typename SymbolType<ObjClass>::Type>(_arg1);
     524  e.set_flat_bit<typename SymbolType<ObjClass>::Type>();
     525  return e;
     526}
     527
     528template <class ObjClass, class CppType1, class CppType2>
     529inline Expr Expr::create_sym (const CppType1& _arg1, const CppType2& _arg2)
     530{
     531  Expr e(1);
     532  new (e.first)
     533    Symbol<ObjClass, typename SymbolType<ObjClass>::Type>(_arg1, _arg2);
     534  e.set_flat_bit<typename SymbolType<ObjClass>::Type>();
     535  return e;
     536}
     537
     538template <class ObjClass, class CppType1, class CppType2, class CppType3>
     539inline Expr Expr::create_sym (
     540    const CppType1& _arg1, const CppType2& _arg2, const CppType3& _arg3
     541)
     542{
     543  Expr e(1);
     544  new (e.first)
     545    Symbol<ObjClass, typename SymbolType<ObjClass>::Type>(_arg1, _arg2, _arg3);
     546  e.set_flat_bit<typename SymbolType<ObjClass>::Type>();
     547  return e;
     548}
     549
     550//! template<class ObjClass, typename CppType>
     551//! inline Expr Expr::create_seq (CppType const* _array, size_t _size)
     552//! {
     553//!   Expr e(_size);
     554//!   e.set_flat_bit<typename SymbolType<ObjClass>::Type>();
     555//!   Term* p = e.get_first();
     556//!   while (_size--)
     557//!     new (p++) Symbol<ObjClass, typename SymbolType<ObjClass>::Type>(*_array++);
     558//!   return e;
     559//! }
     560//!
     561//! template<class ObjClass, typename CppType>
     562//! inline Expr Expr::create_seq (CppType const* _array)
     563//! {
     564//!   return
     565//!     create_seq<ObjClass, CppType>(
     566//!         _array,
     567//!         Symbol<ObjClass, typename SymbolType<ObjClass>::Type>::
     568//!             compute_length(_array)
     569//!     );
     570//! }
     571
    465572template<class TermClass, typename CppType>
    466573inline Expr Expr::create_seq (CppType const* _array, size_t _size)
    467 {
     574{ 
    468575  Expr e(_size);
    469576  if (TermClass::is_flat()) e.flags |= FLAT_BIT;
     
    476583template<class TermClass, typename CppType>
    477584inline Expr Expr::create_seq (CppType const* _array)
    478 {
     585{ 
    479586  return
    480587    create_seq<TermClass, CppType>(_array, TermClass::compute_length(_array));
    481 }
    482 
    483 template <class ObjClass>
    484 inline Expr Expr::create ()
    485 {
    486   return CreateExpr0<ObjClass::term_class, ObjClass>();
    487 }
    488 
    489 template <class ObjClass, class CppType1>
    490 inline Expr Expr::create (const CppType1& _arg1)
    491 {
    492   return CreateExpr1<ObjClass::term_class, ObjClass, CppType1>(_arg1);
    493 }
    494 
    495 template <class ObjClass, class CppType1, class CppType2>
    496 inline Expr Expr::create (const CppType1& _arg1, const CppType2& _arg2)
    497 {
    498   return CreateExpr2<ObjClass::term_class, ObjClass, CppType1, CppType2>(
    499       _arg1, _arg2);
    500 }
    501 
    502 template <class ObjClass>
    503 inline Expr Expr::create_sym ()
    504 {
    505   Expr e(1);
    506   new (e.first) Symbol<ObjClass, typename SymbolType<ObjClass>::Type>();
     588}
     589
     590template<class ObjClass, typename CppType1>
     591inline Expr Expr::create_terms (CppType1 const* _array)
     592{
     593  Expr e(ObjClass::compute_length(_array));
    507594  e.set_flat_bit<typename SymbolType<ObjClass>::Type>();
     595  for (Term* p = e.get_first(); p < e.get_last(); p++)
     596  {
     597    new (p)
     598      Symbol<ObjClass, typename SymbolType<ObjClass>::Type>(&_array);
     599  }
    508600  return e;
    509601}
    510602
    511 template <class ObjClass>
    512 inline Expr Expr::create_static_sym (wchar_t const* _name)
    513 {
    514   Expr e(1);
    515   new (e.first) Symbol<NamedObject<ObjClass>, ObjectTerm>(_name);
    516   e.set_flat_bit<ObjectTerm>();
     603template<class ObjClass, typename CppType1, typename CppType2>
     604inline Expr Expr::create_terms (CppType1 const* _array, CppType2 const& _arg2)
     605{
     606  Expr e(ObjClass::compute_length(_array, _arg2));
     607  e.set_flat_bit<typename SymbolType<ObjClass>::Type>();
     608  for (Term* p = e.get_first(); p < e.get_last(); p++)
     609  {
     610    new (p)
     611      Symbol<ObjClass, typename SymbolType<ObjClass>::Type>(&_array, _arg2);
     612  }
    517613  return e;
    518614}
    519615
    520 
    521 template <class ObjClass, class CppType1>
    522 inline Expr Expr::create_sym (const CppType1& _arg1)
    523 {
    524   Expr e(1);
    525   new (e.first) Symbol<ObjClass, typename SymbolType<ObjClass>::Type>(_arg1);
    526   e.set_flat_bit<typename SymbolType<ObjClass>::Type>();
     616template<class ObjClass1, class ObjClass2>
     617inline Expr Expr::convert () const
     618{
     619  Expr e(get_len());
     620  e.set_flat_bit<typename SymbolType<ObjClass2>::Type>();
     621  Term* q = e.get_first();
     622  for (Term* p = get_first(); p < get_last(); p++, q++) {
     623    new (q)
     624      Symbol<ObjClass2, typename SymbolType<ObjClass2>::Type>(
     625          Term::Convert<ObjClass1, ObjClass2>::convert(*p)
     626      );
     627  }
    527628  return e;
    528629}
    529630
    530 template <class ObjClass, class CppType1, class CppType2>
    531 inline Expr Expr::create_sym (const CppType1& _arg1, const CppType2& _arg2)
    532 {
    533   Expr e(1);
    534   new (e.first)
    535     Symbol<ObjClass, typename SymbolType<ObjClass>::Type>(_arg1, _arg2);
    536   e.set_flat_bit<typename SymbolType<ObjClass>::Type>();
    537   return e;
     631template<class ObjClass1, class ObjClass2>
     632inline Expr::Expr (Expr const& _expr)
     633{
     634  init(_expr.get_len(), 0);
     635  set_flat_bit<typename SymbolType<ObjClass2>::Type>();
     636  Term* q = first;
     637  for (Term* p = _expr.get_first(); p < _expr.get_last(); p++, q++) {
     638    new (q) Symbol<ObjClass2, typename SymbolType<ObjClass2>::Type>(
     639        p->get_object<ObjClass1>().convert<ObjClass2>());
     640  }
    538641}
    539642
     
    9561059        if (!write_wstr(_fp, ws)) return false;
    9571060        if (!f) fputc('\"', _fp);
    958       } else if (p->get_type() == type_int32) {
    959         if (fprintf(_fp, "%" PRIdPTR, ((Int32 const&)(*p)).to_int()) == -1)
    960           return false;
     1061//      } else if (p->get_type() == type_int32) {
     1062//        if (fprintf(_fp, "%" PRIdPTR, ((Int32 const&)(*p)).to_int()) == -1)
     1063//          return false;
    9611064      } else { // if (p->get_type() == type_int) {
    9621065//        mpz_t* z = static_cast<Integer*>(p)->get_mpz_ptr();
     
    10051108INLINE Expr::operator WString () const
    10061109{
     1110  static const WString lp = L"(";
     1111  static const WString rp = L")";
    10071112  WString res;
    10081113  for (Term* p = first; p < last; p++) {
    10091114    if (p->is_sym()) {
    1010       res = res + (WString)(*p);
     1115      res = res + *p;
    10111116    }
    10121117    else {
    1013       res = res + WString(L"(") + (WString)(Expr(p)) + WString(L")");
     1118      res = res + lp + Expr(p) + rp;
    10141119    }
    10151120  }
  • to-imperative/trunk/runtime/rf_func.cc

    r1778 r1986  
    2626      }
    2727      Expr _v_subexpr__arg1 (_v_arg1, 1, (_v_arg1.get_len () - 1));
    28       lsplit (_v_subexpr__arg1, 0, _ve_code, _v_lsplit__arg1);
    29       for ( ; ; iter(_v_subexpr__arg1)++)
     28      RF_lsplit (_v_subexpr__arg1, 0, _ve_code, _v_lsplit__arg1);
     29      for ( ; ; RF_iter(_v_subexpr__arg1)++)
    3030      {
    3131        {
    32           if (!iter(_v_subexpr__arg1))
     32          if (!RF_iter(_v_subexpr__arg1))
    3333          {
    3434            goto _block1__branch1;
     
    4646          }
    4747          Expr _ve_rest (_v_lsplit__arg1, 1, _v_len__rest);
    48           lsplit (Conv_m_Table, 0, _ve__e__e_tmp_m_CppManglerf_m_131_m_20, _v_lsplit__Conv_m_Table);
     48          RF_lsplit (Conv_m_Table, 0, _ve__e__e_tmp_m_CppManglerf_m_131_m_20, _v_lsplit__Conv_m_Table);
    4949          (void)_ve__e__e_tmp_m_CppManglerf_m_131_m_20;
    50           for ( ; ; iter(Conv_m_Table)++)
     50          for ( ; ; RF_iter(Conv_m_Table)++)
    5151          {
    5252            {
    53               if (!iter(Conv_m_Table))
     53              if (!RF_iter(Conv_m_Table))
    5454              {
    5555                RF_RETFAIL;
  • to-imperative/trunk/runtime/rf_macros.hh

    r1778 r1986  
    320320    /*enum { type = c };*/ \
    321321    typedef TermClass Type; \
    322   };
     322  }
    323323
    324324#define RF_SYMBOL(CppType) Symbol<CppType, SymbolType< CppType >::Type>
  • to-imperative/trunk/runtime/rf_symbol.hh

    r1778 r1986  
    2222{};
    2323
     24#if UINTPTR_MAX != UINT32_MAX
     25  template<>
     26  class CheckShortObjectSize<4>
     27  {}; 
     28#endif
     29
     30template<>
     31class CheckShortObjectSize<2>
     32{};
     33
     34template<>
     35class CheckShortObjectSize<1>
     36{};
     37
     38
    2439//template<class C, term_class_t tc = object_term>
    2540//class Symbol;
     
    3550public:
    3651
    37   inline Symbol (C _obj) ;
    38 
    39   inline operator C () ;
    40 
    41   static void ctor (Symbol* _to, Symbol const* _from) ;
    42   static void dtor (Symbol* _term) ;
    43   static bool eq (Symbol const* _term1, Symbol const* _term2) ;
    44   static int compare (Symbol const* _term1, Symbol const* _term2) ;
    45   static uint32_t hash (Symbol const* _term) ;
    46   static pxx::WString to_string (Symbol const* _term) ;
    47 
    48   static short_type_funcs_t funcs ;
    49   static const TypeRegister reg ;
    50 
     52  inline Symbol () ;
     53
     54  template<typename CppType1>
     55  inline Symbol (CppType1 const& _arg1) ;
     56
     57  template<typename CppType1, typename CppType2>
     58  inline Symbol (CppType1 const& _arg1, CppType2 const& _arg2) ;
     59
     60  template<typename CppType1, typename CppType2, typename CppType3>
     61  inline Symbol (
     62      CppType1 const& _arg1, CppType2 const& _arg2, CppType3 const& _arg3) ;
     63
     64  inline C const& get_object () const ;
     65
     66  static void ctor (Symbol* _to, Symbol const* _from) ;
     67  static void dtor (Symbol* _term) ;
     68  static bool eq (Symbol const* _term1, Symbol const* _term2) ;
     69  static int compare (Symbol const* _term1, Symbol const* _term2) ;
     70  static uint32_t hash (Symbol const* _term) ;
     71  static pxx::WString to_string (Symbol const* _term) ;
     72  static size_t get_name (Symbol const* _s, wchar_t const** _np) ;
     73
     74  static short_type_funcs_t funcs ;
     75  static const TypeRegister reg ;
     76
     77  static inline unsigned get_type () ;
     78
     79  template<typename CppType>
     80  static inline size_t compute_length (CppType const* _array) ;
    5181};
    5282
     
    100130
    101131  template<typename CppType1>
    102   inline Symbol (CppType1 _arg1) ;
    103 
    104   template<typename CppType1, typename CppType2>
    105   inline Symbol (CppType1 _arg1, CppType2 _arg2) ;
     132  inline Symbol (CppType1 const& _arg1) ;
     133
     134  template<typename CppType1, typename CppType2>
     135  inline Symbol (CppType1 const& _arg1, CppType2 const& _arg2) ;
     136
     137  template<typename CppType1, typename CppType2, typename CppType3>
     138  inline Symbol (
     139      CppType1 const& _arg1, CppType2 const& _arg2, CppType3 const& _arg3) ;
     140
     141  inline void replace_object () ;
     142
     143  template<typename CppType1>
     144  inline void replace_object (CppType1 const& _arg1) ;
     145
     146  template<typename CppType1, typename CppType2>
     147  inline void replace_object (CppType1 const& _arg1, CppType2 const& _arg2) ;
     148
     149  template<typename CppType1, typename CppType2, typename CppType3>
     150  inline void replace_object (
     151      CppType1 const& _arg1, CppType2 const& _arg2, CppType3 const& _arg3) ;
     152
     153  inline C const& get_object () const ;
    106154
    107155  inline C* get_obj_ptr () const ;
     
    120168
    121169  static inline unsigned get_type() ;
    122 
    123170};
    124171
     
    132179public:
    133180  pxx::WString name;
     181 
    134182  inline NamedObject (wchar_t const* _name) : C(), name(_name) {};
     183 
     184  template<typename CppType1>
     185  inline NamedObject (wchar_t const* _name, CppType1 _arg1) :
     186    C(_arg1), name(_name)
     187  {};
    135188};
    136189
     
    159212  inline Symbol (CppType1 _arg1, CppType2 _arg2) ;
    160213
     214  inline NamedObject<C> const& get_object () const ;
     215
    161216  inline NamedObject<C>* get_obj_ptr () const ;
    162217
  • to-imperative/trunk/runtime/rf_symbol.ih

    r1778 r1986  
    1616
    1717template <class C>
    18 inline Symbol<C, FlatTerm>::Symbol (C _obj) :
    19   Term (reg.get_type())
    20 {
    21   uint_data2 = *reinterpret_cast<uintptr_t*>(&_obj);
    22 }
    23 
    24 /*
    25 template <class C, term_class_t tc>
    26 inline ShortObject<C, tc>::operator C ()
    27 {
    28   return *reinterpret_cast<C*>(&uint_data2);
    29 }
    30 */
     18inline Symbol<C, FlatTerm>::Symbol () :
     19  Term (get_type())
     20{
     21  new (&ptr_data2) C();
     22}
     23
     24template <class C>
     25template <typename CppType1>
     26inline Symbol<C, FlatTerm>::Symbol (CppType1 const& _arg1) :
     27  Term (get_type())
     28{
     29  new (&ptr_data2) C(_arg1);
     30}
     31
     32template <class C>
     33template <typename CppType1, typename CppType2>
     34inline Symbol<C, FlatTerm>::Symbol (
     35    CppType1 const& _arg1, CppType2 const& _arg2
     36) :
     37  Term (get_type())
     38{
     39  new (&ptr_data2) C(_arg1, _arg2);
     40}
     41
     42template <class C>
     43template <typename CppType1, typename CppType2, typename CppType3>
     44inline Symbol<C, FlatTerm>::Symbol (
     45    CppType1 const& _arg1, CppType2 const& _arg2, CppType3 const& _arg3
     46) :
     47  Term (get_type())
     48{
     49  new (&ptr_data2) C(_arg1, _arg2, _arg3);
     50}
     51
     52template <class C>
     53inline C const& Symbol<C, FlatTerm>::get_object () const
     54{
     55  return *reinterpret_cast<C const*>(&int_data2);
     56}
     57
     58template <class C>
     59void Symbol<C, FlatTerm>::ctor (
     60    Symbol<C, FlatTerm>* _to, Symbol<C, FlatTerm> const* _from
     61)
     62{
     63  _to->data1 = _from->data1;
     64  _to->int_data2 = _from->int_data2;
     65}
     66
     67template <class C>
     68void Symbol<C, FlatTerm>::dtor (Symbol<C, FlatTerm>*)
     69{}
     70
     71template <class C>
     72bool Symbol<C, FlatTerm>::eq (
     73    Symbol<C, FlatTerm> const* _s1, Symbol<C, FlatTerm> const* _s2
     74)
     75{
     76  return _s1->int_data2 == _s2->int_data2;
     77}
     78
     79template <class C>
     80int Symbol<C, FlatTerm>::compare (
     81    Symbol<C, FlatTerm> const* _s1, Symbol<C, FlatTerm> const* _s2
     82)
     83{
     84  if (_s1->int_data2 > _s2->int_data2) return 1;
     85  if (_s1->int_data2 < _s2->int_data2) return -1;
     86  return 0;
     87}
     88
     89template <class C>
     90WString Symbol<C, FlatTerm>::to_string (
     91    Symbol<C, FlatTerm> const* _s
     92)
     93{
     94  wchar_t s[2];
     95  s[0] = _s->int_data2;
     96  s[1] = '\0';
     97  return WString(s, 1);
     98}
     99
     100template <class C>
     101size_t Symbol<C, FlatTerm>::get_name (
     102    Symbol<C, FlatTerm> const*, wchar_t const**
     103)
     104{
     105  return (size_t)-1;
     106}
     107
     108template <class C>
     109short_type_funcs_t Symbol<C, FlatTerm>::funcs = {
     110  0,
     111  (void (*)(Term*, Term const*))   (Symbol<C, FlatTerm>::ctor),
     112  (void (*)(Term*))                (Symbol<C, FlatTerm>::dtor),
     113  (bool (*)(Term const*, Term const*))   (Symbol<C, FlatTerm>::eq),
     114  (int  (*)(Term const*, Term const*))   (Symbol<C, FlatTerm>::compare),
     115  null, // (uint32_t (*)(Term const*))            (Symbol<C, FlatTerm>::hash),
     116  (pxx::WString (*)(Term const*))  (Symbol<C, FlatTerm>::to_string),
     117  (size_t (*)(Term const*, wchar_t const**)) (Symbol<C, FlatTerm>::get_name)
     118};
     119
     120template <class C>
     121inline unsigned Symbol<C, FlatTerm>::get_type ()
     122{
     123  return reg.get_type();
     124}
     125
     126template <class C>
     127template <typename CppType>
     128inline size_t Symbol<C, FlatTerm>::compute_length (CppType const* _array)
     129{
     130  size_t len = 0;
     131  while (*_array++) len++;
     132  return len;
     133}
    31134
    32135
     
    126229template <class C>
    127230template <typename CppType1>
    128 inline Symbol<C, ObjectTerm>::Symbol (CppType1 _arg1) :
     231inline Symbol<C, ObjectTerm>::Symbol (CppType1 const& _arg1) :
    129232  Term (reg.get_type())
    130233{
     
    137240template <class C>
    138241template <typename CppType1, typename CppType2>
    139 inline Symbol<C, ObjectTerm>::Symbol (CppType1 _arg1, CppType2 _arg2) :
     242inline Symbol<C, ObjectTerm>::Symbol (
     243    CppType1 const& _arg1, CppType2 const& _arg2
     244) :
    140245  Term (reg.get_type())
    141246{
     
    144249  w->ref_count = 1;
    145250  ptr_data2 = w;
     251}
     252
     253template <class C>
     254template <typename CppType1, typename CppType2, typename CppType3>
     255inline Symbol<C, ObjectTerm>::Symbol (
     256    CppType1 const& _arg1, CppType2 const& _arg2, CppType3 const& _arg3
     257) :
     258  Term (reg.get_type())
     259{
     260  Wrapper* w = static_cast<Wrapper*>(allocator->allocate(sizeof(Wrapper)));
     261  new (&(w->object)) C(_arg1, _arg2, _arg3);
     262  w->ref_count = 1;
     263  ptr_data2 = w;
     264}
     265
     266template <class C>
     267inline void Symbol<C, ObjectTerm>::replace_object ()
     268{
     269  C* p = get_obj_ptr();
     270  p->~C();
     271  new (p) C();
     272}
     273
     274template <class C>
     275template<typename CppType1>
     276inline void Symbol<C, ObjectTerm>::replace_object (CppType1 const& _arg1)
     277{
     278  C* p = get_obj_ptr();
     279  p->~C();
     280  new (p) C(_arg1);
     281}
     282
     283template <class C>
     284template<typename CppType1, typename CppType2>
     285inline void Symbol<C, ObjectTerm>::replace_object (
     286    CppType1 const& _arg1, CppType2 const& _arg2
     287)
     288{
     289  C* p = get_obj_ptr();
     290  p->~C();
     291  new (p) C(_arg1, _arg2);
     292}
     293
     294template <class C>
     295template<typename CppType1, typename CppType2, typename CppType3>
     296inline void Symbol<C, ObjectTerm>::replace_object (
     297    CppType1 const& _arg1, CppType2 const& _arg2, CppType3 const& _arg3
     298)
     299{
     300  C* p = get_obj_ptr();
     301  p->~C();
     302  new (p) C(_arg1, _arg2, _arg3);
     303}
     304
     305template <class C>
     306inline C const& Symbol<C, ObjectTerm>::get_object () const
     307{
     308  return static_cast<Wrapper*>(ptr_data2)->object;
    146309}
    147310
     
    250413
    251414template <class C>
     415inline NamedObject<C> const&
     416Symbol<NamedObject<C>, ObjectTerm>::get_object () const
     417{
     418  return static_cast<Wrapper*>(ptr_data2)->object;
     419}
     420
     421template <class C>
    252422inline NamedObject<C>* Symbol<NamedObject<C>, ObjectTerm>::get_obj_ptr () const
    253423{
  • to-imperative/trunk/runtime/rf_term.hh

    r1885 r1986  
    2121namespace rfrt
    2222{
     23
     24template <class C1, class C2>
     25class OR;
    2326
    2427//
     
    160163  template <class C>
    161164  inline C* cast_to () ;
    162 
     165  ///
     166  /// Get a reference to underlying object.  The object must have type C.
     167  template <class C>
     168  inline C const& get_object () const ;
     169  ///
     170  /// Get a pointer to underlying object for changing it.  The object must have
     171  /// type C and the term must be changable (i.e. to be reference term).
    163172  template <class C>
    164173  inline C* get_obj_ptr () const ;
    165 
     174  ///
     175  /// Check that underlying object has type C.
    166176  template <class C>
    167177  inline bool is_instance_of () const ;
    168 
     178  ///
     179  /// Check that underlying object satisfies the TypeCond::check() condition.
     180  template <class TypeCond>
     181  inline bool check_type () const ;
     182  ///
     183  /// Destruct underlying object, then construct a new one in its place.  The
     184  /// object must have type C and the term must be changable (i.e. to be
     185  /// reference term).  Old object is destroyed at first, so it can't be used
     186  /// in construction of a new one.
     187  template <class ObjClass>
     188  inline void replace_object () ;
     189
     190  template <class ObjClass, class CppType1>
     191  inline void replace_object (CppType1 const& _arg1) ;
     192
     193  template <class ObjClass, class CppType1, class CppType2>
     194  inline void replace_object (CppType1 const& _arg1, CppType2 const& _arg2) ;
     195 
     196  template <class ObjClass, class CppType1, class CppType2, class CppType3>
     197  inline void replace_object (
     198      CppType1 const& _arg1, CppType2 const& _arg2, CppType3 const& _arg3) ;
     199 
    169200  inline operator pxx::WString () const ;
    170201  ///
     
    178209  ) ;
    179210
     211  template <class C1, class C2>
     212  class Convert
     213  {
     214  public:
     215    static C2 convert (C1 const& _obj)
     216    {
     217      return _obj;
     218    }
     219  };
     220
     221  template <class C1, class C2>
     222  class ConvertObject
     223  {
     224  public:
     225    static bool convert (Term const& _t, C2* _p)
     226    {
     227      if (!_t.is_instance_of<C1>())
     228        return false;
     229      *_p = Convert<C1, C2>::convert(_t.get_object<C1>());
     230      return true;
     231    }
     232  };
     233
     234  template <class C11, class C12, class C2>
     235  class ConvertObject<OR<C11, C12>, C2>
     236  {
     237  public:
     238    static bool convert (Term const& _t, C2* _p)
     239    {
     240      if (ConvertObject<C11, C2>::convert(_t, _p))
     241        return true;
     242      return ConvertObject<C12, C2>::convert(_t, _p);
     243    }
     244  };
     245
     246  template <class C1, class C2, class Cond>
     247  class CheckConvert
     248  {
     249  public:
     250    static int convert (Term const& _t, C2* _p)
     251    {
     252      if (!_t.is_instance_of<C1>())
     253        return -1;
     254      C1 obj = _t.get_object<C1>();
     255      if (!Cond::check(obj))
     256        return 0;
     257      *_p = Convert<C1, C2>::convert(obj);
     258      return 1;
     259    }
     260  };
     261
     262  template <class C11, class C12, class C2, class Cond>
     263  class CheckConvert<OR<C11, C12>, C2, Cond>
     264  {
     265  public:
     266    static int convert (Term const& _t, C2* _p)
     267    {
     268      int c = CheckConvert<C11, C2, Cond>::convert(_t, _p);
     269      if (c == -1)
     270        return CheckConvert<C12, C2, Cond>::convert(_t, _p);
     271      return c;
     272    }
     273  };
     274
    180275#if defined(RF_STORE_CHUNK_ORDER_IN_TERM)
    181276  inline unsigned get_order () const
  • to-imperative/trunk/runtime/rf_term.ih

    r1778 r1986  
    182182
    183183template <class C>
     184inline C const& Term::get_object () const
     185{
     186  assert(is_instance_of<C>());
     187  return
     188    static_cast<Symbol<C, typename SymbolType<C>::Type> const*>(this)->
     189      get_object();
     190}
     191
     192template <class C>
    184193inline C* Term::get_obj_ptr () const
    185194{
     195  assert(is_instance_of<C>());
    186196  return
    187197    static_cast<Symbol<C, typename SymbolType<C>::Type> const*>(this)->
Note: See TracChangeset for help on using the changeset viewer.