api更新

This commit is contained in:
2026-08-07 20:20:46 +08:00
parent 3205dd84e8
commit caec91ae3f
13 changed files with 653 additions and 201 deletions
+29 -8
View File
@@ -57,7 +57,7 @@ struct structive::Type_Descriptor<Computed_Device> {
field<&Computed_Device::max_speed>(key<"max_speed">),
field<&Computed_Device::fixed_offset>(key<"fixed_offset">, read_only),
computed_property<Computed_Device, int>([](const auto& view) {
return view.template get<&Computed_Device::max_speed>() - view.template get<&Computed_Device::min_speed>() + view.template get<&Computed_Device::fixed_offset>();
return view.template get<"max_speed">() - view.template get<"min_speed">() + view.template get<"fixed_offset">();
}, key<"speed_span">)
);
}
@@ -128,16 +128,29 @@ template <class Object>
concept Can_Lock_Immutable_Unique = requires(Object& object) {
object.template lock_unique<&Device::immutable_id>();
};
template <class Object>
concept Can_Write_Immutable_By_Key = requires(Object& object) {
object.template write<"immutable_id">(1);
};
using Device_Write_Guard = decltype(std::declval<Device&>().template lock_unique<&Device::temperature>());
template <class Guard>
concept Can_Guard_Set_Immutable = requires(Guard& guard) {
guard.template set<&Device::immutable_id>(1);
};
template <class Guard>
concept Can_Guard_Set_Immutable_By_Key = requires(Guard& guard) {
guard.template set<"immutable_id">(1);
};
static bool update_speed_range(Device& device, int min_speed, int max_speed) {
auto guard = device.lock_unique<&Device::min_speed, &Device::max_speed>();
int old_min = guard.get<&Device::min_speed>();
int old_min = guard.get<"minimum_speed">();
int old_max = guard.get<&Device::max_speed>();
guard.set<&Device::min_speed>(min_speed);
guard.set<"minimum_speed">(min_speed);
guard.set<&Device::max_speed>(max_speed);
if (min_speed <= max_speed) {
return true;
}
guard.set<&Device::min_speed>(old_min);
guard.set<"minimum_speed">(old_min);
guard.set<&Device::max_speed>(old_max);
return false;
}
@@ -148,7 +161,12 @@ static void runtime_read_int(void* context, std::size_t, std::string_view, const
int main() {
static_assert(Property_Described_Object<Device>);
static_assert(!Can_Write_Immutable<Device>);
static_assert(!Can_Write_Immutable_By_Key<Device>);
static_assert(!Can_Lock_Immutable_Unique<Device>);
static_assert(!Can_Guard_Set_Immutable<Device_Write_Guard>);
static_assert(!Can_Guard_Set_Immutable_By_Key<Device_Write_Guard>);
static_assert(requires(const Device& value) { value.template read<"temperature">(); });
static_assert(requires(Device& value) { value.template write<"temperature">(1); });
const auto& schema = type_descriptor<Device>();
using Schema = type_descriptor_schema_t<Device>;
static_assert(Valid_Property_Schema<Schema>);
@@ -171,6 +189,9 @@ int main() {
REQUIRE(device.read<&Device::temperature>() == 21);
device.write<&Device::temperature>(22);
REQUIRE(device.temperature == 22);
REQUIRE(device.read<"temperature">() == 22);
device.write<"temperature">(23);
REQUIRE(device.temperature == 23);
REQUIRE(device.read<&Device::immutable_id>() == 7);
REQUIRE(&device.unsafe_object() == &device);
REQUIRE(&device.schema() == &schema);
@@ -212,13 +233,13 @@ int main() {
REQUIRE(value_visits == 5);
Computed_Device computed;
REQUIRE(computed.lock_slot<&Computed_Device::fixed_offset>() == Resolved_Synchronization_View::unsynchronized_slot);
REQUIRE(computed.read_key<"speed_span">() == 95);
REQUIRE(computed.read<"speed_span">() == 95);
computed.write<&Computed_Device::min_speed>(20);
REQUIRE(computed.read_key<"speed_span">() == 85);
REQUIRE(computed.read<"speed_span">() == 85);
Lockless_Device lockless;
REQUIRE(lockless.read<&Lockless_Device::left>() == 1);
lockless.write<&Lockless_Device::right>(4);
REQUIRE(lockless.read_key<"sum">() == 5);
REQUIRE(lockless.read<"sum">() == 5);
auto lockless_guard = lockless.lock_shared<&Lockless_Device::left, &Lockless_Device::right>();
REQUIRE(lockless_guard.get<&Lockless_Device::left>() == 1);
REQUIRE(lockless_guard.get<&Lockless_Device::right>() == 4);
@@ -234,7 +255,7 @@ int main() {
REQUIRE(erased.runtime_property_count() == 5);
int runtime_value = 0;
REQUIRE(erased.runtime_read("temperature", &runtime_value, &runtime_read_int) == Runtime_Access_Result::ok);
REQUIRE(runtime_value == 22);
REQUIRE(runtime_value == 23);
REQUIRE(erased.runtime_read("immutable_id", &runtime_value, &runtime_read_int) == Runtime_Access_Result::ok);
REQUIRE(runtime_value == 7);
int runtime_write_value = 35;