{"action":"created","comment":{"url":"https://api.github.com/repos/ARMmbed/core-util/pulls/comments/40204337","id":40204337,"diff_hunk":"@@ -0,0 +1,275 @@\n+/* mbed Microcontroller Library\n+ * Copyright (c) 2006-2015 ARM Limited\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *     http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+#ifndef __SHAREDPOINTER_H__\n+#define __SHAREDPOINTER_H__\n+\n+#include \"core-util/core-util.h\"\n+#include \"mbed-alloc/ualloc.h\"\n+\n+#include <stdint.h>\n+#include <stddef.h>\n+\n+#ifndef NDEBUG\n+#include <stdio.h>\n+#define SHAREDPOINTER_DEBUG(...) { printf(__VA_ARGS__); }\n+#else\n+#define SHAREDPOINTER_DEBUG(...) /* nothing */\n+#endif\n+\n+/*\n+    Shared pointer class.\n+\n+    Similar to std::shared_ptr in C++11.\n+\n+    Usage: SharedPointer<class> POINTER(new class())\n+\n+    When POINTER is passed around by value the copy constructor and\n+    destructor counts the number of references to the original object.\n+    If the counter reaches zero, delete is called on the object pointed to.\n+\n+    To avoid loops, \"weak\" references should be used by calling the original\n+    pointer directly through POINTER.get().\n+*/\n+\n+template <class T>\n+class SharedPointer\n+{\n+public:\n+    /*\n+        Create empty SharedPointer not pointing to anything.\n+        Used for variable declaration.\n+    */\n+    SharedPointer()\n+        :   pointer(NULL),\n+            counter(NULL)\n+    {\n+        SHAREDPOINTER_DEBUG(\"SP: %p [%p: %p]\\r\\n\", this, pointer, counter);\n+    }\n+\n+    /*\n+        Create new SharedPointer and take control of object with address _pointer.\n+    */\n+    SharedPointer(T* _pointer)\n+        :   pointer(_pointer)\n+    {\n+        CORE_UTIL_ASSERT(pointer);\n+\n+        // allocate counter on the heap so it can be shared\n+        UAllocTraits_t traits = {0};\n+        counter = (uint32_t*) mbed_ualloc(sizeof(uint32_t), traits);\n+\n+        // initialize counter to 1\n+        CORE_UTIL_ASSERT(counter);\n+        *counter = 1;\n+\n+        SHAREDPOINTER_DEBUG(\"SP: %p [%p: %p = %lu]\\r\\n\", this, pointer, counter, *counter);\n+    }\n+\n+    ~SharedPointer()\n+    {\n+        decrementCounter();\n+    }\n+\n+    /*\n+        Create new SharePointer from other SharedPointer by\n+        copying pointer to original object and pointer to counter.\n+    */\n+    SharedPointer(const SharedPointer& source)\n+        :   pointer(source.pointer),\n+            counter(source.counter)\n+    {\n+        // increment reference counter\n+        if (counter)","path":"core-util/SharedPointer.h","position":96,"original_position":96,"commit_id":"46c59ba6294640924b0bd1b7f098f7ee085cef1e","original_commit_id":"46c59ba6294640924b0bd1b7f098f7ee085cef1e","user":{"login":"marcuschangarm","id":7205609,"avatar_url":"https://avatars.githubusercontent.com/u/7205609?v=3","gravatar_id":"","url":"https://api.github.com/users/marcuschangarm","html_url":"https://github.com/marcuschangarm","followers_url":"https://api.github.com/users/marcuschangarm/followers","following_url":"https://api.github.com/users/marcuschangarm/following{/other_user}","gists_url":"https://api.github.com/users/marcuschangarm/gists{/gist_id}","starred_url":"https://api.github.com/users/marcuschangarm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcuschangarm/subscriptions","organizations_url":"https://api.github.com/users/marcuschangarm/orgs","repos_url":"https://api.github.com/users/marcuschangarm/repos","events_url":"https://api.github.com/users/marcuschangarm/events{/privacy}","received_events_url":"https://api.github.com/users/marcuschangarm/received_events","type":"User","site_admin":false},"body":"You can assign a shared pointer that points to NULL to another shared pointer, e.g.,\r\n\r\n```\r\nSharedPointer<A> ptr1;\r\nSharedPointer<A> ptr2;\r\n\r\nptr1 = ptr2;\r\n```","created_at":"2015-09-23T13:47:51Z","updated_at":"2015-09-23T13:47:51Z","html_url":"https://github.com/ARMmbed/core-util/pull/30#discussion_r40204337","pull_request_url":"https://api.github.com/repos/ARMmbed/core-util/pulls/30","_links":{"self":{"href":"https://api.github.com/repos/ARMmbed/core-util/pulls/comments/40204337"},"html":{"href":"https://github.com/ARMmbed/core-util/pull/30#discussion_r40204337"},"pull_request":{"href":"https://api.github.com/repos/ARMmbed/core-util/pulls/30"}}},"pull_request":{"url":"https://api.github.com/repos/ARMmbed/core-util/pulls/30","id":45674486,"html_url":"https://github.com/ARMmbed/core-util/pull/30","diff_url":"https://github.com/ARMmbed/core-util/pull/30.diff","patch_url":"https://github.com/ARMmbed/core-util/pull/30.patch","issue_url":"https://api.github.com/repos/ARMmbed/core-util/issues/30","number":30,"state":"open","locked":false,"title":"Added referenced counted shared pointer class.","user":{"login":"marcuschangarm","id":7205609,"avatar_url":"https://avatars.githubusercontent.com/u/7205609?v=3","gravatar_id":"","url":"https://api.github.com/users/marcuschangarm","html_url":"https://github.com/marcuschangarm","followers_url":"https://api.github.com/users/marcuschangarm/followers","following_url":"https://api.github.com/users/marcuschangarm/following{/other_user}","gists_url":"https://api.github.com/users/marcuschangarm/gists{/gist_id}","starred_url":"https://api.github.com/users/marcuschangarm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcuschangarm/subscriptions","organizations_url":"https://api.github.com/users/marcuschangarm/orgs","repos_url":"https://api.github.com/users/marcuschangarm/repos","events_url":"https://api.github.com/users/marcuschangarm/events{/privacy}","received_events_url":"https://api.github.com/users/marcuschangarm/received_events","type":"User","site_admin":false},"body":"see https://github.com/ARMmbed/core-util/pull/26","created_at":"2015-09-22T15:02:28Z","updated_at":"2015-09-23T13:47:51Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b0787fd9956fa3f36e6e7a083e312fbb36ea8486","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ARMmbed/core-util/pulls/30/commits","review_comments_url":"https://api.github.com/repos/ARMmbed/core-util/pulls/30/comments","review_comment_url":"https://api.github.com/repos/ARMmbed/core-util/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ARMmbed/core-util/issues/30/comments","statuses_url":"https://api.github.com/repos/ARMmbed/core-util/statuses/46c59ba6294640924b0bd1b7f098f7ee085cef1e","head":{"label":"marcuschangarm:sharedptr","ref":"sharedptr","sha":"46c59ba6294640924b0bd1b7f098f7ee085cef1e","user":{"login":"marcuschangarm","id":7205609,"avatar_url":"https://avatars.githubusercontent.com/u/7205609?v=3","gravatar_id":"","url":"https://api.github.com/users/marcuschangarm","html_url":"https://github.com/marcuschangarm","followers_url":"https://api.github.com/users/marcuschangarm/followers","following_url":"https://api.github.com/users/marcuschangarm/following{/other_user}","gists_url":"https://api.github.com/users/marcuschangarm/gists{/gist_id}","starred_url":"https://api.github.com/users/marcuschangarm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcuschangarm/subscriptions","organizations_url":"https://api.github.com/users/marcuschangarm/orgs","repos_url":"https://api.github.com/users/marcuschangarm/repos","events_url":"https://api.github.com/users/marcuschangarm/events{/privacy}","received_events_url":"https://api.github.com/users/marcuschangarm/received_events","type":"User","site_admin":false},"repo":{"id":42670259,"name":"core-util","full_name":"marcuschangarm/core-util","owner":{"login":"marcuschangarm","id":7205609,"avatar_url":"https://avatars.githubusercontent.com/u/7205609?v=3","gravatar_id":"","url":"https://api.github.com/users/marcuschangarm","html_url":"https://github.com/marcuschangarm","followers_url":"https://api.github.com/users/marcuschangarm/followers","following_url":"https://api.github.com/users/marcuschangarm/following{/other_user}","gists_url":"https://api.github.com/users/marcuschangarm/gists{/gist_id}","starred_url":"https://api.github.com/users/marcuschangarm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcuschangarm/subscriptions","organizations_url":"https://api.github.com/users/marcuschangarm/orgs","repos_url":"https://api.github.com/users/marcuschangarm/repos","events_url":"https://api.github.com/users/marcuschangarm/events{/privacy}","received_events_url":"https://api.github.com/users/marcuschangarm/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/marcuschangarm/core-util","description":"mbed utilities library","fork":true,"url":"https://api.github.com/repos/marcuschangarm/core-util","forks_url":"https://api.github.com/repos/marcuschangarm/core-util/forks","keys_url":"https://api.github.com/repos/marcuschangarm/core-util/keys{/key_id}","collaborators_url":"https://api.github.com/repos/marcuschangarm/core-util/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/marcuschangarm/core-util/teams","hooks_url":"https://api.github.com/repos/marcuschangarm/core-util/hooks","issue_events_url":"https://api.github.com/repos/marcuschangarm/core-util/issues/events{/number}","events_url":"https://api.github.com/repos/marcuschangarm/core-util/events","assignees_url":"https://api.github.com/repos/marcuschangarm/core-util/assignees{/user}","branches_url":"https://api.github.com/repos/marcuschangarm/core-util/branches{/branch}","tags_url":"https://api.github.com/repos/marcuschangarm/core-util/tags","blobs_url":"https://api.github.com/repos/marcuschangarm/core-util/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/marcuschangarm/core-util/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/marcuschangarm/core-util/git/refs{/sha}","trees_url":"https://api.github.com/repos/marcuschangarm/core-util/git/trees{/sha}","statuses_url":"https://api.github.com/repos/marcuschangarm/core-util/statuses/{sha}","languages_url":"https://api.github.com/repos/marcuschangarm/core-util/languages","stargazers_url":"https://api.github.com/repos/marcuschangarm/core-util/stargazers","contributors_url":"https://api.github.com/repos/marcuschangarm/core-util/contributors","subscribers_url":"https://api.github.com/repos/marcuschangarm/core-util/subscribers","subscription_url":"https://api.github.com/repos/marcuschangarm/core-util/subscription","commits_url":"https://api.github.com/repos/marcuschangarm/core-util/commits{/sha}","git_commits_url":"https://api.github.com/repos/marcuschangarm/core-util/git/commits{/sha}","comments_url":"https://api.github.com/repos/marcuschangarm/core-util/comments{/number}","issue_comment_url":"https://api.github.com/repos/marcuschangarm/core-util/issues/comments{/number}","contents_url":"https://api.github.com/repos/marcuschangarm/core-util/contents/{+path}","compare_url":"https://api.github.com/repos/marcuschangarm/core-util/compare/{base}...{head}","merges_url":"https://api.github.com/repos/marcuschangarm/core-util/merges","archive_url":"https://api.github.com/repos/marcuschangarm/core-util/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/marcuschangarm/core-util/downloads","issues_url":"https://api.github.com/repos/marcuschangarm/core-util/issues{/number}","pulls_url":"https://api.github.com/repos/marcuschangarm/core-util/pulls{/number}","milestones_url":"https://api.github.com/repos/marcuschangarm/core-util/milestones{/number}","notifications_url":"https://api.github.com/repos/marcuschangarm/core-util/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/marcuschangarm/core-util/labels{/name}","releases_url":"https://api.github.com/repos/marcuschangarm/core-util/releases{/id}","created_at":"2015-09-17T17:16:45Z","updated_at":"2015-09-17T17:16:46Z","pushed_at":"2015-09-22T15:01:05Z","git_url":"git://github.com/marcuschangarm/core-util.git","ssh_url":"git@github.com:marcuschangarm/core-util.git","clone_url":"https://github.com/marcuschangarm/core-util.git","svn_url":"https://github.com/marcuschangarm/core-util","homepage":"","size":169,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"ARMmbed:master","ref":"master","sha":"bd322397fd8ae0dd9e749945814832f08ad3f5fc","user":{"login":"ARMmbed","id":8916057,"avatar_url":"https://avatars.githubusercontent.com/u/8916057?v=3","gravatar_id":"","url":"https://api.github.com/users/ARMmbed","html_url":"https://github.com/ARMmbed","followers_url":"https://api.github.com/users/ARMmbed/followers","following_url":"https://api.github.com/users/ARMmbed/following{/other_user}","gists_url":"https://api.github.com/users/ARMmbed/gists{/gist_id}","starred_url":"https://api.github.com/users/ARMmbed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ARMmbed/subscriptions","organizations_url":"https://api.github.com/users/ARMmbed/orgs","repos_url":"https://api.github.com/users/ARMmbed/repos","events_url":"https://api.github.com/users/ARMmbed/events{/privacy}","received_events_url":"https://api.github.com/users/ARMmbed/received_events","type":"Organization","site_admin":false},"repo":{"id":39007827,"name":"core-util","full_name":"ARMmbed/core-util","owner":{"login":"ARMmbed","id":8916057,"avatar_url":"https://avatars.githubusercontent.com/u/8916057?v=3","gravatar_id":"","url":"https://api.github.com/users/ARMmbed","html_url":"https://github.com/ARMmbed","followers_url":"https://api.github.com/users/ARMmbed/followers","following_url":"https://api.github.com/users/ARMmbed/following{/other_user}","gists_url":"https://api.github.com/users/ARMmbed/gists{/gist_id}","starred_url":"https://api.github.com/users/ARMmbed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ARMmbed/subscriptions","organizations_url":"https://api.github.com/users/ARMmbed/orgs","repos_url":"https://api.github.com/users/ARMmbed/repos","events_url":"https://api.github.com/users/ARMmbed/events{/privacy}","received_events_url":"https://api.github.com/users/ARMmbed/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ARMmbed/core-util","description":"mbed utilities library","fork":false,"url":"https://api.github.com/repos/ARMmbed/core-util","forks_url":"https://api.github.com/repos/ARMmbed/core-util/forks","keys_url":"https://api.github.com/repos/ARMmbed/core-util/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ARMmbed/core-util/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ARMmbed/core-util/teams","hooks_url":"https://api.github.com/repos/ARMmbed/core-util/hooks","issue_events_url":"https://api.github.com/repos/ARMmbed/core-util/issues/events{/number}","events_url":"https://api.github.com/repos/ARMmbed/core-util/events","assignees_url":"https://api.github.com/repos/ARMmbed/core-util/assignees{/user}","branches_url":"https://api.github.com/repos/ARMmbed/core-util/branches{/branch}","tags_url":"https://api.github.com/repos/ARMmbed/core-util/tags","blobs_url":"https://api.github.com/repos/ARMmbed/core-util/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ARMmbed/core-util/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ARMmbed/core-util/git/refs{/sha}","trees_url":"https://api.github.com/repos/ARMmbed/core-util/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ARMmbed/core-util/statuses/{sha}","languages_url":"https://api.github.com/repos/ARMmbed/core-util/languages","stargazers_url":"https://api.github.com/repos/ARMmbed/core-util/stargazers","contributors_url":"https://api.github.com/repos/ARMmbed/core-util/contributors","subscribers_url":"https://api.github.com/repos/ARMmbed/core-util/subscribers","subscription_url":"https://api.github.com/repos/ARMmbed/core-util/subscription","commits_url":"https://api.github.com/repos/ARMmbed/core-util/commits{/sha}","git_commits_url":"https://api.github.com/repos/ARMmbed/core-util/git/commits{/sha}","comments_url":"https://api.github.com/repos/ARMmbed/core-util/comments{/number}","issue_comment_url":"https://api.github.com/repos/ARMmbed/core-util/issues/comments{/number}","contents_url":"https://api.github.com/repos/ARMmbed/core-util/contents/{+path}","compare_url":"https://api.github.com/repos/ARMmbed/core-util/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ARMmbed/core-util/merges","archive_url":"https://api.github.com/repos/ARMmbed/core-util/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ARMmbed/core-util/downloads","issues_url":"https://api.github.com/repos/ARMmbed/core-util/issues{/number}","pulls_url":"https://api.github.com/repos/ARMmbed/core-util/pulls{/number}","milestones_url":"https://api.github.com/repos/ARMmbed/core-util/milestones{/number}","notifications_url":"https://api.github.com/repos/ARMmbed/core-util/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ARMmbed/core-util/labels{/name}","releases_url":"https://api.github.com/repos/ARMmbed/core-util/releases{/id}","created_at":"2015-07-13T11:20:40Z","updated_at":"2015-09-04T09:50:06Z","pushed_at":"2015-09-23T08:44:55Z","git_url":"git://github.com/ARMmbed/core-util.git","ssh_url":"git@github.com:ARMmbed/core-util.git","clone_url":"https://github.com/ARMmbed/core-util.git","svn_url":"https://github.com/ARMmbed/core-util","homepage":"","size":675,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":7,"forks":5,"open_issues":7,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ARMmbed/core-util/pulls/30"},"html":{"href":"https://github.com/ARMmbed/core-util/pull/30"},"issue":{"href":"https://api.github.com/repos/ARMmbed/core-util/issues/30"},"comments":{"href":"https://api.github.com/repos/ARMmbed/core-util/issues/30/comments"},"review_comments":{"href":"https://api.github.com/repos/ARMmbed/core-util/pulls/30/comments"},"review_comment":{"href":"https://api.github.com/repos/ARMmbed/core-util/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ARMmbed/core-util/pulls/30/commits"},"statuses":{"href":"https://api.github.com/repos/ARMmbed/core-util/statuses/46c59ba6294640924b0bd1b7f098f7ee085cef1e"}}}}