C# has a nice feature called class/struct property. It is a public data member but with the ability to control read/write access, underlying storage or to compute the value on the fly. You can think of it as a glorified getter/setter mechanism: a syntax sugar around get/set methods.
My idea to implement something like this in C++ started few years ago with a code review: a bunch of classes had public data members and no control over who and how they were accessed or modified. I thought to myself: there must be a better way to code it that would appear like public data but, under the hood, gave the author complete control over the variable.
My initial set of requirements was as follows:
- Access and modification syntax must be indistinguishable from a regular member variable.
- Access and modification seamlessly routed through user defined get/set methods.
- The following condition is true: sizeof(T) == sizeof(property<T>).
- Ability to subscribe to modification events.
- Ability to bypass the custom get/set methods by explicit syntax only (explicit function call or type cast to the underlying type).
- Works with primitive and complex types: classes with their own methods defined.
- Works with arithmetic and bitwise operators.
- Works with STL containers and smart pointers.
I don’t want to go over the implementation line by line, it’s almost 700 lines of code and growing. But it is useable enough that I feel comfortable sharing it. Here are the language mechanisms I used to make it work:
- property<T> is a class template that uses policy-based design to specify the get/set control mechanism.
- The property’s policy class allows for full control over the underlying storage: T can be a member variable or anything else capable of storing values: a file, Windows registry, web resource, etc. Currently member variable and file storage are provided but I’m happy to accept other implementations if you feel like contributing!
- property<T> relies on concepts heavily to enable or disable member methods and operators.
- Helper methods are provided such as make_property, strip (gain unrestricted access to the underlying type), and as_volatile.
- Few template type deduction guides are provided such that creating a property<const char*> results in a property<std::string> and more.
- Macros help define a whole bunch of member operators as well as standalone unary and binary ones.
P.S. Today’s blog post brought to you by my friend’s one and only NoSpam Android App! Tired of unwanted calls blowing up your phone day and night?! Tired of picking up numbers you don’t recognize and hearing nothing but static?! Don’t wait! Install today and start sending spammers and scammers to /dev/null
Complete source code:
property.hpp | property.cpp
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
#include <iostream> #include <map> #include <memory> #include <stdexcept> #include <string> #include <vector> #include "property.hpp" void foo(std::string& s) {} void foo(const std::string& s) {} void bar(std::string& s) {} auto qaz(const property<std::string>& prop) { auto tmp = property<std::string>{ prop }; return tmp; } int main() { using namespace std::string_literals; // UPDATE PROCS: called when property is modified... auto update_proc1 = [](const property<std::string>& p, void* ctx) { std::cout << &p << " / " << ctx << " updated with " << p << std::endl; }; auto update_proc2 = [](const property<int>& p, void* ctx) { std::cout << &p << " / " << ctx << " updated with " << p << std::endl; }; auto update_proc3 = [](const property<float>& p, void* ctx) { std::cout << &p << " / " << ctx << " updated with " << p << std::endl; }; auto p1 = make_property<std::string>("C++11"); // property<std::string>{ "C++11" }; auto p2 = make_property(p1); // property<std::string>{ p1 }; auto p3 = make_property(std::move(p2)); // std::move(p2); p2 = qaz("C++11"); p2 = qaz("C++11"s); auto s1 = "C++14"s; auto s2 = "C++17"s; auto p4 = make_property(s1); // property<std::string>{ s1 }; auto p5 = make_property(std::move(s2)); // property<std::string>{ std::move(s2) }; auto p6 = property<std::string>{ "C++20"s }; auto p7 = property<std::string>{ (const std::string&)std::string{ "C++23" } }; p1.set_update_proc(update_proc1, (void*)0x11111111); p2.set_update_proc(update_proc1, (void*)0x22222222); p3.set_update_proc(update_proc1, (void*)0x33333333); p4.set_update_proc(update_proc1, (void*)0x44444444); p1 = p6; p2 = std::move(p7); p3 = s1; p4 = std::move(s1); foo(p3); // OK! implicit user-defined type conversion returns CONST reference! foo(std::as_const(p3)); foo(as_volatile(p3)); foo(strip(std::as_const(p3))); foo(strip(as_volatile(p3))); foo(*p3); // WARNING! explicit call to operator* returns NON-const reference! foo(*std::as_const(p3)); foo(*as_volatile(p3)); foo(p4.get()); // WARNING! explicit call to get() returns NON-const reference! foo(std::as_const(p4).get()); foo(as_volatile(p4).get()); //bar(p4); // ERROR! implicit conversion to non-const reference is forbiden! bar(p4.get()); // OK! explicit call to get() to convert to non-const reference is allowed! bar(*p4); // OK! explicit call to operator* to convert to non-const reference is allowed! bar(strip(p4)); // OK! explicit call to strip is allowed! bar(static_cast<std::string&>(p4)); // OK! explicit conversion to non-const reference is allowed! std::string s3 = p3; std::string s4 = std::move(p3); [[maybe_unused]] auto tmp1 = p1->size(); [[maybe_unused]] auto tmp2 = std::as_const(p2)->length(); [[maybe_unused]] auto tmp3 = as_volatile(p3)->c_str(); tmp1 = p1.invoke(&std::string::size); tmp2 = std::as_const(p2).invoke(&std::string::length); tmp3 = as_volatile(p3).invoke(&std::string::c_str); using Str = std::string; void(Str::*mptr1)(Str::size_type, Str::value_type) = static_cast<void(Str::*)(Str::size_type, Str::value_type)>(&Str::resize); p1.invoke(mptr1, p1->size() + 3, '-'); p2.invoke(mptr1, p2->size() + 5, '*'); Str::iterator(Str::*mptr2)(Str::const_iterator, Str::size_type, Str::value_type) = static_cast<Str::iterator(Str::*)(Str::const_iterator, Str::size_type, Str::value_type)>(&Str::insert); p3.invoke(mptr2, p3->end(), 7, '='); std::cout << p1 << std::endl; std::cout << p2 << std::endl; std::cout << p3 << std::endl; [[maybe_unused]] auto r1 = p1 == p2; [[maybe_unused]] auto r2 = p1 == p1; [[maybe_unused]] auto r3 = p1 == "C++20"; [[maybe_unused]] auto r4 = "C++20" == p1; [[maybe_unused]] auto r5 = p1 == "C++20"s; [[maybe_unused]] auto r6 = "C++20"s == p1; auto p8 = make_property(1); // property<int>{ 1 }; auto p8_2 = make_property<float>(1); // property<float>{ 1 }; auto p9 = make_property(1.f); // property<float>{ 1.f }; auto p9_2 = make_property<int>(1.f); // property<int>{ 1.f }; p8.set_update_proc(update_proc2, (void*)0x88888888); p9.set_update_proc(update_proc3, (void*)0x99999999); //std::cin >> p8; //std::wcin >> p9; p8 = 2; p8 = 2.f; p9 = 3; p9 = 3.f; p8 = p9; p9 = p8; p8 = (int)p9; //p9 = p9; p8 += p8; p9 += p8; p8 += p9; p8 += 1; [[maybe_unused]] auto r7 = p8 < p9; [[maybe_unused]] auto r8 = p8 == 2; [[maybe_unused]] auto r9 = 3 == p9; [[maybe_unused]] auto r10 = p1 == p2; [[maybe_unused]] auto r11 = p1 > p2; auto p10 = +p8; auto p11 = -p9; auto p12 = p8 + p8; auto p13 = p8 - p9; auto p14 = p8 * p9; auto p15 = p8 / p9; auto p16 = p8 % p8; auto p17 = ~p8; auto p19 = p8 & p8; auto p20 = p8 | p8; auto p21 = p8 ^ p8; auto p22 = p8 << p8; auto p23 = p8 >> p8; auto p24 = 10 + p10; auto p25 = p10 + 10; auto v1 = property<std::vector<int>>{ 1, 2, 3, 4, 5 }; auto v2 = make_property(std::vector<int>{ 1, 2, 3, 4, 5 }); [[maybe_unused]] auto tmp4 = v1->size(); [[maybe_unused]] auto i1 = v1[0]; [[maybe_unused]] auto i2 = std::as_const(v1)[0]; [[maybe_unused]] auto i3 = as_volatile(v1)[0]; [[maybe_unused]] auto it1 = v1.begin(); [[maybe_unused]] decltype(auto) it2 = v2.begin(); v1[0] = 11; v1[1] = 14; v1[2] = 17; v1[3] = 20; v1[4] = 23; for (auto i : v1) { std::cout << i << std::endl; } auto m1 = property<std::map<int, int>>(std::map<int, int>{ { 1, 1 }, { 2, 2 }, { 3, 3 } }); auto m2 = make_property(std::map<int, int>{ { 1, 1 }, { 2, 2 }, { 3, 3 } }); m1[1] = 11; m1[2] = 22; m1[3] = 33; for (auto i : m1) { std::cout << i.first << " -> " << i.second << std::endl; } auto ptr1 = make_property(std::make_shared<std::string>("C++23")); auto ptr2 = make_property(std::make_unique<std::string>("C++26")); auto ptr3 = make_property(std::make_shared<std::string>("C++1998")); auto ptr4 = make_property(std::make_unique<std::string>("C++2001")); auto ptr5 = make_property(std::make_shared<int[]>(3)); auto ptr6 = make_property(std::make_shared<int[]>(3)); ptr5[0] = ptr6[0] = 111; ptr5[1] = ptr6[1] = 222; ptr5[2] = ptr6[2] = 333; strip(ptr3).reset(); delete strip(ptr4).release(); ptr1.invoke(&std::shared_ptr<std::string>::get); ptr2.invoke(&std::unique_ptr<std::string>::reset, new std::string("Yey!")); strip(std::as_const(ptr3)).use_count(); strip(as_volatile(ptr4)).get_deleter(); strip(ptr5)[2] = strip(ptr6)[2] = -1; ptr1->length(); (*ptr1).length(); ptr2->size(); (*ptr2).size(); if (ptr1) std::cout << "not null" << std::endl; if (ptr2) std::cout << "not null" << std::endl; if (!ptr3) std::cout << "null" << std::endl; if (!ptr4) std::cout << "null" << std::endl; for (auto i = 0; i < 3; ++i) std::cout << ptr5[i] << ", " << ptr6[i] << std::endl; struct S { int x, y, z; }; struct U : S {}; auto ss = S{ 1, 2, 3 }; auto uu = U{ 4, 5, 6 }; auto ps1 = make_property<S>(1, 2, 3); auto ps2 = make_property(ss); auto ps3 = make_property(S{ 4, 5, 6 }); auto ps5 = make_property<S>(uu); auto ps6 = make_property<S>(U{ 4, 5, 6 }); auto ps8 = property<std::string>(10, '!'); auto ps9 = make_property<std::string>(10, '!'); struct SS { ~SS() { std::cout << "~SS\n"; } }; auto dg1 = property{ "C++17" }; // property<std::string> auto dg2 = property{ L"C++17" }; // property<std::wstring> auto dg3 = make_property("C++17"); // property<std::string> auto dg4 = make_property(L"C++17"); // property<std::wstring> auto dg5 = property{ new int{ 17 } }; // property<std::unique_ptr<int>> auto dg6 = property{ new SS }; // property<std::unique_ptr<SS>> auto dg7 = make_property(new int{ 17 }); // property<std::unique_ptr<int>> auto dg8 = make_property(new SS); // property<std::unique_ptr<SS>> auto dg9 = property{ v1.begin(), v1.end() }; // property<std::vector<int>> auto dg10 = property{ m1.begin(), m1.end() }; // property<std::vector<std::pair<int, int>>> auto dg11 = make_property(v1.begin(), v1.end()); // property<std::vector<int>> auto dg12 = make_property(m1.begin(), m1.end()); // property<std::vector<std::pair<int, int>>> if (&dg1 != &*dg1) throw std::logic_error("Bad pointer!"); if (&dg2 != &*dg2) throw std::logic_error("Bad pointer!"); if (&dg3 != &dg3.get()) throw std::logic_error("Bad pointer!"); if (&dg4 != &dg4.get()) throw std::logic_error("Bad pointer!"); if (&dg5 != dg5.get().get()) throw std::logic_error("Bad pointer!"); if (&dg6 != dg6.get().get()) throw std::logic_error("Bad pointer!"); if (&dg7 != dg7.get().get()) throw std::logic_error("Bad pointer!"); if (&dg8 != dg8.get().get()) throw std::logic_error("Bad pointer!"); strip(dg6).reset(); strip(dg8).reset(); auto fsp1 = fs_property<int>(std::filesystem::path("./fsp1.bin")); auto fsp2 = fs_property<int>(std::filesystem::path("./fsp2.bin"), 17); auto fsp3 = fs_property<float>(std::filesystem::path("./fsp3.bin"), 20.0f); auto fsp4 = fs_property<double>(std::filesystem::path("./fsp4.bin"), 23.0); auto fsp5 = fs_property<std::string>(std::filesystem::path("./fsp5.txt"), "I LUV C++\nI LUV C++"); auto fsp6 = fs_property<std::wstring>(std::filesystem::path("./fsp6.txt"), L"I LUV C++ Too\nI LUV C++ Too\nI LUV C++ Too"); fsp1 = 11; fsp2 = 14; fsp3 = 3.141f; fsp4 = 1720.11; std::cout << "\n" << fsp1 << ", " << fsp2 << ", " << fsp3 << ", " << fsp4 << "\n"; std::cout << fsp5 << "\n"; std::wcout << fsp6 << L"\n"; std::cout << "\nTHE END!\n"; } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
#pragma once #include <concepts> #include <filesystem> #include <fstream> #include <functional> #include <initializer_list> #include <ios> #include <istream> #include <iterator> #include <memory> #include <ostream> #include <string> #include <type_traits> #include <unordered_map> #include <utility> #include <vector> template<typename PT> concept std_smart_pointer = requires (PT p) { typename PT::element_type; { static_cast<bool>(p) }; } and std::disjunction_v< std::is_same<PT, std::shared_ptr<typename PT::element_type>>, std::is_same<PT, std::unique_ptr<typename PT::element_type>>, std::is_same<PT, std::shared_ptr<typename PT::element_type[]>>, std::is_same<PT, std::unique_ptr<typename PT::element_type[]>>>; template<typename CT> concept std_container = requires(CT a, const CT b) { requires std::regular<CT>; requires std::swappable<CT>; requires std::destructible<typename CT::value_type>; requires std::same_as<typename CT::reference, typename CT::value_type&>; requires std::same_as<typename CT::const_reference, const typename CT::value_type&>; requires std::forward_iterator<typename CT::iterator>; requires std::forward_iterator<typename CT::const_iterator>; requires std::signed_integral<typename CT::difference_type>; requires std::same_as<typename CT::difference_type, typename std::iterator_traits<typename CT::iterator>::difference_type>; requires std::same_as<typename CT::difference_type, typename std::iterator_traits<typename CT::const_iterator>::difference_type>; { a.begin() } -> std::same_as<typename CT::iterator>; { a.end() } -> std::same_as<typename CT::iterator>; { b.begin() } -> std::same_as<typename CT::const_iterator>; { b.end() } -> std::same_as<typename CT::const_iterator>; { a.cbegin() } -> std::same_as<typename CT::const_iterator>; { a.cend() } -> std::same_as<typename CT::const_iterator>; { a.size() } -> std::same_as<typename CT::size_type>; { a.max_size() } -> std::same_as<typename CT::size_type>; { a.empty() } -> std::same_as<bool>; }; template<typename T> concept non_pointer = (not std::is_pointer_v<T>); template<typename T> concept non_reference = (not std::is_reference_v<T>); template<typename T> concept basic_property_type = ((non_pointer<T> and non_reference<T>) or std_smart_pointer<T> or std_container<T>); template<basic_property_type T> struct basic_property_policy { using value_type = T; using reference = value_type&; using const_reference = const value_type&; using rvalue_reference = value_type&&; using pointer = value_type*; using const_pointer = const value_type*; basic_property_policy() = default; basic_property_policy(const basic_property_policy& other) : m_value(other.get()) {} basic_property_policy(basic_property_policy&& other) noexcept : m_value(std::move(other.get())) {} ~basic_property_policy() noexcept = default; basic_property_policy(const_reference value) : m_value(value) {} basic_property_policy(rvalue_reference value) : m_value(std::move(value)) {} template<typename U> requires (std::convertible_to<U, T>) basic_property_policy(U&& value) : m_value(static_cast<T>(std::forward<U>(value))) {} template<typename... Args> requires (std::constructible_from<T, Args...>) basic_property_policy(Args&&... args) : m_value(std::forward<Args>(args)...) {} template<typename U> requires (std::constructible_from<T, std::initializer_list<U>>) basic_property_policy(std::initializer_list<U> l) : m_value(l) {} [[nodiscard]] reference get() { return m_value; } [[nodiscard]] const_reference get() const { return m_value; } [[nodiscard]] const_reference get() const volatile { return const_cast<const_reference>(m_value); } void set(const_reference value) { m_value = value; } void set(rvalue_reference value) noexcept { m_value = std::move(value); } private: T m_value; }; template<basic_property_type T> struct file_storage_property_policy { using value_type = T; using reference = value_type&; using const_reference = const value_type&; using rvalue_reference = value_type&&; using pointer = value_type*; using const_pointer = const value_type*; explicit file_storage_property_policy(const std::filesystem::path& path) : m_path(path) {} file_storage_property_policy(const std::filesystem::path& path, const_reference value) : m_path(path) { set(value); } template<typename U = T> requires (not std::same_as<U, std::string> and not std::same_as<U, std::wstring>) [[nodiscard]] value_type get() const { using in_char_t = typename std::ifstream::char_type; auto value = T{}; auto ifs = std::ifstream(m_path, std::ios_base::binary | std::ios_base::in); ifs.exceptions(std::ios::failbit | std::ios::badbit); ifs.read(reinterpret_cast<in_char_t*>(&value), sizeof(value)); ifs.close(); return value; } template<typename U = T> requires (std::same_as<U, std::string> or std::same_as<U, std::wstring>) [[nodiscard]] value_type get() const { using in_char_t = typename std::ifstream::char_type; auto value = U{}; auto ifs = std::ifstream(m_path, std::ios_base::binary | std::ios_base::in | std::ios::ate); ifs.exceptions(std::ios::failbit | std::ios::badbit); auto bytes = ifs.tellg(); value.resize(bytes / sizeof(typename U::value_type)); ifs.seekg(0); ifs.read(reinterpret_cast<in_char_t*>(value.data()), bytes); ifs.close(); return value; } template<typename U = T> requires (not std::same_as<U, std::string> and not std::same_as<U, std::wstring>) void set(const_reference value) { using out_char_t = typename std::ofstream::char_type; auto ofs = std::ofstream(m_path, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc); ofs.exceptions(std::ios::failbit | std::ios::badbit); ofs.write(reinterpret_cast<const out_char_t*>(&value), sizeof(value)); ofs.flush(); ofs.close(); } template<typename U = T> requires (std::same_as<U, std::string> or std::same_as<U, std::wstring>) void set(const_reference value) { using out_char_t = typename std::ofstream::char_type; auto ofs = std::ofstream(m_path, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc); ofs.exceptions(std::ios::failbit | std::ios::badbit); ofs.write(reinterpret_cast<const out_char_t*>(value.data()), value.length() * sizeof(typename U::value_type)); ofs.flush(); ofs.close(); } private: std::filesystem::path m_path; }; template<basic_property_type T, typename P> class basic_property : private P { public: using typename P::value_type; using typename P::reference; using typename P::const_reference; using typename P::rvalue_reference; using typename P::pointer; using typename P::const_pointer; using P::P; using P::get; basic_property() = default; basic_property(const basic_property&) = default; basic_property(basic_property&&) noexcept = default; ~basic_property() noexcept { clear_update_proc(); } basic_property(const_reference value) : P(value) {} basic_property(rvalue_reference value) : P(std::move(value)) {} template<typename U> requires (std::convertible_to<U, T>) basic_property(U&& value) : P(std::forward<U>(value)) {} template<typename... Args> requires (std::constructible_from<T, Args...>) basic_property(Args&&... args) : P(std::forward<Args>(args)...) {} template<typename U> requires (std::constructible_from<T, std::initializer_list<U>>) basic_property(std::initializer_list<U> l) : P(l) {} basic_property& operator = (const basic_property& other) { P::set(other.get()); dispatch_update(); return *this; } basic_property& operator = (basic_property&& other) noexcept { P::set(std::move(other.get())); dispatch_update(); return *this; } basic_property& operator = (const_reference value) { P::set(value); dispatch_update(); return *this; } basic_property& operator = (rvalue_reference value) noexcept { P::set(std::move(value)); dispatch_update(); return *this; } template<typename U> requires (std::constructible_from<T, U> and not std::same_as<T, std::remove_cvref_t<U>> and not std::same_as<basic_property, std::remove_cvref_t<U>>) basic_property& operator = (U&& value) { P::set(std::forward<U>(value)); dispatch_update(); return *this; } [[nodiscard]] auto operator <=> (const basic_property& other) const { return P::get() <=> other.get(); } [[nodiscard]] bool operator == (const basic_property& other) const { return P::get() == other.get(); } template<typename U> requires (not std::same_as<basic_property, std::remove_cvref_t<U>>) [[nodiscard]] auto operator <=> (const U& other) const { return P::get() <=> other; } template<typename U> requires (not std::same_as<basic_property, std::remove_cvref_t<U>>) [[nodiscard]] bool operator == (const U& other) const { return P::get() == other; } #if defined(DEFINE_PROPERTY_OPERATOR) #undef DEFINE_PROPERTY_OPERATOR #endif #define DEFINE_PROPERTY_OPERATOR(OP) \ basic_property& operator OP (const basic_property& other) \ { \ P::get() OP other.get(); \ dispatch_update(); \ return *this; \ } \ \ template<typename U> requires (not std::same_as<basic_property, std::remove_cvref_t<U>>) \ basic_property& operator OP (const U& value) \ { \ P::get() OP value; \ dispatch_update(); \ return *this; \ } DEFINE_PROPERTY_OPERATOR(+=); DEFINE_PROPERTY_OPERATOR(-=); DEFINE_PROPERTY_OPERATOR(*=); DEFINE_PROPERTY_OPERATOR(/=); DEFINE_PROPERTY_OPERATOR(%=); DEFINE_PROPERTY_OPERATOR(&=); DEFINE_PROPERTY_OPERATOR(|=); DEFINE_PROPERTY_OPERATOR(^=); DEFINE_PROPERTY_OPERATOR(<<=); DEFINE_PROPERTY_OPERATOR(>>=); #undef DEFINE_PROPERTY_OPERATOR explicit operator reference () { return P::get(); } operator const_reference () const { return P::get(); } operator const_reference () const volatile { return P::get(); } template<typename PT = T> requires (not std_smart_pointer<PT>) [[nodiscard]] pointer operator & () { return &P::get(); } template<typename PT = T> requires (not std_smart_pointer<PT>) [[nodiscard]] const_pointer operator & () const { return &P::get(); } template<typename PT = T> requires (not std_smart_pointer<PT>) [[nodiscard]] const_pointer operator & () const volatile { return &P::get(); } template<typename PT = T> requires (not std_smart_pointer<PT>) [[nodiscard]] reference operator * () { return P::get(); } template<typename PT = T> requires (not std_smart_pointer<PT>) [[nodiscard]] const_reference operator * () const { return P::get(); } template<typename PT = T> requires (not std_smart_pointer<PT>) [[nodiscard]] const_reference operator * () const volatile { return P::get(); } template<typename PT = T> requires (not std_smart_pointer<PT>) pointer operator -> () { return &P::get(); } template<typename PT = T> requires (not std_smart_pointer<PT>) const_pointer operator -> () const { return &P::get(); } template<typename PT = T> requires (not std_smart_pointer<PT>) const_pointer operator -> () const volatile { return &P::get(); } template<typename PT = T> requires (std_smart_pointer<PT>) explicit operator bool() const noexcept { return static_cast<bool>(P::get()); } template<typename PT = T> requires (std_smart_pointer<PT>) [[nodiscard]] PT::element_type* operator & () { return P::get().get(); } template<typename PT = T> requires (std_smart_pointer<PT>) [[nodiscard]] const PT::element_type* operator & () const { return P::get().get(); } template<typename PT = T> requires (std_smart_pointer<PT>) [[nodiscard]] const PT::element_type* operator & () const volatile { return P::get().get(); } template<typename PT = T> requires (std_smart_pointer<PT>) [[nodiscard]] PT::element_type& operator * () { return *P::get(); } template<typename PT = T> requires (std_smart_pointer<PT>) [[nodiscard]] const PT::element_type& operator * () const { return *P::get(); } template<typename PT = T> requires (std_smart_pointer<PT>) [[nodiscard]] const PT::element_type& operator * () const volatile { return *P::get(); } template<typename PT = T> requires (std_smart_pointer<PT>) reference operator -> () { return P::get(); } template<typename PT = T> requires (std_smart_pointer<PT>) const_reference operator -> () const { return P::get(); } template<typename PT = T> requires (std_smart_pointer<PT>) const_reference operator -> () const volatile { return P::get(); } template<typename U> requires (std_smart_pointer<T> or std_container<T>) [[nodiscard]] decltype(auto) operator [] (U&& index) { return P::get()[std::forward<U>(index)]; } template<typename U> requires (std_smart_pointer<T> or std_container<T>) [[nodiscard]] decltype(auto) operator [] (U&& index) const { return P::get()[std::forward<U>(index)]; } template<typename U> requires (std_smart_pointer<T> or std_container<T>) [[nodiscard]] decltype(auto) operator [] (U&& index) const volatile { return P::get()[std::forward<U>(index)]; } template<typename C = T> requires (std_container<C>) [[nodiscard]] decltype(auto) begin() { return P::get().begin(); } template<typename C = T> requires (std_container<C>) [[nodiscard]] decltype(auto) end() { return P::get().end(); } template<typename C = T> requires (std_container<C>) [[nodiscard]] decltype(auto) begin() const { return P::get().begin(); } template<typename C = T> requires (std_container<C>) [[nodiscard]] decltype(auto) end() const { return P::get().end(); } template<typename C = T> requires (std_container<C>) [[nodiscard]] decltype(auto) cbegin() { return P::get().cbegin(); } template<typename C = T> requires (std_container<C>) [[nodiscard]] decltype(auto) cend() { return P::get().cend(); } template<typename Func, typename... Args> requires (std::invocable<Func, T, Args...>) auto invoke(Func&& func, Args&&... args) -> std::invoke_result_t<Func, T, Args...> { return std::invoke(std::forward<Func>(func), P::get(), std::forward<Args>(args)...); } template<typename Func, typename... Args> requires (std::invocable<Func, T, Args...>) auto invoke(Func&& func, Args&&... args) const -> std::invoke_result_t<Func, T, Args...> { return std::invoke(std::forward<Func>(func), P::get(), std::forward<Args>(args)...); } template<typename Func, typename... Args> requires (std::invocable<Func, T, Args...>) auto invoke(Func&& func, Args&&... args) const volatile -> std::invoke_result_t<Func, T, Args...> { return std::invoke(std::forward<Func>(func), P::get(), std::forward<Args>(args)...); } template<typename Proc> requires (std::invocable<Proc, basic_property, void*>) void set_update_proc(Proc&& proc, void* ctx) { k_update_proc_map.insert_or_assign(this, std::make_pair(std::forward<Proc>(proc), ctx)); } void clear_update_proc() noexcept { k_update_proc_map.erase(this); } friend std::ostream& operator << (std::ostream& output, const basic_property& prop) { output << prop.get(); return output; } friend std::wostream& operator << (std::wostream& output, const basic_property& prop) { output << prop.get(); return output; } friend std::istream& operator >> (std::istream& input, basic_property& prop) { input >> prop.get(); prop.dispatch_update(); return input; } friend std::wistream& operator >> (std::wistream& input, basic_property& prop) { input >> prop.get(); prop.dispatch_update(); return input; } private: using update_proc_t = std::function<void(const basic_property&, void*)>; using update_proc_map_key_t = void*; using update_proc_map_val_t = std::pair<update_proc_t, void*>; using update_proc_map_t = std::unordered_map<update_proc_map_key_t, update_proc_map_val_t>; inline static auto k_update_proc_map = update_proc_map_t{}; void dispatch_update() noexcept { if (auto it = k_update_proc_map.find(this); it != std::end(k_update_proc_map)) { auto& proc = it->second.first; auto& ctx = it->second.second; proc(*this, ctx); } } }; basic_property(const char*) -> basic_property<std::string, basic_property_policy<std::string>>; basic_property(const wchar_t*) -> basic_property<std::wstring, basic_property_policy<std::wstring>>; template<typename T> basic_property(T*) -> basic_property<std::unique_ptr<T>, basic_property_policy<std::unique_ptr<T>>>; template<typename Iterator> basic_property(Iterator, Iterator) -> basic_property<std::vector<typename std::iterator_traits<Iterator>::value_type>, basic_property_policy<std::vector<typename std::iterator_traits<Iterator>::value_type>>>; template<basic_property_type T, typename P = basic_property_policy<T>> [[nodiscard]] auto make_property(const basic_property<T, P>& prop) { using U = std::remove_cvref_t<T>; return basic_property<U, P>(prop); } template<basic_property_type T, typename P = basic_property_policy<T>> [[nodiscard]] auto make_property(basic_property<T, P>&& prop) { using U = std::remove_cvref_t<T>; return basic_property<U, P>(std::move(prop)); } template<basic_property_type T, typename P = basic_property_policy<T>> [[nodiscard]] auto make_property(const T& value) { using U = std::remove_cvref_t<T>; return basic_property<U, P>(value); } template<basic_property_type T, typename P = basic_property_policy<T>> [[nodiscard]] auto make_property(T&& value) { using U = std::remove_cvref_t<T>; return basic_property<U, P>(std::move(value)); } template<basic_property_type T, typename U, typename P = basic_property_policy<T>> requires (std::convertible_to<U, T>) [[nodiscard]] auto make_property(U&& value) { using V = std::remove_cvref_t<T>; return basic_property<V, P>(std::forward<U>(value)); } template<basic_property_type T, typename... Args, typename P = basic_property_policy<T>> requires (std::constructible_from<T, Args...>) [[nodiscard]] auto make_property(Args&&... args) { using U = std::remove_cvref_t<T>; return basic_property<U, P>(std::forward<Args>(args)...); } template<basic_property_type T, typename U, typename P = basic_property_policy<T>> requires (std::constructible_from<T, std::initializer_list<U>>) [[nodiscard]] auto make_property(std::initializer_list<U> l) { using V = std::remove_cvref_t<T>; return basic_property<V, P>(l); } [[nodiscard]] auto make_property(const char* str) { return basic_property(str); } [[nodiscard]] auto make_property(const wchar_t* str) { return basic_property(str); } template<typename T> requires (std::is_pointer_v<T>) [[nodiscard]] auto make_property(T ptr) { return basic_property(ptr); } template<typename Iterator> requires (std::input_iterator<Iterator>) [[nodiscard]] auto make_property(Iterator begin, Iterator end) { return basic_property(begin, end); } template<typename T, typename P> [[nodiscard]] decltype(auto) strip(basic_property<T, P>& prop) { return static_cast<T&>(prop); } template<typename T, typename P> [[nodiscard]] decltype(auto) strip(const basic_property<T, P>& prop) { return static_cast<const T&>(prop); } template<typename T, typename P> [[nodiscard]] decltype(auto) strip(const volatile basic_property<T, P>& prop) { return static_cast<const T&>(prop); } template<typename T, typename P> [[nodiscard]] const volatile basic_property<T, P>& as_volatile(const basic_property<T, P>& prop) { return const_cast<const volatile basic_property<T, P>&>(prop); } #if defined(DEFINE_PROPERTY_OPERATOR_UNARY) #undef DEFINE_PROPERTY_OPERATOR_UNARY #endif #define DEFINE_PROPERTY_OPERATOR_UNARY(PT, OP) \ template<typename T> \ [[nodiscard]] auto operator OP (const PT<T>& prop) -> \ PT<decltype(OP std::declval<T>())> \ { return { OP prop.get() }; } #if defined(DEFINE_PROPERTY_OPERATOR_BINARY) #undef DEFINE_PROPERTY_OPERATOR_BINARY #endif #define DEFINE_PROPERTY_OPERATOR_BINARY(PT, OP) \ template<typename T1, typename T2> \ [[nodiscard]] auto operator OP (const PT<T1>& lhs, const PT<T2>& rhs) -> \ PT<decltype(std::declval<T1>() OP std::declval<T2>())> \ { return { lhs.get() OP rhs.get() }; } \ \ template<typename T, typename U> requires (not std::same_as<PT<T>, std::remove_cvref_t<U>>) \ [[nodiscard]] auto operator OP (const PT<T>& lhs, const U& rhs) -> \ PT<decltype(std::declval<T>() OP std::declval<U>())> \ { return { lhs.get() OP rhs }; } \ \ template<typename U, typename T> requires (not std::same_as<std::remove_cvref_t<U>, PT<T>> \ and not std::same_as<std::remove_cvref_t<U>, std::ostream> and not std::same_as<std::remove_cvref_t<U>, std::wostream> \ and not std::same_as<std::remove_cvref_t<U>, std::istream> and not std::same_as<std::remove_cvref_t<U>, std::wistream>) \ [[nodiscard]] auto operator OP (const U& lhs, const PT<T>& rhs) -> \ PT<decltype(std::declval<U>() OP std::declval<T>())> \ { return { lhs OP rhs.get() }; } template<basic_property_type T> using property = basic_property<T, basic_property_policy<T>>; DEFINE_PROPERTY_OPERATOR_UNARY(property, +); DEFINE_PROPERTY_OPERATOR_UNARY(property, -); DEFINE_PROPERTY_OPERATOR_BINARY(property, +); DEFINE_PROPERTY_OPERATOR_BINARY(property, -); DEFINE_PROPERTY_OPERATOR_BINARY(property, *); DEFINE_PROPERTY_OPERATOR_BINARY(property, /); DEFINE_PROPERTY_OPERATOR_BINARY(property, %) DEFINE_PROPERTY_OPERATOR_UNARY(property, ~); DEFINE_PROPERTY_OPERATOR_BINARY(property, &); DEFINE_PROPERTY_OPERATOR_BINARY(property, |); DEFINE_PROPERTY_OPERATOR_BINARY(property, ^); DEFINE_PROPERTY_OPERATOR_BINARY(property, <<); DEFINE_PROPERTY_OPERATOR_BINARY(property, >>); template<basic_property_type T> using fs_property = basic_property<T, file_storage_property_policy<T>>; DEFINE_PROPERTY_OPERATOR_UNARY(fs_property, +); DEFINE_PROPERTY_OPERATOR_UNARY(fs_property, -); DEFINE_PROPERTY_OPERATOR_BINARY(fs_property, +); DEFINE_PROPERTY_OPERATOR_BINARY(fs_property, -); DEFINE_PROPERTY_OPERATOR_BINARY(fs_property, *); DEFINE_PROPERTY_OPERATOR_BINARY(fs_property, /); DEFINE_PROPERTY_OPERATOR_BINARY(fs_property, %) DEFINE_PROPERTY_OPERATOR_UNARY(fs_property, ~); DEFINE_PROPERTY_OPERATOR_BINARY(fs_property, &); DEFINE_PROPERTY_OPERATOR_BINARY(fs_property, |); DEFINE_PROPERTY_OPERATOR_BINARY(fs_property, ^); DEFINE_PROPERTY_OPERATOR_BINARY(fs_property, <<); DEFINE_PROPERTY_OPERATOR_BINARY(fs_property, >>); #undef DEFINE_PROPERTY_OPERATOR_UNARY #undef DEFINE_PROPERTY_OPERATOR_BINARY |
