mc2024w03
Link zu Concepts-Einführung: https://godbolt.org/z/7vbnb9xqn
# Exercise `FileResource`
** FINAL VERSION SO FAR (not quite completed) **
https://godbolt.org/z/rxYre6d7E
Eignung von C++ für Embedded Systeme
- Prepared Code: https://godbolt.org/z/sGTTh9x3j
- Doesn't compile because of missing header file.
- Result Code: https://godbolt.org/z/veenGfvx8
## TODO Step 0
- Review the code and briefly describe its purpose.
- Which feature(s) of "Modern C++" do you recognice?
- Currently only Constructor Delegation (C++11)
- Make C++98 compatible with conditionally compilation
- Result Code: https://godbolt.org/z/KeKPexc6n
- https://godbolt.org/z/e4aYEdExn
## TODO Step 1
- Fix the failed test:
- The expecation in the test was wrong.
- Add a a message at the end all tests passed OK.
- uuResult Code: https://godbolt.org/z/1bE1EPf4x
## TODO Step 2
- Remove the comment before `test_failed_open()` in `main`
- Add a meaningful implementation of this test.
- Result Code: https://godbolt.org/z/441zzsbf8
## TODO Step 3
- Add more "Modern C++" features (while keeping C++98 compatible)
- replace `NULL with `nullptr`
- use uniform initialization wherever it can be applied
- replace the class `typedef` with the C++11 `using`-syntax
- Result Code: https://godbolt.org/z/oachPMdWb
- Alternative 1: https://godbolt.org/z/47Kr8s43x
- Alternative 2: https://godbolt.org/z/zYGhY1EhG
## TODO Step 4
- Currently only one or the four constructors is a atually tested.
- Write more assertion-based tests as function `test_all_constructors`.
- Don't forget test the automatically created copy constructor!
- Note that also an assignment operator will be automatically created.
- Result Code:
## TODO Step 5
- Which constructor alone would suffice to provide the functionality of four?
- Only the one accepting `std::string`-s as argument suffices
- Reason is `std::string` has a constructor accpting `const char *`
- Disadvantage:temporarily constructed `std::string` objects
- Result Code:
## TODO Step 6:
- Decission is made sharing `FileResource`-s is made need not be supported.
- Automatically created copying operations are blocked (classsic way).
- Adapt the tests (temporarily disable it only)
- Result Code:
## TODO Step 7:
- Decision is made to only support C++14 (or better)
- Automatically created operations are block with `= delete`
- `FileResrouce`-s now are "no-copy", "no-move"
- Result Code: https://godbolt.org/z/M18rr6v7j
## TODO Step 8:
- Make it possible to return `FileResource`-s.
- Manually implement a "move constructor" (neccessary for that purpose)
- Manually implement a "move assignment" (it's "nicer" to have that one too)
- Adapt the test code
- Result Code:
## TODO Step 9:
- Instead of a raw pointer to hold the `FILE*` use an `std::unique_ptr<FILE>`
- It needs to have a custom deleter to call `std::fclose`
- Be careful not to hand over `nullptr` to `std::fclose` (= UB!)
- Move constructor and assignment can now be `= default`
- `operator FILE*` needs to be slightly adapted
- Result Code:
## TODO Step 10 (optional)
- Discuss "robustness aspect" of `operator FILE*` might return invalid pointer
- Is it a good idea to assert its not `nullptr` in the operation
- But how to test that for validity then?
- repurpose `use_count` for that? (Though it's a misnomer)
- maybe rename ot: `is_valid()`
- alternatively provide `explicit operator bool()`
- Result Code:
## TODO Step 11:
- Demonstrate that `FileResource`-s now are not copyable
- Reason is the data member `std::unique_ptr<FILE>` is "move only"
- Demonstrate by removing the `=delete` for copy operarions
- Temporarily enable tests that copy: ==> COMPILE ERROR
- Result Code:
## TODO Step 12
- Instead of the current `std::unique_ptr` use `std::shared_ptr`
- Demonstrate that this easily support shallow copying
- Adapt the test code
TBD (MW)
https://godbolt.org/z/94Tnorj9o mit assignmnet OK
https://godbolt.org/z/ae36frosd