Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
Variable.h
Go to the documentation of this file.
1#pragma once
3#include <misc/gen/pointer.h>
4
5namespace sf
6{
7
14 : public InformationBase
15 , public VariableTypes
16{
17 public:
21 static void initialize();
22
26 static void uninitialize();
27
32
36 Variable(const Variable& v);
37
43 Variable(id_type id, bool set_desired);
44
52 explicit Variable(const std::string& definition, id_type id_ofs = 0);
53
59 explicit Variable(const Definition& def, id_type id_ofs = 0);
60
64 ~Variable() override;
65
74 bool setup(const std::string& definition, id_type id_ofs = 0);
75
82 bool setup(const Definition& definition, id_type id_ofs = 0);
83
90 inline bool setup(const Variable& v);
91
102 inline bool setup(id_type id, bool set_did = false);
103
111 bool setId(id_type id, bool skip_self = false) const;
112
117 [[nodiscard]] bool isValid() const;
118
123 [[nodiscard]] bool isConverted() const;
124
131 bool setConvert(bool enable);
132
138 [[nodiscard]] bool isNumber() const;
139
145 void makeOwner();
146
152
157 [[nodiscard]] bool isOwner() const;
158
163 [[nodiscard]] inline bool isGlobal() const;
164
171 bool setGlobal(bool global);
172
180 bool setExport(bool global);
181
186 [[nodiscard]] bool isExported() const;
187
192 bool isTemporary() const;
193
198 void setTemporary(bool on_off);
199
207 bool applyTemporary(bool skip_self = true) const;
208
216 bool updateTemporary(bool skip_self = false);
217
223 [[nodiscard]] bool isTemporaryDifferent() const;
224
231
236 [[nodiscard]] VariableHandler* getHandler() const;
237
243 static void setConvertHandler(VariableHandler* handler);
244
248 [[nodiscard]] static VariableHandler* getConvertHandler();
249
257 size_type emitEvent(EEvent event, bool skip_self = false);
258
263
269
275 template<typename T>
276 void setData(T data);
277
283 template<typename T = uint64_t>
284 [[nodiscard]] T getData() const;
285
292 bool setCur(const Value& value, bool skip_self = false);
293
300 [[nodiscard]] bool setCur(const Value& value, bool skip_self = false) const;
301
308 [[nodiscard]] bool loadCur(const Value& value) const;
309
317 static std::string filterString(const std::string& str, EStringType type, size_t max_len);
318
326 bool increase(int steps, bool skip_self = false);
327
335 bool unsetFlag(flags_type flag, bool skip_self = false) const;
336
345 bool setFlag(flags_type flag, bool skip_self = false) const;
346
355 bool updateFlags(flags_type flags, bool skip_self = false) const;
356
361 [[nodiscard]] flags_type getFlags() const;
362
367 [[nodiscard]] flags_type getCurFlags() const;
368
380 bool setConvertValues(const std::string& unit, const Value& multiplier, const Value& offset, int digits = std::numeric_limits<int>::max()) const;
381
394 bool setConvertValues(bool convert = true) const;
395
400 [[nodiscard]] id_type getId() const;
401
406 [[nodiscard]] id_type getDesiredId() const;
407
416 [[nodiscard]] std::string getName(int levels = 0) const;
417
422 [[nodiscard]] int getNameLevelCount() const;
423
428 [[nodiscard]] std::string getUnit() const;
429
435 [[nodiscard]] std::string getUnit(bool converted) const;
436
442 [[nodiscard]] std::string getConvertOption() const;
443
449 [[nodiscard]] EStringType getStringType() const;
450
456 static std::string_view getStringType(EStringType type);
457
463 static EStringType getStringType(const std::string& flags);
464
469 [[nodiscard]] std::string getDescription() const;
470
476 [[nodiscard]] bool isFlag(int flag) const;
477
482 [[nodiscard]] std::string getCurFlagsString() const;
483
488 [[nodiscard]] std::string getFlagsString() const;
489
494 [[nodiscard]] bool isReadOnly() const;
495
500 [[nodiscard]] int getSigDigits() const;
501
507 [[nodiscard]] int getSigDigits(bool converted) const;
508
515 [[nodiscard]] int getRequiredDigits() const;
516
522 [[nodiscard]] const Value& getDef() const;
523
528 [[nodiscard]] const Value& getMin() const;
529
534 [[nodiscard]] const Value& getMax() const;
535
540 [[nodiscard]] const Value& getRnd() const;
541
547 [[nodiscard]] const Value& getCur() const;
548
554 [[nodiscard]] const Value& getDef(bool converted) const;
555
560 [[nodiscard]] const Value& getMin(bool converted) const;
561
568 [[nodiscard]] const Value& getMax(bool converted) const;
569
576 [[nodiscard]] const Value& getRnd(bool converted) const;
577
583 //
584 [[nodiscard]] const Value& getCur(bool converted) const;
585
589 [[nodiscard]] const State::Vector& getStates() const;
590
595 [[nodiscard]] size_type getUsageCount() const;
596
601 [[nodiscard]] size_type getStateCount() const;
602
609 [[nodiscard]] size_type getState(const Value& v) const;
610
616 [[nodiscard]] std::string getStateName(size_type state) const;
617
623 [[nodiscard]] const Value& getStateValue(size_type state) const;
624
630 static Value::EType getType(std::string_view type);
631
637 static std::string getType(Value::EType type);
638
643 [[nodiscard]] Value::EType getType() const;
644
650
656 static size_type getInstanceCount(bool global_only = true);
657
664
669 static std::string getFieldName(int field);
670
678
687
695 static const Variable& getInstanceById(id_type id, const PtrVector& list);
696
701
705 int operator==(const Variable& v) const;
706
713 [[nodiscard]] std::string getCurString(bool use_states = true) const;
714
718 [[nodiscard]] std::string getSetupString() const;
719
724 void clipRound(Value& value) const;
725
732 [[nodiscard]] Value convert(const Value& value, bool to_org = false) const;
733
737 bool writeUpdate(std::ostream& os) const;
738
747 static bool readUpdate(std::istream& is, bool skip_self = false, PtrVector& list = null_ref<PtrVector>());
748
754 bool write(std::ostream& os) const;
755
762 static Definition getDefinition(const std::string& str);
763
772 static bool read(std::istream& is, bool skip_self = false, PtrVector& list = null_ref<PtrVector>());
773
785 static bool create(std::istream& is, PtrVector& list = null_ref<PtrVector>(), bool global = true, int& err_line = null_ref<int>());
786
792 static flags_type toFlags(const std::string& flags);
793
799 static std::string getFlagsString(flags_type flags);
800
806 static std::string_view getEventName(EEvent event);
807
812 void operator delete(void*);// NOLINT(misc-new-delete-overloads)
813
814 protected:
819 explicit Variable(bool global);
820
821 private:
825 explicit Variable(void*, void*);
826
833 bool updateValue(const Value& value, bool skip_self) const;
834
839 bool updateTempValue(const Value& value, bool skip_self);
840
847 [[maybe_unused]] size_type emitLocalEvent(EEvent event, bool skip_self = true) const;
848
855 [[maybe_unused]] size_type emitGlobalEvent(EEvent event, bool skip_self = true) const;
856
864 void emitEvent(EEvent ev, const Variable& caller);
865
871 static VariableReference* getReferenceById(id_type id);
872
877 static void removeHandler(const VariableHandler* handler);
878
884 bool attachRef(VariableReference* ref);
885
890 size_type attachDesired() const;
891
895 VariableReference* _reference{nullptr};
899 uint64_t _data{0};
903 bool _converted{false};
907 Value* _temporary{nullptr};
911 VariableHandler* _handler{nullptr};
915 id_type _desiredId{0};
919 bool _global{false};
920
921 friend class VariableHandler;
922
923 friend class VariableReference;
924
925 friend class VariableStatic;
926};
927
931_GII_FUNC std::ostream& operator<<(std::ostream& os, const Variable& v);
932
936_GII_FUNC std::istream& operator>>(std::istream& is, Variable& v);
937
941_GII_FUNC std::ostream& operator<<(std::ostream& os, const Variable::State::Vector& v);
942
943}// namespace sf
944
945// Include all inlined functions and template implementations.
946#include <gii/gen/Variable.hpp>
Base class for all generic information objects to be able to put them in a typed list together.
Definition InformationBase.h:13
unsigned long long id_type
Type used for the identifying integer (64-bits).
Definition InformationBase.h:30
size_t size_type
Type used for size of vectors.
Definition InformationBase.h:38
int32_t flags_type
Type used for the flag integer.
Definition InformationBase.h:34
Value container class able to performing arithmetic functions.
Definition Value.h:19
EType
Enumerate for available types.
Definition Value.h:34
Base class used for giving a Variable instance access to a member functions of a derived class....
Definition VariableHandler.h:14
Base class of the sf::Variable having all global static data members.
Definition VariableStatic.h:15
This base class contains all local types of used in the Variable class. Multiple inheritance makes th...
Definition VariableTypes.h:26
EStringType
Enumeration of string types used for filtering.
Definition VariableTypes.h:254
EEvent
Events send to the handler set with sf::Variable::setHandler.
Definition VariableTypes.h:130
Class for creating and referencing global or local created parameters or settings called variables....
Definition Variable.h:16
const Value & getCur(bool converted) const
Gets the current value converted or not converted independent of the conversion flag.
int getNameLevelCount() const
Gets the amount of levels of the full name path.
const Value & getMax() const
Gets the variable Maximum value. Is converted when converted flag is true.
bool setConvertValues(bool convert=true) const
Enables or disables unit conversion. Only owners can make this call successfully. Depending on the g...
static std::string filterString(const std::string &str, EStringType type, size_t max_len)
Filters the passed string according the given type.
const Value & getMin(bool converted) const
Gets the Minimum value converted or not converted independent of the conversion flag.
bool setCur(const Value &value, bool skip_self=false) const
Same as setCur() but for const instances objects of this instance.
bool increase(int steps, bool skip_self=false)
Increase current variable value by step increments also negative values. Emits event veValueChange wh...
bool isReadOnly() const
Checks if variable is allowed to change its value.
bool isOwner() const
Checks if this instance is the owner of this variable.
size_type getState(const Value &v) const
Gets the state index of passed value when it exists.
bool setCur(const Value &value, bool skip_self=false)
Both return true if there was a change and notify all variables of the same variable id through an ev...
std::string getName(int levels=0) const
Gets the name or a part of the name of the attached variable. Returns the variable name which is defa...
void setData(T data)
Sets the data for this instance for user purposes.
bool setGlobal(bool global)
Sets the variable to be global. This can only happen if it attached to Zero Variable so when the id i...
std::string getStateName(size_type state) const
Gets the display name of the passed state's index.
Variable(id_type id, bool set_desired)
Creates an instance having a reference with 'id'.
std::string getDescription() const
Gets the purpose description of the attached variable.
std::string getSetupString() const
Gets the setup std::string for this variable.
static void uninitialize()
Called when terminating the application.
const Value & getCur() const
Gets the variable current value. Is converted when converted flag is true.
static bool create(std::istream &is, PtrVector &list=null_ref< PtrVector >(), bool global=true, int &err_line=null_ref< int >())
Reads multiple variable setup strings from stream separated by newline characters....
Value convert(const Value &value, bool to_org=false) const
Gets the passed value converted to new units or back again.
static Variable & getInstanceById(id_type id, PtrVector &list)
Gets variable with the given id. When not found it returns the zero variable.
bool updateTemporary(bool skip_self=false)
Updates the temporary value with the real value.
bool isFlag(int flag) const
Gets if a flag or flags has been set currently.
static size_type getCount()
Gets the amount of variables having different ID's in the system.
Value::EType getType() const
Gets current type enumerate of this variable instance.
int getSigDigits() const
Gets the normalized decimal point location for this instance.
const Value & getRnd(bool converted) const
Gets the Rounding value converted or not converted independent of the conversion flag.
~Variable() override
Virtual destructor.
static bool readUpdate(std::istream &is, bool skip_self=false, PtrVector &list=null_ref< PtrVector >())
load single current value and flags from stream. If 'list' is other than the default that list is use...
Variable(const Definition &def, id_type id_ofs=0)
Creates a variable using the passed definition structure.
bool isNumber() const
Checks if the value is a number or not. Result is true when the value type is either Value::vitFloat ...
Variable & operator=(const Variable &v)
Assignment operator that attaches this instance to the same VariableReference as 'v'.
flags_type getFlags() const
Gets the flags at the time the instance was created.
const Value & getDef() const
Gets the variable default value. Is converted when converted flag is true.
bool setup(const Variable &v)
Sets reference to other variable. This is the only way for local variables.
bool setFlag(flags_type flag, bool skip_self=false) const
Sets a flag or multiple flags on the reference. Only for an owner can call this function successful....
bool setExport(bool global)
Makes an owning local variable appear globally. Useful when reading/creating instances from stored fi...
static std::string_view getEventName(EEvent event)
Gets the event name to make debugging easier.
int operator==(const Variable &v) const
Comparison operator.
static void initialize()
Must be called to initialize the statics which this class depends on.
flags_type getCurFlags() const
Gets the current flags for this instance.
static std::string_view getStringType(EStringType type)
Gets the description string for passed string type.
bool applyTemporary(bool skip_self=true) const
Applies the temporary value of this instance. Used mainly in dialogs to apply the changed value to be...
size_type emitEvent(EEvent event, bool skip_self=false)
Initiate event for all instances of this variable depending on the event value. Returns the amount of...
const State::Vector & getStates() const
Gets the complete state vector as a reference.
static Value::EType getType(std::string_view type)
Gets enumerate value of the passed string.
static EStringType getStringType(const std::string &flags)
Gets the string type value of the passed string.
size_type getUsageCount() const
Gets the usage count of this variable reference.
Variable(const std::string &definition, id_type id_ofs=0)
Creates a variable according to the definition std::string passed to it. The "id" from the definition...
VariableHandler * getHandler() const
Gets the link set by setHandler().
bool unsetFlag(flags_type flag, bool skip_self=false) const
Unsets a flag or multiple flags of the attached VariableReference. Emits event veValueChange when a c...
size_type getStateCount() const
Gets the state count.
std::string getConvertOption() const
Gets the conversion option string. This string determines how the value is to be converted when expor...
id_type getId() const
Gets the current attached ID.
Variable()
Default constructor for a default global variable.
const Value & getMax(bool converted) const
Gets the Maximum value converted or not converted independent of the conversion flag.
std::string getCurString(bool use_states=true) const
Gets current converted value in default formatted string or state. When a temporary value is used tha...
bool setup(const std::string &definition, id_type id_ofs=0)
Creates a variable according to the definition std::string passed to it. The passed identifier offset...
static Definition getDefinition(const std::string &str)
std::string getUnit() const
Gets the variable unit.
bool writeUpdate(std::ostream &os) const
Writes id and current value and flags to the stream.
void setTemporary(bool on_off)
When true is passed a local temporary value is used instead of current.
T getData() const
Gets the data for this instance for user purposes Set with setData().
std::string getCurFlagsString() const
Gets the current flags in a std::string form.
static VariableHandler * getConvertHandler()
Gets the link to the global conversion handler.
std::string getFlagsString() const
Gets the flags at setup in a std::string form.
std::string getUnit(bool converted) const
Gets variable unit of converted or non-converted value.
bool loadCur(const Value &value) const
Used in settings loading routines which use the owner to Set a new value. Checks if clients have writ...
static std::string getFieldName(int field)
Gets the name of the passed field enumerate EField.
EStringType getStringType() const
Gets the string type which is determined by the character(s) in the unit field. The maximum length is...
static const Variable & getInstanceById(id_type id, const PtrVector &list)
Gets variable with the given id. When not found it returns the zero variable.
bool write(std::ostream &os) const
Writes id and current value to the stream.
int getSigDigits(bool converted) const
Gets the normalized decimal point location for this instance converted or not converted.
static std::string getFlagsString(flags_type flags)
Gets the passed flags in a std::string form.
static std::string getType(Value::EType type)
Gets type string of given enumerate value.
const Value & getStateValue(size_type state) const
Gets the value of the passed state index.
id_type getDesiredId() const
Gets the desired id of this instance.
static PtrVector getList()
Retrieves a list of instances available (global + exported). Useful in populating a selection dialog.
void setHandler(VariableHandler *handler)
Only one link is available at a time. Sets an event handler for this variable, passing NULL wil disab...
bool setup(const Definition &definition, id_type id_ofs=0)
Creates a variable using the definition structure.
Variable & getOwner() const
Gets a reference to the owner/server instance of this variable.
bool setConvert(bool enable)
Enables or disables conversion of floating point values. If a change was triggered a veConverted even...
bool setConvertValues(const std::string &unit, const Value &multiplier, const Value &offset, int digits=std::numeric_limits< int >::max()) const
Sets the global conversion values and signals the clients. Only with owners a call is successful and ...
static bool read(std::istream &is, bool skip_self=false, PtrVector &list=null_ref< PtrVector >())
void clipRound(Value &value) const
Clips the passed value between min and max values.
void makeOwner()
Makes this instance owner of this variable instance and emits the appropriate events....
Variable(const Variable &v)
Copy constructor. Copies also the Desired ID data member.
const Value & getRnd() const
Gets the variable Rounding value. Converted when converted flag is true.
bool setup(id_type id, bool set_did=false)
Sets reference to other by id. The global list of references is searched for the passed id and refere...
bool isExported() const
Checks if the variable is an exported local variable.
static flags_type toFlags(const std::string &flags)
Converts a std::string with flag characters to an integer.
const Value & getDef(bool converted) const
Gets the default value converted or not Is converted independent of the conversion flag.
static const Variable & getInstanceById(id_type id)
Gets variable with the given id. When not found it returns the zero variable.
void setDesiredId(id_type id)
Sets the desired id member variable to the passed id. If the passed 'id' is zero the automatic attach...
bool isTemporaryDifferent() const
Check if the temporary value differs from the actual value. Used mainly in dialogs to enable an apply...
void setDesiredId()
Sets the desired id member variable to the current attached reference id.
bool isConverted() const
Checks if the current instance is converting floating point values.
bool updateFlags(flags_type flags, bool skip_self=false) const
Replaces all flags with the passed flags. Only for an owner can call this function successful....
bool isTemporary() const
Checks if a temporary is used instead of current actual value.
static size_type getInstanceCount(bool global_only=true)
Gets the total amount of variable instances in the system.
static void setConvertHandler(VariableHandler *handler)
Special handler for converting float variables globally. Sets an event handler for this variable,...
int getRequiredDigits() const
Gets the required digits for this instance based on round, min and max values. Converted or not the v...
bool isGlobal() const
Check if this instance is a global variable or not.
bool isValid() const
Checks validity of the attached reference and used after a setup call.
bool setId(id_type id, bool skip_self=false) const
Only for local non exported owning variables to set the id after setup is called using a string....
Variable(bool global)
Creates global or local variable instance depending on the value passed.
const Value & getMin() const
Gets the variable Minimum value. Is converted when converted flag is true.
#define _GII_CLASS
Definition gii/global.h:38
#define _GII_FUNC
Definition gii/global.h:37
Definition Application.h:10
_GII_FUNC std::istream & operator>>(std::istream &is, ResultData &)
Stream operator for setting up this instance with a setup std::string.
_GII_FUNC std::ostream & operator<<(std::ostream &os, const ResultData &)
Stream operator for the setup std::string.
Definition VariableReference.h:13
Structure used to set up a variable The method Variable::getDefinition() transforms the definition st...
Definition VariableTypes.h:274