Skip to content

Commit

Permalink
Implemented numeric_item template class, switched to type-safer code …
Browse files Browse the repository at this point in the history
…for passing seconds around.
  • Loading branch information
Daniel K. O. (dkosmari) committed Jun 2, 2024
1 parent eda3a25 commit 30f9d3b
Show file tree
Hide file tree
Showing 27 changed files with 540 additions and 553 deletions.
7 changes: 3 additions & 4 deletions include/cfg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace cfg {

namespace defaults {
extern const bool auto_tz;
extern const int msg_duration;
extern const std::chrono::seconds msg_duration;
extern const int notify;
extern const std::string server;
extern const bool sync;
Expand All @@ -44,7 +44,7 @@ namespace cfg {


extern bool auto_tz;
extern int msg_duration;
extern std::chrono::seconds msg_duration;
extern int notify;
extern std::string server;
extern bool sync;
Expand All @@ -58,8 +58,7 @@ namespace cfg {
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
61 changes: 0 additions & 61 deletions include/wupsxx/duration_item.hpp

This file was deleted.

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
Loading

0 comments on commit 30f9d3b

Please sign in to comment.