Skip to content

Commit ce8c400

Browse files
committed
object: add property APIs
1 parent d5bd149 commit ce8c400

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

src/api/wayfire/object.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define OBJECT_HPP
33

44
#include <typeinfo>
5+
#include <optional>
56
#include <memory>
67
#include <string>
78
#include <cstdint>
@@ -24,6 +25,13 @@ class custom_data_t
2425
custom_data_t& operator =(const custom_data_t& other) = default;
2526
};
2627

28+
template<class T>
29+
class property_data_t : public custom_data_t
30+
{
31+
public:
32+
T value;
33+
};
34+
2735
/**
2836
* A base class for "objects". Objects provide signals and ways for plugins to
2937
* store custom data about the object.
@@ -114,6 +122,44 @@ class object_base_t
114122

115123
virtual ~object_base_t();
116124

125+
bool has_property(std::string name)
126+
{
127+
return has_data(name);
128+
}
129+
130+
template<class T>
131+
std::optional<T> get_property(std::string name)
132+
{
133+
auto data = get_data<property_data_t<T>>(name);
134+
if (!data)
135+
{
136+
return {};
137+
}
138+
139+
return data->value;
140+
}
141+
142+
template<class T>
143+
void set_property(std::string name, T value)
144+
{
145+
auto data = get_data<property_data_t<T>>(name);
146+
if (has_data(name) && !data)
147+
{
148+
_warn_wrong_type(name);
149+
return;
150+
}
151+
152+
if (!data)
153+
{
154+
auto prop = std::make_unique<property_data_t<T>>();
155+
prop->value = value;
156+
store_data<property_data_t<T>>(std::move(prop), name);
157+
} else
158+
{
159+
data->value = value;
160+
}
161+
}
162+
117163
object_base_t(const object_base_t &) = delete;
118164
object_base_t(object_base_t &&) = delete;
119165
object_base_t& operator =(const object_base_t&) = delete;
@@ -135,6 +181,8 @@ class object_base_t
135181
/** Store the given data under the given name */
136182
void _store_data(std::unique_ptr<custom_data_t> data, std::string name);
137183

184+
void _warn_wrong_type(std::string name);
185+
138186
class obase_impl;
139187
std::unique_ptr<obase_impl> obase_priv;
140188
};

src/api/wayfire/plugin.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ using wayfire_plugin_load_func = wf::plugin_interface_t * (*)();
107107
/**
108108
* The version is defined as macro as well, to allow conditional compilation.
109109
*/
110-
#define WAYFIRE_API_ABI_VERSION_MACRO 2025'09'09
110+
#define WAYFIRE_API_ABI_VERSION_MACRO 2025'09'11
111111

112112
/**
113113
* The version of Wayfire's API/ABI

src/core/object.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <unordered_map>
33
#include <wayfire/signal-provider.hpp>
44
#include <wayfire/nonstd/safe-list.hpp>
5+
#include <wayfire/util/log.hpp>
56

67
struct wf::signal::provider_t::impl
78
{
@@ -139,3 +140,9 @@ void wf::object_base_t::_clear_data()
139140
erase_data(key);
140141
}
141142
}
143+
144+
void wf::object_base_t::_warn_wrong_type(std::string name)
145+
{
146+
LOGW("Tried to access data with name '", name, "' using the wrong type. Actual type: ",
147+
typeid(_fetch_data(name)).name());
148+
}

0 commit comments

Comments
 (0)