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
58 bool isZero() const;
59
65 explicit Variable(const Definition& def, id_type id_ofs = 0);
66
70 ~Variable() override;
71
80 bool setup(const std::string& definition, id_type id_ofs = 0);
81
88 bool setup(const Definition& definition, id_type id_ofs = 0);
89
96 inline bool setup(const Variable& v);
97
108 inline bool setup(id_type id, bool set_did = false);
109
117 bool setId(id_type id, bool skip_self = false) const;
118
123 [[nodiscard]] bool isValid() const;
124
129 [[nodiscard]] bool isConverted() const;
130
137 bool setConvert(bool enable);
138
143 [[nodiscard]] bool isNumber() const;
144
150 void makeOwner();
151
157
162 [[nodiscard]] bool isOwner() const;
163
168 [[nodiscard]] inline bool isGlobal() const;
169
176 bool setGlobal(bool global);
177
185 bool setExport(bool global);
186
191 [[nodiscard]] bool isExported() const;
192
197 bool isTemporary() const;
198
203 void setTemporary(bool on_off);
204
212 bool applyTemporary(bool skip_self = true) const;
213
221 bool updateTemporary(bool skip_self = false);
222
228 [[nodiscard]] bool isTemporaryDifferent() const;
229
236
241 [[nodiscard]] VariableHandler* getHandler() const;
242
248 static void setConvertHandler(VariableHandler* handler);
249
253 [[nodiscard]] static VariableHandler* getConvertHandler();
254
262 size_type emitEvent(EEvent event, bool skip_self = false);
263
268
274
280 template<typename T>
281 void setData(T data);
282
288 template<typename T = uint64_t>
289 [[nodiscard]] T getData() const;
290
297 bool setCur(const Value& value, bool skip_self = false);
298
305 [[nodiscard]] bool setCur(const Value& value, bool skip_self = false) const;
306
313 [[nodiscard]] bool loadCur(const Value& value) const;
314
322 static std::string filterString(const std::string& str, EStringType type, size_t max_len);
323
331 bool increase(int steps, bool skip_self = false);
332
340 bool unsetFlag(flags_type flag, bool skip_self = false) const;
341
350 bool setFlag(flags_type flag, bool skip_self = false) const;
351
360 bool updateFlags(flags_type flags, bool skip_self = false) const;
361
366 [[nodiscard]] flags_type getFlags() const;
367
372 [[nodiscard]] flags_type getCurFlags() const;
373
385 bool setConvertValues(const std::string& unit, const Value& multiplier, const Value& offset, int digits = std::numeric_limits<int>::max()) const;
386
399 bool setConvertValues(bool convert = true) const;
400
405 [[nodiscard]] id_type getId() const;
406
411 [[nodiscard]] id_type getDesiredId() const;
412
421 [[nodiscard]] std::string getName(int level = 0) const;
422
427 [[nodiscard]] int getNameLevelCount() const;
428
433 [[nodiscard]] std::string getUnit() const;
434
440 [[nodiscard]] std::string getUnit(bool converted) const;
441
447 [[nodiscard]] std::string getConvertOption() const;
448
454 [[nodiscard]] EStringType getStringType() const;
455
461 static std::string_view getStringType(EStringType type);
462
468 static EStringType getStringType(const std::string& flags);
469
474 [[nodiscard]] std::string getDescription() const;
475
481 [[nodiscard]] bool isFlag(int flag) const;
482
487 [[nodiscard]] std::string getCurFlagsString() const;
488
493 [[nodiscard]] std::string getFlagsString() const;
494
499 [[nodiscard]] bool isReadOnly() const;
500
505 [[nodiscard]] int getSigDigits() const;
506
512 [[nodiscard]] int getSigDigits(bool converted) const;
513
520 [[nodiscard]] int getRequiredDigits() const;
521
527 [[nodiscard]] const Value& getDef() const;
528
533 [[nodiscard]] const Value& getMin() const;
534
539 [[nodiscard]] const Value& getMax() const;
540
545 [[nodiscard]] const Value& getRnd() const;
546
552 [[nodiscard]] const Value& getCur() const;
553
559 [[nodiscard]] const Value& getDef(bool converted) const;
560
565 [[nodiscard]] const Value& getMin(bool converted) const;
566
573 [[nodiscard]] const Value& getMax(bool converted) const;
574
581 [[nodiscard]] const Value& getRnd(bool converted) const;
582
588 //
589 [[nodiscard]] const Value& getCur(bool converted) const;
590
594 [[nodiscard]] const State::Vector& getStates() const;
595
600 [[nodiscard]] size_type getUsageCount() const;
601
606 [[nodiscard]] size_type getStateCount() const;
607
613 [[nodiscard]] size_type getState(const Value& v) const;
614
620 [[nodiscard]] std::string getStateName(size_type state) const;
621
627 [[nodiscard]] const Value& getStateValue(size_type state) const;
628
634 static Value::EType getType(std::string_view type);
635
641 static std::string getType(Value::EType type);
642
647 [[nodiscard]] Value::EType getType() const;
648
654
660 static size_type getInstanceCount(bool global_only = true);
661
668
673 static std::string getFieldName(int field);
674
682
691
699 static const Variable& getInstanceById(id_type id, const PtrVector& list);
700
705
709 int operator==(const Variable& v) const;
710
717 [[nodiscard]] std::string getCurString(bool use_states = true) const;
718
722 [[nodiscard]] std::string getSetupString() const;
723
728 void clipRound(Value& value) const;
729
736 [[nodiscard]] Value convert(const Value& value, bool to_org = false) const;
737
741 bool writeUpdate(std::ostream& os) const;
742
750 static bool readUpdate(std::istream& is, bool skip_self = false, PtrVector& list = null_ref<PtrVector>());
751
757 bool write(std::ostream& os) const;
758
765 static Definition getDefinition(const std::string& str);
766
774 static bool read(std::istream& is, bool skip_self = false, PtrVector& list = null_ref<PtrVector>());
775
787 static bool create(std::istream& is, PtrVector& list = null_ref<PtrVector>(), bool global = true, int& err_line = null_ref<int>());
788
794 static flags_type toFlags(const std::string& flags);
795
801 static std::string getFlagsString(flags_type flags);
802
808 static std::string_view getEventName(EEvent event);
809
814 void operator delete(void*);
815
816 protected:
821 explicit Variable(bool global);
822
823 private:
827 explicit Variable(void*, void*);
828
835 bool updateValue(const Value& value, bool skip_self) const;
836
841 bool updateTempValue(const Value& value, bool skip_self);
842
849 [[maybe_unused]] size_type emitLocalEvent(EEvent event, bool skip_self = true) const;
850
857 [[maybe_unused]] size_type emitGlobalEvent(EEvent event, bool skip_self = true) const;
858
865 void emitEvent(EEvent ev, const Variable& caller);
866
872 static VariableReference* getReferenceById(id_type id);
873
878 static void removeHandler(const VariableHandler* handler);
879
885 bool attachRef(VariableReference* ref);
886
891 size_type attachDesired() const;
892
896 VariableReference* _reference{nullptr};
900 uint64_t _data{0};
904 bool _converted{false};
908 Value* _temporary{nullptr};
912 VariableHandler* _handler{nullptr};
916 id_type _desiredId{0};
920 bool _global{false};
921
922 friend class VariableHandler;
923
924 friend class VariableReference;
925
926 friend class VariableStatic;
927};
928
932_GII_FUNC std::ostream& operator<<(std::ostream& os, const Variable& v);
933
937_GII_FUNC std::istream& operator>>(std::istream& is, Variable& v);
938
942_GII_FUNC std::ostream& operator<<(std::ostream& os, const Variable::State::Vector& v);
943
947class LocalVariable final : public Variable
948{
949 public:
955};
956
957}// namespace sf
958
959#ifndef __JETBRAINS_IDE__
960 // Include all inlined functions and template implementations.
961 #include <gii/gen/Variable.hpp>
962#endif
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
Create a local variable by calling the protected constructor.
Definition Variable.h:948
LocalVariable()
Default constructor only. To data members in classes.
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 sent to the handler set with Variable::setHandler. For broadcast events the variable paramet...
Definition VariableTypes.h:129
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 number of levels from the full name path.
const Value & getMax() const
Gets the variable Maximum value. Is converted when the '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 to the given type string.
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 the 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 the passed value v 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...
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 is attached to Zero Variable so when the 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 the '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 the inout 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 number 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 >())
Loads the current value and flags from the input stream.
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.
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 the 'converted' flag is true.
bool setup(const Variable &v)
Sets reference to another 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 a 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 number 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,...
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 enumeration 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 the given enumeration 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 and exported). Useful in populating a selection dialo...
void setHandler(VariableHandler *handler)
Only one link is available at a time. Sets an event handler for this variable, passing NULL will disa...
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,...
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 >())
Load current values from the input stream.
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....
std::string getName(int level=0) const
Gets the name or a part of the name from the attached variable. Returns the variable name which is de...
Variable(const Variable &v)
Copy constructor. Copies also the Desired ID data member.
const Value & getRnd() const
Gets the variable Rounding value. Is converted when the '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 attac...
bool isZero() const
Check if the current reference is zero.
bool isTemporaryDifferent() const
Check if the temporary value differs from the actual value. Used mainly in dialogs to enable an 'Appl...
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 the current actual value.
static size_type getInstanceCount(bool global_only=true)
Gets the total number 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,...
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 a global or local variable instance depending on the value passed.
const Value & getMin() const
Gets the variable Minimum value. Is converted when the '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