Compare commits

..

No commits in common. "e8ebd43cddca1f57c6d7d0f40f13e93e7c519a85" and "ba5f40356e897659b702fabde24276f4d7bf188f" have entirely different histories.

30 changed files with 536 additions and 1974 deletions

574
LICENSE
View File

@ -1,373 +1,201 @@
Mozilla Public License Version 2.0 Apache License
================================== Version 2.0, January 2004
http://www.apache.org/licenses/
1. Definitions
-------------- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1.1. "Contributor" 1. Definitions.
means each individual or legal entity that creates, contributes to
the creation of, or owns Covered Software. "License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
1.2. "Contributor Version"
means the combination of the Contributions of others (if any) used "Licensor" shall mean the copyright owner or entity authorized by
by a Contributor and that particular Contributor's Contribution. the copyright owner that is granting the License.
1.3. "Contribution" "Legal Entity" shall mean the union of the acting entity and all
means Covered Software of a particular Contributor. other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
1.4. "Covered Software" "control" means (i) the power, direct or indirect, to cause the
means Source Code Form to which the initial Contributor has attached direction or management of such entity, whether by contract or
the notice in Exhibit A, the Executable Form of such Source Code otherwise, or (ii) ownership of fifty percent (50%) or more of the
Form, and Modifications of such Source Code Form, in each case outstanding shares, or (iii) beneficial ownership of such entity.
including portions thereof.
"You" (or "Your") shall mean an individual or Legal Entity
1.5. "Incompatible With Secondary Licenses" exercising permissions granted by this License.
means
"Source" form shall mean the preferred form for making modifications,
(a) that the initial Contributor has attached the notice described including but not limited to software source code, documentation
in Exhibit B to the Covered Software; or source, and configuration files.
(b) that the Covered Software was made available under the terms of "Object" form shall mean any form resulting from mechanical
version 1.1 or earlier of the License, but not also under the transformation or translation of a Source form, including but
terms of a Secondary License. not limited to compiled object code, generated documentation,
and conversions to other media types.
1.6. "Executable Form"
means any form of the work other than Source Code Form. "Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
1.7. "Larger Work" copyright notice that is included in or attached to the work
means a work that combines Covered Software with other material, in (an example is provided in the Appendix below).
a separate file or files, that is not Covered Software.
"Derivative Works" shall mean any work, whether in Source or Object
1.8. "License" form, that is based on (or derived from) the Work and for which the
means this document. editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
1.9. "Licensable" of this License, Derivative Works shall not include works that remain
means having the right to grant, to the maximum extent possible, separable from, or merely link (or bind by name) to the interfaces of,
whether at the time of the initial grant or subsequently, any and the Work and Derivative Works thereof.
all of the rights conveyed by this License.
"Contribution" shall mean any work of authorship, including
1.10. "Modifications" the original version of the Work and any modifications or additions
means any of the following: to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
(a) any file in Source Code Form that results from an addition to, or by an individual or Legal Entity authorized to submit on behalf of
deletion from, or modification of the contents of Covered the copyright owner. For the purposes of this definition, "submitted"
Software; or means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
(b) any new file in Source Code Form that contains any Covered communication on electronic mailing lists, source code control systems,
Software. and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
1.11. "Patent Claims" of a Contributor excluding communication that is conspicuously marked or otherwise
means any patent claim(s), including without limitation, method, designated in writing by the copyright owner as "Not a Contribution."
process, and apparatus claims, in any patent Licensable by such
Contributor that would be infringed, but for the grant of the "Contributor" shall mean Licensor and any individual or Legal Entity
License, by the making, using, selling, offering for sale, having on behalf of whom a Contribution has been received by Licensor and
made, import, or transfer of either its Contributions or its subsequently incorporated within the Work.
Contributor Version.
2. Grant of Copyright License. Subject to the terms and conditions of
1.12. "Secondary License" this License, each Contributor hereby grants to You a perpetual,
means either the GNU General Public License, Version 2.0, the GNU worldwide, non-exclusive, no-charge, royalty-free, irrevocable
Lesser General Public License, Version 2.1, the GNU Affero General copyright license to reproduce, prepare Derivative Works of,
Public License, Version 3.0, or any later versions of those publicly display, publicly perform, sublicense, and distribute the
licenses. Work and such Derivative Works in Source or Object form.
1.13. "Source Code Form" 3. Grant of Patent License. Subject to the terms and conditions of
means the form of the work preferred for making modifications. this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
1.14. "You" (or "Your") (except as stated in this section) patent license to make, have made,
means an individual or a legal entity exercising rights under this use, offer to sell, sell, import, and otherwise transfer the Work,
License. For legal entities, "You" includes any entity that where such license applies only to those patent claims licensable
controls, is controlled by, or is under common control with You. For by such Contributor that are necessarily infringed by their
purposes of this definition, "control" means (a) the power, direct Contribution(s) alone or by combination of their Contribution(s)
or indirect, to cause the direction or management of such entity, with the Work to which such Contribution(s) was submitted. If You
whether by contract or otherwise, or (b) ownership of more than institute patent litigation against any entity (including a
fifty percent (50%) of the outstanding shares or beneficial cross-claim or counterclaim in a lawsuit) alleging that the Work
ownership of such entity. or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
2. License Grants and Conditions granted to You under this License for that Work shall terminate
-------------------------------- as of the date such litigation is filed.
2.1. Grants 4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
Each Contributor hereby grants You a world-wide, royalty-free, modifications, and in Source or Object form, provided that You
non-exclusive license: meet the following conditions:
(a) under intellectual property rights (other than patent or trademark) (a) You must give any other recipients of the Work or
Licensable by such Contributor to use, reproduce, make available, Derivative Works a copy of this License; and
modify, display, perform, distribute, and otherwise exploit its
Contributions, either on an unmodified basis, with Modifications, or (b) You must cause any modified files to carry prominent notices
as part of a Larger Work; and stating that You changed the files; and
(b) under Patent Claims of such Contributor to make, use, sell, offer (c) You must retain, in the Source form of any Derivative Works
for sale, have made, import, and otherwise transfer either its that You distribute, all copyright, patent, trademark, and
Contributions or its Contributor Version. attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
2.2. Effective Date the Derivative Works; and
The licenses granted in Section 2.1 with respect to any Contribution (d) If the Work includes a "NOTICE" text file as part of its
become effective for each Contribution on the date the Contributor first distribution, then any Derivative Works that You distribute must
distributes such Contribution. include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
2.3. Limitations on Grant Scope pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
The licenses granted in this Section 2 are the only rights granted under as part of the Derivative Works; within the Source form or
this License. No additional rights or licenses will be implied from the documentation, if provided along with the Derivative Works; or,
distribution or licensing of Covered Software under this License. within a display generated by the Derivative Works, if and
Notwithstanding Section 2.1(b) above, no patent license is granted by a wherever such third-party notices normally appear. The contents
Contributor: of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
(a) for any code that a Contributor has removed from Covered Software; notices within Derivative Works that You distribute, alongside
or or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
(b) for infringements caused by: (i) Your and any other third party's as modifying the License.
modifications of Covered Software, or (ii) the combination of its
Contributions with other software (except as part of its Contributor You may add Your own copyright statement to Your modifications and
Version); or may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
(c) under Patent Claims infringed by Covered Software in the absence of for any such Derivative Works as a whole, provided Your use,
its Contributions. reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
This License does not grant any rights in the trademarks, service marks,
or logos of any Contributor (except as may be necessary to comply with 5. Submission of Contributions. Unless You explicitly state otherwise,
the notice requirements in Section 3.4). any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
2.4. Subsequent Licenses this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
No Contributor makes additional grants as a result of Your choice to the terms of any separate license agreement you may have executed
distribute the Covered Software under a subsequent version of this with Licensor regarding such Contributions.
License (see Section 10.2) or under the terms of a Secondary License (if
permitted under the terms of Section 3.3). 6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
2.5. Representation except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
Each Contributor represents that the Contributor believes its
Contributions are its original creation(s) or it has sufficient rights 7. Disclaimer of Warranty. Unless required by applicable law or
to grant the rights to its Contributions conveyed by this License. agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
2.6. Fair Use WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
This License is not intended to limit any rights You have under of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
applicable copyright doctrines of fair use, fair dealing, or other PARTICULAR PURPOSE. You are solely responsible for determining the
equivalents. appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
2.7. Conditions
8. Limitation of Liability. In no event and under no legal theory,
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted whether in tort (including negligence), contract, or otherwise,
in Section 2.1. unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
3. Responsibilities liable to You for damages, including any direct, indirect, special,
------------------- incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
3.1. Distribution of Source Form Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
All distribution of Covered Software in Source Code Form, including any other commercial damages or losses), even if such Contributor
Modifications that You create or to which You contribute, must be under has been advised of the possibility of such damages.
the terms of this License. You must inform recipients that the Source
Code Form of the Covered Software is governed by the terms of this 9. Accepting Warranty or Additional Liability. While redistributing
License, and how they can obtain a copy of this License. You may not the Work or Derivative Works thereof, You may choose to offer,
attempt to alter or restrict the recipients' rights in the Source Code and charge a fee for, acceptance of support, warranty, indemnity,
Form. or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
3.2. Distribution of Executable Form on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
If You distribute Covered Software in Executable Form then: defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
(a) such Covered Software must also be made available in Source Code of your accepting any such warranty or additional liability.
Form, as described in Section 3.1, and You must inform recipients of
the Executable Form how they can obtain a copy of such Source Code END OF TERMS AND CONDITIONS
Form by reasonable means in a timely manner, at a charge no more
than the cost of distribution to the recipient; and APPENDIX: How to apply the Apache License to your work.
(b) You may distribute such Executable Form under the terms of this To apply the Apache License to your work, attach the following
License, or sublicense it under different terms, provided that the boilerplate notice, with the fields enclosed by brackets "{}"
license for the Executable Form does not attempt to limit or alter replaced with your own identifying information. (Don't include
the recipients' rights in the Source Code Form under this License. the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
3.3. Distribution of a Larger Work file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
You may create and distribute a Larger Work under terms of Your choice, identification within third-party archives.
provided that You also comply with the requirements of this License for
the Covered Software. If the Larger Work is a combination of Covered Copyright {yyyy} {name of copyright owner}
Software with a work governed by one or more Secondary Licenses, and the
Covered Software is not Incompatible With Secondary Licenses, this Licensed under the Apache License, Version 2.0 (the "License");
License permits You to additionally distribute such Covered Software you may not use this file except in compliance with the License.
under the terms of such Secondary License(s), so that the recipient of You may obtain a copy of the License at
the Larger Work may, at their option, further distribute the Covered
Software under the terms of either this License or such Secondary http://www.apache.org/licenses/LICENSE-2.0
License(s).
Unless required by applicable law or agreed to in writing, software
3.4. Notices distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
You may not remove or alter the substance of any license notices See the License for the specific language governing permissions and
(including copyright notices, patent notices, disclaimers of warranty, limitations under the License.
or limitations of liability) contained within the Source Code Form of
the Covered Software, except that You may alter any license notices to
the extent required to remedy known factual inaccuracies.
3.5. Application of Additional Terms
You may choose to offer, and to charge a fee for, warranty, support,
indemnity or liability obligations to one or more recipients of Covered
Software. However, You may do so only on Your own behalf, and not on
behalf of any Contributor. You must make it absolutely clear that any
such warranty, support, indemnity, or liability obligation is offered by
You alone, and You hereby agree to indemnify every Contributor for any
liability incurred by such Contributor as a result of warranty, support,
indemnity or liability terms You offer. You may include additional
disclaimers of warranty and limitations of liability specific to any
jurisdiction.
4. Inability to Comply Due to Statute or Regulation
---------------------------------------------------
If it is impossible for You to comply with any of the terms of this
License with respect to some or all of the Covered Software due to
statute, judicial order, or regulation then You must: (a) comply with
the terms of this License to the maximum extent possible; and (b)
describe the limitations and the code they affect. Such description must
be placed in a text file included with all distributions of the Covered
Software under this License. Except to the extent prohibited by statute
or regulation, such description must be sufficiently detailed for a
recipient of ordinary skill to be able to understand it.
5. Termination
--------------
5.1. The rights granted under this License will terminate automatically
if You fail to comply with any of its terms. However, if You become
compliant, then the rights granted under this License from a particular
Contributor are reinstated (a) provisionally, unless and until such
Contributor explicitly and finally terminates Your grants, and (b) on an
ongoing basis, if such Contributor fails to notify You of the
non-compliance by some reasonable means prior to 60 days after You have
come back into compliance. Moreover, Your grants from a particular
Contributor are reinstated on an ongoing basis if such Contributor
notifies You of the non-compliance by some reasonable means, this is the
first time You have received notice of non-compliance with this License
from such Contributor, and You become compliant prior to 30 days after
Your receipt of the notice.
5.2. If You initiate litigation against any entity by asserting a patent
infringement claim (excluding declaratory judgment actions,
counter-claims, and cross-claims) alleging that a Contributor Version
directly or indirectly infringes any patent, then the rights granted to
You by any and all Contributors for the Covered Software under Section
2.1 of this License shall terminate.
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
end user license agreements (excluding distributors and resellers) which
have been validly granted by You or Your distributors under this License
prior to termination shall survive termination.
************************************************************************
* *
* 6. Disclaimer of Warranty *
* ------------------------- *
* *
* Covered Software is provided under this License on an "as is" *
* basis, without warranty of any kind, either expressed, implied, or *
* statutory, including, without limitation, warranties that the *
* Covered Software is free of defects, merchantable, fit for a *
* particular purpose or non-infringing. The entire risk as to the *
* quality and performance of the Covered Software is with You. *
* Should any Covered Software prove defective in any respect, You *
* (not any Contributor) assume the cost of any necessary servicing, *
* repair, or correction. This disclaimer of warranty constitutes an *
* essential part of this License. No use of any Covered Software is *
* authorized under this License except under this disclaimer. *
* *
************************************************************************
************************************************************************
* *
* 7. Limitation of Liability *
* -------------------------- *
* *
* Under no circumstances and under no legal theory, whether tort *
* (including negligence), contract, or otherwise, shall any *
* Contributor, or anyone who distributes Covered Software as *
* permitted above, be liable to You for any direct, indirect, *
* special, incidental, or consequential damages of any character *
* including, without limitation, damages for lost profits, loss of *
* goodwill, work stoppage, computer failure or malfunction, or any *
* and all other commercial damages or losses, even if such party *
* shall have been informed of the possibility of such damages. This *
* limitation of liability shall not apply to liability for death or *
* personal injury resulting from such party's negligence to the *
* extent applicable law prohibits such limitation. Some *
* jurisdictions do not allow the exclusion or limitation of *
* incidental or consequential damages, so this exclusion and *
* limitation may not apply to You. *
* *
************************************************************************
8. Litigation
-------------
Any litigation relating to this License may be brought only in the
courts of a jurisdiction where the defendant maintains its principal
place of business and such litigation shall be governed by laws of that
jurisdiction, without reference to its conflict-of-law provisions.
Nothing in this Section shall prevent a party's ability to bring
cross-claims or counter-claims.
9. Miscellaneous
----------------
This License represents the complete agreement concerning the subject
matter hereof. If any provision of this License is held to be
unenforceable, such provision shall be reformed only to the extent
necessary to make it enforceable. Any law or regulation which provides
that the language of a contract shall be construed against the drafter
shall not be used to construe this License against a Contributor.
10. Versions of the License
---------------------------
10.1. New Versions
Mozilla Foundation is the license steward. Except as provided in Section
10.3, no one other than the license steward has the right to modify or
publish new versions of this License. Each version will be given a
distinguishing version number.
10.2. Effect of New Versions
You may distribute the Covered Software under the terms of the version
of the License under which You originally received the Covered Software,
or under the terms of any subsequent version published by the license
steward.
10.3. Modified Versions
If you create software not governed by this License, and you want to
create a new license for such software, you may create and use a
modified version of this License if you rename the license and remove
any references to the name of the license steward (except to note that
such modified license differs from this License).
10.4. Distributing Source Code Form that is Incompatible With Secondary
Licenses
If You choose to distribute Source Code Form that is Incompatible With
Secondary Licenses under the terms of this version of the License, the
notice described in Exhibit B of this License must be attached.
Exhibit A - Source Code Form License Notice
-------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
If it is not possible or desirable to put the notice in a particular
file, then You may include the notice in a location (such as a LICENSE
file in a relevant directory) where a recipient would be likely to look
for such a notice.
You may add additional accurate notices of copyright ownership.
Exhibit B - "Incompatible With Secondary Licenses" Notice
---------------------------------------------------------
This Source Code Form is "Incompatible With Secondary Licenses", as
defined by the Mozilla Public License, v. 2.0.

View File

@ -58,15 +58,15 @@ Compile software:
lutin -C -P ememory-test lutin -C -P ememory-test
License (MPL v2.0) License (APACHE v2.0)
===================== =====================
Copyright ememory Edouard DUPIN Copyright ememory Edouard DUPIN
Licensed under the Mozilla Public License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
https://www.mozilla.org/MPL/2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,

View File

@ -1,12 +1,10 @@
EMEMORY library {#mainpage} EMEMORY library {#mainpage}
=============== ===============
@tableofcontents
What is EMEMORY, and how can I use it? What is EMEMORY, and how can I use it?
====================================== --------------------------------------
EMEMORY, or Ewol Memory interface implementing ememory::SharedPtr and ememory::UniquePtr. EMEMORY, or Ewol Memory interface is a simple abstraction layer over std::shared_ptr.
EMEMORY is designed for: EMEMORY is designed for:
- Permit to change the backend of shered_ptr when we want - Permit to change the backend of shered_ptr when we want
@ -15,25 +13,25 @@ EMEMORY is designed for:
Now it is just a wrapper over shared_ptr Now it is just a wrapper over shared_ptr
What languages are supported? What languages are supported?
============================= -----------------------------
EMEMORY is written in C++. EMEMORY is written in C++.
Are there any licensing restrictions? Are there any licensing restrictions?
===================================== -------------------------------------
EMEMORY is **FREE software** and _all sub-library are FREE and staticly linkable !!!_ EMEMORY is **FREE software** and _all sub-library are FREE and staticly linkable !!!_
License (MPL v2.0) License (APACHE-2.0)
================== --------------------
Copyright ememory Edouard DUPIN Copyright ememory Edouard DUPIN
Licensed under the Mozilla Public License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
<https://www.mozilla.org/MPL/2.0> <http://www.apache.org/licenses/LICENSE-2.0>
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,

View File

@ -1,7 +1,7 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#include <ememory/Counter.hpp> #include <ememory/Counter.hpp>
@ -11,7 +11,7 @@ ememory::Counter::Counter(bool _fromWeak):
m_counterShared(1), m_counterShared(1),
m_counterWeak(0), m_counterWeak(0),
m_mutex() { m_mutex() {
EMEMORY_DBG("Create counter(" << _fromWeak << ")"); EMEMORY_DBG("new counter(" << _fromWeak << ")");
if (_fromWeak == true) { if (_fromWeak == true) {
m_counterShared = 0; m_counterShared = 0;
m_counterWeak = 1; m_counterWeak = 1;
@ -20,13 +20,13 @@ ememory::Counter::Counter(bool _fromWeak):
} }
ememory::Counter::~Counter() { ememory::Counter::~Counter() {
EMEMORY_DBG("Delete counter"); EMEMORY_DBG("delete counter");
} }
int64_t ememory::Counter::incrementShared(bool _fromWeak) { int64_t ememory::Counter::incrementShared(bool _fromWeak) {
int64_t out; int64_t out;
{ {
ethread::UniqueLock lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
EMEMORY_DBG("shared++ (start) ==> w:" << m_counterWeak << " s:" << m_counterShared << " " << int64_t(this)); EMEMORY_DBG("shared++ (start) ==> w:" << m_counterWeak << " s:" << m_counterShared << " " << int64_t(this));
if (m_counterShared != 0 || _fromWeak == false) { if (m_counterShared != 0 || _fromWeak == false) {
m_counterShared++; m_counterShared++;
@ -38,7 +38,7 @@ int64_t ememory::Counter::incrementShared(bool _fromWeak) {
} }
ememory::Counter::remove ememory::Counter::decrementShared() { ememory::Counter::remove ememory::Counter::decrementShared() {
ethread::UniqueLock lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
EMEMORY_DBG("shared-- (start) ==> w:" << m_counterWeak << " s:" << m_counterShared << " " << int64_t(this)); EMEMORY_DBG("shared-- (start) ==> w:" << m_counterWeak << " s:" << m_counterShared << " " << int64_t(this));
if (m_counterShared != 0) { if (m_counterShared != 0) {
m_counterShared--; m_counterShared--;
@ -62,7 +62,7 @@ ememory::Counter::remove ememory::Counter::decrementShared() {
int64_t ememory::Counter::incrementWeak() { int64_t ememory::Counter::incrementWeak() {
int64_t out; int64_t out;
{ {
ethread::UniqueLock lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
EMEMORY_DBG("weak++ (start) ==> w:" << m_counterWeak << " s:" << m_counterShared << " " << int64_t(this)); EMEMORY_DBG("weak++ (start) ==> w:" << m_counterWeak << " s:" << m_counterShared << " " << int64_t(this));
m_counterWeak++; m_counterWeak++;
out = m_counterWeak; out = m_counterWeak;
@ -72,7 +72,7 @@ int64_t ememory::Counter::incrementWeak() {
} }
ememory::Counter::remove ememory::Counter::decrementWeak() { ememory::Counter::remove ememory::Counter::decrementWeak() {
ethread::UniqueLock lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
EMEMORY_DBG("weak-- (stop) ==> w:" << m_counterWeak << " s:" << m_counterShared << " " << int64_t(this)); EMEMORY_DBG("weak-- (stop) ==> w:" << m_counterWeak << " s:" << m_counterShared << " " << int64_t(this));
if (m_counterWeak != 0) { if (m_counterWeak != 0) {
m_counterWeak--; m_counterWeak--;
@ -92,7 +92,7 @@ ememory::Counter::remove ememory::Counter::decrementWeak() {
int64_t ememory::Counter::getCountWeak() const { int64_t ememory::Counter::getCountWeak() const {
int64_t out; int64_t out;
{ {
ethread::UniqueLock lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
out = m_counterWeak; out = m_counterWeak;
} }
return out; return out;
@ -101,7 +101,7 @@ int64_t ememory::Counter::getCountWeak() const {
int64_t ememory::Counter::getCountShared() const { int64_t ememory::Counter::getCountShared() const {
int64_t out; int64_t out;
{ {
ethread::UniqueLock lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
out = m_counterShared; out = m_counterShared;
} }
return out; return out;
@ -110,7 +110,7 @@ int64_t ememory::Counter::getCountShared() const {
int64_t ememory::Counter::getCount() const { int64_t ememory::Counter::getCount() const {
int64_t out; int64_t out;
{ {
ethread::UniqueLock lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
out = m_counterWeak + m_counterShared; out = m_counterWeak + m_counterShared;
} }
return out; return out;

View File

@ -1,17 +1,16 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#pragma once #pragma once
#include <ethread/Mutex.hpp> #include <mutex>
// define type : uintXX_t and intXX_t // define type : uintXX_t and intXX_t
#define __STDC_LIMIT_MACROS #define __STDC_LIMIT_MACROS
// note in android include the macro of min max are overwitten // note in android include the macro of min max are overwitten
extern "C" { #include <cstdint>
#include <stdint.h>
}
namespace ememory { namespace ememory {
/** /**
* @brief Couter is an important part of the SharedPtr/WeakPtr implementation. This use a simple refcounting method dut thread-safe * @brief Couter is an important part of the SharedPtr/WeakPtr implementation. This use a simple refcounting method dut thread-safe
@ -30,7 +29,7 @@ namespace ememory {
private: private:
int64_t m_counterShared; //!< Count of the active SharedPtr int64_t m_counterShared; //!< Count of the active SharedPtr
int64_t m_counterWeak; //!< Count of the active WeakPtr int64_t m_counterWeak; //!< Count of the active WeakPtr
mutable ethread::Mutex m_mutex; //!< local counter mutex to prevent the thread concurent removing mutable std::mutex m_mutex; //!< local counter mutex to prevent the thread concurent removing
public: public:
/** /**
* @brief Contructor * @brief Contructor

View File

@ -1,12 +1,12 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#pragma once #pragma once
#include <etk/Vector.hpp> #include <vector>
#include <ethread/Mutex.hpp> #include <mutex>
#include <ememory/SharedPtr.hpp> #include <ememory/SharedPtr.hpp>
#include <ememory/WeakPtr.hpp> #include <ememory/WeakPtr.hpp>

View File

@ -1,68 +0,0 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#include <ememory/RefCounter.hpp>
#include <ememory/debug.hpp>
#include <etk/typeInfo.hpp>
ETK_DECLARE_TYPE(ememory::RefCounter);
#ifdef DEBUG
static int32_t& getLocalDebugCounter() {
static int32_t g_uid = 0;
return g_uid;
}
void ememory::resetDebugRefCounter() {
getLocalDebugCounter() = 0;
}
#endif
ememory::RefCounter::RefCounter() {
#ifdef DEBUG
m_uid = getLocalDebugCounter()++;
#endif
}
ememory::RefCounter::~RefCounter() {
if (m_refCount != 0) {
EMEMORY_ERROR("delete a RefCounted element that is keep by somewhere !! " << m_refCount);
}
}
void ememory::RefCounter::refKeep() {
m_refCount++;
}
void ememory::RefCounter::refRelease() {
int refCount = --m_refCount;
if (refCount == 0) {
// No more element ==> remove it.
this->~RefCounter();
ETK_DELETE(ememory::RefCounter, this);
// NOTE: Do nothing more than this ==> it will not work
return;
}
if (refCount < 0) {
#ifdef DEBUG
EMEMORY_ERROR("request release a refcounted One more time than needed !! " << m_refCount << " uid=" << m_uid);
#else
EMEMORY_ERROR("request release a refcounted One more time than needed !! " << m_refCount);
#endif
m_refCount = 0;
}
}
int ememory::RefCounter::getRefCount() const {
return m_refCount;
}
uint64_t ememory::RefCounter::getRawPointer() const {
#ifdef DEBUG
return m_uid;
#endif
return uint64_t(this);
}

View File

@ -1,65 +0,0 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#pragma once
#include <etk/types.hpp>
#include <ememory/RefPtr.hpp>
namespace ememory {
/**
* @brief Use the element as a refcounted element
*/
class RefCounter {
#ifndef ETK_MEMORY_CHECKER
protected:
#else
public:
#endif
RefCounter();
// Virtualize destructor in private to prevent user ot remove it without permition
virtual ~RefCounter();
private:
int32_t m_refCount = 1;
#ifdef DEBUG
int32_t m_uid;
#endif
public:
/**
* @brief Keep a copy of this reference-counted element.
*/
void refKeep();
/**
* @brief Relese a copy of this reference-counted element.
*/
void refRelease();
/**
* @brief Get the number of time the object is required.
*/
int32_t getRefCount() const;
/**
* @brief Get the currect class SharedPtr
* @return Request SharedPtr
*/
//ememory::RefPtr<EMEMORY_TYPE> refFromThis();
/**
* @brief Get the currect class SharedPtr
* @return Request const SharedPtr
*/
//const ememory::RefPtr<EMEMORY_TYPE> refFromThis() const;
protected:
/**
* @brief get the RAW pointer value of this element (for debug only)
*/
uint64_t getRawPointer() const;
};
#ifdef DEBUG
void resetDebugRefCounter();
#endif
}
#include <ememory/details/EnableSharedFromThis.hxx>

View File

@ -1,167 +0,0 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#pragma once
#include <ememory/debug.hpp>
#include <ememory/RefCounter.hpp>
namespace ememory {
class RefCounter;
/**
* @brief ememory::Ref is a smart pointer on RefCounted element.
* this is the good way to use a RefCounter object to not missed to remove one of them.
*
* @note this is smaller than SharedPtr in memory impact and code done, then it is faster.
*/
template<typename EMEMORY_TYPE>
class RefPtr {
private:
EMEMORY_TYPE* m_element = null; //!< Pointer on the Data
public:
#ifndef PARSE_DOXYGEN
template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsSame<EMEMORY_TYPE2, EMEMORY_TYPE>::value
&& etk::IsBaseOf<ememory::RefCounter, EMEMORY_TYPE2>::value
, int>::type = 0>
RefPtr(EMEMORY_TYPE2* _element);
#else
/**
* @brief Contructor whith the pointer of data
* @param[in] _element allocated data (RefPtr will remove it)
*/
RefPtr(EMEMORY_TYPE2* _element);
#endif
public:
/**
* @brief Contructor on null
*/
RefPtr(etk::NullPtr);
/**
* @brief Contructor empty
*/
RefPtr();
/**
* @brief creator (to use wher create a new object (using nuw or ...)
* @param[in] _obj Pointer on the Data
*/
static inline RefPtr create(EMEMORY_TYPE* _obj);
/**
* @brief Contructor (API for casting)
* @param[in] _obj Pointer on the Data
*/
RefPtr(EMEMORY_TYPE* _obj);
/**
* @brief copy Contructor
* @param[in] _obj RefPtr to copy
*/
RefPtr(const RefPtr<EMEMORY_TYPE>& _obj);
/**
* @brief copy Contructor
* @param[in] _obj RefPtr to copy
*/
RefPtr(RefPtr<EMEMORY_TYPE>&& _obj);
/**
* @brief Destructor
*/
~RefPtr();
/**
* @brief Asignement operator
* @param[in] _obj RefPtr to copy
* @return Reference on this
*/
RefPtr& operator= (const RefPtr<EMEMORY_TYPE>& _obj);
/**
* @brief Asignement operator (asign null)
* @return Reference on this
*/
RefPtr& operator= (etk::NullPtr);
public:
#ifndef PARSE_DOXYGEN
template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsBaseOf<EMEMORY_TYPE, EMEMORY_TYPE2>::value
, int>::type = 0>
RefPtr(const RefPtr<EMEMORY_TYPE2>& _obj);
template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsBaseOf<EMEMORY_TYPE, EMEMORY_TYPE2>::value
, int>::type = 0>
RefPtr& operator= (const RefPtr<EMEMORY_TYPE2>& _obj);
#endif
public:
/**
* @brief Reset the RefPtr ==> Remove data if needed
*/
void reset();
/**
* @brief Get the number of conencted RefPtr
* @return Number of RefPtr on this data
*/
int useCount() const;
/**
* @brief Check if the RefPtr have an internal data (not null)
* @return true The pointer is not asigned, false otherwise
*/
bool operator==(etk::NullPtr) const;
/**
* @brief Check if two RefPtr are the same data (maybe not the same cast)
* @param[in] _obj Object to compare
* @return true The Object have the same pointer reference, false otherwise
*/
template<class EMEMORY_TYPE2>
bool operator==(const RefPtr<EMEMORY_TYPE2>& _obj) const;
/**
* @brief Check if the RefPtr have NOT an internal data (null)
* @return true The pointer is asigned, false otherwise
*/
bool operator!=(etk::NullPtr) const;
/**
* @brief Check if two RefPtr are NOT the same data (maybe not the same cast)
* @param[in] _obj Object to compare
* @return true The Object have NOT the same pointer reference, false otherwise
*/
template<class EMEMORY_TYPE2>
bool operator!=(const RefPtr<EMEMORY_TYPE2>& _obj) const;
/**
* @brief Get a const pointer on the data
* @return Data const pointer
*/
const EMEMORY_TYPE* get() const;
/**
* @brief Get a pointer on the data
* @return Data pointer
*/
EMEMORY_TYPE* get();
/**
* @brief Const dereferences the stored pointer.
* @return Const pointer on the Data
*/
const EMEMORY_TYPE* operator->() const;
/**
* @brief Dereferences the stored pointer.
* @return Pointer on the Data
*/
EMEMORY_TYPE* operator->();
/**
* @brief Get a const reference on the data
* @return Data const reference
*/
const EMEMORY_TYPE& operator*() const;
/**
* @brief Get a reference on the data
* @return Data reference
*/
EMEMORY_TYPE& operator*();
/**
* @brief Swap 2 Object inside the RefPtr
* @param[in] _obj Object to swap with
*/
void swap(RefPtr<EMEMORY_TYPE>& _obj);
// TODO: unique
// TODO: bool
};
}
#include <ememory/details/RefPtr.hxx>

View File

@ -1,11 +1,12 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#pragma once #pragma once
#include <etk/Function.hpp> #include <vector>
#include <mutex>
#include <ememory/debug.hpp> #include <ememory/debug.hpp>
#include <ememory/Counter.hpp> #include <ememory/Counter.hpp>
#include <ememory/EnableSharedFromThis.hpp> #include <ememory/EnableSharedFromThis.hpp>
@ -13,6 +14,7 @@
namespace ememory { namespace ememory {
template<typename> class WeakPtr; template<typename> class WeakPtr;
template<typename> class EnableSharedFromThis; template<typename> class EnableSharedFromThis;
using deleterCall = std::function<void(void* _data)>;
/** /**
* @brief ememory::SharedPtr is a smart pointer that retains shared ownership of an object through a pointer. * @brief ememory::SharedPtr is a smart pointer that retains shared ownership of an object through a pointer.
* Several SharedPtr objects may own the same object. The object is destroyed and its memory deallocated when * Several SharedPtr objects may own the same object. The object is destroyed and its memory deallocated when
@ -30,44 +32,42 @@ namespace ememory {
*/ */
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
class SharedPtr { class SharedPtr {
using deleterCall = etk::Function<void(void* _data)>;
friend class WeakPtr<EMEMORY_TYPE>; friend class WeakPtr<EMEMORY_TYPE>;
private: private:
EMEMORY_TYPE* m_element = null; //!< Pointer on the Data EMEMORY_TYPE* m_element; //!< Pointer on the Data
ememory::Counter* m_counter = null; //!< Pointer on the counter ememory::Counter* m_counter; //!< Pointer on the counter
deleterCall m_deleter; //!< Function to call to delete the data pointer deleterCall m_deleter; //!< Function to call to delete the data pointer
/** /**
* @brief Create the function to remove the pointer of the data * @brief Create the function to remove the pointer of the data
* @return deleter function (need access to a voind data access) * @return deleter function (need access to a voind data access)
*/ */
deleterCall createDeleter() const { deleterCall createDeleter() const {
return [](void* _data) { ETK_DELETE(EMEMORY_TYPE, _data);}; return [](void* _data) { delete((EMEMORY_TYPE*)_data);};
} }
public: public:
#ifndef PARSE_DOXYGEN #ifndef PARSE_DOXYGEN
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsSame<EMEMORY_TYPE2, EMEMORY_TYPE>::value typename std::enable_if< std::is_same<EMEMORY_TYPE2, EMEMORY_TYPE>::value
&& etk::IsBaseOf<ememory::EnableSharedFromThisBase, EMEMORY_TYPE2>::value && std::is_base_of<ememory::EnableSharedFromThisBase, EMEMORY_TYPE2>::value
, int>::type = 0> , int>::type = 0>
SharedPtr(EMEMORY_TYPE2* _element, deleterCall&& _deleter = [](void* _data) { ETK_DELETE(EMEMORY_TYPE, _data);}); SharedPtr(EMEMORY_TYPE2* _element);
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsSame<EMEMORY_TYPE2, EMEMORY_TYPE>::value typename std::enable_if< std::is_same<EMEMORY_TYPE2, EMEMORY_TYPE>::value
&& !etk::IsBaseOf<ememory::EnableSharedFromThisBase, EMEMORY_TYPE2>::value && !std::is_base_of<ememory::EnableSharedFromThisBase, EMEMORY_TYPE2>::value
, int>::type = 0> , int>::type = 0>
SharedPtr(EMEMORY_TYPE2* _element, deleterCall&& _deleter = [](void* _data) { ETK_DELETE(EMEMORY_TYPE, _data);}); SharedPtr(EMEMORY_TYPE2* _element);
#else #else
/** /**
* @brief Contructor whith the pointer of data * @brief Contructor whith the pointer of data
* @param[in] _element allocated data (SharedPtr will remove it) * @param[in] _element allocated data (SharedPtr will remove it)
* @param[in] _deleter Deleter function of the data.
*/ */
SharedPtr(EMEMORY_TYPE2* _element, deleterCall&& _deleter); SharedPtr(EMEMORY_TYPE2* _element);
#endif #endif
public: public:
/** /**
* @brief Contructor on null * @brief Contructor on nullptr
*/ */
SharedPtr(etk::NullPtr); SharedPtr(std::nullptr_t);
/** /**
* @brief Contructor empty * @brief Contructor empty
*/ */
@ -99,18 +99,18 @@ namespace ememory {
*/ */
SharedPtr& operator= (const SharedPtr<EMEMORY_TYPE>& _obj); SharedPtr& operator= (const SharedPtr<EMEMORY_TYPE>& _obj);
/** /**
* @brief Asignement operator (asign null) * @brief Asignement operator (asign nullptr)
* @return Reference on this * @return Reference on this
*/ */
SharedPtr& operator= (etk::NullPtr); SharedPtr& operator= (std::nullptr_t);
public: public:
#ifndef PARSE_DOXYGEN #ifndef PARSE_DOXYGEN
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsBaseOf<EMEMORY_TYPE, EMEMORY_TYPE2>::value typename std::enable_if< std::is_base_of<EMEMORY_TYPE, EMEMORY_TYPE2>::value
, int>::type = 0> , int>::type = 0>
SharedPtr(const SharedPtr<EMEMORY_TYPE2>& _obj); SharedPtr(const SharedPtr<EMEMORY_TYPE2>& _obj);
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsBaseOf<EMEMORY_TYPE, EMEMORY_TYPE2>::value typename std::enable_if< std::is_base_of<EMEMORY_TYPE, EMEMORY_TYPE2>::value
, int>::type = 0> , int>::type = 0>
SharedPtr& operator= (const SharedPtr<EMEMORY_TYPE2>& _obj); SharedPtr& operator= (const SharedPtr<EMEMORY_TYPE2>& _obj);
#endif #endif
@ -125,10 +125,10 @@ namespace ememory {
*/ */
int64_t useCount() const; int64_t useCount() const;
/** /**
* @brief Check if the SharedPtr have an internal data (not null) * @brief Check if the SharedPtr have an internal data (not nullptr)
* @return true The pointer is not asigned, false otherwise * @return true The pointer is not asigned, false otherwise
*/ */
bool operator==(etk::NullPtr) const; bool operator==(std::nullptr_t) const;
/** /**
* @brief Check if two SharedPtr are the same data (maybe not the same cast) * @brief Check if two SharedPtr are the same data (maybe not the same cast)
* @param[in] _obj Object to compare * @param[in] _obj Object to compare
@ -137,10 +137,10 @@ namespace ememory {
template<class EMEMORY_TYPE2> template<class EMEMORY_TYPE2>
bool operator==(const SharedPtr<EMEMORY_TYPE2>& _obj) const; bool operator==(const SharedPtr<EMEMORY_TYPE2>& _obj) const;
/** /**
* @brief Check if the SharedPtr have NOT an internal data (null) * @brief Check if the SharedPtr have NOT an internal data (nullptr)
* @return true The pointer is asigned, false otherwise * @return true The pointer is asigned, false otherwise
*/ */
bool operator!=(etk::NullPtr) const; bool operator!=(std::nullptr_t) const;
/** /**
* @brief Check if two SharedPtr are NOT the same data (maybe not the same cast) * @brief Check if two SharedPtr are NOT the same data (maybe not the same cast)
* @param[in] _obj Object to compare * @param[in] _obj Object to compare

View File

@ -1,155 +0,0 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#pragma once
#include <etk/types.hpp>
namespace ememory {
template <class EMEM_UPTR_TYPE>
class UniquePtr {
public:
using deleterCall = etk::Function<void(void* _data)>;
//private: Do it better ...
public:
EMEM_UPTR_TYPE* m_pointer;
deleterCall m_deleter; //!< Function to call to delete the data pointer
private:
template <class EMEM_UPTR_TYPE_2>
UniquePtr(UniquePtr<EMEM_UPTR_TYPE_2> &) = delete;
template <class EMEM_UPTR_TYPE_2>
UniquePtr &operator=(UniquePtr<EMEM_UPTR_TYPE_2> &) = delete;
public:
UniquePtr() :
m_pointer(null),
m_deleter(null) {
}
UniquePtr(etk::NullPtr) :
m_pointer(null),
m_deleter(null) {
}
explicit UniquePtr(EMEM_UPTR_TYPE* _obj, deleterCall&& _deleter = [](void* _data) { ETK_DELETE(EMEM_UPTR_TYPE, _data);}) :
m_pointer(_obj),
m_deleter(_deleter) {
ETK_MEM_CHECK_POINTER(_obj);
}
template <class EMEM_UPTR_TYPE_2>
UniquePtr(UniquePtr<EMEM_UPTR_TYPE_2>&& _obj) :
m_pointer(null),
m_deleter(null) {
etk::swap(_obj.m_pointer, m_pointer);
etk::swap(_obj.m_deleter, m_deleter);
}
~UniquePtr() {
reset();
}
UniquePtr& operator=(etk::NullPtr) {
reset();
return *this;
}
UniquePtr& operator=(UniquePtr&& _obj) {
reset();
etk::swap(_obj.m_pointer, m_pointer);
etk::swap(_obj.m_deleter, m_deleter);
return *this;
}
/*
template <class EMEM_UPTR_TYPE_2>
UniquePtr& operator=(UniquePtr<EMEM_UPTR_TYPE_2>& _obj){
reset();
m_pointer = _obj.m_pointer;
_obj.m_pointer = null;
return *this;
}*/
const EMEM_UPTR_TYPE& operator*() const {
return *m_pointer;
}
EMEM_UPTR_TYPE& operator*() {
return *m_pointer;
}
const EMEM_UPTR_TYPE* operator->() const {
return m_pointer;
}
EMEM_UPTR_TYPE* operator->() {
return m_pointer;
}
EMEM_UPTR_TYPE* get() const{
return m_pointer;
}
EMEM_UPTR_TYPE *release(){
EMEM_UPTR_TYPE *tmp = m_pointer;
m_pointer = null;
return tmp;
}
void reset(){
if (m_deleter != null) {
m_deleter(m_pointer);
}
m_pointer = null;
}
void swap(UniquePtr &_obj){
etk::swap(_obj.m_pointer, m_pointer);
etk::swap(_obj.m_deleter, m_deleter);
}
/**
* @brief Check if the UniquePtr have an internal data (not null)
* @return true The pointer is not asigned, false otherwise
*/
bool operator==(etk::NullPtr) const {
return m_pointer == null;
}
/**
* @brief Check if the UniquePtr have not an internal data (equal null)
* @return true The pointer is asigned, false otherwise
*/
bool operator!=(etk::NullPtr) const {
return m_pointer != null;
}
};
template<class EMEM_UPTR_TYPE, class... EMEM_UPTR_ARG>
UniquePtr<EMEM_UPTR_TYPE> makeUniquePtr(EMEM_UPTR_ARG ... _obj) {
return ememory::UniquePtr<EMEM_UPTR_TYPE>(ETK_NEW(EMEM_UPTR_TYPE, _obj...));
}
template<class EMEM_UPTR_TYPE_OUT, class EMEM_UPTR_TYPE, class... EMEM_UPTR_ARG>
UniquePtr<EMEM_UPTR_TYPE_OUT> makeUniquePtrCast(EMEM_UPTR_ARG ... _obj) {
return ememory::UniquePtr<EMEM_UPTR_TYPE_OUT>(ETK_NEW(EMEM_UPTR_TYPE, _obj...));
}
template <class EMEM_UPTR_TYPE>
inline void swap(UniquePtr<EMEM_UPTR_TYPE> &_obj1, UniquePtr<EMEM_UPTR_TYPE> &_obj2) {
_obj1.swap(_obj2);
}
/*
template <class EMEM_UPTR_TYPE_1, class EMEM_UPTR_DELETER_1, class EMEM_UPTR_TYPE_2, class EMEM_UPTR_DELETER_2>
bool operator==(const UniquePtr<EMEM_UPTR_TYPE_1, EMEM_UPTR_DELETER_1> &x, const UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER_2> &y) {
return x.get() == y.get();
}
template <class EMEM_UPTR_TYPE_1, class EMEM_UPTR_DELETER_1, class EMEM_UPTR_TYPE_2, class EMEM_UPTR_DELETER_2>
bool operator!=(const UniquePtr<EMEM_UPTR_TYPE_1, EMEM_UPTR_DELETER_1> &x, const UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER_2> &y) {
return x.get() != y.get();
}
template <class EMEM_UPTR_TYPE_1, class EMEM_UPTR_DELETER_1, class EMEM_UPTR_TYPE_2, class EMEM_UPTR_DELETER_2>
bool operator<(const UniquePtr<EMEM_UPTR_TYPE_1, EMEM_UPTR_DELETER_1> &x, const UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER_2> &y) {
return x.get() < y.get();
}
template <class EMEM_UPTR_TYPE_1, class EMEM_UPTR_DELETER_1, class EMEM_UPTR_TYPE_2, class EMEM_UPTR_DELETER_2>
bool operator<=(const UniquePtr<EMEM_UPTR_TYPE_1, EMEM_UPTR_DELETER_1> &x, const UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER_2> &y) {
return x.get() <= y.get();
}
template <class EMEM_UPTR_TYPE_1, class EMEM_UPTR_DELETER_1, class EMEM_UPTR_TYPE_2, class EMEM_UPTR_DELETER_2>
bool operator>(const UniquePtr<EMEM_UPTR_TYPE_1, EMEM_UPTR_DELETER_1> &x, const UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER_2> &y) {
return x.get() > y.get();
}
template <class EMEM_UPTR_TYPE_1, class EMEM_UPTR_DELETER_1, class EMEM_UPTR_TYPE_2, class EMEM_UPTR_DELETER_2>
bool operator>=(const UniquePtr<EMEM_UPTR_TYPE_1, EMEM_UPTR_DELETER_1> &x, const UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER_2> &y) {
return x.get() >= y.get();
}
*/
}

View File

@ -1,12 +1,12 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#pragma once #pragma once
#include <etk/Vector.hpp> #include <vector>
#include <ethread/Mutex.hpp> #include <mutex>
#include <ememory/debug.hpp> #include <ememory/debug.hpp>
#include <ememory/Counter.hpp> #include <ememory/Counter.hpp>
@ -29,9 +29,9 @@ namespace ememory {
*/ */
WeakPtr(); WeakPtr();
/** /**
* @brief null contructor * @brief nullptr contructor
*/ */
WeakPtr(etk::NullPtr); WeakPtr(std::nullptr_t);
private: private:
/** /**
* @brief Contructor of the Weak Ptr (specific for EnableSharedFromThis) * @brief Contructor of the Weak Ptr (specific for EnableSharedFromThis)
@ -68,17 +68,17 @@ namespace ememory {
*/ */
WeakPtr<EMEMORY_TYPE>& operator= (const SharedPtr<EMEMORY_TYPE>& _obj); WeakPtr<EMEMORY_TYPE>& operator= (const SharedPtr<EMEMORY_TYPE>& _obj);
/** /**
* @brief null asignement * @brief nullptr asignement
* @return Reference on this * @return Reference on this
*/ */
WeakPtr<EMEMORY_TYPE>& operator= (etk::NullPtr); WeakPtr<EMEMORY_TYPE>& operator= (std::nullptr_t);
/** /**
* @brief Copy contuctor of herited WeakPtr * @brief Copy contuctor of herited WeakPtr
* @param[in] _obj Object to copy * @param[in] _obj Object to copy
*/ */
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsVoid<EMEMORY_TYPE>::value typename std::enable_if< std::is_void<EMEMORY_TYPE>::value
&& !etk::IsVoid<EMEMORY_TYPE2>::value && !std::is_void<EMEMORY_TYPE2>::value
, int>::type = 0> , int>::type = 0>
WeakPtr(const SharedPtr<EMEMORY_TYPE2>& _obj); WeakPtr(const SharedPtr<EMEMORY_TYPE2>& _obj);
/** /**
@ -87,18 +87,18 @@ namespace ememory {
* @return Reference on this * @return Reference on this
*/ */
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsVoid<EMEMORY_TYPE>::value typename std::enable_if< std::is_void<EMEMORY_TYPE>::value
&& !etk::IsVoid<EMEMORY_TYPE2>::value && !std::is_void<EMEMORY_TYPE2>::value
, int>::type = 0> , int>::type = 0>
WeakPtr<EMEMORY_TYPE>& operator= (const SharedPtr<EMEMORY_TYPE2>& _obj); WeakPtr<EMEMORY_TYPE>& operator= (const SharedPtr<EMEMORY_TYPE2>& _obj);
public: public:
/* /*
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsBaseOf<EMEMORY_TYPE, EMEMORY_TYPE2>::value typename std::enable_if< std::is_base_of<EMEMORY_TYPE, EMEMORY_TYPE2>::value
, int>::type = 0> , int>::type = 0>
WeakPtr(const WeakPtr<EMEMORY_TYPE2>& _obj); WeakPtr(const WeakPtr<EMEMORY_TYPE2>& _obj);
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsBaseOf<EMEMORY_TYPE, EMEMORY_TYPE2>::value typename std::enable_if< std::is_base_of<EMEMORY_TYPE, EMEMORY_TYPE2>::value
, int>::type = 0> , int>::type = 0>
WeakPtr& operator= (const WeakPtr<EMEMORY_TYPE2>& _obj); WeakPtr& operator= (const WeakPtr<EMEMORY_TYPE2>& _obj);
*/ */
@ -123,28 +123,28 @@ namespace ememory {
* @return created SharedPtr * @return created SharedPtr
*/ */
ememory::SharedPtr<EMEMORY_TYPE> lock(); ememory::SharedPtr<EMEMORY_TYPE> lock();
/**
* @brief Check if the WeakPtr have an internal data (not null)
* @return true The pointer is not asigned, false otherwise
*/
bool operator==(etk::NullPtr) const;
/** /**
* @brief Check if two WeakPtr are the same data * @brief Check if two WeakPtr are the same data
* @param[in] _obj Object to compare * @param[in] _obj Object to compare
* @return true The Object have the same pointer reference, false otherwise * @return true The Object have the same pointer reference, false otherwise
*/ */
bool operator==(const WeakPtr& _obj) const; bool operator==(const WeakPtr& _obj);
/** /**
* @brief Check if the SharedPtr have NOT an internal data (null) * @brief Check if the WeakPtr have an internal data (not nullptr)
* @return true The pointer is asigned, false otherwise * @return true The pointer is not asigned, false otherwise
*/ */
bool operator!=(etk::NullPtr) const; bool operator==(std::nullptr_t) const;
/** /**
* @brief Check if two WeakPtr are different data * @brief Check if two WeakPtr are different data
* @param[in] _obj Object to compare * @param[in] _obj Object to compare
* @return true The Object have NOT the same pointer reference, false otherwise * @return true The Object have NOT the same pointer reference, false otherwise
*/ */
bool operator!=(const WeakPtr& _obj) const; bool operator!=(const WeakPtr& _obj);
/**
* @brief Check if the SharedPtr have NOT an internal data (nullptr)
* @return true The pointer is asigned, false otherwise
*/
bool operator!=(std::nullptr_t) const;
/** /**
* @brief Swap 2 Object inside the WeakPtr * @brief Swap 2 Object inside the WeakPtr
* @param[in] _obj Object to swap with * @param[in] _obj Object to swap with

View File

@ -3,7 +3,7 @@
* *
* @copyright 2011, Edouard DUPIN, all right reserved * @copyright 2011, Edouard DUPIN, all right reserved
* *
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#include <ememory/debug.hpp> #include <ememory/debug.hpp>

View File

@ -3,14 +3,12 @@
* *
* @copyright 2011, Edouard DUPIN, all right reserved * @copyright 2011, Edouard DUPIN, all right reserved
* *
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#pragma once #pragma once
#include <elog/log.hpp> #include <elog/log.hpp>
extern "C" { #include <cassert>
#include <assert.h>
}
namespace ememory { namespace ememory {
int32_t getLogId(); int32_t getLogId();

View File

@ -1,7 +1,7 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#pragma once #pragma once

View File

@ -1,200 +0,0 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#pragma once
#include <ememory/debug.hpp>
#include <ememory/RefPtr.hpp>
template<typename EMEMORY_TYPE>
template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsSame<EMEMORY_TYPE2, EMEMORY_TYPE>::value
&& etk::IsBaseOf<ememory::RefCounter, EMEMORY_TYPE2>::value
, int>::type>
ememory::RefPtr<EMEMORY_TYPE>::RefPtr(EMEMORY_TYPE2* _element):
m_element(_element) {
EMEMORY_DBG("Create shared");
if (m_element == null) {
return;
}
EMEMORY_DBG("Check if the user use the memory allocator or personal system...");
ETK_MEM_CHECK_POINTER(_element);
EMEMORY_DBG(" ==> get previous pointer");
m_element->refKeep();
return;
}
template<typename EMEMORY_TYPE>
ememory::RefPtr<EMEMORY_TYPE> ememory::RefPtr<EMEMORY_TYPE>::create(EMEMORY_TYPE* _obj) {
EMEMORY_DBG("Create shared");
ememory::RefPtr<EMEMORY_TYPE> tmp{_obj};
_obj->refRelease();
return tmp;
}
template<typename EMEMORY_TYPE>
ememory::RefPtr<EMEMORY_TYPE>::RefPtr():
m_element(null) {
EMEMORY_DBG("Create shared");
}
template<typename EMEMORY_TYPE>
ememory::RefPtr<EMEMORY_TYPE>::RefPtr(etk::NullPtr):
m_element(null) {
EMEMORY_DBG("Create shared");
}
template<typename EMEMORY_TYPE>
ememory::RefPtr<EMEMORY_TYPE>::RefPtr(EMEMORY_TYPE* _obj):
m_element(_obj) {
EMEMORY_DBG("Create shared (from a cast)");
if (m_element == null) {
return;
}
m_element->refKeep();
}
template<typename EMEMORY_TYPE>
ememory::RefPtr<EMEMORY_TYPE>::~RefPtr() {
EMEMORY_DBG("delete shared");
reset();
}
template<typename EMEMORY_TYPE>
ememory::RefPtr<EMEMORY_TYPE>::RefPtr(const ememory::RefPtr<EMEMORY_TYPE>& _obj):
m_element(_obj.m_element) {
if (m_element == null) {
return;
}
m_element->refKeep();
};
template<typename EMEMORY_TYPE>
ememory::RefPtr<EMEMORY_TYPE>& ememory::RefPtr<EMEMORY_TYPE>::operator= (const ememory::RefPtr<EMEMORY_TYPE>& _obj) {
reset();
m_element = _obj.m_element;
if (m_element == null) {
return *this;
}
m_element->refKeep();
return *this;
}
template<typename EMEMORY_TYPE>
ememory::RefPtr<EMEMORY_TYPE>& ememory::RefPtr<EMEMORY_TYPE>::operator= (etk::NullPtr) {
reset();
return *this;
}
template<typename EMEMORY_TYPE>
ememory::RefPtr<EMEMORY_TYPE>::RefPtr(ememory::RefPtr<EMEMORY_TYPE>&& _obj) {
m_element = _obj.m_element;
_obj.m_element = null;
}
template<typename EMEMORY_TYPE>
template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsBaseOf<EMEMORY_TYPE, EMEMORY_TYPE2>::value
, int>::type>
ememory::RefPtr<EMEMORY_TYPE>::RefPtr(const ememory::RefPtr<EMEMORY_TYPE2>& _obj):
m_element(const_cast<EMEMORY_TYPE2*>(_obj.get())) {
if (m_element == null) {
return;
}
m_element->refKeep();
}
template<typename EMEMORY_TYPE>
template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsBaseOf<EMEMORY_TYPE, EMEMORY_TYPE2>::value
, int>::type>
ememory::RefPtr<EMEMORY_TYPE>& ememory::RefPtr<EMEMORY_TYPE>::operator= (const RefPtr<EMEMORY_TYPE2>& _obj) {
reset();
m_element = const_cast<EMEMORY_TYPE2*>(_obj.get());
if (m_element == null) {
return *this;
}
m_element->refKeep();
return *this;
}
template<typename EMEMORY_TYPE>
void ememory::RefPtr<EMEMORY_TYPE>::reset() {
if(m_element == null) {
return;
}
EMEMORY_DBG("reset RefPtr (start)");
m_element->refRelease();
m_element = null;
EMEMORY_DBG("reset RefPtr (stop)");
}
template<typename EMEMORY_TYPE>
int ememory::RefPtr<EMEMORY_TYPE>::useCount() const {
if (m_element == null) {
return 0;
}
return m_element->getRefCount();
}
template<typename EMEMORY_TYPE>
bool ememory::RefPtr<EMEMORY_TYPE>::operator==(etk::NullPtr) const {
return m_element == null;
}
template<typename EMEMORY_TYPE>
template<class EMEMORY_TYPE2>
bool ememory::RefPtr<EMEMORY_TYPE>::operator==(const RefPtr<EMEMORY_TYPE2>& _obj) const {
return m_element == _obj.get();
}
template<typename EMEMORY_TYPE>
bool ememory::RefPtr<EMEMORY_TYPE>::operator!=(etk::NullPtr) const {
return m_element != null;
}
template<typename EMEMORY_TYPE>
template<typename EMEMORY_TYPE2>
bool ememory::RefPtr<EMEMORY_TYPE>::operator!=(const RefPtr<EMEMORY_TYPE2>& _obj) const {
return m_element != _obj.get();
}
template<typename EMEMORY_TYPE>
const EMEMORY_TYPE* ememory::RefPtr<EMEMORY_TYPE>::get() const {
return m_element;
}
template<typename EMEMORY_TYPE>
EMEMORY_TYPE* ememory::RefPtr<EMEMORY_TYPE>::get() {
return m_element;
}
template<typename EMEMORY_TYPE>
const EMEMORY_TYPE* ememory::RefPtr<EMEMORY_TYPE>::operator->() const {
return m_element;
}
template<typename EMEMORY_TYPE>
EMEMORY_TYPE* ememory::RefPtr<EMEMORY_TYPE>::operator->() {
return m_element;
}
template<typename EMEMORY_TYPE>
const EMEMORY_TYPE& ememory::RefPtr<EMEMORY_TYPE>::operator*() const {
return *m_element;
}
template<typename EMEMORY_TYPE>
EMEMORY_TYPE& ememory::RefPtr<EMEMORY_TYPE>::operator*() {
return *m_element;
}
template<typename EMEMORY_TYPE>
void ememory::RefPtr<EMEMORY_TYPE>::swap(RefPtr& _obj) {
etk::swap(_obj.m_element, m_element);
}

View File

@ -1,31 +1,31 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#pragma once #pragma once
#include <vector>
#include <mutex>
#include <ememory/debug.hpp> #include <ememory/debug.hpp>
#include <ememory/WeakPtr.hpp> #include <ememory/WeakPtr.hpp>
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsSame<EMEMORY_TYPE2, EMEMORY_TYPE>::value typename std::enable_if< std::is_same<EMEMORY_TYPE2, EMEMORY_TYPE>::value
&& etk::IsBaseOf<ememory::EnableSharedFromThisBase, EMEMORY_TYPE2>::value && std::is_base_of<ememory::EnableSharedFromThisBase, EMEMORY_TYPE2>::value
, int>::type> , int>::type>
ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(EMEMORY_TYPE2* _element, deleterCall&& _deleter): ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(EMEMORY_TYPE2* _element):
m_element(_element), m_element(_element),
m_counter(null), m_counter(nullptr),
m_deleter(_deleter) { m_deleter(createDeleter()) {
EMEMORY_DBG("Create shared"); EMEMORY_DBG("new shared");
if (m_element == null) { if (m_element == nullptr) {
return; return;
} }
EMEMORY_DBG("Check if the user use the memory allocator or personal system...");
ETK_MEM_CHECK_POINTER(_element);
EMEMORY_DBG(" ==> get previous pointer"); EMEMORY_DBG(" ==> get previous pointer");
m_counter = m_element->weakFromThis().getCounter(); m_counter = m_element->weakFromThis().getCounter();
if (m_counter != null) { if (m_counter != nullptr) {
m_counter->incrementShared(); m_counter->incrementShared();
} }
return; return;
@ -35,36 +35,34 @@ ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(EMEMORY_TYPE2* _element, deleterCall
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsSame<EMEMORY_TYPE2, EMEMORY_TYPE>::value typename std::enable_if< std::is_same<EMEMORY_TYPE2, EMEMORY_TYPE>::value
&& !etk::IsBaseOf<ememory::EnableSharedFromThisBase, EMEMORY_TYPE2>::value && !std::is_base_of<ememory::EnableSharedFromThisBase, EMEMORY_TYPE2>::value
, int>::type> , int>::type>
ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(EMEMORY_TYPE2* _element, deleterCall&& _deleter): ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(EMEMORY_TYPE2* _element):
m_element(_element), m_element(_element),
m_counter(null), m_counter(nullptr),
m_deleter(_deleter) { m_deleter(createDeleter()) {
EMEMORY_DBG("Create shared"); EMEMORY_DBG("new shared");
if (m_element == null) { if (m_element == nullptr) {
return; return;
} }
EMEMORY_DBG("Check if the user use the memory allocator or personal system..."); m_counter = new ememory::Counter(false);
ETK_MEM_CHECK_POINTER(_element);
m_counter = ETK_NEW(ememory::Counter, false);
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(): ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr():
m_element(null), m_element(nullptr),
m_counter(null), m_counter(nullptr),
m_deleter(createDeleter()) { m_deleter(createDeleter()) {
EMEMORY_DBG("Create shared"); EMEMORY_DBG("new shared");
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(etk::NullPtr): ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(std::nullptr_t):
m_element(null), m_element(nullptr),
m_counter(null), m_counter(nullptr),
m_deleter(createDeleter()) { m_deleter(createDeleter()) {
EMEMORY_DBG("Create shared"); EMEMORY_DBG("new shared");
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
@ -72,9 +70,9 @@ ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(EMEMORY_TYPE* _obj, ememory::Counter
m_element(_obj), m_element(_obj),
m_counter(_counter), m_counter(_counter),
m_deleter(createDeleter()) { m_deleter(createDeleter()) {
EMEMORY_DBG("Create shared (from a cast)"); EMEMORY_DBG("new shared (from a cast)");
if (_obj == null) { if (_obj == nullptr) {
m_counter = null; m_counter = nullptr;
return; return;
} }
m_counter->incrementShared(); m_counter->incrementShared();
@ -91,14 +89,14 @@ ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(const ememory::SharedPtr<EMEMORY_TYP
m_element(_obj.m_element), m_element(_obj.m_element),
m_counter(_obj.m_counter), m_counter(_obj.m_counter),
m_deleter(_obj.m_deleter) { m_deleter(_obj.m_deleter) {
if ( m_element == null if ( m_element == nullptr
|| m_counter == null) { || m_counter == nullptr) {
m_element = null; m_element = nullptr;
m_counter = null; m_counter = nullptr;
m_deleter = null; m_deleter = nullptr;
return; return;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return; return;
} }
m_counter->incrementShared(); m_counter->incrementShared();
@ -110,14 +108,14 @@ ememory::SharedPtr<EMEMORY_TYPE>& ememory::SharedPtr<EMEMORY_TYPE>::operator= (c
m_element = _obj.m_element; m_element = _obj.m_element;
m_counter = _obj.m_counter; m_counter = _obj.m_counter;
m_deleter = _obj.m_deleter; m_deleter = _obj.m_deleter;
if ( m_element == null if ( m_element == nullptr
|| m_counter == null) { || m_counter == nullptr) {
m_element = null; m_element = nullptr;
m_counter = null; m_counter = nullptr;
m_deleter = null; m_deleter = nullptr;
return *this; return *this;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return *this; return *this;
} }
m_counter->incrementShared(); m_counter->incrementShared();
@ -125,7 +123,7 @@ ememory::SharedPtr<EMEMORY_TYPE>& ememory::SharedPtr<EMEMORY_TYPE>::operator= (c
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
ememory::SharedPtr<EMEMORY_TYPE>& ememory::SharedPtr<EMEMORY_TYPE>::operator= (etk::NullPtr) { ememory::SharedPtr<EMEMORY_TYPE>& ememory::SharedPtr<EMEMORY_TYPE>::operator= (std::nullptr_t) {
reset(); reset();
return *this; return *this;
} }
@ -137,26 +135,26 @@ ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(ememory::SharedPtr<EMEMORY_TYPE>&& _
m_element = _obj.m_element; m_element = _obj.m_element;
m_counter = _obj.m_counter; m_counter = _obj.m_counter;
m_deleter = _obj.m_deleter; m_deleter = _obj.m_deleter;
_obj.m_element = null; _obj.m_element = nullptr;
_obj.m_counter = null; _obj.m_counter = nullptr;
_obj.m_deleter = null; _obj.m_deleter = nullptr;
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsBaseOf<EMEMORY_TYPE, EMEMORY_TYPE2>::value typename std::enable_if< std::is_base_of<EMEMORY_TYPE, EMEMORY_TYPE2>::value
, int>::type> , int>::type>
ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(const ememory::SharedPtr<EMEMORY_TYPE2>& _obj): ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(const ememory::SharedPtr<EMEMORY_TYPE2>& _obj):
m_element(const_cast<EMEMORY_TYPE2*>(_obj.get())), m_element(const_cast<EMEMORY_TYPE2*>(_obj.get())),
m_counter(const_cast<ememory::Counter*>(_obj.getCounter())), m_counter(const_cast<ememory::Counter*>(_obj.getCounter())),
m_deleter(createDeleter()) { m_deleter(createDeleter()) {
if ( m_element == null if ( m_element == nullptr
|| m_counter == null) { || m_counter == nullptr) {
m_element = null; m_element = nullptr;
m_counter = null; m_counter = nullptr;
return; return;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return; return;
} }
m_counter->incrementShared(); m_counter->incrementShared();
@ -164,20 +162,20 @@ ememory::SharedPtr<EMEMORY_TYPE>::SharedPtr(const ememory::SharedPtr<EMEMORY_TYP
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsBaseOf<EMEMORY_TYPE, EMEMORY_TYPE2>::value typename std::enable_if< std::is_base_of<EMEMORY_TYPE, EMEMORY_TYPE2>::value
, int>::type> , int>::type>
ememory::SharedPtr<EMEMORY_TYPE>& ememory::SharedPtr<EMEMORY_TYPE>::operator= (const SharedPtr<EMEMORY_TYPE2>& _obj) { ememory::SharedPtr<EMEMORY_TYPE>& ememory::SharedPtr<EMEMORY_TYPE>::operator= (const SharedPtr<EMEMORY_TYPE2>& _obj) {
reset(); reset();
m_element = const_cast<EMEMORY_TYPE2*>(_obj.get()); m_element = const_cast<EMEMORY_TYPE2*>(_obj.get());
m_counter = const_cast<ememory::Counter*>(_obj.getCounter()); m_counter = const_cast<ememory::Counter*>(_obj.getCounter());
m_deleter = createDeleter(); m_deleter = createDeleter();
if ( m_element == null if ( m_element == nullptr
|| m_counter == null) { || m_counter == nullptr) {
m_element = null; m_element = nullptr;
m_counter = null; m_counter = nullptr;
return *this; return *this;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return *this; return *this;
} }
m_counter->incrementShared(); m_counter->incrementShared();
@ -186,56 +184,52 @@ ememory::SharedPtr<EMEMORY_TYPE>& ememory::SharedPtr<EMEMORY_TYPE>::operator= (c
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
void ememory::SharedPtr<EMEMORY_TYPE>::reset() { void ememory::SharedPtr<EMEMORY_TYPE>::reset() {
if(m_counter == null) { if(m_counter == nullptr) {
m_element = null; // in case ... m_element = nullptr; // in case ...
m_deleter = null; m_deleter = nullptr;
return; return;
} }
EMEMORY_DBG("reset sharedPtr (start)"); EMEMORY_DBG("reset sharedPtr (start)");
ememory::Counter::remove rmData = m_counter->decrementShared(); ememory::Counter::remove rmData = m_counter->decrementShared();
switch(rmData) { switch(rmData) {
case ememory::Counter::remove::all: case ememory::Counter::remove::all:
ETK_DELETE(ememory::Counter, m_counter); delete m_counter;
if (m_deleter != null) { if (m_deleter != nullptr) {
if (m_element != null) { m_deleter((void*)m_element);
m_deleter((void*)m_element);
}
} else { } else {
EMEMORY_WARNING("Maybe a leak ==> no deleter of the SharedPtr"); EMEMORY_WARNING("Maybe a leak ==> no deleter of the SharedPtr");
} }
break; break;
case ememory::Counter::remove::data: case ememory::Counter::remove::data:
if (m_deleter != null) { if (m_deleter != nullptr) {
if (m_element != null) { m_deleter((void*)m_element);
m_deleter((void*)m_element);
}
} else { } else {
EMEMORY_WARNING("Maybe a leak ==> no deleter of the SharedPtr"); EMEMORY_WARNING("Maybe a leak ==> no deleter of the SharedPtr");
} }
break; break;
case ememory::Counter::remove::counter: case ememory::Counter::remove::counter:
ETK_DELETE(ememory::Counter, m_counter); delete m_counter;
break; break;
case ememory::Counter::remove::none: case ememory::Counter::remove::none:
break; break;
} }
m_deleter = null; m_deleter = nullptr;
m_counter = null; m_counter = nullptr;
m_element = null; m_element = nullptr;
EMEMORY_DBG("reset sharedPtr (stop)"); EMEMORY_DBG("reset sharedPtr (stop)");
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
int64_t ememory::SharedPtr<EMEMORY_TYPE>::useCount() const { int64_t ememory::SharedPtr<EMEMORY_TYPE>::useCount() const {
if (m_counter == null) { if (m_counter == nullptr) {
return 0; return 0;
} }
return m_counter->getCountShared(); return m_counter->getCountShared();
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
bool ememory::SharedPtr<EMEMORY_TYPE>::operator==(etk::NullPtr) const { bool ememory::SharedPtr<EMEMORY_TYPE>::operator==(std::nullptr_t) const {
return m_counter == null; return m_counter == nullptr;
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
@ -245,8 +239,8 @@ bool ememory::SharedPtr<EMEMORY_TYPE>::operator==(const SharedPtr<EMEMORY_TYPE2>
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
bool ememory::SharedPtr<EMEMORY_TYPE>::operator!=(etk::NullPtr) const { bool ememory::SharedPtr<EMEMORY_TYPE>::operator!=(std::nullptr_t) const {
return m_counter != null; return m_counter != nullptr;
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
@ -288,9 +282,15 @@ EMEMORY_TYPE& ememory::SharedPtr<EMEMORY_TYPE>::operator*() {
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
void ememory::SharedPtr<EMEMORY_TYPE>::swap(SharedPtr& _obj) { void ememory::SharedPtr<EMEMORY_TYPE>::swap(SharedPtr& _obj) {
etk::swap(_obj.m_element, m_element); EMEMORY_TYPE* tmpE = m_element;
etk::swap(_obj.m_counter, m_counter); ememory::Counter* tmpC = m_counter;
etk::swap(_obj.m_deleter, m_deleter); deleterCall tmpD = m_deleter;
m_element = _obj.m_element;
m_counter = _obj.m_counter;
m_deleter = _obj.m_deleter;
_obj.m_element = tmpE;
_obj.m_counter = tmpC;
_obj.m_deleter = tmpD;
} }
@ -323,77 +323,58 @@ namespace ememory {
class SharedPtr<void> { class SharedPtr<void> {
friend class WeakPtr<void>; friend class WeakPtr<void>;
public: public:
using deleterCall = etk::Function<void(void* _data)>; using deleterCall = std::function<void(void* _data)>;
private: private:
void* m_element; void* m_element;
ememory::Counter* m_counter; ememory::Counter* m_counter;
deleterCall m_deleter; //!< Function to call to delete the data pointer
public: public:
SharedPtr(void* _element); SharedPtr(void* _element);
public: public:
SharedPtr(etk::NullPtr): SharedPtr(std::nullptr_t):
m_element(null), m_element(nullptr),
m_counter(null) { m_counter(nullptr) {
EMEMORY_DBG("Create shared<void>"); EMEMORY_DBG("new shared<void>");
} }
SharedPtr(): SharedPtr():
m_element(null), m_element(nullptr),
m_counter(null) { m_counter(nullptr) {
EMEMORY_DBG("Create shared<void>"); EMEMORY_DBG("new shared<void>");
} }
~SharedPtr() { ~SharedPtr() {
EMEMORY_DBG("delete shared"); EMEMORY_DBG("delete shared");
reset(); reset();
} }
SharedPtr(void* _obj, ememory::Counter* _counter): SharedPtr(void* _obj, ememory::Counter* _counter):
m_element(_obj), m_element(_obj),
m_counter(_counter) { m_counter(_counter) {
EMEMORY_DBG("Create shared (from a cast)"); EMEMORY_DBG("new shared (from a cast)");
if (_obj == null) { if (_obj == nullptr) {
m_counter = null; m_counter = nullptr;
return; return;
} }
m_counter->incrementShared(); m_counter->incrementShared();
} }
SharedPtr& operator= (etk::NullPtr) { SharedPtr& operator= (std::nullptr_t) {
reset(); reset();
return *this; return *this;
} }
SharedPtr(SharedPtr<void>&& _obj) { SharedPtr(SharedPtr<void>&& _obj) {
m_element = _obj.m_element; m_element = _obj.m_element;
m_counter = _obj.m_counter; m_counter = _obj.m_counter;
m_deleter = _obj.m_deleter; _obj.m_element = nullptr;
_obj.m_element = null; _obj.m_counter = nullptr;
_obj.m_counter = null;
_obj.m_deleter = null;
} }
template<class EMEMORY_TYPE2> template<class EMEMORY_TYPE2>
SharedPtr(const SharedPtr<EMEMORY_TYPE2>& _obj): SharedPtr(const SharedPtr<EMEMORY_TYPE2>& _obj):
m_element((void*)_obj.get()), m_element((void*)_obj.get()),
m_counter(_obj.getCounter()), m_counter(_obj.getCounter()) {
m_deleter(_obj.getDeleter()) { if ( m_element == nullptr
if ( m_element == null || m_counter == nullptr) {
|| m_counter == null) { m_element = nullptr;
m_element = null; m_counter = nullptr;
m_counter = null;
return; return;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return;
}
m_counter->incrementShared();
}
SharedPtr(const SharedPtr<void>& _obj) {
m_element = _obj.m_element;
m_counter = _obj.m_counter;
m_deleter = _obj.m_deleter;
if ( m_element == null
|| m_counter == null) {
m_element = null;
m_counter = null;
return;
}
if (m_counter == null) {
return; return;
} }
m_counter->incrementShared(); m_counter->incrementShared();
@ -403,31 +384,13 @@ namespace ememory {
reset(); reset();
m_element = (void*)_obj.get(); m_element = (void*)_obj.get();
m_counter = _obj.getCounter(); m_counter = _obj.getCounter();
m_deleter = _obj.getDeleter(); if ( m_element == nullptr
if ( m_element == null || m_counter == nullptr) {
|| m_counter == null) { m_element = nullptr;
m_element = null; m_counter = nullptr;
m_counter = null;
return *this; return *this;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return *this;
}
m_counter->incrementShared();
return *this;
}
SharedPtr& operator= (const SharedPtr<void>& _obj) {
reset();
m_element = _obj.m_element;
m_counter = _obj.m_counter;
m_deleter = _obj.m_deleter;
if ( m_element == null
|| m_counter == null) {
m_element = null;
m_counter = null;
return *this;
}
if (m_counter == null) {
return *this; return *this;
} }
m_counter->incrementShared(); m_counter->incrementShared();
@ -435,57 +398,45 @@ namespace ememory {
} }
public: public:
void reset() { void reset() {
if(m_counter == null) { if(m_counter == nullptr) {
m_element = null; // in case ... m_element = nullptr; // in case ...
return; return;
} }
EMEMORY_DBG("reset sharedPtr (start)"); EMEMORY_DBG("reset sharedPtr (start)");
ememory::Counter::remove rmData = m_counter->decrementShared(); ememory::Counter::remove rmData = m_counter->decrementShared();
switch(rmData) { switch(rmData) {
case ememory::Counter::remove::all: case ememory::Counter::remove::all:
ETK_DELETE(ememory::Counter, m_counter); delete m_counter;
if (m_deleter != null) { EMEMORY_WARNING("Maybe a leak ==> no deleter of the SharedPtr<void>");
if (m_element != null) {
m_deleter((void*)m_element);
}
} else {
EMEMORY_WARNING("Maybe a leak ==> no deleter of the SharedPtr<void>");
}
break; break;
case ememory::Counter::remove::data: case ememory::Counter::remove::data:
if (m_deleter != null) { EMEMORY_WARNING("Maybe a leak ==> no deleter of the SharedPtr<void>");
if (m_element != null) {
m_deleter((void*)m_element);
}
} else {
EMEMORY_WARNING("Maybe a leak ==> no deleter of the SharedPtr<void>");
}
break; break;
case ememory::Counter::remove::counter: case ememory::Counter::remove::counter:
ETK_DELETE(ememory::Counter, m_counter); delete m_counter;
break; break;
case ememory::Counter::remove::none: case ememory::Counter::remove::none:
break; break;
} }
m_counter = null; m_counter = nullptr;
m_element = null; m_element = nullptr;
EMEMORY_DBG("reset sharedPtr (stop)"); EMEMORY_DBG("reset sharedPtr (stop)");
} }
int64_t useCount() const { int64_t useCount() const {
if (m_counter == null) { if (m_counter == nullptr) {
return 0; return 0;
} }
return m_counter->getCountShared(); return m_counter->getCountShared();
} }
bool operator==(etk::NullPtr) const { bool operator==(std::nullptr_t) const {
return m_counter == null; return m_counter == nullptr;
} }
template<class EMEMORY_TYPE2> template<class EMEMORY_TYPE2>
bool operator==(const SharedPtr<EMEMORY_TYPE2>& _obj) const { bool operator==(const SharedPtr<EMEMORY_TYPE2>& _obj) const {
return m_counter == _obj.m_counter; return m_counter == _obj.m_counter;
} }
bool operator!=(etk::NullPtr) const { bool operator!=(std::nullptr_t) const {
return m_counter != null; return m_counter != nullptr;
} }
template<class EMEMORY_TYPE2> template<class EMEMORY_TYPE2>
bool operator!=(const SharedPtr<EMEMORY_TYPE2>& _obj) const { bool operator!=(const SharedPtr<EMEMORY_TYPE2>& _obj) const {
@ -514,9 +465,6 @@ namespace ememory {
ememory::Counter* getCounter() const { ememory::Counter* getCounter() const {
return m_counter; return m_counter;
} }
deleterCall getDeleter() const {
return m_deleter;
}
// TODO: unique // TODO: unique
// TODO: bool // TODO: bool
}; };

View File

@ -1,37 +1,38 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#pragma once #pragma once
#include <ethread/Mutex.hpp> #include <vector>
#include <mutex>
#include <ememory/debug.hpp> #include <ememory/debug.hpp>
#include <ememory/Counter.hpp> #include <ememory/Counter.hpp>
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(): ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr():
m_element(null), m_element(nullptr),
m_counter(null) { m_counter(nullptr) {
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(etk::NullPtr): ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(std::nullptr_t):
m_element(null), m_element(nullptr),
m_counter(null) { m_counter(nullptr) {
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(EMEMORY_TYPE* _element): ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(EMEMORY_TYPE* _element):
m_element(_element), m_element(_element),
m_counter(null) { m_counter(nullptr) {
EMEMORY_DBG("New weak"); EMEMORY_DBG("new weak");
if (m_element == null) { if (m_element == nullptr) {
return; return;
} }
m_counter = ETK_NEW(ememory::Counter, true); m_counter = new ememory::Counter(true);
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
@ -43,13 +44,13 @@ template<typename EMEMORY_TYPE>
ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(const ememory::WeakPtr<EMEMORY_TYPE>& _obj): ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(const ememory::WeakPtr<EMEMORY_TYPE>& _obj):
m_element(_obj.m_element), m_element(_obj.m_element),
m_counter(_obj.m_counter) { m_counter(_obj.m_counter) {
if ( m_element == null if ( m_element == nullptr
|| m_counter == null) { || m_counter == nullptr) {
m_element = null; m_element = nullptr;
m_counter = null; m_counter = nullptr;
return; return;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return; return;
} }
m_counter->incrementWeak(); m_counter->incrementWeak();
@ -60,13 +61,13 @@ ememory::WeakPtr<EMEMORY_TYPE>& ememory::WeakPtr<EMEMORY_TYPE>::operator= (const
reset(); reset();
m_element = _obj.m_element; m_element = _obj.m_element;
m_counter = _obj.m_counter; m_counter = _obj.m_counter;
if ( m_element == null if ( m_element == nullptr
|| m_counter == null) { || m_counter == nullptr) {
m_element = null; m_element = nullptr;
m_counter = null; m_counter = nullptr;
return *this; return *this;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return *this; return *this;
} }
m_counter->incrementWeak(); m_counter->incrementWeak();
@ -74,7 +75,7 @@ ememory::WeakPtr<EMEMORY_TYPE>& ememory::WeakPtr<EMEMORY_TYPE>::operator= (const
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
ememory::WeakPtr<EMEMORY_TYPE>& ememory::WeakPtr<EMEMORY_TYPE>::operator= (etk::NullPtr) { ememory::WeakPtr<EMEMORY_TYPE>& ememory::WeakPtr<EMEMORY_TYPE>::operator= (std::nullptr_t) {
reset(); reset();
return *this; return *this;
} }
@ -83,21 +84,21 @@ template<typename EMEMORY_TYPE>
ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(ememory::WeakPtr<EMEMORY_TYPE>&& _obj) { ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(ememory::WeakPtr<EMEMORY_TYPE>&& _obj) {
m_element = _obj.m_element; m_element = _obj.m_element;
m_counter = _obj.m_counter; m_counter = _obj.m_counter;
_obj.m_element = null; _obj.m_element = nullptr;
_obj.m_counter = null; _obj.m_counter = nullptr;
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(const ememory::SharedPtr<EMEMORY_TYPE>& _obj): ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(const ememory::SharedPtr<EMEMORY_TYPE>& _obj):
m_element(_obj.m_element), m_element(_obj.m_element),
m_counter(_obj.m_counter) { m_counter(_obj.m_counter) {
if ( m_element == null if ( m_element == nullptr
|| m_counter == null) { || m_counter == nullptr) {
m_element = null; m_element = nullptr;
m_counter = null; m_counter = nullptr;
return; return;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return; return;
} }
m_counter->incrementWeak(); m_counter->incrementWeak();
@ -109,13 +110,13 @@ ememory::WeakPtr<EMEMORY_TYPE>& ememory::WeakPtr<EMEMORY_TYPE>::operator= (const
reset(); reset();
m_element = _obj.m_element; m_element = _obj.m_element;
m_counter = _obj.m_counter; m_counter = _obj.m_counter;
if ( m_element == null if ( m_element == nullptr
|| m_counter == null) { || m_counter == nullptr) {
m_element = null; m_element = nullptr;
m_counter = null; m_counter = nullptr;
return *this; return *this;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return *this; return *this;
} }
m_counter->incrementWeak(); m_counter->incrementWeak();
@ -125,19 +126,19 @@ ememory::WeakPtr<EMEMORY_TYPE>& ememory::WeakPtr<EMEMORY_TYPE>::operator= (const
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsVoid<EMEMORY_TYPE>::value typename std::enable_if< std::is_void<EMEMORY_TYPE>::value
&& !etk::IsVoid<EMEMORY_TYPE2>::value && !std::is_void<EMEMORY_TYPE2>::value
, int>::type> , int>::type>
ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(const ememory::SharedPtr<EMEMORY_TYPE2>& _obj): ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(const ememory::SharedPtr<EMEMORY_TYPE2>& _obj):
m_element((void*)_obj.get()), m_element((void*)_obj.get()),
m_counter(_obj.getCounter()) { m_counter(_obj.getCounter()) {
if ( m_element == null if ( m_element == nullptr
|| m_counter == null) { || m_counter == nullptr) {
m_element = null; m_element = nullptr;
m_counter = null; m_counter = nullptr;
return; return;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return; return;
} }
m_counter->incrementWeak(); m_counter->incrementWeak();
@ -146,20 +147,20 @@ ememory::WeakPtr<EMEMORY_TYPE>::WeakPtr(const ememory::SharedPtr<EMEMORY_TYPE2>&
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsVoid<EMEMORY_TYPE>::value typename std::enable_if< std::is_void<EMEMORY_TYPE>::value
&& !etk::IsVoid<EMEMORY_TYPE2>::value && !std::is_void<EMEMORY_TYPE2>::value
, int>::type> , int>::type>
ememory::WeakPtr<EMEMORY_TYPE>& ememory::WeakPtr<EMEMORY_TYPE>::operator= (const ememory::SharedPtr<EMEMORY_TYPE2>& _obj) { ememory::WeakPtr<EMEMORY_TYPE>& ememory::WeakPtr<EMEMORY_TYPE>::operator= (const ememory::SharedPtr<EMEMORY_TYPE2>& _obj) {
reset(); reset();
m_element = (void*)_obj.get(); m_element = (void*)_obj.get();
m_counter = _obj.getCounter(); m_counter = _obj.getCounter();
if ( m_element == null if ( m_element == nullptr
|| m_counter == null) { || m_counter == nullptr) {
m_element = null; m_element = nullptr;
m_counter = null; m_counter = nullptr;
return *this; return *this;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return *this; return *this;
} }
m_counter->incrementWeak(); m_counter->incrementWeak();
@ -169,37 +170,37 @@ ememory::WeakPtr<EMEMORY_TYPE>& ememory::WeakPtr<EMEMORY_TYPE>::operator= (const
/* /*
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsBaseOf<EMEMORY_TYPE, EMEMORY_TYPE2>::value typename std::enable_if< std::is_base_of<EMEMORY_TYPE, EMEMORY_TYPE2>::value
, int>::type> , int>::type>
WeakPtr(const WeakPtr<EMEMORY_TYPE2>& _obj): WeakPtr(const WeakPtr<EMEMORY_TYPE2>& _obj):
m_element(_obj.m_element), m_element(_obj.m_element),
m_counter(_obj.getCounter()) { m_counter(_obj.getCounter()) {
if ( m_element == null if ( m_element == nullptr
|| m_counter == null) { || m_counter == nullptr) {
m_element = null; m_element = nullptr;
m_counter = null; m_counter = nullptr;
return; return;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return; return;
} }
m_counter->incrementWeak(); m_counter->incrementWeak();
} }
template<class EMEMORY_TYPE2, template<class EMEMORY_TYPE2,
typename etk::EnableIf< etk::IsBaseOf<EMEMORY_TYPE, EMEMORY_TYPE2>::value typename std::enable_if< std::is_base_of<EMEMORY_TYPE, EMEMORY_TYPE2>::value
, int>::type> , int>::type>
WeakPtr& operator= (const WeakPtr<EMEMORY_TYPE2>& _obj) { WeakPtr& operator= (const WeakPtr<EMEMORY_TYPE2>& _obj) {
reset(); reset();
m_element = _obj.m_element; m_element = _obj.m_element;
m_counter = _obj.getCounter(); m_counter = _obj.getCounter();
if ( m_element == null if ( m_element == nullptr
|| m_counter == null) { || m_counter == nullptr) {
m_element = null; m_element = nullptr;
m_counter = null; m_counter = nullptr;
return *this; return *this;
} }
if (m_counter == null) { if (m_counter == nullptr) {
return *this; return *this;
} }
m_counter->incrementWeak(); m_counter->incrementWeak();
@ -208,8 +209,8 @@ WeakPtr& operator= (const WeakPtr<EMEMORY_TYPE2>& _obj) {
*/ */
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
void ememory::WeakPtr<EMEMORY_TYPE>::reset() { void ememory::WeakPtr<EMEMORY_TYPE>::reset() {
if(m_counter == null) { if(m_counter == nullptr) {
m_element = null; // in case ... m_element = nullptr; // in case ...
return; return;
} }
ememory::Counter::remove rmData = m_counter->decrementWeak(); ememory::Counter::remove rmData = m_counter->decrementWeak();
@ -221,18 +222,18 @@ void ememory::WeakPtr<EMEMORY_TYPE>::reset() {
EMEMORY_ERROR("in WeakPtr request remove all (impossible case ...)"); EMEMORY_ERROR("in WeakPtr request remove all (impossible case ...)");
break; break;
case ememory::Counter::remove::counter: case ememory::Counter::remove::counter:
ETK_DELETE(ememory::Counter, m_counter); delete m_counter;
break; break;
case ememory::Counter::remove::none: case ememory::Counter::remove::none:
break; break;
} }
m_counter = null; m_counter = nullptr;
m_element = null; m_element = nullptr;
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
int ememory::WeakPtr<EMEMORY_TYPE>::useCount() const { int ememory::WeakPtr<EMEMORY_TYPE>::useCount() const {
if (m_counter == null) { if (m_counter == nullptr) {
return 0; return 0;
} }
return m_counter->getCountShared(); return m_counter->getCountShared();
@ -240,7 +241,7 @@ int ememory::WeakPtr<EMEMORY_TYPE>::useCount() const {
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
bool ememory::WeakPtr<EMEMORY_TYPE>::expired() const { bool ememory::WeakPtr<EMEMORY_TYPE>::expired() const {
if (m_counter == null) { if (m_counter == nullptr) {
return true; return true;
} }
return m_counter->getCountShared() == 0; return m_counter->getCountShared() == 0;
@ -249,7 +250,7 @@ bool ememory::WeakPtr<EMEMORY_TYPE>::expired() const {
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
ememory::SharedPtr<EMEMORY_TYPE> ememory::WeakPtr<EMEMORY_TYPE>::lock() { ememory::SharedPtr<EMEMORY_TYPE> ememory::WeakPtr<EMEMORY_TYPE>::lock() {
ememory::SharedPtr<EMEMORY_TYPE> out; ememory::SharedPtr<EMEMORY_TYPE> out;
if (m_counter == null) { if (m_counter == nullptr) {
return out; return out;
} }
int64_t count = m_counter->incrementShared(true); int64_t count = m_counter->incrementShared(true);
@ -258,32 +259,36 @@ ememory::SharedPtr<EMEMORY_TYPE> ememory::WeakPtr<EMEMORY_TYPE>::lock() {
} }
out.m_counter = m_counter; out.m_counter = m_counter;
out.m_element = m_element; out.m_element = m_element;
return etk::move(out); return std::move(out);
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
bool ememory::WeakPtr<EMEMORY_TYPE>::operator==(const ememory::WeakPtr<EMEMORY_TYPE>& _obj) const { bool ememory::WeakPtr<EMEMORY_TYPE>::operator==(const ememory::WeakPtr<EMEMORY_TYPE>& _obj) {
return m_counter == _obj.m_counter; return m_counter == _obj.m_counter;
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
bool ememory::WeakPtr<EMEMORY_TYPE>::operator==(etk::NullPtr) const { bool ememory::WeakPtr<EMEMORY_TYPE>::operator==(std::nullptr_t) const {
return m_counter == null; return m_counter == nullptr;
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
bool ememory::WeakPtr<EMEMORY_TYPE>::operator!=(const ememory::WeakPtr<EMEMORY_TYPE>& _obj) const { bool ememory::WeakPtr<EMEMORY_TYPE>::operator!=(const ememory::WeakPtr<EMEMORY_TYPE>& _obj) {
return m_counter != _obj.m_counter; return m_counter != _obj.m_counter;
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
bool ememory::WeakPtr<EMEMORY_TYPE>::operator!=(etk::NullPtr) const { bool ememory::WeakPtr<EMEMORY_TYPE>::operator!=(std::nullptr_t) const {
return m_counter != null; return m_counter != nullptr;
} }
template<typename EMEMORY_TYPE> template<typename EMEMORY_TYPE>
void ememory::WeakPtr<EMEMORY_TYPE>::swap(ememory::WeakPtr<EMEMORY_TYPE>& _obj) { void ememory::WeakPtr<EMEMORY_TYPE>::swap(ememory::WeakPtr<EMEMORY_TYPE>& _obj) {
etk::swap(_obj.m_element, m_element); EMEMORY_TYPE* tmpE = m_element;
etk::swap(_obj.m_counter, m_counter); ememory::Counter* tmpC = m_counter;
m_element = _obj.m_element;
m_counter = _obj.m_counter;
_obj.m_element = tmpE;
_obj.m_counter = tmpC;
} }

View File

@ -1,17 +1,15 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#pragma once #pragma once
#include <etk/Vector.hpp> #include <vector>
#include <ethread/Mutex.hpp> #include <mutex>
#include <ememory/SharedPtr.hpp> #include <ememory/SharedPtr.hpp>
#include <ememory/RefPtr.hpp>
#include <ememory/WeakPtr.hpp> #include <ememory/WeakPtr.hpp>
#include <ememory/EnableSharedFromThis.hpp> #include <ememory/EnableSharedFromThis.hpp>
#include <etk/Allocator.hpp>
/** /**
* @brief Ememory is a namespace to represent the @code #include <memory> @endcode part ==> simple memory access abstraction * @brief Ememory is a namespace to represent the @code #include <memory> @endcode part ==> simple memory access abstraction
@ -24,7 +22,7 @@ namespace ememory {
*/ */
template<class EMEMORY_TYPE, typename... EMEMORY_ARGS> template<class EMEMORY_TYPE, typename... EMEMORY_ARGS>
static ememory::SharedPtr<EMEMORY_TYPE> makeShared(EMEMORY_ARGS && ..._args) { static ememory::SharedPtr<EMEMORY_TYPE> makeShared(EMEMORY_ARGS && ..._args) {
return ememory::SharedPtr<EMEMORY_TYPE>(ETK_NEW(EMEMORY_TYPE, etk::forward<EMEMORY_ARGS>(_args)...)); return ememory::SharedPtr<EMEMORY_TYPE>(new EMEMORY_TYPE(std::forward<EMEMORY_ARGS>(_args)...));
} }
/** /**
* @brief Cast in Dynamic the input SharedPtr into an other type like dynamic_cast on pointer * @brief Cast in Dynamic the input SharedPtr into an other type like dynamic_cast on pointer
@ -89,81 +87,4 @@ namespace ememory {
inline ememory::SharedPtr<EMEMORY_TYPE_CAST> constPointerCast(const ememory::SharedPtr<EMEMORY_TYPE>& _obj) { inline ememory::SharedPtr<EMEMORY_TYPE_CAST> constPointerCast(const ememory::SharedPtr<EMEMORY_TYPE>& _obj) {
return ememory::SharedPtr<EMEMORY_TYPE_CAST>(const_cast<EMEMORY_TYPE*>(_obj.get()), _obj.getCounter()); return ememory::SharedPtr<EMEMORY_TYPE_CAST>(const_cast<EMEMORY_TYPE*>(_obj.get()), _obj.getCounter());
} }
/**
* @brief Create a RefPtr with specific arguments
* @param[in] _args Multiples argument to add in the EMEMORY_TYPE public constructor.
* @return the requested created RefPtr
*/
template<class EMEMORY_TYPE,
typename etk::EnableIf< etk::IsBaseOf<ememory::RefCounter, EMEMORY_TYPE>::value
, int>::type = 0,
typename... EMEMORY_ARGS>
static ememory::RefPtr<EMEMORY_TYPE> makeRef(EMEMORY_ARGS && ..._args) {
return ememory::RefPtr<EMEMORY_TYPE>::create(ETK_NEW(EMEMORY_TYPE, etk::forward<EMEMORY_ARGS>(_args)...));
}
/**
* @brief Cast in Dynamic the input RefPtr into an other type like dynamic_cast on pointer
* @param[in] _obj Object To cast
* @return Casted Object
*/
template<class EMEMORY_TYPE_CAST, class EMEMORY_TYPE>
inline ememory::RefPtr<EMEMORY_TYPE_CAST> dynamicRefCast(ememory::RefPtr<EMEMORY_TYPE>& _obj) {
return ememory::RefPtr<EMEMORY_TYPE_CAST>(dynamic_cast<EMEMORY_TYPE_CAST*>(_obj.get()));
}
/**
* @brief CONST Cast in Dynamic the input RefPtr into an other type like dynamic_cast on pointer
* @param[in] _obj Object To cast
* @return Casted Object
*/
template<class EMEMORY_TYPE_CAST, class EMEMORY_TYPE>
inline const ememory::RefPtr<EMEMORY_TYPE_CAST> dynamicRefCast(const ememory::RefPtr<EMEMORY_TYPE>& _obj) {
return ememory::RefPtr<EMEMORY_TYPE_CAST>(dynamic_cast<EMEMORY_TYPE_CAST*>(const_cast<EMEMORY_TYPE*>(_obj.get())));
}
/**
* @brief Cast in static the input RefPtr into an other type like static_cast on pointer
* @param[in] _obj Object To cast
* @return Casted Object
*/
template<class EMEMORY_TYPE_CAST, class EMEMORY_TYPE>
inline ememory::RefPtr<EMEMORY_TYPE_CAST> staticRefCast(ememory::RefPtr<EMEMORY_TYPE>& _obj) {
return ememory::RefPtr<EMEMORY_TYPE_CAST>(static_cast<EMEMORY_TYPE_CAST*>(_obj.get()));
}
/**
* @brief CONST Cast in static the input RefPtr into an other type like static_cast on pointer
* @param[in] _obj Object To cast
* @return Casted Object
*/
template<class EMEMORY_TYPE_CAST, class EMEMORY_TYPE>
inline const ememory::RefPtr<EMEMORY_TYPE_CAST> staticRefCast(const ememory::RefPtr<EMEMORY_TYPE>& _obj) {
return ememory::RefPtr<EMEMORY_TYPE_CAST>(static_cast<EMEMORY_TYPE_CAST*>(const_cast<EMEMORY_TYPE*>(_obj.get())));
}
/**
* @brief Cast in reinterpret the input RefPtr into an other type like reinterpret_cast on pointer
* @param[in] _obj Object To cast
* @return Casted Object
*/
template<class EMEMORY_TYPE_CAST, class EMEMORY_TYPE>
inline ememory::RefPtr<EMEMORY_TYPE_CAST> reinterpretRefCast(ememory::RefPtr<EMEMORY_TYPE>& _obj) {
return ememory::RefPtr<EMEMORY_TYPE_CAST>(reinterpret_cast<EMEMORY_TYPE_CAST*>(_obj.get()));
}
/**
* @brief CONST Cast in reinterpret the input RefPtr into an other type like reinterpret_cast on pointer
* @param[in] _obj Object To cast
* @return Casted Object
*/
template<class EMEMORY_TYPE_CAST, class EMEMORY_TYPE>
inline const ememory::RefPtr<EMEMORY_TYPE_CAST> reinterpretRefCast(const ememory::RefPtr<EMEMORY_TYPE>& _obj) {
return ememory::RefPtr<EMEMORY_TYPE_CAST>(reinterpret_cast<EMEMORY_TYPE_CAST*>(const_cast<EMEMORY_TYPE*>(_obj.get())));
}
/**
* @brief Cast in const the input RefPtr into an other type like const_cast on pointer (remove constness)
* @param[in] _obj Object To cast
* @return Casted Object
*/
template<class EMEMORY_TYPE_CAST, class EMEMORY_TYPE>
inline ememory::RefPtr<EMEMORY_TYPE_CAST> constRefCast(const ememory::RefPtr<EMEMORY_TYPE>& _obj) {
return ememory::RefPtr<EMEMORY_TYPE_CAST>(const_cast<EMEMORY_TYPE*>(_obj.get()));
}
} }

View File

@ -1,5 +1,5 @@
#!/usr/bin/python #!/usr/bin/python
import realog.debug as debug import lutin.debug as debug
import lutin.tools as tools import lutin.tools as tools
@ -13,7 +13,7 @@ def get_desc():
return "e-memory test-unit" return "e-memory test-unit"
def get_licence(): def get_licence():
return "MPL-2" return "APACHE-2"
def get_compagny_type(): def get_compagny_type():
return "com" return "com"
@ -27,18 +27,15 @@ def get_maintainer():
def configure(target, my_module): def configure(target, my_module):
my_module.add_src_file([ my_module.add_src_file([
'test/main.cpp', 'test/main.cpp',
'test/testUnique.cpp',
'test/testRef.cpp',
'test/testShared.cpp', 'test/testShared.cpp',
'test/testWeak.cpp', 'test/testWeak.cpp',
'test/testEnableSharedFromThis.cpp', 'test/testEnableSharedFromThis.cpp',
'test/testCasts.cpp' 'test/testCasts.cpp'
]) ])
my_module.add_depend([ my_module.add_depend([
'cxx',
'ememory', 'ememory',
'test-debug', 'gtest',
'etest', 'test-debug'
]) ])
return True return True

View File

@ -1,5 +1,5 @@
#!/usr/bin/python #!/usr/bin/python
import realog.debug as debug import lutin.debug as debug
import lutin.tools as tools import lutin.tools as tools
@ -10,7 +10,7 @@ def get_desc():
return "Ewol memory basic interface (have a thread safe shared_ptr with constness)" return "Ewol memory basic interface (have a thread safe shared_ptr with constness)"
def get_licence(): def get_licence():
return "MPL-2" return "APACHE-2"
def get_compagny_type(): def get_compagny_type():
return "com" return "com"
@ -29,31 +29,26 @@ def configure(target, my_module):
# add the file to compile: # add the file to compile:
my_module.add_src_file([ my_module.add_src_file([
'ememory/debug.cpp', 'ememory/debug.cpp',
'ememory/Counter.cpp', 'ememory/Counter.cpp'
'ememory/RefCounter.cpp',
]) ])
my_module.add_header_file([ my_module.add_header_file([
'ememory/debug.hpp', 'ememory/debug.hpp',
'ememory/memory.hpp', 'ememory/memory.hpp',
'ememory/Counter.hpp', 'ememory/Counter.hpp',
'ememory/RefCounter.hpp',
'ememory/RefPtr.hpp',
'ememory/SharedPtr.hpp', 'ememory/SharedPtr.hpp',
'ememory/UniquePtr.hpp',
'ememory/WeakPtr.hpp', 'ememory/WeakPtr.hpp',
'ememory/EnableSharedFromThis.hpp', 'ememory/EnableSharedFromThis.hpp',
'ememory/details/memory.hxx', 'ememory/details/memory.hxx',
'ememory/details/SharedPtr.hxx', 'ememory/details/SharedPtr.hxx',
'ememory/details/RefPtr.hxx',
'ememory/details/WeakPtr.hxx', 'ememory/details/WeakPtr.hxx',
'ememory/details/EnableSharedFromThis.hxx', 'ememory/details/EnableSharedFromThis.hxx'
]) ])
# build in C++ mode # build in C++ mode
my_module.compile_version("c++", 2011) my_module.compile_version("c++", 2011)
# add dependency of the generic C++ library: # add dependency of the generic C++ library:
my_module.add_depend('etk-core') my_module.add_depend('cxx')
my_module.add_optionnal_depend('elog') my_module.add_optionnal_depend('elog')
my_module.add_path(".") my_module.add_path(".")

View File

@ -1,20 +1,20 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#include <test-debug/debug.hpp> #include <test-debug/debug.hpp>
#include <etk/Vector.hpp> #include <vector>
#include <etest/etest.hpp> #include <gtest/gtest.h>
//#include <etk/etk.hpp> #include <etk/etk.hpp>
int main(int _argc, const char *_argv[]) { int main(int _argc, const char *_argv[]) {
//etk::init(_argc, _argv); ::testing::InitGoogleTest(&_argc, const_cast<char **>(_argv));
etest::init(_argc, _argv); etk::init(_argc, _argv);
for (int32_t iii=0; iii<_argc ; ++iii) { for (int32_t iii=0; iii<_argc ; ++iii) {
etk::String data = _argv[iii]; std::string data = _argv[iii];
if ( data == "-h" if ( data == "-h"
|| data == "--help") { || data == "--help") {
TEST_PRINT(_argv[0] << " - help : "); TEST_PRINT(_argv[0] << " - help : ");

View File

@ -1,6 +1,6 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#pragma once #pragma once

View File

@ -1,10 +1,10 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#include <etest/etest.hpp> #include <gtest/gtest.h>
#include <ememory/memory.hpp> #include <ememory/memory.hpp>
#include "main.hpp" #include "main.hpp"

View File

@ -1,10 +1,10 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#include <etest/etest.hpp> #include <gtest/gtest.h>
#include <ememory/memory.hpp> #include <ememory/memory.hpp>
#include "main.hpp" #include "main.hpp"

View File

@ -1,208 +0,0 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#include <etest/etest.hpp>
#include <ememory/memory.hpp>
#include <test-debug/debug.hpp>
#include "main.hpp"
namespace {
class MyTestString:
public ememory::RefCounter,
public etk::String {
public:
MyTestString() = default;
MyTestString(const etk::String& _value):
etk::String(_value) {
}
MyTestString& operator = (const etk::String& _value) {
etk::String::operator=(_value);
return *this;
}
};
}
TEST(TestRef, createAndDestroy) {
ememory::RefPtr<MyTestString> data = ememory::makeRef<MyTestString>("coucou");
EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou");
EXPECT_EQ(data == null, false);
EXPECT_EQ(data != null, true);
data.reset();
EXPECT_EQ(data.useCount(), 0);
EXPECT_EQ(data == null, true);
EXPECT_EQ(data != null, false);
}
TEST(TestRef, createAndDestroy_2) {
ememory::RefPtr<MyTestString> data = ememory::RefPtr<MyTestString>::create(ETK_NEW(MyTestString, "coucou"));
EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou");
EXPECT_EQ(data == null, false);
EXPECT_EQ(data != null, true);
data.reset();
EXPECT_EQ(data.useCount(), 0);
EXPECT_EQ(data == null, true);
EXPECT_EQ(data != null, false);
}
TEST(TestRef, createAndDestroy_3) {
ememory::RefPtr<MyTestString> data = ememory::RefPtr<MyTestString>::create(new MyTestString("coucou"));
EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou");
EXPECT_EQ(data == null, false);
EXPECT_EQ(data != null, true);
data.reset();
EXPECT_EQ(data.useCount(), 0);
EXPECT_EQ(data == null, true);
EXPECT_EQ(data != null, false);
}
TEST(TestRef, copy) {
ememory::RefPtr<MyTestString> data = ememory::makeRef<MyTestString>("coucou");
EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou");
ememory::RefPtr<MyTestString> dataCopy = data;
EXPECT_EQ(data.useCount(), 2);
EXPECT_EQ(data == dataCopy, true);
EXPECT_EQ(data != dataCopy, false);
data.reset();
EXPECT_EQ(data == dataCopy, false);
EXPECT_EQ(data != dataCopy, true);
EXPECT_EQ(data.useCount(), 0);
EXPECT_EQ(dataCopy.useCount(), 1);
EXPECT_EQ(*dataCopy, "coucou");
dataCopy.reset();
EXPECT_EQ(dataCopy.useCount(), 0);
dataCopy.reset();
EXPECT_EQ(dataCopy.useCount(), 0);
}
TEST(TestRef, swap) {
ememory::RefPtr<MyTestString> data = ememory::makeRef<MyTestString>("coucou");
EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou");
ememory::RefPtr<MyTestString> dataCopy;
EXPECT_EQ(dataCopy.useCount(), 0);
dataCopy.swap(data);
EXPECT_EQ(data.useCount(), 0);
EXPECT_EQ(dataCopy.useCount(), 1);
EXPECT_EQ(*dataCopy, "coucou");
}
TEST(TestRef, callOperator) {
ememory::RefPtr<MyTestString> data = ememory::makeRef<MyTestString>("coucou");
EXPECT_EQ(data->size(), 6);
}
static void functionCallRef(etk::String& _data) {
_data = "plop";
}
TEST(TestRef, callOperatorStar) {
ememory::RefPtr<MyTestString> data = ememory::makeRef<MyTestString>("coucou");
EXPECT_EQ(data->size(), 6);
EXPECT_EQ(*data, "coucou");
*data = "ragout";
EXPECT_EQ(data->size(), 6);
EXPECT_EQ(*data, "ragout");
functionCallRef(*data);
EXPECT_EQ(data->size(), 4);
EXPECT_EQ(*data, "plop");
}
namespace {
class basicClass : public ememory::RefCounter {
};
class heritedClass : public basicClass {
};
}
TEST(TestRef, heritage) {
ememory::RefPtr<heritedClass> data = ememory::makeRef<heritedClass>();
ememory::RefPtr<basicClass> data2 = data;
ememory::RefPtr<basicClass> data3(data);
EXPECT_NE(data, null);
EXPECT_NE(data2, null);
EXPECT_NE(data3, null);
}
static uint32_t isDestroy = 0;
namespace {
class testContructDestruct : public ememory::RefCounter {
private:
uint32_t m_addValue;
public:
testContructDestruct(uint32_t _addValue):
m_addValue(_addValue) {
isDestroy += m_addValue;
TEST_DEBUG("Create class " << m_addValue);
}
testContructDestruct(testContructDestruct&& _obj):
m_addValue(_obj.m_addValue) {
_obj.m_addValue = 0;
TEST_DEBUG("move contruction " << m_addValue);
}
virtual ~testContructDestruct() {
if (m_addValue == 0) {
TEST_DEBUG("Remove class (after move)");
return;
}
TEST_DEBUG("Remove Class " << m_addValue);
isDestroy -= m_addValue;
}
testContructDestruct& operator= (testContructDestruct&& _obj) {
TEST_DEBUG("move operator " << m_addValue);
if (this != &_obj) {
etk::swap(m_addValue, _obj.m_addValue);
}
return *this;
}
};
}
TEST(TestRef, destroyElementAtTheCorectMoment) {
isDestroy = 0;
{
etk::Vector<ememory::RefPtr<testContructDestruct>> list;
list.pushBack(ememory::makeRef<testContructDestruct>(55));
EXPECT_EQ(list.size(), 1);
EXPECT_EQ(isDestroy, 55);
auto it = list.erase(list.begin());
EXPECT_EQ(isDestroy, 0);
EXPECT_EQ(list.size(), 0);
EXPECT_EQ(it, list.end());
}
EXPECT_EQ(isDestroy, 0);
}
TEST(TestRef, destroyElementAtTheCorectMoment_2) {
isDestroy = 0;
{
etk::Vector<ememory::RefPtr<testContructDestruct>> list;
list.pushBack(ememory::makeRef<testContructDestruct>(4));
list.pushBack(ememory::makeRef<testContructDestruct>(30));
list.pushBack(ememory::makeRef<testContructDestruct>(1000));
list.pushBack(ememory::makeRef<testContructDestruct>(200));
EXPECT_EQ(list.size(), 4);
EXPECT_EQ(isDestroy, 1234);
auto it = list.erase(list.begin());
EXPECT_EQ(list.size(), 3);
EXPECT_EQ(isDestroy, 1230);
it = list.erase(list.begin()+1);
EXPECT_EQ(isDestroy, 230);
EXPECT_EQ(list.size(), 2);
}
EXPECT_EQ(isDestroy, 0);
}

View File

@ -1,56 +1,30 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#include <etest/etest.hpp> #include <gtest/gtest.h>
#include <ememory/memory.hpp> #include <ememory/memory.hpp>
#include <test-debug/debug.hpp>
#include "main.hpp" #include "main.hpp"
TEST(TestShared, createAndDestroy) { TEST(TestShared, createAndDestroy) {
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou"); ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
EXPECT_EQ(data.useCount(), 1); EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou"); EXPECT_EQ(*data, "coucou");
EXPECT_EQ(data == null, false); EXPECT_EQ(data == nullptr, false);
EXPECT_EQ(data != null, true); EXPECT_EQ(data != nullptr, true);
data.reset(); data.reset();
EXPECT_EQ(data.useCount(), 0); EXPECT_EQ(data.useCount(), 0);
EXPECT_EQ(data == null, true); EXPECT_EQ(data == nullptr, true);
EXPECT_EQ(data != null, false); EXPECT_EQ(data != nullptr, false);
}
TEST(TestShared, createAndDestroy_2) {
ememory::SharedPtr<etk::String> data = ememory::SharedPtr<etk::String>(ETK_NEW(etk::String, "coucou"));
EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou");
EXPECT_EQ(data == null, false);
EXPECT_EQ(data != null, true);
data.reset();
EXPECT_EQ(data.useCount(), 0);
EXPECT_EQ(data == null, true);
EXPECT_EQ(data != null, false);
}
TEST(TestShared, createAndDestroy_3) {
ememory::SharedPtr<etk::String> data = ememory::SharedPtr<etk::String>(new etk::String("coucou"));
EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou");
EXPECT_EQ(data == null, false);
EXPECT_EQ(data != null, true);
data.reset();
EXPECT_EQ(data.useCount(), 0);
EXPECT_EQ(data == null, true);
EXPECT_EQ(data != null, false);
} }
TEST(TestShared, copy) { TEST(TestShared, copy) {
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou"); ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
EXPECT_EQ(data.useCount(), 1); EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou"); EXPECT_EQ(*data, "coucou");
ememory::SharedPtr<etk::String> dataCopy = data; ememory::SharedPtr<std::string> dataCopy = data;
EXPECT_EQ(data.useCount(), 2); EXPECT_EQ(data.useCount(), 2);
EXPECT_EQ(data == dataCopy, true); EXPECT_EQ(data == dataCopy, true);
EXPECT_EQ(data != dataCopy, false); EXPECT_EQ(data != dataCopy, false);
@ -67,10 +41,10 @@ TEST(TestShared, copy) {
} }
TEST(TestShared, swap) { TEST(TestShared, swap) {
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou"); ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
EXPECT_EQ(data.useCount(), 1); EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou"); EXPECT_EQ(*data, "coucou");
ememory::SharedPtr<etk::String> dataCopy; ememory::SharedPtr<std::string> dataCopy;
EXPECT_EQ(dataCopy.useCount(), 0); EXPECT_EQ(dataCopy.useCount(), 0);
dataCopy.swap(data); dataCopy.swap(data);
EXPECT_EQ(data.useCount(), 0); EXPECT_EQ(data.useCount(), 0);
@ -79,16 +53,16 @@ TEST(TestShared, swap) {
} }
TEST(TestShared, callOperator) { TEST(TestShared, callOperator) {
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou"); ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
EXPECT_EQ(data->size(), 6); EXPECT_EQ(data->size(), 6);
} }
static void functionCallRef(etk::String& _data) { static void functionCallRef(std::string& _data) {
_data = "plop"; _data = "plop";
} }
TEST(TestShared, callOperatorStar) { TEST(TestShared, callOperatorStar) {
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou"); ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
EXPECT_EQ(data->size(), 6); EXPECT_EQ(data->size(), 6);
EXPECT_EQ(*data, "coucou"); EXPECT_EQ(*data, "coucou");
*data = "ragout"; *data = "ragout";
@ -100,164 +74,11 @@ TEST(TestShared, callOperatorStar) {
} }
TEST(TestShared, setInVoid) { TEST(TestShared, setInVoid) {
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou"); ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
ememory::SharedPtr<void> dataVoid(data); ememory::SharedPtr<void> dataVoid(data);
ememory::SharedPtr<void> dataVoid2; ememory::SharedPtr<void> dataVoid2;
dataVoid2 = data; dataVoid2 = data;
EXPECT_EQ(data.useCount(), 3); EXPECT_EQ(data.useCount(), 3);
} }
class basicClass {
};
class heritedClass : public basicClass {
};
TEST(TestShared, heritage) {
ememory::SharedPtr<heritedClass> data = ememory::makeShared<heritedClass>();
ememory::SharedPtr<basicClass> data2 = data;
ememory::SharedPtr<basicClass> data3(data);
EXPECT_NE(data, null);
EXPECT_NE(data2, null);
EXPECT_NE(data3, null);
}
static uint32_t isDestroy = 0;
namespace {
class testContructDestruct {
private:
uint32_t m_addValue;
public:
testContructDestruct(uint32_t _addValue):
m_addValue(_addValue) {
isDestroy += m_addValue;
TEST_DEBUG("Create class " << m_addValue);
}
testContructDestruct(testContructDestruct&& _obj):
m_addValue(_obj.m_addValue) {
_obj.m_addValue = 0;
TEST_DEBUG("move contruction " << m_addValue);
}
virtual ~testContructDestruct() {
if (m_addValue == 0) {
TEST_DEBUG("Remove class (after move)");
return;
}
TEST_DEBUG("Remove Class " << m_addValue);
isDestroy -= m_addValue;
}
testContructDestruct& operator= (testContructDestruct&& _obj) {
TEST_DEBUG("move operator " << m_addValue);
if (this != &_obj) {
etk::swap(m_addValue, _obj.m_addValue);
}
return *this;
}
};
class testContructDestruct2 : public ememory::EnableSharedFromThis<testContructDestruct2> {
private:
uint32_t m_addValue;
public:
testContructDestruct2(uint32_t _addValue):
m_addValue(_addValue) {
isDestroy += m_addValue;
TEST_DEBUG("Create class " << m_addValue);
}
testContructDestruct2(testContructDestruct2&& _obj):
m_addValue(_obj.m_addValue) {
_obj.m_addValue = 0;
TEST_DEBUG("move contruction " << m_addValue);
}
virtual ~testContructDestruct2() {
if (m_addValue == 0) {
TEST_DEBUG("Remove class (after move)");
return;
}
TEST_DEBUG("Remove Class " << m_addValue);
isDestroy -= m_addValue;
}
testContructDestruct2& operator= (testContructDestruct2&& _obj) {
TEST_DEBUG("move operator " << m_addValue);
if (this != &_obj) {
etk::swap(m_addValue, _obj.m_addValue);
}
return *this;
}
};
}
TEST(TestShared, destroyElementAtTheCorectMoment) {
isDestroy = 0;
{
etk::Vector<ememory::SharedPtr<testContructDestruct>> list;
list.pushBack(ememory::makeShared<testContructDestruct>(55));
EXPECT_EQ(list.size(), 1);
EXPECT_EQ(isDestroy, 55);
auto it = list.erase(list.begin());
EXPECT_EQ(isDestroy, 0);
EXPECT_EQ(list.size(), 0);
EXPECT_EQ(it, list.end());
}
EXPECT_EQ(isDestroy, 0);
}
TEST(TestShared, destroyElementAtTheCorectMoment_2) {
isDestroy = 0;
{
etk::Vector<ememory::SharedPtr<testContructDestruct>> list;
list.pushBack(ememory::makeShared<testContructDestruct>(4));
list.pushBack(ememory::makeShared<testContructDestruct>(30));
list.pushBack(ememory::makeShared<testContructDestruct>(1000));
list.pushBack(ememory::makeShared<testContructDestruct>(200));
EXPECT_EQ(list.size(), 4);
EXPECT_EQ(isDestroy, 1234);
auto it = list.erase(list.begin());
EXPECT_EQ(list.size(), 3);
EXPECT_EQ(isDestroy, 1230);
it = list.erase(list.begin()+1);
EXPECT_EQ(isDestroy, 230);
EXPECT_EQ(list.size(), 2);
}
EXPECT_EQ(isDestroy, 0);
}
TEST(TestShared, destroyElementAtTheCorectMoment_3) {
isDestroy = 0;
{
etk::Vector<ememory::SharedPtr<testContructDestruct2>> list;
list.pushBack(ememory::makeShared<testContructDestruct2>(55));
EXPECT_EQ(list.size(), 1);
EXPECT_EQ(isDestroy, 55);
auto it = list.erase(list.begin());
EXPECT_EQ(isDestroy, 0);
EXPECT_EQ(list.size(), 0);
EXPECT_EQ(it, list.end());
}
EXPECT_EQ(isDestroy, 0);
}
TEST(TestShared, destroyElementAtTheCorectMoment_4) {
isDestroy = 0;
{
etk::Vector<ememory::SharedPtr<testContructDestruct2>> list;
list.pushBack(ememory::makeShared<testContructDestruct2>(4));
list.pushBack(ememory::makeShared<testContructDestruct2>(30));
list.pushBack(ememory::makeShared<testContructDestruct2>(1000));
list.pushBack(ememory::makeShared<testContructDestruct2>(200));
EXPECT_EQ(list.size(), 4);
EXPECT_EQ(isDestroy, 1234);
auto it = list.erase(list.begin());
EXPECT_EQ(list.size(), 3);
EXPECT_EQ(isDestroy, 1230);
it = list.erase(list.begin()+1);
EXPECT_EQ(isDestroy, 230);
EXPECT_EQ(list.size(), 2);
}
EXPECT_EQ(isDestroy, 0);
}

View File

@ -1,85 +0,0 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license MPL v2.0 (see license file)
*/
#include <etest/etest.hpp>
#include <ememory/UniquePtr.hpp>
#include <test-debug/debug.hpp>
TEST(TestEmemoryUniquePtr, Creation_1) {
ememory::UniquePtr<uint32_t> testData;
EXPECT_EQ(testData.get(), null);
}
TEST(TestEmemoryUniquePtr, Creation_2) {
ememory::UniquePtr<uint32_t> testData(new uint32_t(55));
EXPECT_NE(testData.get(), null);
EXPECT_EQ(*testData.get(), 55);
}
TEST(TestEmemoryUniquePtr, Creation_3) {
ememory::UniquePtr<uint32_t> testData(ETK_NEW(uint32_t, 55));
EXPECT_NE(testData.get(), null);
EXPECT_EQ(*testData.get(), 55);
}
TEST(TestEmemoryUniquePtr, Creation_4) {
ememory::UniquePtr<uint32_t> testData = ememory::makeUniquePtr<uint32_t>(456789);
EXPECT_NE(testData.get(), null);
EXPECT_EQ(*testData.get(), 456789);
}
uint32_t vals[] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
class testCreateAndDestroy {
public:
uint32_t m_val;
public:
testCreateAndDestroy(uint32_t _val):
m_val(_val) {
vals[m_val] = 1;
}
~testCreateAndDestroy() {
vals[m_val] = -1;
}
};
TEST(TestEmemoryUniquePtr, reset) {
memset(vals, 0, sizeof(vals));
EXPECT_EQ(vals[1], 0);
ememory::UniquePtr<testCreateAndDestroy> testData = ememory::makeUniquePtr<testCreateAndDestroy>(1);
EXPECT_NE(testData.get(), null);
EXPECT_EQ(vals[1], 1);
testData.reset();
EXPECT_EQ(testData.get(), null);
EXPECT_EQ(vals[1], -1);
}
TEST(TestEmemoryUniquePtr, reset_2) {
memset(vals, 0, sizeof(vals));
EXPECT_EQ(vals[1], 0);
ememory::UniquePtr<testCreateAndDestroy> testData = ememory::makeUniquePtr<testCreateAndDestroy>(1);
EXPECT_NE(testData.get(), null);
EXPECT_EQ(vals[1], 1);
testData = null;
EXPECT_EQ(testData.get(), null);
EXPECT_EQ(vals[1], -1);
}
TEST(TestEmemoryUniquePtr, overwrite) {
memset(vals, 0, sizeof(vals));
ememory::UniquePtr<testCreateAndDestroy> testData = ememory::makeUniquePtr<testCreateAndDestroy>(1);
EXPECT_NE(testData.get(), null);
EXPECT_EQ(vals[1], 1);
EXPECT_EQ(vals[2], 0);
testData = ememory::makeUniquePtr<testCreateAndDestroy>(2);
EXPECT_EQ(vals[1], -1);
EXPECT_EQ(vals[2], 1);
testData.reset();
EXPECT_EQ(vals[1], -1);
EXPECT_EQ(vals[2], -1);
}

View File

@ -1,17 +1,17 @@
/** @file /** @file
* @author Edouard DUPIN * @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved * @copyright 2016, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file) * @license APACHE v2.0 (see license file)
*/ */
#include <etest/etest.hpp> #include <gtest/gtest.h>
#include <ememory/memory.hpp> #include <ememory/memory.hpp>
#include "main.hpp" #include "main.hpp"
TEST(TestWeak, createAndDestroy) { TEST(TestWeak, createAndDestroy) {
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou"); ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
EXPECT_EQ(data.useCount(), 1); EXPECT_EQ(data.useCount(), 1);
ememory::WeakPtr<etk::String> dataWeak = data; ememory::WeakPtr<std::string> dataWeak = data;
EXPECT_EQ(data.useCount(), 1); EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(dataWeak.useCount(), 1); EXPECT_EQ(dataWeak.useCount(), 1);
data.reset(); data.reset();
@ -20,12 +20,12 @@ TEST(TestWeak, createAndDestroy) {
} }
TEST(TestWeak, retrive) { TEST(TestWeak, retrive) {
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou"); ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
EXPECT_EQ(data.useCount(), 1); EXPECT_EQ(data.useCount(), 1);
ememory::WeakPtr<etk::String> dataWeak = data; ememory::WeakPtr<std::string> dataWeak = data;
EXPECT_EQ(data.useCount(), 1); EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(dataWeak.useCount(), 1); EXPECT_EQ(dataWeak.useCount(), 1);
ememory::SharedPtr<etk::String> dataRetrive = dataWeak.lock(); ememory::SharedPtr<std::string> dataRetrive = dataWeak.lock();
EXPECT_EQ(data.useCount(), 2); EXPECT_EQ(data.useCount(), 2);
EXPECT_EQ(dataWeak.useCount(), 2); EXPECT_EQ(dataWeak.useCount(), 2);
data.reset(); data.reset();
@ -37,7 +37,7 @@ TEST(TestWeak, retrive) {
EXPECT_EQ(dataRetrive.useCount(), 0); EXPECT_EQ(dataRetrive.useCount(), 0);
EXPECT_EQ(dataWeak.useCount(), 0); EXPECT_EQ(dataWeak.useCount(), 0);
dataRetrive = dataWeak.lock(); dataRetrive = dataWeak.lock();
EXPECT_EQ(dataRetrive == null, true); EXPECT_EQ(dataRetrive == nullptr, true);
EXPECT_EQ(dataRetrive.useCount(), 0); EXPECT_EQ(dataRetrive.useCount(), 0);
EXPECT_EQ(dataWeak.useCount(), 0); EXPECT_EQ(dataWeak.useCount(), 0);
} }

View File

@ -1 +1 @@
1.0.0 0.4.0