Changeset 1315


Ignore:
Timestamp:
07/11/24 17:26:06 (3 months ago)
Author:
Maciej Komosinski
Message:

Offset of fields within an object: replaced subtraction of pointers/addresses with the official offsetof()

Location:
cpp/frams/param
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/param/param.cpp

    r1302 r1315  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2022  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    2121#define SAVE_SELECTED_NAMES
    2222#define WARN_MISSING_NAME
    23 
    24 char MakeCodeGuardHappy;
    2523
    2624ParamEntry empty_paramtab[] =
     
    559557        const char* fname = f->VgetPath();
    560558        if (fname != NULL)
    561                 {
     559        {
    562560                fileinfo = SString::sprintf(" while reading from '%s'", fname);
    563561                if (options.linenum)
    564562                        fileinfo += SString::sprintf(" (line %d)", *options.linenum);
    565                 }
     563        }
    566564        return fileinfo;
    567565}
     
    613611                        if (seen[i])
    614612                        {
    615                                 SString fileinfo = getFileMessageFor(f,options);
     613                                SString fileinfo = getFileMessageFor(f, options);
    616614                                logPrintf("ParamInterface", "load", LOG_WARN, "Multiple '%s.%s' properties found%s", getName(), id(i), fileinfo.c_str());
    617615                        }
     
    622620                                if (p0[p_len + 1] == '~')
    623621                                {
    624                                         if (p0[p_len+2])
     622                                        if (p0[p_len + 2])
    625623                                        {
    626                                                 SString fileinfo = getFileMessageFor(f,options);
    627                                                 logPrintf("ParamInterface", "load", LOG_WARN, "Ignored unexpected characters after '~': '%s' in '%s:'%s", p0+p_len+1, id(i), fileinfo.c_str());
     624                                                SString fileinfo = getFileMessageFor(f, options);
     625                                                logPrintf("ParamInterface", "load", LOG_WARN, "Ignored unexpected characters after '~': '%s' in '%s:'%s", p0 + p_len + 1, id(i), fileinfo.c_str());
    628626                                        }
    629                                
     627
    630628                                        SString s;
    631629                                        if (!readUntilTilde(f, s))
     
    939937        {
    940938        case 'd': t += "integer";
    941         {paInt a, b, c; int n = getMinMaxIntFromTypeDef(type, a, b, c); if ((n >= 2) && (b >= a)) t += SString::sprintf(" %d..%d", a, b); if (n >= 3) t += SString::sprintf(" (default %d)", c); }
     939        { paInt a, b, c; int n = getMinMaxIntFromTypeDef(type, a, b, c); if ((n >= 2) && (b >= a)) t += SString::sprintf(" %d..%d", a, b); if (n >= 3) t += SString::sprintf(" (default %d)", c); }
    942940        break;
    943941        case 'f': t += "float";
    944         {double a, b, c; int n = getMinMaxDoubleFromTypeDef(type, a, b, c); if ((n >= 2) && (b >= a)) t += SString::sprintf(" %g..%g", a, b); if (n >= 3) t += SString::sprintf(" (default %g)", c); }
     942        { double a, b, c; int n = getMinMaxDoubleFromTypeDef(type, a, b, c); if ((n >= 2) && (b >= a)) t += SString::sprintf(" %g..%g", a, b); if (n >= 3) t += SString::sprintf(" (default %g)", c); }
    945943        break;
    946944        case 's': t += "string";
    947         {int a, b; SString c; int n = getMinMaxStringFromTypeDef(type, a, b, c); if ((n >= 2) && (b > 0)) t += SString::sprintf(", max %d chars", b); if (n >= 3) t += SString::sprintf(" (default \"%s\")", c.c_str()); }
     945        { int a, b; SString c; int n = getMinMaxStringFromTypeDef(type, a, b, c); if ((n >= 2) && (b > 0)) t += SString::sprintf(", max %d chars", b); if (n >= 3) t += SString::sprintf(" (default \"%s\")", c.c_str()); }
    948946        break;
    949947        case 'x': t += "untyped value"; break;
  • cpp/frams/param/param.h

    r1184 r1315  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2022  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    216216// implementations:
    217217
    218 extern char MakeCodeGuardHappy;
    219 
    220218#define PROCOFFSET(_proc_) ( (void (*)(void*,ExtValue*,ExtValue*)) &(FIELDSTRUCT :: _proc_ ## _statrick))
    221219#define STATICPROCOFFSET(_proc_) ( (void (*)(void*,ExtValue*,ExtValue*)) &(FIELDSTRUCT :: _proc_))
     
    223221#define SETOFFSET(_proc_) ( (int (*)(void*,const ExtValue*)) &(FIELDSTRUCT :: _proc_ ## _statrick))
    224222
    225 #define FIELDOFFSET(_fld_) ((intptr_t)((char*)(&((FIELDSTRUCT*)&MakeCodeGuardHappy)->_fld_)-((char*)((FIELDSTRUCT*)&MakeCodeGuardHappy))))
     223#define FIELDOFFSET(_fld_) ((intptr_t)offsetof(FIELDSTRUCT, _fld_)) // https://stackoverflow.com/questions/177885/looking-for-something-similar-to-offsetof-for-non-pod-types , https://stackoverflow.com/questions/713963/why-does-this-implementation-of-offsetof-work
    226224
    227225#ifdef _DEBUG
     
    349347
    350348#ifdef CHECK_PARAMENTRY_COUNT
    351        
     349
    352350        template <int COUNT>
    353         Param(ParamEntry (&t)[COUNT],void* o = 0, const char*n = 0) :SimpleAbstractParam(o, n), tab(t)
     351        Param(ParamEntry(&t)[COUNT], void* o = 0, const char*n = 0) :SimpleAbstractParam(o, n), tab(t)
    354352        {
    355                 if (!n&&tab) myname = tab[0].name;
     353                if (!n && tab) myname = tab[0].name;
    356354                //printf("Param(ParamEntry t[%d]) %s\n",COUNT,myname?myname:"unknown name");
    357355                if (tab)
    358                         {
    359                         int in_array = COUNT-1-tab[0].group;
     356                {
     357                        int in_array = COUNT - 1 - tab[0].group;
    360358                        if (tab[0].flags != in_array)
    361                                 printf("\nCHECK_PARAMENTRY_COUNT: %d items in ParamEntry[] array, declared %d (%s)\n\n",in_array, tab[0].flags, myname?myname:"unknown name");
    362                         if (in_array>0 && tab[COUNT-1].id != NULL) //in_array>0 -> completely empty paramtab can't end with the usual zero row because that's also its first row
    363                                 printf("\nCHECK_PARAMENTRY_COUNT: last entry is not null (%s)\n\n",myname?myname:"unknown name");
    364                         }
     359                                printf("\nCHECK_PARAMENTRY_COUNT: %d items in ParamEntry[] array, declared %d (%s)\n\n", in_array, tab[0].flags, myname ? myname : "unknown name");
     360                        if (in_array > 0 && tab[COUNT - 1].id != NULL) //in_array>0 -> completely empty paramtab can't end with the usual zero row because that's also its first row
     361                                printf("\nCHECK_PARAMENTRY_COUNT: last entry is not null (%s)\n\n", myname ? myname : "unknown name");
     362                }
    365363        }
    366364
    367         template<typename T, typename std::enable_if_t<std::is_same<ParamEntry*,T>::value>* = nullptr > //SFINAE-fu because the normal ParamEntry* overload would be also called for ParamEntry[COUNT] argument
     365        template<typename T, typename std::enable_if_t<std::is_same<ParamEntry*, T>::value>* = nullptr > //SFINAE-fu because the normal ParamEntry* overload would be also called for ParamEntry[COUNT] argument
    368366        Param(T t, void* o = 0, const char*n = 0) :SimpleAbstractParam(o, n), tab(t)
    369367        {
    370368                //printf("Param(ParamEntry* t) %s\n",tab?tab[0].name:"");
    371                 if (!n&&tab) myname = tab[0].name;
     369                if (!n && tab) myname = tab[0].name;
    372370        }
    373371
     
    378376
    379377#else // CHECK_PARAMENTRY_COUNT
    380        
    381         Param(ParamEntry *t = 0, void* o = 0, const char*n = 0) :SimpleAbstractParam(o, n), tab(t)
     378
     379        Param(ParamEntry *t = 0, void* o = 0, const char*n = 0) : SimpleAbstractParam(o, n), tab(t)
    382380        {
    383                 if (!n&&tab) myname = tab[0].name;
     381                if (!n && tab) myname = tab[0].name;
    384382        }
    385        
     383
    386384#endif // CHECK_PARAMENTRY_COUNT
    387        
     385
    388386        Param(const Param& p) :SimpleAbstractParam(p.object, p.myname), tab(p.tab) {}
    389387        void operator=(const Param&p) { object = p.object; myname = p.myname; tab = p.tab; }
Note: See TracChangeset for help on using the changeset viewer.