Changeset 1184


Ignore:
Timestamp:
10/17/22 12:28:47 (18 months ago)
Author:
Maciej Komosinski
Message:

Introduced a class to quickly copy field values between two compatible Param's, and added a constructor that checks if the paramtab structure is correct

Location:
cpp/frams/param
Files:
2 edited

Legend:

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

    r1155 r1184  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2022  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    4949        return (const char*)memchr((const void*)t, ch, limit - t);
    5050}
     51
     52
     53Param2ParamCopy::Param2ParamCopy(ParamInterface& schema, const std::initializer_list<const char*> names)
     54{
     55        for (const char* n : names)
     56        {
     57                int i = schema.findId(n);
     58                if (i >= 0)
     59                        fields.push_back(i);
     60                else
     61                        logPrintf("Param2ParamCopy", "findId", LOG_CRITICAL, "Can't find '%s.%s'", schema.getName(), n);
     62        }
     63}
     64
     65void Param2ParamCopy::operator()(const ParamInterface& from, ParamInterface& to)
     66{
     67        ExtValue tmp;
     68        for (int i : fields)
     69        {
     70                ((ParamInterface&)from).get(i, tmp);
     71                to.set(i, tmp);
     72        }
     73}
     74
    5175
    5276void ParamInterface::copyFrom(ParamInterface *src)
  • cpp/frams/param/param.h

    r1155 r1184  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2017  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2022  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    88#include <stdio.h>
    99#include <stdint.h>
     10#include <vector>
    1011#include <frams/util/sstring.h>
    1112#include <frams/util/sstringutils.h>
     
    199200};
    200201
     202
     203
     204/** Copy fields between two compatible params. A Param2ParamCopy object can be static so the field names are only looked up once. */
     205class Param2ParamCopy
     206{
     207        std::vector<int> fields;
     208public:
     209        Param2ParamCopy(ParamInterface& schema, const std::initializer_list<const char*> names);
     210        void operator()(const ParamInterface& from, ParamInterface& to);
     211};
     212
     213
     214
     215
    201216// implementations:
    202217
     
    317332};
    318333
     334#ifdef _DEBUG
     335#define CHECK_PARAMENTRY_COUNT // This could be the only version, because it is not significantly more costly than the other one. Now we have a _DEBUG (checking, slightly slower) version and a non-_DEBUG (i.e. release, non-checking, slightly faster) version. Since both versions are completely different, the non-_DEBUG (release) version is not verified at all during thorough _DEBUG testing.
     336#endif
     337
    319338class Param : public SimpleAbstractParam
    320339{
     
    329348                */
    330349
     350#ifdef CHECK_PARAMENTRY_COUNT
     351       
     352        template <int COUNT>
     353        Param(ParamEntry (&t)[COUNT],void* o = 0, const char*n = 0) :SimpleAbstractParam(o, n), tab(t)
     354        {
     355                if (!n&&tab) myname = tab[0].name;
     356                //printf("Param(ParamEntry t[%d]) %s\n",COUNT,myname?myname:"unknown name");
     357                if (tab)
     358                        {
     359                        int in_array = COUNT-1-tab[0].group;
     360                        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                        }
     365        }
     366
     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
     368        Param(T t, void* o = 0, const char*n = 0) :SimpleAbstractParam(o, n), tab(t)
     369        {
     370                //printf("Param(ParamEntry* t) %s\n",tab?tab[0].name:"");
     371                if (!n&&tab) myname = tab[0].name;
     372        }
     373
     374        Param() :SimpleAbstractParam(NULL, NULL), tab(NULL)
     375        {
     376                //printf("Param(NULL)\n");
     377        }
     378
     379#else // CHECK_PARAMENTRY_COUNT
     380       
    331381        Param(ParamEntry *t = 0, void* o = 0, const char*n = 0) :SimpleAbstractParam(o, n), tab(t)
    332382        {
    333383                if (!n&&tab) myname = tab[0].name;
    334384        }
    335 
     385       
     386#endif // CHECK_PARAMENTRY_COUNT
     387       
    336388        Param(const Param& p) :SimpleAbstractParam(p.object, p.myname), tab(p.tab) {}
    337389        void operator=(const Param&p) { object = p.object; myname = p.myname; tab = p.tab; }
Note: See TracChangeset for help on using the changeset viewer.