Public Types | Public Member Functions | List of all members
ime::Preference Class Reference

A key-value pair that can be saved to a file on the disk. More...

#include <Preference.h>

Public Types

enum class  Type {
  Bool , String , Int , UInt ,
  Double , Float
}
 The type of the value in the key-value pair. More...
 

Public Member Functions

 Preference (const std::string &key, Type type)
 Constructor. More...
 
template<typename T >
 Preference (const std::string &key, Type type, T value, const std::string &description="")
 Constructor. More...
 
template<typename T >
void setValue (T value)
 Set the value of the preference. More...
 
bool hasValue () const
 Check if the preference has a value or not. More...
 
template<typename T >
getValue () const
 Get the value of the preference. More...
 
Type getType () const
 Get the type of the preference. More...
 
const std::string & getKey () const
 Get the key/name of the preference. More...
 
void setDescription (const std::string &description)
 Set the text to be displayed above the key-value pair. More...
 
const std::string & getDescription () const
 Get the preference description. More...
 

Detailed Description

A key-value pair that can be saved to a file on the disk.

A Preference is very similar to a Property. What sets them apart is that a Preference can only store objects whose type are known to it whereas a Property can store objects of any type. In addition, the contents of a Preference can be saved to a file on the disk

Definition at line 43 of file Preference.h.

Member Enumeration Documentation

◆ Type

enum class ime::Preference::Type
strong

The type of the value in the key-value pair.

Enumerator
Bool 

bool

String 

std::string type (NOT "const char*")

Int 

int

UInt 

unsigned int

Double 

double

Float 

float

Definition at line 48 of file Preference.h.

Constructor & Destructor Documentation

◆ Preference() [1/2]

ime::Preference::Preference ( const std::string &  key,
Type  type 
)

Constructor.

Parameters
keyThe unique identifier of the preference
typeThe type of the value in the key-value pair

Note that once set, the key and type of the preference cannot be changed later

◆ Preference() [2/2]

template<typename T >
ime::Preference::Preference ( const std::string &  key,
Type  type,
value,
const std::string &  description = "" 
)

Constructor.

Parameters
keyThe unique identifier of the preference
typeThe type of the value in the key-value pair
valueThe value of the preference
descriptionAn optional message describing the key-value pair
Exceptions
InvalidArgumentExceptionIf the type of the value argument does not correspond to the type specified by type argument

Note that once set, the key and type cannot be changed later.

A description is an optional short message describing what the preference is. When saved to the disk, it will be preceded by a hashtag sign (#), indicating that it is a comment

Member Function Documentation

◆ getDescription()

const std::string & ime::Preference::getDescription ( ) const

Get the preference description.

Returns
The preference description

◆ getKey()

const std::string & ime::Preference::getKey ( ) const

Get the key/name of the preference.

Returns
The key of the preference

◆ getType()

Type ime::Preference::getType ( ) const

Get the type of the preference.

Returns
The type of the preference

◆ getValue()

template<typename T >
T ime::Preference::getValue ( ) const

Get the value of the preference.

Returns
The value stored in the property
Exceptions
InvalidArgumentExceptionif T is not the same as the ime::Preference::Type given in the constructor or the preference does not have a value
See also
setValue, hasValue and getType

◆ hasValue()

bool ime::Preference::hasValue ( ) const

Check if the preference has a value or not.

Returns
True if the preference contains a value or false if it does not contain a value
See also
setValue

◆ setDescription()

void ime::Preference::setDescription ( const std::string &  description)

Set the text to be displayed above the key-value pair.

Parameters
descriptionThe description to be set

A description is an optional short message describing what the preference is. When saved to the disk, it will be preceded by a hashtag sign (#), indicating that it is a comment

By default, the description is an empty string

Warning
The description must not contain new line characters

◆ setValue()

template<typename T >
void ime::Preference::setValue ( value)

Set the value of the preference.

Parameters
valueThe value to be set
Exceptions
InvalidArgumentExceptionif the type of value does not correspond to the ime::Preference::Type given in the constructor
See also
getValue, hasValue and getType

The documentation for this class was generated from the following file: