Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

SqPlus DECLARE_INSTANCE_TYPE_NOCOPY fix

Last post 10-03-2008, 5:19 AM by ats. 6 replies.
Sort Posts: Previous Next
  •  10-01-2008, 6:41 AM 2805

    SqPlus DECLARE_INSTANCE_TYPE_NOCOPY fix

    I noticed the DECLARE_INSTANCE_TYPE_NOCOPY is not working, so I did some mod to get it working I added

    #define DECLARE_INSTANCE_TYPE_NAME_NOCOPY_(TYPE,NAME) namespace SqPlus { \
    DECLARE_INSTANCE_TYPE_BASE_(TYPE,NAME) \
    template<> inline void Push(HSQUIRRELVM v,TYPE * value) { \
    if (!value) sq_pushnull(v); \
    else if (!CreateNativeClassInstance(v,GetTypeName(*value),value,0)) \
    throw SquirrelError( _SC( "Push(): could not create INSTANCE (check registration name)")); } \
    template<> inline void Push(HSQUIRRELVM v,TYPE & value) { throw SquirrelError( _SC( "Push(): could not create INSTANCE copy (check registration name)")); } \
    } // nameSpace SqPlus

    and changed them to call it

    #define DECLARE_INSTANCE_TYPE_NOCOPY(TYPE) \
    DECLARE_INSTANCE_TYPE_NAME_NOCOPY_(TYPE,TYPE) \
    DECLARE_NONCOPY_TYPE(TYPE)

    #define DECLARE_INSTANCE_TYPE_NOCOPY_NAME(TYPE,NAME) \
    DECLARE_INSTANCE_TYPE_NAME_NOCOPY_(TYPE,NAME) \
    DECLARE_NONCOPY_TYPE(TYPE)
  •  10-01-2008, 4:23 PM 2807 in reply to 2805

    Re: SqPlus DECLARE_INSTANCE_TYPE_NOCOPY fix

    Do you have some context for this (a declaration triggering it and a
    compiler/run-time error).

    If I can understand the issue it's easier to commit a change to SqPlus SVN.

    Regards
    // Arst.
  •  10-02-2008, 4:23 AM 2809 in reply to 2807

    Re: SqPlus DECLARE_INSTANCE_TYPE_NOCOPY fix

    I have some classes with private copy ctor and assignment op, so if I define SQPLUS_DISABLE_COPY_INSTANCES, this works across all DECLARE_INSTANCE_TYPE. I need to mix both, so that can be created, some can't. So I noticed there is this macro DECLARE_INSTANCE_TYPE_NOCOPY but it couldn't compile so my changes fix it.
  •  10-02-2008, 7:04 AM 2811 in reply to 2809

    Re: SqPlus DECLARE_INSTANCE_TYPE_NOCOPY fix

    OK, I see what went wrong.

    Next time I spend time on SqPlus SVN I'll include the change.

    Thanks
    // ATS.
  •  10-02-2008, 10:42 PM 2814 in reply to 2811

    Re: SqPlus DECLARE_INSTANCE_TYPE_NOCOPY fix

    we got the same problem as mentioned in this thread.

    but we fix it according to IsCopyable traits

    here is our modifications...

    template<typename T, bool Copyable = IsCopyable<T>::value >
    struct CreateCopyInstanceHelper
    {
        inline static BOOL Create(HSQUIRRELVM v, const SQChar * className,const T & classToCopy)
        {
            if (!CreateConstructNativeClassInstance(v,className)) {
                return FALSE;
            } // if
            SQUserPointer up=0;
            sq_getinstanceup(v,-1,&up,ClassType<T>::type());
            if (!up) return FALSE;
            T * newClass = (T *)up;
            *newClass = classToCopy; // <TODO> Optimized version that uses the copy constructor.
            return TRUE;
        }
    };

    template<typename T>
    struct CreateCopyInstanceHelper<T, false>
    {
        inline static BOOL Create(HSQUIRRELVM v, const SQChar * className,const T & classToCopy)
        {
            return FALSE;
        }
    };

    template<typename T>
    inline BOOL CreateCopyInstance(HSQUIRRELVM v, const SQChar * className,const T & classToCopy) {
    #ifndef SQPLUS_DISABLE_COPY_INSTANCES
      return CreateCopyInstanceHelper<T>::Create(v, className, classToCopy);
    #else
      return FALSE;
    #endif 
    } // CreateCopyInstance


    Regards,

    KZ

  •  10-03-2008, 4:06 AM 2815 in reply to 2814

    Re: SqPlus DECLARE_INSTANCE_TYPE_NOCOPY fix

    I see. This fix could work too.
  •  10-03-2008, 5:19 AM 2816 in reply to 2814

    Re: SqPlus DECLARE_INSTANCE_TYPE_NOCOPY fix

    Yes, the last approach is good. We'll go for that.

    Regards
    //ATS.

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems