Last active
September 18, 2022 23:21
-
-
Save Loki-Astari/14e1aa35d178843c300ad3bfeb0f877c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace ThorsAnvil | |
| { | |
| class SomeThingWithResource | |
| { | |
| public: | |
| // Construction / Destruction | |
| SomeThingWithResource() | |
| : // Initalize Resouces | |
| {} | |
| ~SomeThingWithResource() | |
| { | |
| // release resources | |
| } | |
| // Optionally some other constructors | |
| // Maybe they replace the default one. | |
| // Copy and Move Cosntruction | |
| SomeThingWithResource(SomeThingWithResource const& src) | |
| : // Copy resources | |
| { | |
| } | |
| SomeThingWithResource(SomeThingWithResource&& src) noexcept | |
| : // Initialize empty resource | |
| // Note they still have to be valid. | |
| // So when they are moved to src it is still a valid object. | |
| { | |
| swap(src); | |
| } | |
| // Assignment. | |
| // Handles both copy and move operations. | |
| // An expression that is an rvalue ref will be moved into rhs using move constructor | |
| // An expression that is an lvalue will be copied into rhs using copy constructor | |
| SomeThingWithResource& operator=(SomeThingWithResource rhs) noexept | |
| { | |
| // Thus: | |
| // if used with rvalue it becomes swap/swap | |
| // if used with lvalue it becomes copy/swap | |
| swap(rhs); // swap the state of the two object. | |
| return *this; // return a reference to this. | |
| } | |
| // Swapping is exception safe: | |
| void swap(SomeThingWithResource& other) noexept | |
| { | |
| using std::swap; | |
| // Swap resources of this and other | |
| } | |
| private: | |
| // Put resources here. | |
| // If we have resources here (i.e. something that needs to be maintained in a valid state) | |
| // This something is something that needs to correctl obey the rule of 3/5 then swapping seems | |
| // like the cheapest mechanism. It makes sure that resources are in a good state. Not released | |
| // unless required. | |
| // | |
| // If this is a simpler class that holds state that is not that important. | |
| // or all states are valid. Then swap may be overkill. | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace ThorsAnvil | |
| { | |
| class SimpleString | |
| { | |
| public: | |
| SimpleString() | |
| : currentLength(0) | |
| , maxLength(32) | |
| , data(new char[maxLength]) | |
| {} | |
| ~SimpleString() | |
| { | |
| delete[] data; | |
| } | |
| SimpleString(SimpleString const& src) | |
| , currentLength(src.currentLength) | |
| , maxLength(std::max(src.currentLength, 32)) | |
| : data(new char[maxLength]) | |
| {} | |
| SimpleString(SimpleString&& src) noexcept | |
| : currentLength(0) | |
| , maxLength(32) | |
| , data(new char[maxLength]) | |
| { | |
| swap(src); | |
| } | |
| SimpleString& operator=(SimpleString rhs) noexept | |
| { | |
| swap(rhs); | |
| return *this; | |
| } | |
| void swap(SimpleString& other) noexept | |
| { | |
| using std::swap; | |
| swap(currentLength, other.currentLength); | |
| swap(maxLength, other.maxLength); | |
| swap(data, other.data); | |
| } | |
| private: | |
| int currentLength; | |
| int maxLength; | |
| char* data; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment