Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 9030379

Browse files
committed
Merge pull request #143 from krocard/fix_leak_and_warning_next
Fix default builder leak and make a warning non fatal in criterion's test: - Catch parses expression to detect operators at build time by overloading operators. This leaded gcc to warn that some expression might be ambiguous after macro extension. - Port C++14's make_unique. Manage default builders with unique_ptr to guard again leaks at compile time.
2 parents d5764c8 + f27c60b commit 9030379

4 files changed

Lines changed: 51 additions & 5 deletions

File tree

parameter/DefaultElementLibrary.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
#include <map>
3636
#include <string>
37+
#include <memory>
38+
#include <utility>
3739

3840
/** Factory that creates an element given an xml element. If no matching builder is found, it uses
3941
* the default builder.
@@ -46,17 +48,16 @@ class CDefaultElementLibrary: public CElementLibrary
4648
{
4749
public:
4850

49-
CDefaultElementLibrary() : _defaultBuilder(NULL) {}
5051
virtual ~CDefaultElementLibrary() {}
5152

5253
/** Set the default builder used in fallback mechanism.
5354
* @see createElement() for more detail on this mechanism.
5455
*
5556
* @param[in] defaultBuilder if NULL default builder mechanism, else provided builder is used.
5657
*/
57-
void setDefaultBuilder(CDefaultElementBuilder* defaultBuilder)
58+
void setDefaultBuilder(std::unique_ptr<CDefaultElementBuilder> defaultBuilder)
5859
{
59-
_defaultBuilder = defaultBuilder;
60+
_defaultBuilder = std::move(defaultBuilder);
6061
}
6162

6263

@@ -73,7 +74,7 @@ class CDefaultElementLibrary: public CElementLibrary
7374
CElement* createElement(const CXmlElement& xmlElement) const;
7475

7576
private:
76-
CDefaultElementBuilder* _defaultBuilder;
77+
std::unique_ptr<CDefaultElementBuilder> _defaultBuilder;
7778
};
7879

7980
template<class CDefaultElementBuilder>

parameter/SystemClass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <assert.h>
3939
#include "PluginLocation.h"
4040
#include "Utility.h"
41+
#include "Memory.hpp"
4142

4243
#define base CConfigurableElement
4344

@@ -109,7 +110,7 @@ bool CSystemClass::loadSubsystems(string& strError,
109110
_pSubsystemLibrary->addElementBuilder("Virtual", new VirtualSubsystemBuilder(_logger));
110111
// Set virtual subsytem as builder fallback if required
111112
if (bVirtualSubsystemFallback) {
112-
_pSubsystemLibrary->setDefaultBuilder(new VirtualSubsystemBuilder(_logger));
113+
_pSubsystemLibrary->setDefaultBuilder(make_unique<VirtualSubsystemBuilder>(_logger));
113114
}
114115

115116
// Add subsystem defined in shared libraries

parameter/criterion/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ if(BUILD_TESTING)
5656
# Add unit test
5757
add_executable(criterionUnitTest test/CriterionUnitTest.cpp)
5858

59+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=parentheses")
60+
5961
target_link_libraries(criterionUnitTest criterion)
6062
add_test(NAME criterionUnitTest
6163
COMMAND criterionUnitTest)

utility/Memory.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2011-2014, Intel Corporation
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation and/or
13+
* other materials provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its contributors
16+
* may be used to endorse or promote products derived from this software without
17+
* specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include <memory>
32+
33+
/** Implementation of C++14's std::make_unique.
34+
*
35+
* TODO: Specialisation for array types is not implemented.
36+
*/
37+
template<class T, class... Args>
38+
std::unique_ptr<T> make_unique(Args &&... args)
39+
{
40+
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
41+
}
42+

0 commit comments

Comments
 (0)