Compare commits

...

26 Commits

Author SHA1 Message Date
e8ebd43cdd [RELEASE] Release v1.0.0 2021-02-16 21:47:02 +01:00
618e2b7a2a [DEV] add a unique id in debug mode ==> simplify debug 2019-08-18 12:37:20 +02:00
d3fa101aeb [DEBUG] update new API of lutin log 2019-05-03 10:18:22 +02:00
0f2e11e735 [DEV] build with etk memory log 2018-10-09 23:03:07 +02:00
4fdacb9294 [DEV] update etk-core 2018-09-07 23:51:16 +02:00
1a7f42dbbb [DEV] coorect constness 2018-08-19 22:56:05 +02:00
934356875e [DEV] add deleter at the sharedPtr<void> 2018-08-12 14:53:30 +02:00
2a211653e0 [DEV] add refPtr and RefCounter object to manage refconted element (somtimes better than sharedPtr: smaller, faster, less segmentation) 2018-07-20 21:13:31 +02:00
4bc9a2f27b [DEBUG] correct uniquePtr 2018-07-13 22:01:57 +02:00
7125777204 [DEV] correct missing init 2018-06-25 23:55:44 +02:00
06c25b86d2 [DEBUG] midding copy contructor of void 2018-06-25 23:27:06 +02:00
43685d778f [DEV] update nullptr in null (specific etk) 2018-06-19 22:15:52 +02:00
b872ff3646 [DEBUG] build back sample 2017-11-07 10:19:31 +01:00
8e594532a1 [DEV] update to the new ETK allocator wrapper 2017-10-21 19:05:21 +02:00
c2248bb20b [DEV] add test to check correct remove in vector 2017-10-13 00:06:44 +02:00
70dd3441e3 [DEV] Add deleter in SharedPtr when contruct the object 2017-10-09 10:19:16 +02:00
643a37b289 [DEV] better swap 2017-09-26 15:50:14 +02:00
405604179d [DEBUG] continue removing STL 2017-09-17 15:23:44 +02:00
e64b7496b7 [DEV] continue removing STL 2017-09-14 00:59:21 +02:00
965b951ee4 [DEV] remove STL 2017-09-07 23:38:26 +02:00
24f752f79b [DEV] test unique ptr 2017-09-01 22:49:13 +02:00
ee9a5de786 [DEV] try develop UniquePtr 2017-08-28 21:05:29 +02:00
fa16d76623 [DEV] continue removing stl 2017-08-28 00:04:39 +02:00
bf044da356 [DEV] update check in remove 2017-02-13 21:10:09 +01:00
b3c39ee6c0 Change licence APACHE-2 to MPL-2 ==> force source redistribution and permit static link 2017-01-05 21:28:23 +01:00
8ab56499fa [DEV] update dev tag version 2016-10-24 22:23:26 +02:00
30 changed files with 1943 additions and 505 deletions

510
LICENSE
View File

