Skip to content

Commit

Permalink
Merged the number-item branch into master.
Browse files Browse the repository at this point in the history
- All numeric items are now implemented by one code base in the `numeric_item<>` template
  class.

- Each type of numeric item can show a unit; "ms" and "s" for time duration items.

- `cfg::msg_duration` and `cfg::tolerance` are now of `std::chrono::duration<>` types.

- `wupsxx/storage.{cpp,hpp}` now has specializations for `std::chrono::duration<>` types.

- Some time-related code was moved from `utils.{cpp,hpp}` to `time_utils.{cpp,hpp}`.

- "Seconds" are not passed around in a raw `double` type anymore, the
  `time_utils::dbl_seconds` type is used.
  • Loading branch information
Daniel K. O. (dkosmari) committed Jun 3, 2024
2 parents bd07316 + 28cf7fe commit 92f6d85
Show file tree
Hide file tree
Showing 25 changed files with 574 additions and 379 deletions.
33 changes: 16 additions & 17 deletions include/cfg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,32 @@ namespace cfg {
}

namespace defaults {
extern const bool auto_tz;
extern const int msg_duration;
extern const int notify;
extern const std::string server;
extern const bool sync;
extern const int threads;
extern const int tolerance;
extern const bool auto_tz;
extern const std::chrono::seconds msg_duration;
extern const int notify;
extern const std::string server;
extern const bool sync;
extern const int threads;
extern const std::chrono::milliseconds tolerance;
}


extern bool auto_tz;
extern int msg_duration;
extern int notify;
extern std::string server;
extern bool sync;
extern int threads;
extern int tolerance;
extern std::chrono::minutes utc_offset;
extern bool auto_tz;
extern std::chrono::seconds msg_duration;
extern int notify;
extern std::string server;
extern bool sync;
extern int threads;
extern std::chrono::milliseconds tolerance;
extern std::chrono::minutes utc_offset;


void load();
void reload();
void save();
void migrate_old_config();

std::chrono::minutes get_utc_offset();
void set_utc_offset(std::chrono::minutes tz_offset);
void set_and_store_utc_offset(std::chrono::minutes tz_offset);

} // namespace cfg

Expand Down
5 changes: 4 additions & 1 deletion include/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
#include <utility> // pair<>

#include "net/address.hpp"
#include "time_utils.hpp"


