itom
Loading...
Searching...
No Matches
param.h
1/* ********************************************************************
2 itom software
3 URL: http://www.uni-stuttgart.de/ito
4 Copyright (C) 2020, Institut für Technische Optik (ITO),
5 Universität Stuttgart, Germany
6
7 This file is part of itom and its software development toolkit (SDK).
8
9 itom is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Library General Public Licence as published by
11 the Free Software Foundation; either version 2 of the Licence, or (at
12 your option) any later version.
13
14 In addition, as a special exception, the Institut für Technische
15 Optik (ITO) gives you certain additional rights.
16 These rights are described in the ITO LGPL Exception version 1.0,
17 which can be found in the file LGPL_EXCEPTION.txt in this package.
18
19 itom is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
22 General Public Licence for more details.
23
24 You should have received a copy of the GNU Library General Public License
25 along with itom. If not, see <http://www.gnu.org/licenses/>.
26*********************************************************************** */
27
28#pragma once
29
30/* includes */
31
32#include "byteArray.h"
33#include "commonGlobal.h"
34#include "paramMeta.h"
35#include "retVal.h"
36#include "typeDefs.h"
37
38#include <algorithm>
39#include <atomic>
40#include <limits>
41#include <stdarg.h>
42#include <stdexcept>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46
47/* definition and macros */
48/* global variables (avoid) */
49/* content */
50
51namespace ito {
52class Param;
53template <typename _Tp> struct ItomParamHelper;
54
55const uint32 paramFlagMask = 0xFFFF0000;
57const uint32 paramTypeMask = 0x0000FFFF;
59
63{
64 float64 real;
65 float64 imag;
66};
67
71 int8 i8Val;
72
74 int32 i32Val;
75
77 float64 f64Val;
78
81
83 void* ptrVal;
84};
85
86//-------------------------------------------------------------------------------------
88/* \brief Base class for parameter container.
89
90 This base class only holds the name of the parameter, the type of the internal value,
91 the value itself and
92 some optional flags.
93 */
94class ITOMCOMMON_EXPORT ParamBase
95{
96public:
98 /* For compatibility reasons with older versions of itom, values in this enumeration
99 must only set bits in the range 17-32 of an uint32 value, since they can be
100 used together with values of the Type enum below, using bits 1-16.
101 Internally, the flags are stored as uint16 object, where these enumeration
102 values are shifted by 16bits.
103 */
104 enum Flag
105 {
106 /* If this bit is set, this parameter should be automatically
107 stored if for instance a plugin is closed and if the plugin
108 with the same identifier is opened again, the parameter
109 value will be tried to be reconstructed from the stored value. */
110 NoAutosave = 0x010000,
111
112 /* Flag to define this parameter to be readonly. It cannot for instance not be changed
113 from Python. Internally, the setVal method does not check for Readonly.
114 This has to be programmed manually. Plugins can also set or unset this flag
115 during the runtime. */
116 Readonly = 0x020000,
117
118 /* Flag to mark this parameter as input value only.
119 If a plugin defines this flag without the Out-flag, it pretends
120 to only consume the given value, but the consumer will not change the value at any time.
121 If the Out-flag is set, too, the consumer (e.g. plugin) will read and write the
122 value, such that for instance a given dataObject will have another content after having
123 called a method of a plugin. Return parameters of a method must never have the In-flag set.
124 */
125 In = 0x040000,
126
127 /* Flag to mark this parameter, that the consumer will create or change
128 the value of this parameter.
129 This can be set together with In (or alone). All return parameters of an method in
130 an algorithm plugin, must have this flag set. For other mandatory or optional
131 input parameter, this flag can only be set together with In and means then,
132 that the content of the given parameter will be changed by the called method. */
133 Out = 0x080000,
134
135 /* Flag to mark a parameter to be temporarily not available.
136 Plugins can also set or unset this flag
137 during the runtime. */
138 NotAvailable = 0x100000,
139 };
140
141 enum Type
142 {
143 /* Helper-bit for all types that only contain a pointer to the real value and
144 not the value itself. For these types, the caller need to keep the pointed object
145 until both caller and called method do not use it any more! */
146 Pointer = 0x0001,
147
149 Char = 0x0002,
150
152 Int = 0x0004,
153
155 Double = 0x0008,
156
158 Complex = 0x0400,
159
161 DObjPtr = 0x0010 | Pointer,
162
164 String = 0x0020 | Pointer,
165
167 HWRef = 0x0040 | Pointer,
168
170 CharArray = Char | Pointer,
171
173 IntArray = Int | Pointer,
174
176 DoubleArray = Double | Pointer,
177
179 ComplexArray = Complex | Pointer,
180
182 PointCloudPtr = 0x0080 | Pointer,
183
185 PointPtr = 0x0100 | Pointer,
186
188 PolygonMeshPtr = 0x0200 | Pointer,
189
191 StringList = 0x0800 | Pointer
192 };
193
194
195 //--------------------------------------------------------------------------------------------
196 // CONSTRUCTORS, COPY-CONSTRUCTOR, DESTRUCTOR
197 //--------------------------------------------------------------------------------------------
199 ParamBase();
200
201 // type-less ParamBase with name only
202 ParamBase(const ByteArray& name);
203
204 // constructor with type and name
205 ParamBase(const ByteArray& name, const uint32 typeAndFlags);
206
207 // constructor with name and type and char val
208 ParamBase(const ByteArray& name, const uint32 typeAndFlags, const char* val);
209
210 // constructor with name and type and float64 val
211 ParamBase(const ByteArray& name, const uint32 typeAndFlags, const float64 val);
212
213 // constructor with name and type and int32 val
214 ParamBase(const ByteArray& name, const uint32 typeAndFlags, const int32 val);
215
216 // constructor with name and type and complex128 val
217 ParamBase(const ByteArray& name, const uint32 typeAndFlags, const complex128 val);
218
219 // array constructor with name and type, size and array
220 ParamBase(
221 const ByteArray& name, const uint32 typeAndFlags, const uint32 size, const char* values);
222
223 // array constructor with name and type, size and array
224 ParamBase(
225 const ByteArray& name, const uint32 typeAndFlags, const uint32 size, const int32* values);
226
227 // array constructor with name and type, size and array
228 ParamBase(
229 const ByteArray& name, const uint32 typeAndFlags, const uint32 size, const float64* values);
230
231 // array constructor with name and type, size and array
232 ParamBase(
233 const ByteArray& name,
234 const uint32 typeAndFlags,
235 const uint32 size,
236 const complex128* values);
237
238 // array constructor with name and type, size and string list
239 ParamBase(
240 const ByteArray& name,
241 const uint32 typeAndFlags,
242 const uint32 size,
243 const ByteArray* values);
244
246 virtual ~ParamBase();
247
249 ParamBase(const ParamBase& other);
250
252 ParamBase(ParamBase&& other) noexcept;
253
254 //--------------------------------------------------------------------------------------------
255 // ASSIGNMENT AND OPERATORS
256 //--------------------------------------------------------------------------------------------
257
259 const ParamBase operator[](const int index) const;
260
263 ParamBase& operator=(const ParamBase& rhs);
264
266 inline ParamBase& operator=(ParamBase&& other) noexcept
267 {
268 if (this != &other)
269 {
270 std::swap(d, other.d);
271 }
272
273 return *this;
274 }
275
277 ito::RetVal copyValueFrom(const ito::ParamBase* rhs);
278
279 //--------------------------------------------------------------------------------------------
280 // COMPARISON OPERATORS (two values are equal if both their type and the content is equal)
281 //--------------------------------------------------------------------------------------------
282 bool operator==(const ParamBase& rhs) const;
283
284 inline bool operator!=(const ParamBase& rhs) const
285 {
286 return !(*this == rhs);
287 }
288
289 //--------------------------------------------------------------------------------------------
290 // SET/GET FURTHER PROPERTIES
291 //--------------------------------------------------------------------------------------------
292
294 bool isNumeric(void) const;
295
297 bool isNumericArray(void) const;
298
301 bool isValid(void) const;
302
304 uint16 getType() const;
305
307 uint32 getFlags() const;
308
310 void setFlags(const uint32 flags);
311
313 const char* getName(void) const;
314
317 ito::ByteArray getNameWithIndexSuffix(int index) const;
318
321 bool getAutosave() const;
322
325 void setAutosave(const bool autosave);
326
328 /* The return value of this method depends on the type of parameter:
329
330 For scalar parameters, like Char, Int, Double, Complex, 1 is always returned.
331
332 For number array parameters (CharArray, IntArray, DoubleArray, ComplexArray, the
333 number of values in the array is returned. Even if the array is a nullptr,
334 0 is returned. This changed from itom 5.0 on. Before -1 was returned
335 if the array is a nullptr (which is equal to a length of 0).
336
337 StringList parameter behave like the other scalar parameters.
338
339 For String parameters, the length of the string is returned, but
340 -1 is returned if the internal string is nullptr.
341
342 An invalid parameter will also return -1 always.
343 */
344 int getLen() const;
345
352 template <typename _Tp> inline ito::RetVal setVal(_Tp val)
353 {
354 return ItomParamHelper<_Tp>::setVal(this, val, 0);
355 }
356
364 template <typename _Tp> inline ito::RetVal setVal(_Tp val, int len)
365 {
366 return ItomParamHelper<_Tp>::setVal(this, val, len);
367 }
368
375 template <typename _Tp> inline _Tp getVal() const
376 {
377 int len = 0;
378 return ItomParamHelper<_Tp>::getVal(this, len);
379 }
380
388 template <typename _Tp> inline _Tp getVal(int& len) const
389 {
390 return ItomParamHelper<_Tp>::getVal(this, len);
391 }
392
393private:
395 struct Data
396 {
397 Data(const ByteArray& name_ = "") : ref(0), flags(0), type(0), len(0), name(name_)
398 {
399 memset(&data, 0, sizeof(ParamBaseData));
400 }
401
403 std::atomic_int ref;
404
407
409 uint32 len;
410
412 uint16 flags;
413
415 uint16 type;
416
418 ByteArray name;
419 };
420
421
423 mutable Data* d;
424
427 void clearData(Data* data);
428
432 /*
433 \param allocNewArray if false, now new arrays, strings or string list
434 memory is allocated and the len is set to 0. This should only be
435 set to false, if it is assured that the memory is allocated by
436 another part of the code.
437 */
438 void detach(bool allocNewArray = true);
439
442 inline void decRefAndFree(Data* x)
443 {
444 if (x && (!(x->ref--)))
445 {
446 clearData(x);
447 delete (x);
448 x = nullptr;
449 }
450 }
451
453 inline void incRef(Data* x)
454 {
455 if (x)
456 {
457 (x->ref)++;
458 }
459 }
460
462 void setDefaultAutosaveFlag();
463
464 void checkAndCorrectInOutFlag();
465
466 template <typename _Tp> friend struct ItomParamHelper;
467};
468
469//----------------------------------------------------------------------------------------------------------------------------------
476class ITOMCOMMON_EXPORT Param : public ParamBase
477{
478public:
479 //--------------------------------------------------------------------------------------------
480 // CONSTRUCTORS, COPY-CONSTRUCTOR, DESTRUCTOR
481 //--------------------------------------------------------------------------------------------
483 /*
484 This parameter has no documentation string and no meta information.
485 The type is 0 (invalid).
486 The name is empty.
487 */
488 Param();
489
491 /*
492 This parameter has no documentation string and no meta information.
493 The type is 0 (invalid).
494
495 \param name is the name of the parameter
496 */
497 Param(const ByteArray& name);
498
500 /*
501 This parameter has no documentation string and no meta information.
502
503 \param name is the name of the parameter
504 \type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
505 e.g. ito::ParamBase::Char | ito::ParamBase::In for a read-only input parameter
506 */
507 Param(const ByteArray& name, const uint32 typeAndFlags);
508
510 /*
511 This parameter has no meta information. They can be added using setMetaInfo.
512
513 \param name is the name of the parameter
514 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
515 e.g. ito::ParamBase::String | ito::ParamBase::In for a read-only input parameter
516 \param val is the default string (const char*) of this parameter
517 \param info can be a documentation string for this parameter, an empty string or nullptr
518 */
519 Param(const ByteArray& name, const uint32 typeAndFlags, const char* val, const char* info);
520
522 /*
523 This parameter will automatically get meta information of type ito::CharMeta with a minimum and
524 maximum value.
525
526 \param name is the name of the parameter
527 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
528 e.g. ito::ParamBase::Char | ito::ParamBase::In for a read-only input parameter
529 \param minVal is the minimum allowed value (added to meta information)
530 \param maxVal is the maximum allowed value (added to meta information)
531 \param val is the default int8 value
532 \param info can be a documentation string for this parameter, an empty string or nullptr
533 */
534 Param(
535 const ByteArray& name,
536 const uint32 typeAndFlags,
537 const char minVal,
538 const char maxVal,
539 const char val,
540 const char* info);
541
543 /*
544 This parameter will automatically get meta information of type ito::IntMeta with a minimum and
545 maximum value.
546
547 \param name is the name of the parameter
548 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
549 e.g. ito::ParamBase::Int | ito::ParamBase::In for a read-only input parameter
550 \param minVal is the minimum allowed value (added to meta information)
551 \param maxVal is the maximum allowed value (added to meta information)
552 \param val is the default int32 value
553 \param info can be a documentation string for this parameter, an empty string or nullptr
554 */
555 Param(
556 const ByteArray& name,
557 const uint32 typeAndFlags,
558 const int32 minVal,
559 const int32 maxVal,
560 const int32 val,
561 const char* info);
562
564 /*
565 This parameter will automatically get meta information of type ito::DoubleMeta with a minimum
566 and maximum value.
567
568 \param name is the name of the parameter
569 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
570 e.g. ito::ParamBase::Double | ito::ParamBase::In for a read-only input parameter
571 \param minVal is the minimum allowed value (added to meta information)
572 \param maxVal is the maximum allowed value (added to meta information)
573 \param val is the default float64 value
574 \param info can be a documentation string for this parameter, an empty string or nullptr
575 */
576 Param(
577 const ByteArray& name,
578 const uint32 typeAndFlags,
579 const float64 minVal,
580 const float64 maxVal,
581 const float64 val,
582 const char* info);
583
585 /*
586 This parameter has no meta information. They can be added using setMetaInfo.
587
588 \param name is the name of the parameter
589 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
590 e.g. ito::ParamBase::CharArray | ito::ParamBase::In for a read-only input parameter
591 \param size is the size of the given default values array
592 \param values is the default int8 array value
593 \param info can be a documentation string for this parameter, an empty string or nullptr
594 */
595 Param(
596 const ByteArray& name,
597 const uint32 typeAndFlags,
598 const unsigned int size,
599 const char* values,
600 const char* info); // array constructor with name and type, size and array
601
603 /*
604 This parameter has no meta information. They can be added using setMetaInfo.
605
606 \param name is the name of the parameter
607 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
608 e.g. ito::ParamBase::IntArray | ito::ParamBase::In for a read-only input parameter
609 \param size is the size of the given default values array
610 \param values is the default int32 array value
611 \param info can be a documentation string for this parameter, an empty string or nullptr
612 */
613 Param(
614 const ByteArray& name,
615 const uint32 typeAndFlags,
616 const unsigned int size,
617 const int32* values,
618 const char* info);
619
621 /*
622 This parameter has no meta information. They can be added using setMetaInfo.
623
624 \param name is the name of the parameter
625 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
626 e.g. ito::ParamBase::DoubleArray | ito::ParamBase::In for a read-only input parameter
627 \param size is the size of the given default values array
628 \param values is the default float64 array value
629 \param info can be a documentation string for this parameter, an empty string or nullptr
630 */
631 Param(
632 const ByteArray& name,
633 const uint32 typeAndFlags,
634 const unsigned int size,
635 const float64* values,
636 const char* info);
637
639 /*
640 This parameter has no meta information.
641
642 \param name is the name of the parameter
643 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
644 e.g. ito::ParamBase::ComplexArray | ito::ParamBase::In for a read-only input parameter
645 \param size is the size of the given default values array
646 \param values is the default complex array value
647 \param info can be a documentation string for this parameter, an empty string or nullptr
648 */
649 Param(
650 const ByteArray& name,
651 const uint32 typeAndFlags,
652 const unsigned int size,
653 const complex128* values,
654 const char* info);
655
657 /*
658 This parameter has no meta information.
659
660 \param name is the name of the parameter
661 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
662 e.g. ito::ParamBase::ComplexArray | ito::ParamBase::In for a read-only input parameter
663 \param size is the size of the given default values array
664 \param values is the default ByteArray list
665 \param info can be a documentation string for this parameter, an empty string or nullptr
666 */
667 Param(
668 const ByteArray& name,
669 const uint32 typeAndFlags,
670 const unsigned int size,
671 const ByteArray* values,
672 const char* info);
673
675 /*
676 \param name is the name of the parameter
677 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
678 e.g. ito::ParamBase::Char | ito::ParamBase::In for a read-only input parameter
679 \param val is the default value
680 \param meta might be nullptr, if no meta information should be passed to this parameter.
681 If a pointer is given, it has to be an object of ito::CharMeta.
682 The ownership is taken by this parameter!
683 \param info can be a documentation string for this parameter, an empty string or nullptr
684 */
685 Param(
686 const ByteArray& name,
687 const uint32 typeAndFlags,
688 const char val,
689 ParamMeta* meta,
690 const char* info);
691
693 /*
694 \param name is the name of the parameter
695 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
696 e.g. ito::ParamBase::Int | ito::ParamBase::In for a read-only input parameter
697 \param val is the default value
698 \param meta might be nullptr, if no meta information should be passed to this parameter.
699 If a pointer is given, it has to be an object of ito::IntMeta.
700 The ownership is taken by this parameter!
701 \param info can be a documentation string for this parameter, an empty string or nullptr
702 */
703 Param(
704 const ByteArray& name,
705 const uint32 typeAndFlags,
706 const int32 val,
707 ParamMeta* meta,
708 const char* info);
709
711 /*
712 \param name is the name of the parameter
713 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
714 e.g. ito::ParamBase::Double | ito::ParamBase::In for a read-only input parameter
715 \param val is the default value
716 \param meta might be nullptr, if no meta information should be passed to this parameter.
717 If a pointer is given, it has to be an object of ito::DoubleMeta.
718 The ownership is taken by this parameter!
719 \param info can be a documentation string for this parameter, an empty string or nullptr
720 */
721 Param(
722 const ByteArray& name,
723 const uint32 typeAndFlags,
724 const float64 val,
725 ParamMeta* meta,
726 const char* info);
727
729 /*
730 \param name is the name of the parameter
731 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
732 e.g. ito::ParamBase::Complex | ito::ParamBase::In for a read-only input parameter
733 \param val is the default value
734 \param meta might be nullptr, if no meta information should be passed to this parameter.
735 Currently, there is no meta information class for complex value parameters!
736 \param info can be a documentation string for this parameter, an empty string or nullptr
737 */
738 Param(
739 const ByteArray& name,
740 const uint32 typeAndFlags,
741 const complex128 val,
742 ParamMeta* meta,
743 const char* info);
744
746 /*
747 \param name is the name of the parameter
748 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
749 e.g. ito::ParamBase::CharArray | ito::ParamBase::In for a read-only input parameter
750 \param size is the length of the default array, passed to values
751 \param values is the pointer to the default array of values
752 \param meta might be nullptr, if no meta information should be passed to this parameter.
753 If a pointer is given, it has to be an object of ito::CharArrayMeta.
754 The ownership is taken by this parameter!
755 \param info can be a documentation string for this parameter, an empty string or nullptr
756 */
757 Param(
758 const ByteArray& name,
759 const uint32 typeAndFlags,
760 const unsigned int size,
761 const char* values,
762 ParamMeta* meta,
763 const char* info);
764
766 /*
767 \param name is the name of the parameter
768 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
769 e.g. ito::ParamBase::IntArray | ito::ParamBase::In for a read-only input parameter
770 \param size is the length of the default array, passed to values
771 \param values is the pointer to the default array of values
772 \param meta might be nullptr, if no meta information should be passed to this parameter.
773 If a pointer is given, it has to be an object of ito::IntArrayMeta, ito::IntervalMeta,
774 ito::RangeMeta or ito::RectMeta.
775 The ownership is taken by this parameter!
776 \param info can be a documentation string for this parameter, an empty string or nullptr
777 */
778 Param(
779 const ByteArray& name,
780 const uint32 typeAndFlags,
781 const unsigned int size,
782 const int32* values,
783 ParamMeta* meta,
784 const char* info);
785
787 /*
788 \param name is the name of the parameter
789 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
790 e.g. ito::ParamBase::DoubleArray | ito::ParamBase::In for a read-only input parameter
791 \param size is the length of the default array, passed to values
792 \param values is the pointer to the default array of values
793 \param meta might be nullptr, if no meta information should be passed to this parameter.
794 If a pointer is given, it has to be an object of ito::DoubleArrayMeta or
795 ito::DoubleIntervalMeta. The ownership is taken by this parameter! \param info can be a
796 documentation string for this parameter, an empty string or nullptr
797 */
798 Param(
799 const ByteArray& name,
800 const uint32 typeAndFlags,
801 const unsigned int size,
802 const float64* values,
803 ParamMeta* meta,
804 const char* info);
805
807 /*
808 \param name is the name of the parameter
809 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
810 e.g. ito::ParamBase::ComplexArray | ito::ParamBase::In for a read-only input parameter
811 \param size is the length of the default array, passed to values
812 \param values is the pointer to the default array of values
813 \param meta might be nullptr, if no meta information should be passed to this parameter.
814 If a pointer is given, the ownership is taken by this parameter!
815 Currently, there is no meta information class available for complex array parameters!
816 \param info can be a documentation string for this parameter, an empty string or nullptr
817 */
818 Param(
819 const ByteArray& name,
820 const uint32 typeAndFlags,
821 const unsigned int size,
822 const complex128* values,
823 ParamMeta* meta,
824 const char* info);
825
827 /*
828 \param name is the name of the parameter
829 \param type is a flag mask, that might consists of combinations of ito::ParamBase::Type,
830 e.g. ito::ParamBase::ComplexArray | ito::ParamBase::In for a read-only input parameter
831 \param size is the length of the default array, passed to values
832 \param values is the pointer to the default string list values
833 \param meta might be nullptr, if no meta information should be passed to this parameter.
834 If a pointer is given, the ownership is taken by this parameter!
835 Currently, there is no meta information class available for complex array parameters!
836 \param info can be a documentation string for this parameter, an empty string or nullptr
837 */
838 Param(
839 const ByteArray& name,
840 const uint32 typeAndFlags,
841 const unsigned int size,
842 const ByteArray* values,
843 ParamMeta* meta,
844 const char* info);
845
847 ~Param();
848
850 Param(const Param& copyConstr);
851
853 Param(Param&& rvalue);
854
855 //--------------------------------------------------------------------------------------------
856 // ASSIGNMENT AND OPERATORS
857 //--------------------------------------------------------------------------------------------
858
860 const Param operator[](const int index) const;
861
864 Param& operator=(const Param& rhs);
865
867 Param& operator=(Param&& rvalue) noexcept;
868
871 ito::RetVal copyValueFrom(const ParamBase* rhs);
872
873private:
876 {
877 MetaShared(ParamMeta* m) : meta(m), ref(0)
878 {
879 }
880
881 ParamMeta* meta;
882 std::atomic_int ref;
883 };
884
887
889 ByteArray m_info;
890
891 /* call this method, if the internal meta object
892 is likely to be changed. If so, a deep copy of the
893 internal meta object will be done (if it is currently shared),
894 such that other Param objects are not affected by the possible meta
895 change. */
896 void detachMeta();
897
898 inline void incRefMeta(MetaShared* ms) const
899 {
900 if (ms)
901 {
902 ms->ref++;
903 }
904 }
905
906 inline void decRefMeta(MetaShared* ms) const
907 {
908 if (ms && !(ms->ref--))
909 {
910 delete ms->meta;
911 delete ms;
912 ms = nullptr;
913 }
914 }
915
916public:
917 //--------------------------------------------------------------------------------------------
918 // SET/GET FURTHER PROPERTIES
919 //--------------------------------------------------------------------------------------------
921 inline const char* getInfo() const
922 {
923 return m_info.data();
924 }
925
928 inline void setInfo(const char* info)
929 {
930 m_info = info;
931 }
932
934 inline void setInfo(const ByteArray& info)
935 {
936 m_info = info;
937 }
938
941 inline const ParamMeta* getMeta() const
942 {
943 return m_pMetaShared ? m_pMetaShared->meta : nullptr;
944 }
945
948 inline const ParamMeta* getConstMeta() const
949 {
950 return m_pMetaShared ? m_pMetaShared->meta : nullptr;
951 }
952
955 inline ParamMeta* getMeta(void)
956 {
957 if (m_pMetaShared && m_pMetaShared->ref > 0)
958 {
959 // decouple the internal meta object, since it
960 // could be changed by the caller of this method.
961 detachMeta();
962 }
963
964 return m_pMetaShared ? m_pMetaShared->meta : nullptr;
965 }
966
969 /*
970 Example: intParam.getMetaT<ito::IntMeta>();
971
972 Usually, it is more explicit to call getConstMetaT instead of this method.
973 */
974 template <typename _Tp> inline const _Tp* getMetaT(void) const
975 {
976 if (m_pMetaShared)
977 {
978 return static_cast<const _Tp*>(m_pMetaShared->meta);
979 }
980
981 return nullptr;
982 }
983
986 /*
987 Example: intParam.getMetaT<ito::IntMeta>();
988 */
989 template <typename _Tp> inline const _Tp* getConstMetaT(void) const
990 {
991 if (m_pMetaShared)
992 {
993 return static_cast<const _Tp*>(m_pMetaShared->meta);
994 }
995
996 return nullptr;
997 }
998
1001 /*
1002 Example: intParam.getMetaT<ito::IntMeta>();
1003 */
1004 template <typename _Tp> inline _Tp* getMetaT(void)
1005 {
1006 if (m_pMetaShared)
1007 {
1008 if (m_pMetaShared->ref > 0)
1009 {
1010 // decouple the internal meta object, since it
1011 // could be changed by the caller of this method.
1012 detachMeta();
1013 }
1014
1015 return static_cast<_Tp*>(m_pMetaShared->meta);
1016 }
1017
1018 return nullptr;
1019 }
1020
1022
1029 void setMeta(ParamMeta* meta, bool takeOwnership = false);
1030
1032 /* The returned value is only valid, if a meta has been set and if it
1033 contains a minimum value. Else -Inf is returned. */
1034 float64 getMin() const;
1035
1037 /* The returned value is only valid, if a meta has been set and if it
1038 contains a maximum value. Else +Inf is returned. */
1039 float64 getMax() const;
1040};
1041
1042//-------------------------------------------------------------------------------------
1044/* template helper class (with template specializations) for getting or setting
1045 the value of the parameter. These classes must be defined in the header file.
1046 This class is a friend of ito::ParamBase.
1047 */
1048template <typename _Tp> struct ItomParamHelper
1049{
1050 static ito::RetVal setVal(ito::ParamBase* param, const _Tp val, int len = 0)
1051 {
1052 static_assert(std::is_pointer<_Tp>::value, "unsupported template type.");
1053
1054 switch (param->d->type)
1055 {
1061 param->detach();
1062 param->d->data.ptrVal = (void*)(reinterpret_cast<const void*>(val));
1063 return ito::retOk;
1064 }
1065
1067 param->detach(false);
1068 auto cVal_ = param->d->data.ptrVal;
1069
1070 if (val)
1071 {
1072 int str_len = static_cast<int>(strlen((const char*)val));
1073
1074 if (((ito::uint32)str_len != param->d->len) || (param->d->data.ptrVal == nullptr))
1075 {
1076 param->d->data.ptrVal = new char[str_len + 1];
1077 }
1078 else
1079 {
1080 cVal_ = nullptr;
1081 }
1082
1083 std::copy_n((const char*)val, str_len + 1, (char*)param->d->data.ptrVal);
1084 param->d->len = str_len;
1085 }
1086 else
1087 {
1088 param->d->data.ptrVal = 0;
1089 param->d->len = 0;
1090 }
1091
1092 if (cVal_)
1093 {
1094 delete[](char*) cVal_;
1095 }
1096 }
1097 return ito::retOk;
1099 param->detach(false);
1100 auto cVal_ = param->d->data.ptrVal;
1101
1102 if ((val) && (len > 0))
1103 {
1104 if ((ito::uint32)len != param->d->len)
1105 {
1106 param->d->data.ptrVal = new char[len];
1107 param->d->len = len;
1108 }
1109 else
1110 {
1111 cVal_ = nullptr;
1112 }
1113
1114 std::copy_n((const char*)val, len, (char*)param->d->data.ptrVal);
1115 }
1116 else
1117 {
1118 param->d->data.ptrVal = nullptr;
1119 param->d->len = 0;
1120 }
1121
1122 if (cVal_)
1123 {
1124 delete[](char*) cVal_;
1125 }
1126 }
1127 return ito::retOk;
1129 param->detach(false);
1130 auto cVal_ = param->d->data.ptrVal;
1131
1132 if ((val) && (len > 0))
1133 {
1134 if ((ito::uint32)len != param->d->len)
1135 {
1136 param->d->data.ptrVal = new int32[len];
1137 param->d->len = len;
1138 }
1139 else
1140 {
1141 cVal_ = nullptr;
1142 }
1143
1144 std::copy_n((const int32*)val, len, (int32*)param->d->data.ptrVal);
1145 }
1146 else
1147 {
1148 param->d->data.ptrVal = nullptr;
1149 param->d->len = 0;
1150 }
1151
1152 if (cVal_)
1153 {
1154 delete[](int32*) cVal_;
1155 }
1156 }
1157 return ito::retOk;
1159 param->detach(false);
1160 auto cVal_ = param->d->data.ptrVal;
1161
1162 if ((val) && (len > 0))
1163 {
1164 if ((ito::uint32)len != param->d->len)
1165 {
1166 param->d->data.ptrVal = new float64[len];
1167 param->d->len = len;
1168 }
1169 else
1170 {
1171 cVal_ = nullptr;
1172 }
1173
1174 std::copy_n((const float64*)val, len, (float64*)param->d->data.ptrVal);
1175 }
1176 else
1177 {
1178 param->d->data.ptrVal = nullptr;
1179 param->d->len = 0;
1180 }
1181
1182 if (cVal_)
1183 {
1184 delete[](float64*) cVal_;
1185 }
1186 }
1187 return ito::retOk;
1189 param->detach(false);
1190 auto cVal_ = param->d->data.ptrVal;
1191
1192 if ((val) && (len > 0))
1193 {
1194 if ((ito::uint32)len != param->d->len)
1195 {
1196 param->d->data.ptrVal = new complex128[len];
1197 param->d->len = len;
1198 }
1199 else
1200 {
1201 cVal_ = nullptr;
1202 }
1203
1204 std::copy_n((const complex128*)val, len, (complex128*)param->d->data.ptrVal);
1205 }
1206 else
1207 {
1208 param->d->data.ptrVal = nullptr;
1209 param->d->len = 0;
1210 }
1211
1212 if (cVal_)
1213 {
1214 delete[](complex128*) cVal_;
1215 }
1216 }
1217 return ito::retOk;
1218 case ito::ParamBase::StringList:{
1219 param->detach(false);
1220 auto cVal_ = param->d->data.ptrVal;
1221
1222 if ((val) && (len > 0))
1223 {
1224 ito::ByteArray* dest = new ito::ByteArray[len];
1225 param->d->data.ptrVal = dest;
1226
1227 for (int i = 0; i < len; ++i)
1228 {
1229 dest[i] = ((const ito::ByteArray*)(val))[i]; // operator=
1230 }
1231
1232 param->d->len = len;
1233 }
1234 else
1235 {
1236 param->d->data.ptrVal = nullptr;
1237 param->d->len = 0;
1238 }
1239
1240 if (cVal_)
1241 {
1242 delete[](ito::ByteArray*) cVal_;
1243 }
1244 }
1245 return ito::retOk;
1246 default:
1247 return ito::RetVal(
1249 0,
1250 "_Tp parameter of setVal<_Tp> does not match the type of the parameter");
1251 }
1252 }
1253
1254 static _Tp getVal(const ito::ParamBase* param, int& len)
1255 {
1256 static_assert(std::is_pointer<_Tp>::value, "unsupported template type.");
1257
1258 if (std::is_pointer<_Tp>::value)
1259 {
1260 if (std::is_same<_Tp, ito::ByteArray*>::value ||
1261 std::is_same<_Tp, void*>::value ||
1262 std::is_same<_Tp, char*>::value ||
1263 std::is_same<_Tp, ito::int8*>::value ||
1264 std::is_same<_Tp, ito::int16*>::value ||
1265 std::is_same<_Tp, ito::int32*>::value ||
1266 std::is_same<_Tp, ito::float32*>::value ||
1267 std::is_same<_Tp, ito::float64*>::value ||
1268 std::is_same<_Tp, ito::complex64*>::value ||
1269 std::is_same<_Tp, ito::complex128*>::value)
1270 {
1271 const_cast<ito::ParamBase*>(param)->detach();
1272 }
1273 }
1274
1275 switch (param->d->type)
1276 {
1278 if (param->d->data.ptrVal)
1279 {
1280 len = static_cast<int>(strlen((const char*)param->d->data.ptrVal));
1281 return reinterpret_cast<_Tp>((char*)param->d->data.ptrVal);
1282 }
1283 else
1284 {
1285 len = 0;
1286 return nullptr;
1287 }
1288
1293 case ito::ParamBase::StringList:
1294 if (param->d->data.ptrVal)
1295 {
1296 len = param->d->len;
1297 return reinterpret_cast<_Tp>((char*)param->d->data.ptrVal);
1298 }
1299 else
1300 {
1301 len = 0;
1302 return nullptr;
1303 }
1304
1310 return reinterpret_cast<_Tp>((char*)param->d->data.ptrVal);
1311
1312 default:
1313 throw std::logic_error("Param::getVal<_Tp>: Non-matching type!");
1314 }
1315 }
1316};
1317
1318template <> struct ItomParamHelper<float64>
1319{
1320 static ito::RetVal setVal(ito::ParamBase* param, float64 val, int /*len = 0*/)
1321 {
1322 param->detach();
1323
1324 switch (param->d->type)
1325 {
1327 param->d->data.i8Val = static_cast<int8>(val);
1328 return ito::retOk;
1330 param->d->data.i32Val = static_cast<int32>(val);
1331 return ito::retOk;
1333 param->d->data.f64Val = val;
1334 return ito::retOk;
1336 param->d->data.c128Val.real = val;
1337 param->d->data.c128Val.imag = 0.0;
1338 return ito::retOk;
1339 default:
1340 return ito::RetVal(
1342 0,
1343 "float64 value passed to setVal<float64> does not match the type of the parameter");
1344 }
1345 }
1346
1347 static float64 getVal(const ito::ParamBase* param, int& /*len*/)
1348 {
1349 switch (param->d->type)
1350 {
1352 return static_cast<float64>(param->d->data.i8Val);
1354 return static_cast<float64>(param->d->data.i32Val);
1356 return param->d->data.f64Val;
1358 return param->d->data.c128Val.real;
1359 default:
1360 throw std::logic_error("Param::getVal<float64>: Non-matching type!");
1361 }
1362 }
1363};
1364
1365template <> struct ItomParamHelper<int32>
1366{
1367 static ito::RetVal setVal(ito::ParamBase* param, int32 val, int /*len = 0*/)
1368 {
1369 param->detach();
1370
1371 switch (param->d->type)
1372 {
1374 param->d->data.i8Val = static_cast<int8>(val);
1375 return ito::retOk;
1377 param->d->data.i32Val = val;
1378 return ito::retOk;
1380 param->d->data.f64Val = static_cast<float64>(val);
1381 return ito::retOk;
1383 param->d->data.c128Val.real = static_cast<float64>(val);
1384 param->d->data.c128Val.imag = 0.0;
1385 return ito::retOk;
1386 default:
1387 return ito::RetVal(
1389 0,
1390 "int32 value passed to setVal<int32> does not match the type of the parameter");
1391 }
1392 }
1393
1394 static int32 getVal(const ito::ParamBase* param, int& /*len*/)
1395 {
1396 switch (param->d->type)
1397 {
1399 return param->d->data.i8Val;
1401 return param->d->data.i32Val;
1403 return static_cast<int32>(param->d->data.f64Val);
1405 return static_cast<int32>(param->d->data.c128Val.real);
1406 case 0:
1407 throw std::invalid_argument("Param::getVal<int32>: non existent parameter");
1408
1409 default:
1410 throw std::logic_error("Param::getVal<int32>: Non-matching type!");
1411 }
1412 }
1413};
1414
1415template <> struct ItomParamHelper<int8>
1416{
1417 static ito::RetVal setVal(ito::ParamBase* param, int8 val, int /*len = 0*/)
1418 {
1419 param->detach();
1420
1421 switch (param->d->type)
1422 {
1424 param->d->data.i8Val = static_cast<int8>(val);
1425 return ito::retOk;
1427 param->d->data.i32Val = static_cast<int32>(val);
1428 return ito::retOk;
1430 param->d->data.f64Val = static_cast<float64>(val);
1431 return ito::retOk;
1433 param->d->data.c128Val.real = static_cast<float64>(val);
1434 param->d->data.c128Val.imag = 0.0;
1435 return ito::retOk;
1436 default:
1437 return ito::RetVal(
1439 0,
1440 "char value passed to setVal<int8> does not match the type of the parameter");
1441 }
1442 }
1443
1444 static int8 getVal(const ito::ParamBase* param, int& /*len*/)
1445 {
1446 switch (param->d->type)
1447 {
1449 return static_cast<int8>(param->d->data.i8Val);
1451 return static_cast<int8>(param->d->data.i32Val);
1453 return static_cast<int8>(param->d->data.f64Val);
1455 return static_cast<int8>(param->d->data.c128Val.real);
1456 case 0:
1457 throw std::invalid_argument("Param::getVal<int8>: non existent parameter");
1458
1459 default:
1460 throw std::logic_error("Param::getVal<int8>: Non-matching type!");
1461 }
1462 }
1463};
1464
1465template <> struct ItomParamHelper<char>
1466{
1467 static ito::RetVal setVal(ito::ParamBase* param, char val, int /*len = 0*/)
1468 {
1469 param->detach();
1470
1471 switch (param->d->type)
1472 {
1474 param->d->data.i8Val = static_cast<int8>(val);
1475 return ito::retOk;
1477 param->d->data.i32Val = static_cast<int32>(val);
1478 return ito::retOk;
1480 param->d->data.f64Val = static_cast<float64>(val);
1481 return ito::retOk;
1483 param->d->data.c128Val.real = static_cast<float64>(val);
1484 param->d->data.c128Val.imag = 0.0;
1485 return ito::retOk;
1486 default:
1487 return ito::RetVal(
1489 0,
1490 "char value passed to setVal<char> does not match the type of the parameter");
1491 }
1492 }
1493
1494 static char getVal(const ito::ParamBase* param, int& /*len*/)
1495 {
1496 switch (param->d->type)
1497 {
1499 return static_cast<char>(param->d->data.i8Val);
1501 return static_cast<char>(param->d->data.i32Val);
1503 return static_cast<char>(param->d->data.f64Val);
1505 return static_cast<char>(param->d->data.c128Val.real);
1506 case 0:
1507 throw std::invalid_argument("Param::getVal<char>: non existent parameter");
1508
1509 default:
1510 throw std::logic_error("Param::getVal<char>: Non-matching type!");
1511 }
1512 }
1513};
1514
1515template <> struct ItomParamHelper<complex128>
1516{
1517 static ito::RetVal setVal(ito::ParamBase* param, const complex128 val, int /*len = 0*/)
1518 {
1519 param->detach();
1520
1521 switch (param->d->type)
1522 {
1524 param->d->data.c128Val.real = val.real();
1525 param->d->data.c128Val.imag = val.imag();
1526 return ito::retOk;
1527 default:
1528 return ito::RetVal(
1530 0,
1531 "complex128 value passed to setVal<complex128> does not match the type of the "
1532 "parameter");
1533 }
1534 }
1535
1536 static complex128 getVal(const ito::ParamBase* param, int& /*len*/)
1537 {
1538 switch (param->d->type)
1539 {
1541 return complex128(param->d->data.i8Val, 0.0);
1543 return complex128(param->d->data.i32Val, 0.0);
1545 return complex128(param->d->data.f64Val, 0.0);
1547 return complex128(param->d->data.c128Val.real, param->d->data.c128Val.imag);
1548 default:
1549 throw std::logic_error("Param::getVal<complex128>: Non-matching type!");
1550 }
1551 }
1552};
1553
1554} // end namespace ito
This is a Qt-free class for byte arrays (strings) without specific encoding information.
Definition byteArray.h:65
const char * data() const
return the pointer to the internal character array. If it is empty, the returned pointer still points...
Definition byteArray.h:144
<
Definition param.h:95
_Tp getVal(int &len) const
Definition param.h:388
void detach(bool allocNewArray=true)
Definition param.cpp:1044
void incRef(Data *x)
depending on the type, set the default value for the autosave flag.
Definition param.h:453
ito::RetVal setVal(_Tp val, int len)
Definition param.h:364
ito::RetVal setVal(_Tp val)
Definition param.h:352
Type
Definition param.h:142
@ Double
complex (complex128) parameter
Definition param.h:155
@ Int
double (float64) parameter
Definition param.h:152
@ PointCloudPtr
point parameter (pointer, no auto-safe possible)
Definition param.h:182
@ DObjPtr
string parameter
Definition param.h:161
@ IntArray
array of doubles
Definition param.h:173
@ CharArray
array of integers
Definition param.h:170
@ Char
integer (int32) parameter
Definition param.h:149
@ PointPtr
polygon mesh parameter (pointer, no auto-safe possible)
Definition param.h:185
@ Complex
dataObject parameter (pointer, no auto-save possible)
Definition param.h:158
@ ComplexArray
point cloud parameter (pointer, no auto-safe possible)
Definition param.h:179
@ String
reference to another plugin instance (pointer, no auto-save possible)
Definition param.h:164
@ HWRef
array of characters
Definition param.h:167
@ DoubleArray
array of complex numbers
Definition param.h:176
@ PolygonMeshPtr
list of strings, given as ito::ByteArray
Definition param.h:188
_Tp getVal() const
Definition param.h:375
void decRefAndFree(Data *x)
increments the reference counter of data
Definition param.h:442
Data * d
Definition param.h:423
Flag
< Flag section, new for itom > 4.1. Before it was part of the Type enumeration.
Definition param.h:105
ParamBase & operator=(ParamBase &&other) noexcept
just copies the value from the right-hand-side tParam (rhs) to this tParam.
Definition param.h:266
class for parameter handling e.g. to pass parameters to plugins
Definition param.h:477
void setInfo(const char *info)
set the info string (description) to info.
Definition param.h:928
const ParamMeta * getMeta() const
Definition param.h:941
const char * getInfo() const
< returns content of info string (string is not copied)
Definition param.h:921
const ParamMeta * getConstMeta() const
Definition param.h:948
void setInfo(const ByteArray &info)
Definition param.h:934
MetaShared * m_pMetaShared
description of this parameter
Definition param.h:886
const _Tp * getConstMetaT(void) const
Definition param.h:989
const _Tp * getMetaT(void) const
Definition param.h:974
ParamMeta * getMeta(void)
Definition param.h:955
Base class for all meta-information classes.
Definition paramMeta.h:58
Class for managing status values (like errors or warning)
Definition retVal.h:54
Definition apiFunctionsGraph.cpp:40
const uint32 paramFlagMask
Definition param.h:55
bool operator!=(const ByteArray &a1, const char *a2)
comparison operator that returns true if the content of a1 is not equal to the given zero-terminated ...
Definition byteArray.h:210
bool operator==(const ByteArray &a1, const char *a2)
comparison operator that returns true if the content of a1 is equal to the given zero-terminated stri...
Definition byteArray.h:198
const uint32 paramTypeMask
Definition param.h:57
@ retError
Definition typeDefs.h:60
@ retOk
Definition typeDefs.h:58
<
Definition param.h:1049
< struct for the shared container for meta information shared meta information object
Definition param.h:876
< struct used as shared memory of ParamBase shared data container
Definition param.h:396
ParamBaseData data
length of array if the type is an array type (including string or string list)
Definition param.h:406
uint16 type
name of this parameter
Definition param.h:415
uint32 len
flags, bitmask of the higher level values in ParamBase::Flag
Definition param.h:409
Data(const ByteArray &name_="")
Definition param.h:397
uint16 flags
type, correspond to ParamBase::Type
Definition param.h:412
std::atomic_int ref
value as union
Definition param.h:403
Union for the internal parameter value of class ParamBase.
Definition param.h:63
Definition param.h:69
complex128_ c128Val
8 bytes
Definition param.h:80
int8 i8Val
< 1 byte
Definition param.h:71
float64 f64Val
16 bytes
Definition param.h:77
int32 i32Val
8 bytes
Definition param.h:74