{"id":16141165,"url":"https://github.com/mzying2001/property-cpp","last_synced_at":"2025-04-06T18:35:20.121Z","repository":{"id":257598995,"uuid":"858760292","full_name":"Mzying2001/property-cpp","owner":"Mzying2001","description":"让 C++ 支持属性语法","archived":false,"fork":false,"pushed_at":"2024-10-19T02:52:04.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-13T00:37:52.916Z","etag":null,"topics":["cpp","cpp11"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mzying2001.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-17T13:42:16.000Z","updated_at":"2024-10-19T02:52:07.000Z","dependencies_parsed_at":"2024-09-17T17:17:25.603Z","dependency_job_id":"b97482b8-0081-431f-b813-4476f4b79a3a","html_url":"https://github.com/Mzying2001/property-cpp","commit_stats":null,"previous_names":["mzying2001/property-cpp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mzying2001%2Fproperty-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mzying2001%2Fproperty-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mzying2001%2Fproperty-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mzying2001%2Fproperty-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mzying2001","download_url":"https://codeload.github.com/Mzying2001/property-cpp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247533619,"owners_count":20954415,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","cpp11"],"created_at":"2024-10-09T23:54:40.391Z","updated_at":"2025-04-06T18:35:20.084Z","avatar_url":"https://github.com/Mzying2001.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `prop.h`\n\n一个头文件为 C++ 提供类似 C# 的属性语法。\n\n## 示例\n\n以下定义了一个 `Person` 类，包含一个可读可写属性 `Age` 以及一个只读属性 `AgeStr`，代码演示了属性的声明和初始化。\n\n```C++\nclass Person\n{\n    // 为当前类启用成员属性宏支持\n    ENABLE_PROPERTY(Person);\n\n    // 属性Age维护的字段\n    int _age;\n\npublic:\n    // 声明一个属性Age\n    PROPERTY_RW(int, Age);\n\n    // 声明AgeStr只读属性，表示Age的字符串\n    PROPERTY_R(string, AgeStr);\n\n    // 构造函数\n    Person()\n        : Age(\n              // 使用GETTER宏和SETTER宏设置属性的getter和setter\n              GETTER(int) {\n                  cout \u003c\u003c \"get Age\" \u003c\u003c endl;\n                  return self._age; // self表示当前对象\n              },\n              SETTER(int) {\n                  cout \u003c\u003c \"set Age: \" \u003c\u003c value \u003c\u003c endl; // value表示给属性设置的值\n                  if (value \u003c= 0) {\n                      cout \u003c\u003c \"error: Age can not smaller than 1\" \u003c\u003c endl;\n                      return; // 若设置的Age范围不正确则输出错误，不更改值\n                  }\n                  self._age = value;\n              }),\n\n          AgeStr(\n              // 只读属性只有getter\n              GETTER(string) {\n                  cout \u003c\u003c \"get AgeStr\" \u003c\u003c endl;\n                  return to_string(self._age);\n              })\n    {\n        _age = 1; // Age默认值\n    }\n};\n```\n\n测试代码如下：\n\n```C++\nint main()\n{\n    Person p;\n    cout \u003c\u003c p.Age \u003c\u003c endl; // get Age\n\n    p.Age = -1;            // error: Age can not smaller than 1\n    cout \u003c\u003c p.Age \u003c\u003c endl; // 仍然为1\n\n    p.Age = 10;\n    cout \u003c\u003c p.Age \u003c\u003c endl; // 10\n\n    Person p2 = p;                      // 对象可以正常拷贝\n    p2.Age++;                           // 先get后set\n    cout \u003c\u003c p2.AgeStr-\u003ec_str() \u003c\u003c endl; // 使用-\u003e可以访问属性成员\n\n    return 0;\n}\n```\n\n运行结果：\n\n```\nget Age\n1\nset Age: -1\nerror: Age can not smaller than 1\nget Age\n1\nset Age: 10\nget Age\n10\nget Age\nset Age: 11\nget AgeStr\n11\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmzying2001%2Fproperty-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmzying2001%2Fproperty-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmzying2001%2Fproperty-cpp/lists"}