namespace core {

std::pair<double, double> ntp_query(net::address address);
using time_utils::dbl_seconds;

std::pair<dbl_seconds, dbl_seconds> ntp_query(net::address address);

void run();

Expand Down
14 changes: 10 additions & 4 deletions include/ntp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
#include <cstdint>
#include <string>

#include "time_utils.hpp"


// For details, see https://www.ntp.org/reflib/rfc/rfc5905.txt

namespace ntp {
// For details, see https://www.ntp.org/reflib/rfc/rfc5905.txt

using time_utils::dbl_seconds;


// This is u32.32 fixed-point format, seconds since 1900-01-01 00:00:00 UTC
class timestamp {
Expand All @@ -22,9 +28,9 @@ namespace ntp {

timestamp(std::uint64_t v) = delete;

// Allow explicit conversions from/to double
explicit timestamp(double d) noexcept;
explicit operator double() const noexcept;
// Allow explicit conversions from/to dbl_seconds
explicit timestamp(dbl_seconds d) noexcept;
explicit operator dbl_seconds() const noexcept;

// Checks if timestamp is non-zero. Zero has a special meaning.
constexpr explicit operator bool() const noexcept { return stored; }
Expand Down
48 changes: 48 additions & 0 deletions include/time_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT

#ifndef TIME_UTILS_HPP
#define TIME_UTILS_HPP

#include <chrono>
#include <string>
#include <type_traits>


namespace time_utils {

// Type-safe way to pass seconds around as double
using dbl_seconds = std::chrono::duration<double>;


// Type trait to identify when a type is std::chrono::duration<>

template<typename T>
struct is_duration : std::false_type {};

template<typename R, typename P>
struct is_duration<std::chrono::duration<R, P>> : std::true_type {};

// convenience variable template
template<typename T>
constexpr bool is_duration_v = is_duration<T>::value;


template<typename T>
concept duration = is_duration_v<T>;


template<duration T>
std::string to_string(T t);



// Generate time duration strings for humans.
std::string seconds_to_human(dbl_seconds s, bool show_positive = false);


std::string tz_offset_to_string(std::chrono::minutes offset);

}


#endif
7 changes: 6 additions & 1 deletion include/utc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
#ifndef UTC_HPP
#define UTC_HPP

#include "time_utils.hpp"


namespace utc {

using time_utils::dbl_seconds;


// Seconds since 2000-01-01 00:00:00 UTC
struct timestamp {
double value;
dbl_seconds value;
};


Expand Down
8 changes: 0 additions & 8 deletions include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

namespace utils {

// Generate time duration strings for humans.
std::string seconds_to_human(double s, bool show_positive = false);


/**
* Split input string into tokens, according to separators.
*
Expand Down Expand Up @@ -46,10 +42,6 @@ namespace utils {
std::chrono::minutes>
fetch_timezone();


std::string tz_offset_to_string(std::chrono::minutes offset);


} // namespace utils

#endif
19 changes: 19 additions & 0 deletions include/wupsxx/duration_items.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT

#ifndef WUPSXX_DURATION_ITEMS_HPP
#define WUPSXX_DURATION_ITEMS_HPP

#include <chrono>

#include "numeric_item.hpp"


namespace wups::config {

using milliseconds_item = numeric_item<std::chrono::milliseconds>;

using seconds_item = numeric_item<std::chrono::seconds>;

} // namespace wups::config

#endif
47 changes: 2 additions & 45 deletions include/wupsxx/int_item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,12 @@

#include <memory>

#include "item.hpp"
#include "var_watch.hpp"
#include "numeric_item.hpp"


namespace wups::config {

class int_item : public item {

protected:

var_watch<int> variable;
const int default_value;
int min_value;
int max_value;
int fast_increment;
int slow_increment;

public:

int_item(const std::optional<std::string>& key,
const std::string& label,
int& variable, int default_value,
int min_value, int max_value,
int fast_increment = 10,
int slow_increment = 1);

static
std::unique_ptr<int_item>
create(const std::optional<std::string>& key,
const std::string& label,
int& variable, int default_value,
int min_value, int max_value,
int fast_increment = 10,
int slow_increment = 1);

virtual int get_display(char* buf, std::size_t size) const override;

virtual int get_selected_display(char* buf, std::size_t size) const override;

virtual void restore() override;

virtual void on_input(WUPSConfigSimplePadData input,
WUPS_CONFIG_SIMPLE_INPUT repeat) override;

private:

void on_changed();

};
using int_item = numeric_item<int>;

} // namespace wups::config

Expand Down
63 changes: 63 additions & 0 deletions include/wupsxx/numeric_item.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT

#ifndef WUPSXX_NUMERIC_ITEM_HPP
#define WUPSXX_NUMERIC_ITEM_HPP

#include <memory>

#include "item.hpp"
#include "var_watch.hpp"


namespace wups::config {


template<typename T>
class numeric_item : public item {

protected:

var_watch<T> variable;
const T default_value;
T min_value;
T max_value;
T fast_increment;
T slow_increment;

public:

numeric_item(const std::optional<std::string>& key,
const std::string& label,
T& variable, T default_value,
T min_value, T max_value,
T fast_increment = T{10},
T slow_increment = T{1});

static
std::unique_ptr<numeric_item>
create(const std::optional<std::string>& key,
const std::string& label,
T& variable, T default_value,
T min_value, T max_value,
T fast_increment = T{10},
T slow_increment = T{1});


virtual int get_display(char* buf, std::size_t size) const override;

virtual int get_selected_display(char* buf, std::size_t size) const override;

virtual void restore() override;

virtual void on_input(WUPSConfigSimplePadData input,
WUPS_CONFIG_SIMPLE_INPUT repeat) override;

private:

void on_changed();

};

} // namespace wups::config

#endif
20 changes: 20 additions & 0 deletions include/wupsxx/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <wups/storage.h>

#include "storage_error.hpp"
#include "../time_utils.hpp"


namespace wups::storage {
Expand All @@ -29,6 +30,17 @@ namespace wups::storage {
}


template<time_utils::duration T>
std::expected<T, storage_error>
load(const std::string& key)
{
auto value = load<int>(key);
if (!value)
return std::unexpected{value.error()};
return T{*value};
}


template<typename T>
void
store(const std::string& key, const T& value)
Expand All @@ -40,6 +52,14 @@ namespace wups::storage {
}


template<time_utils::duration T>
void
store(const std::string& key, const T& value)
{
return store<int>(key, value.count());
}


void save();

void reload();
Expand Down
Loading

0 comments on commit 92f6d85

Please sign in to comment.