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
144 [[nodiscard]] bool isNumber() const;
145
151 void makeOwner();
152
158
163 [[nodiscard]] bool isOwner() const;
164
169 [[nodiscard]] inline bool isGlobal() const;
170
177 bool setGlobal(bool global);
178
186 bool setExport(bool global);
187
192 [[nodiscard]] bool isExported() const;
193
198 bool isTemporary() const;
199
204 void setTemporary(bool on_off);
205
213 bool applyTemporary(bool skip_self = true) const;
214
222 bool updateTemporary(bool skip_self = false);
223
229 [[nodiscard]] bool isTemporaryDifferent() const;
230
237
242 [[nodiscard]] VariableHandler* getHandler() const;
243
249 static void setConvertHandler(VariableHandler* handler);
250
254 [[nodiscard]] static VariableHandler* getConvertHandler();
255
263 size_type emitEvent(EEvent event, bool skip_self = false);
264
269
275
281 template<typename T>
282 void setData(T data);
283
289 template<typename T = uint64_t>
290 [[nodiscard]] T getData() const;
291
298 bool setCur(const Value& value, bool skip_self = false);
299
306 [[nodiscard]] bool setCur(const Value& value, bool skip_self = false) const;
307
314 [[nodiscard]] bool loadCur(const Value& value) const;
315
323 static std::string filterString(const std::string& str, EStringType type, size_t max_len);
324
332 bool increase(int steps, bool skip_self = false);
333
341 bool unsetFlag(flags_type flag, bool skip_self = false) const;
342
351 bool setFlag(flags_type flag, bool skip_self = false) const;
352
361 bool updateFlags(flags_type flags, bool skip_self = false) const;
362
367 [[nodiscard]] flags_type getFlags() const;
368
373 [[nodiscard]] flags_type getCurFlags() const;
374
386 bool setConvertValues(const std::string& unit, const Value& multiplier, const Value& offset, int digits = std::numeric_limits<int>::max()) const;
387
400 bool setConvertValues(bool convert = true) const;
401
406 [[nodiscard]] id_type getId() const;
407
412 [[nodiscard]] id_type getDesiredId() const;
413
422 [[nodiscard]] std::string getName(int levels = 0) const;
423
428 [[nodiscard]] int getNameLevelCount() const;
429
434 [[nodiscard]] std::string getUnit() const;
435
441 [[nodiscard]] std::string getUnit(bool converted) const;
442
448 [[nodiscard]] std::string getConvertOption() const;
449
455 [[nodiscard]] EStringType getStringType() const;
456
462 static std::string_view getStringType(EStringType type);
463
469 static EStringType getStringType(const std::string& flags);
470
475 [[nodiscard]] std::string getDescription() const;
476
482 [[nodiscard]] bool isFlag(int flag) const;
483
488 [[nodiscard]] std::string getCurFlagsString() const;
489
494 [[nodiscard]] std::string getFlagsString() const;
495
500 [[nodiscard]] bool isReadOnly() const;
501
506 [[nodiscard]] int getSigDigits() const;
507
513 [[nodiscard]] int getSigDigits(bool converted) const;
514
521 [[nodiscard]] int getRequiredDigits() const;
522
528 [[nodiscard]] const Value& getDef() const;
529
534 [[nodiscard]] const Value& getMin() const;
535
540 [[nodiscard]] const Value& getMax() const;
541
546 [[nodiscard]] const Value& getRnd() const;
547
553 [[nodiscard]] const Value& getCur() const;
554
560 [[nodiscard]] const Value& getDef(bool converted) const;
561
566 [[nodiscard]] const Value& getMin(bool converted) const;
567
574 [[nodiscard]] const Value& getMax(bool converted) const;
575
582 [[nodiscard]] const Value& getRnd(bool converted) const;
583
589 //
590 [[nodiscard]] const Value& getCur(bool converted) const;
591
595 [[nodiscard]] const State::Vector& getStates() const;
596
601 [[nodiscard]] size_type getUsageCount() const;
602
607 [[nodiscard]] size_type getStateCount() const;
608
615 [[nodiscard]] size_type getState(const Value& v) const;
616
622 [[nodiscard]] std::string getStateName(size_type state) const;
623
629 [[nodiscard]] const Value& getStateValue(size_type state) const;
630
636 static Value::EType getType(std::string_view type);
637
643 static std::string getType(Value::EType type);
644
649 [[nodiscard]] Value::EType getType() const;
650
656
662 static size_type getInstanceCount(bool global_only = true);
663
670
675 static std::string getFieldName(int field);
676
684
693
701 static const Variable& getInstanceById(id_type id, const PtrVector& list);
702
707
711 int operator==(const Variable& v) const;
712
719 [[nodiscard]] std::string getCurString(bool use_states = true) const;
720
724 [[nodiscard]] std::string getSetupString() const;
725
730 void clipRound(Value& value) const;
731
738 [[nodiscard]] Value convert(const Value& value, bool to_org = false) const;
739
743 bool writeUpdate(std::ostream& os) const;
744
753 static bool readUpdate(std::istream& is, bool skip_self = false, PtrVector& list = null_ref<PtrVector>());
754
760 bool write(std::ostream& os) const;
761
768 static Definition getDefinition(const std::string& str);
769
778 static bool read(std::istream& is, bool skip_self = false, PtrVector& list = null_ref<PtrVector>());
779
791 static bool create(std::istream& is, PtrVector& list = null_ref<PtrVector>(), bool global = true, int& err_line = null_ref<int>());
792
798 static flags_type toFlags(const std::string& flags);
799
805 static std::string getFlagsString(flags_type flags);
806
812 static std::string_view getEventName(EEvent event);
813
818 void operator delete(void*);
819
820 protected:
825 explicit Variable(bool global);
826
827 private:
831 explicit Variable(void*, void*);
832
839 bool updateValue(const Value& value, bool skip_self) const;
840
845 bool updateTempValue(const Value& value, bool skip_self);
846
853 [[maybe_unused]] size_type emitLocalEvent(EEvent event, bool skip_self = true) const;
854
861 [[maybe_unused]] size_type emitGlobalEvent(EEvent event, bool skip_self = true) const;
862
870 void emitEvent(EEvent ev, const Variable& caller);
871
877 static VariableReference* getReferenceById(id_type id);
878
883 static void removeHandler(const VariableHandler* handler);
884
890 bool attachRef(VariableReference* ref);
891
896 size_type attachDesired() const;
897
901 VariableReference* _reference{nullptr};
905 uint64_t _data{0};
909 bool _converted{false};
913 Value* _temporary{nullptr};
917 VariableHandler* _handler{nullptr};
921 id_type _desiredId{0};
925 bool _global{false};
926
927 friend class VariableHandler;
928
929 friend class VariableReference;
930
931 friend class VariableStatic;
932};
933
937_GII_FUNC std::ostream& operator<<(std::ostream& os, const Variable& v);
938
942_GII_FUNC std::istream& operator>>(std::istream& is, Variable& v);
943
947_GII_FUNC std::ostream& operator<<(std::ostream& os, const Variable::State::Vector& v);
948
952class LocalVariable final : public Variable
953{
954 public:
960};
961
962}// namespace sf
963
964// Include all inlined functions and template implementations.
965#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
Create a local variable by calling the protected constructor.
Definition Variable.h:953
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:255
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 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 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:275