@ -1,201 +1,373 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Mozilla Public License Version 2.0
==================================
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions
--------------
1. Definitions.
1.1. "Contributor"
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
by a Contributor and that particular Contributor's Contribution.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
1.3. "Contribution"
means Covered Software of a particular Contributor.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
1.4. "Covered Software"
means Source Code Form to which the initial Contributor has attached
the notice in Exhibit A, the Executable Form of such Source Code
Form, and Modifications of such Source Code Form, in each case
including portions thereof.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
1.5. "Incompatible With Secondary Licenses"
means
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
(a) that the initial Contributor has attached the notice described
in Exhibit B to the Covered Software; or
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
(b) that the Covered Software was made available under the terms of
version 1.1 or earlier of the License, but not also under the
terms of a Secondary License.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
1.6. "Executable Form"
means any form of the work other than Source Code Form.
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
1.7. "Larger Work"
means a work that combines Covered Software with other material, in
a separate file or files, that is not Covered Software.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
1.8. "License"
means this document.
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
1.9. "Licensable"
means having the right to grant, to the maximum extent possible,
whether at the time of the initial grant or subsequently, any and
all of the rights conveyed by this License.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
1.10. "Modifications"
means any of the following:
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
(a) any file in Source Code Form that results from an addition to,
deletion from, or modification of the contents of Covered
Software; or
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(b) any new file in Source Code Form that contains any Covered
Software.
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
1.11. "Patent Claims" of a Contributor
means any patent claim(s), including without limitation, method,
process, and apparatus claims, in any patent Licensable by such
Contributor that would be infringed, but for the grant of the
License, by the making, using, selling, offering for sale, having
made, import, or transfer of either its Contributions or its
Contributor Version.
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
1.12. "Secondary License"
means either the GNU General Public License, Version 2.0, the GNU
Lesser General Public License, Version 2.1, the GNU Affero General
Public License, Version 3.0, or any later versions of those
licenses.
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
1.13. "Source Code Form"
means the form of the work preferred for making modifications.
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
1.14. "You" (or "Your")
means an individual or a legal entity exercising rights under this
License. For legal entities, "You" includes any entity that
controls, is controlled by, or is under common control with You. For
purposes of this definition, "control" means (a) the power, direct
or indirect, to cause the direction or management of such entity,
whether by contract or otherwise, or (b) ownership of more than
fifty percent (50%) of the outstanding shares or beneficial
ownership of such entity.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
2. License Grants and Conditions
--------------------------------
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
2.1. Grants
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
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 hereby grants You a world-wide, royalty-free,
non-exclusive license:
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
(a) under intellectual property rights (other than patent or trademark)
Licensable by such Contributor to use, reproduce, make available,
modify, display, perform, distribute, and otherwise exploit its
Contributions, either on an unmodified basis, with Modifications, or
as part of a Larger Work; and
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
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
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
(b) under Patent Claims of such Contributor to make, use, sell, offer
for sale, have made, import, and otherwise transfer either its
Contributions or its Contributor Version.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
2.2. Effective Date
END OF TERMS AND CONDITIONS
The licenses granted in Section 2.1 with respect to any Contribution
become effective for each Contribution on the date the Contributor first
distributes such Contribution.
APPENDIX: How to apply the Apache License to your work.
2.3. Limitations on Grant Scope
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
The licenses granted in this Section 2 are the only rights granted under
this License. No additional rights or licenses will be implied from the
distribution or licensing of Covered Software under this License.
Notwithstanding Section 2.1(b) above, no patent license is granted by a
Contributor:
Copyright {yyyy} {name of copyright owner}
(a) for any code that a Contributor has removed from Covered Software;
or
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
(b) for infringements caused by: (i) Your and any other third party's
modifications of Covered Software, or (ii) the combination of its
Contributions with other software (except as part of its Contributor
Version); or
http://www.apache.org/licenses/LICENSE-2.0
(c) under Patent Claims infringed by Covered Software in the absence of
its Contributions.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the 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
the notice requirements in Section 3.4).
2.4. Subsequent Licenses
No Contributor makes additional grants as a result of Your choice to
distribute the Covered Software under a subsequent version of this
License (see Section 10.2) or under the terms of a Secondary License (if
permitted under the terms of Section 3.3).
2.5. Representation
Each Contributor represents that the Contributor believes its
Contributions are its original creation(s) or it has sufficient rights
to grant the rights to its Contributions conveyed by this License.
2.6. Fair Use
This License is not intended to limit any rights You have under
applicable copyright doctrines of fair use, fair dealing, or other
equivalents.
2.7. Conditions
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
in Section 2.1.
3. Responsibilities
-------------------
3.1. Distribution of Source Form
All distribution of Covered Software in Source Code Form, including any
Modifications that You create or to which You contribute, must be under
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
License, and how they can obtain a copy of this License. You may not
attempt to alter or restrict the recipients' rights in the Source Code
Form.
3.2. Distribution of Executable Form
If You distribute Covered Software in Executable Form then:
(a) such Covered Software must also be made available in Source Code
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
Form by reasonable means in a timely manner, at a charge no more
than the cost of distribution to the recipient; and
(b) You may distribute such Executable Form under the terms of this
License, or sublicense it under different terms, provided that the
license for the Executable Form does not attempt to limit or alter
the recipients' rights in the Source Code Form under this License.
3.3. Distribution of a Larger Work
You may create and distribute a Larger Work under terms of Your choice,
provided that You also comply with the requirements of this License for
the Covered Software. If the Larger Work is a combination of Covered
Software with a work governed by one or more Secondary Licenses, and the
Covered Software is not Incompatible With Secondary Licenses, this
License permits You to additionally distribute such Covered Software
under the terms of such Secondary License(s), so that the recipient of
the Larger Work may, at their option, further distribute the Covered
Software under the terms of either this License or such Secondary
License(s).
3.4. Notices
You may not remove or alter the substance of any license notices
(including copyright notices, patent notices, disclaimers of warranty,
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
License (APACHE v2.0)
License (MPL v2.0)
=====================
Copyright ememory Edouard DUPIN
Licensed under the Apache License, Version 2.0 (the "License");
Licensed under the Mozilla Public License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
https://www.mozilla.org/MPL/2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,

View File

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

View File

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

View File

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

View File

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

68
ememory/RefCounter.cpp Normal file
View File

@ -0,0 +1,68 @@
/** @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);
}

65
ememory/RefCounter.hpp Normal file
View File

@ -0,0 +1,65 @@
/** @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>

167
ememory/RefPtr.hpp Normal file
View File

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

155
ememory/UniquePtr.hpp Normal file
View File

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

View File

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

View File

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

View File

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

200
ememory/details/RefPtr.hxx Normal file
View File

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

View File

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

View File

@ -1,15 +1,17 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
* @license MPL v2.0 (see license file)
*/
#pragma once
#include <vector>
#include <mutex>
#include <etk/Vector.hpp>
#include <ethread/Mutex.hpp>
#include <ememory/SharedPtr.hpp>
#include <ememory/RefPtr.hpp>
#include <ememory/WeakPtr.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
@ -22,7 +24,7 @@ namespace ememory {
*/
template<class EMEMORY_TYPE, typename... EMEMORY_ARGS>
static ememory::SharedPtr<EMEMORY_TYPE> makeShared(EMEMORY_ARGS && ..._args) {
return ememory::SharedPtr<EMEMORY_TYPE>(new EMEMORY_TYPE(std::forward<EMEMORY_ARGS>(_args)...));
return ememory::SharedPtr<EMEMORY_TYPE>(ETK_NEW(EMEMORY_TYPE, etk::forward<EMEMORY_ARGS>(_args)...));
}
/**
* @brief Cast in Dynamic the input SharedPtr into an other type like dynamic_cast on pointer
@ -87,4 +89,81 @@ namespace ememory {
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());
}
/**
* @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
import lutin.debug as debug
import realog.debug as debug
import lutin.tools as tools
@ -13,7 +13,7 @@ def get_desc():
return "e-memory test-unit"
def get_licence():
return "APACHE-2"
return "MPL-2"
def get_compagny_type():
return "com"
@ -27,15 +27,18 @@ def get_maintainer():
def configure(target, my_module):
my_module.add_src_file([
'test/main.cpp',
'test/testUnique.cpp',
'test/testRef.cpp',
'test/testShared.cpp',
'test/testWeak.cpp',
'test/testEnableSharedFromThis.cpp',
'test/testCasts.cpp'
])
my_module.add_depend([
'cxx',
'ememory',
'gtest',
'test-debug'
'test-debug',
'etest',
])
return True

View File

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

View File

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

View File

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

View File

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

View File

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

208
test/testRef.cpp Normal file
View File

@ -0,0 +1,208 @@
/** @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,30 +1,56 @@
/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
* @license MPL v2.0 (see license file)
*/
#include <gtest/gtest.h>
#include <etest/etest.hpp>
#include <ememory/memory.hpp>
#include <test-debug/debug.hpp>
#include "main.hpp"
TEST(TestShared, createAndDestroy) {
ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou");
EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou");
EXPECT_EQ(data == nullptr, false);
EXPECT_EQ(data != nullptr, true);
EXPECT_EQ(data == null, false);
EXPECT_EQ(data != null, true);
data.reset();
EXPECT_EQ(data.useCount(), 0);
EXPECT_EQ(data == nullptr, true);
EXPECT_EQ(data != nullptr, false);
EXPECT_EQ(data == null, true);
EXPECT_EQ(data != null, 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) {
ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou");
EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou");
ememory::SharedPtr<std::string> dataCopy = data;
ememory::SharedPtr<etk::String> dataCopy = data;
EXPECT_EQ(data.useCount(), 2);
EXPECT_EQ(data == dataCopy, true);
EXPECT_EQ(data != dataCopy, false);
@ -41,10 +67,10 @@ TEST(TestShared, copy) {
}
TEST(TestShared, swap) {
ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou");
EXPECT_EQ(data.useCount(), 1);
EXPECT_EQ(*data, "coucou");
ememory::SharedPtr<std::string> dataCopy;
ememory::SharedPtr<etk::String> dataCopy;
EXPECT_EQ(dataCopy.useCount(), 0);
dataCopy.swap(data);
EXPECT_EQ(data.useCount(), 0);
@ -53,16 +79,16 @@ TEST(TestShared, swap) {
}
TEST(TestShared, callOperator) {
ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou");
EXPECT_EQ(data->size(), 6);
}
static void functionCallRef(std::string& _data) {
static void functionCallRef(etk::String& _data) {
_data = "plop";
}
TEST(TestShared, callOperatorStar) {
ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou");
EXPECT_EQ(data->size(), 6);
EXPECT_EQ(*data, "coucou");
*data = "ragout";
@ -74,11 +100,164 @@ TEST(TestShared, callOperatorStar) {
}
TEST(TestShared, setInVoid) {
ememory::SharedPtr<std::string> data = ememory::makeShared<std::string>("coucou");
ememory::SharedPtr<etk::String> data = ememory::makeShared<etk::String>("coucou");
ememory::SharedPtr<void> dataVoid(data);
ememory::SharedPtr<void> dataVoid2;
dataVoid2 = data;
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);
}

85
test/testUnique.cpp Normal file
View File

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

View File

@ -1 +1 @@
0.4.0
1.0.0