Skip to content

Introduce mbed::NonCopyable traits #4594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 26, 2017
Merged

Conversation

pan-
Copy link
Member

@pan- pan- commented Jun 20, 2017

Description

The NonCopyable template class disable auto generation of copy assignment operator and copy constructor for classes inheriting from it.

It avoid the private declaration of the copy assignment operator and copy constructor in favor of an explicit element in the base class list.

It is a good practice to tag non value type as NonCopyable, because it inform of invalid copy operation at compile time.

As an example consider the following code:

class Resource; 

class Foo { 
public:     
    Foo() : _resource(new Resource()) { }
    ~Foo() { delete _resource; } 
private:
    Resource* _resource;
};
 
Foo get_foo();
 
Foo foo = get_foo();

There is a bug in this function, it returns a temporary value which will be
byte copied into foo then destroyed. Unfortunately, internally the Foo class
manage a pointer to a Resource object. This pointer will be released when the
temporary is destroyed and foo will manage a pointer to an already released
Resource.

If the class Foo is marked as NonCopyable then it is not possible to write function with the signature Foo get_foo(); because an error will be generated at compile time.

class Resource; 

class Foo : private mbed::NonCopyable<Foo>{ 
public:     
    Foo() : _resource(new Resource()) { }
    ~Foo() { delete _resource; } 
private:
    Resource* _resource;
};
 

Foo get_foo();
Foo foo = get_foo(); // compile time error 

The extensive documentation as well as the implementation details can be found in the NonCopyable header.

Classes in rtos, platform, event and driver have been either fixed or updated to take advantage of this new traits.

Classes in features have not been touched.

Status

READY

Todos

  • Review

@pan- pan- requested review from geky and 0xc0170 June 20, 2017 13:47
The NonCopyable template class avoid autogeneration of copy assignement
and copy construction function for classes inheriting from it.
pan- added 7 commits June 20, 2017 16:23
…le, FileSystemLike, LocalFileHandle, LocalFileSystem and PlatformMutex as non copyable.

This avoid unwanted copy of these type which is a programming error.
…tor by a NonCopyable tag.

The class concerned by this change are: ATCmdParser, CallChain, FileBase and Stream.
The types marked are: Mail, MemoryPool, Mutex, Queue, RtosTimer and Semaphore.
…or by NonCopyable traits.

Modified classes are: BusIn, BusOut, BusInOut and InterruptManager.
… traits.

Classes changed: CAN, Ethernet, FlashIAP, I2C, InterruptIn, LowPowerTicker, LowPowerTimeout, LowPowerTimer, RawSerial, Serial, SerialBase, SPI, SPISlave, Ticker, Timeout, Timer, TimerEvent and UARTSerial.
@0xc0170
Copy link
Contributor

0xc0170 commented Jun 21, 2017

The jenkins CI fails

Error: TimerEvent.h@31,0: #330-D: “mbed::NonCopyable::NonCopyable(const mbed::NonCopyable &) [with T=mbed::TimerEvent]” (declared at line 157 of “./mbed-os/platform/NonCopyable.h”) is inaccessible

@pan-
Copy link
Member Author

pan- commented Jun 21, 2017

The CI failure is due to a programming error in the ST ble shield library: a Timeout object was incorrectly copied. Detecting this type of programming errors at compile time is the main motivation behind this PR.

I have submitted a fix: ARMmbed/ble-x-nucleo-idb0xa1#27

@pan-
Copy link
Member Author

pan- commented Jun 21, 2017

Since the issue is detected by mbed-os-cliapp, I've also submitted a PR to update the ST BlueNRG library: https://github.com/ARMmbed/mbed-os-cliapp/pull/283

Copy link
Contributor

@geky geky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

Excellent documentation and I find it enjoyable that we already found a bug XD

@pan-
Copy link
Member Author

pan- commented Jun 22, 2017

@0xc0170 the BLUENRG library has been updated in mbed-os cliapp, may you relaunch the tests ?

@0xc0170
Copy link
Contributor

0xc0170 commented Jun 22, 2017

@0xc0170 the BLUENRG library has been updated in mbed-os cliapp, may you relaunch the tests ?

restarted

Copy link
Member

@bulislaw bulislaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@adbridge
Copy link
Contributor

/morph test

@mbed-bot
Copy link

Result: SUCCESS

Your command has finished executing! Here's what you wrote!

/morph test

Output

mbed Build Number: 620

All builds and test passed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants