Browse Source

using std::shared_ptr is too complicated for this.

heck-rework
heck 3 years ago
parent
commit
3b556c2e99
  1. 25
      test/test_nr1.cc

25
test/test_nr1.cc

@ -86,6 +86,8 @@ namespace pEp {
// set() can only be called once, will throw otherwise. // set() can only be called once, will throw otherwise.
class String { class String {
public: public:
// Usually you _have_ to use this ctor, because its impossible to create a static
// object if you have to know the address of a dynamically created one.
String() = default; String() = default;
// Best to use this constructor, as the object is in a valid state when the wrapped // Best to use this constructor, as the object is in a valid state when the wrapped
@ -112,9 +114,7 @@ namespace pEp {
// init // init
_c_str_pp = c_str_pp; _c_str_pp = c_str_pp;
// _c_str_p.reset(*_c_str_pp, [&](char* ptr) { this->_free(ptr); });
_c_str_p = *_c_str_pp; _c_str_p = *_c_str_pp;
_is_initialized = true; _is_initialized = true;
pEpLogClass(to_string() + " - taking ownership"); pEpLogClass(to_string() + " - taking ownership");
} }
@ -129,31 +129,14 @@ namespace pEp {
// DEALLOCATION // DEALLOCATION
_free(); _free();
// // if we point to an initialized string
// if (*_c_str_pp != nullptr) {
// // if the string there is not the one we created
//// if (*_c_str_pp != _c_str_p.get()) {
// if (*_c_str_pp != _c_str_p) {
// // we need to free it before we lose the pointer to it
// pEpLog("freeing foreign allocated string");
// pEp::free(*_c_str_pp);
// // } // else it will be free automatically upon .reset()
// }
// //and we need to anyways free the strings we create ourselfes
// if
// pEp::free(_c_str_p;)
// }
// ALLOCATION // ALLOCATION
if (str.empty()) { if (str.empty()) {
// if the new value is empty str, lets point to nothing // if the new value is empty str, lets point to nothing
*_c_str_pp = nullptr; *_c_str_pp = nullptr;
// _c_str_p.reset(*_c_str_pp);
_c_str_p = *_c_str_pp; _c_str_p = *_c_str_pp;
} else { } else {
*_c_str_pp = pEp::alloc(str); *_c_str_pp = pEp::alloc(str);
// _c_str_p.reset(*_c_str_pp, [&](char* ptr) { this->_free(ptr); });
_c_str_p = *_c_str_pp; _c_str_p = *_c_str_pp;
} }
pEpLogClass("After: " + to_string()); pEpLogClass("After: " + to_string());
@ -198,7 +181,6 @@ namespace pEp {
static bool log_enabled; static bool log_enabled;
private: private:
void _free() void _free()
{ {
@ -219,7 +201,6 @@ namespace pEp {
bool _is_initialized{ false }; bool _is_initialized{ false };
char** _c_str_pp{ nullptr }; char** _c_str_pp{ nullptr };
// std::shared_ptr<char> _c_str_p{};
char* _c_str_p{ nullptr }; char* _c_str_p{ nullptr };
Adapter::pEpLog::pEpLogger logger{ "pEp::String", log_enabled }; Adapter::pEpLog::pEpLogger logger{ "pEp::String", log_enabled };
@ -324,7 +305,7 @@ void test_getters(char const* const* const c_str_p, pEp::String& pstr)
} else { } else {
std::string tmp{ pstr }; std::string tmp{ pstr };
pEpLog("operator std::string(): " + tmp); pEpLog("operator std::string(): " + tmp);
assert(tmp == ""); assert(tmp.empty());
} }
} }

Loading…
Cancel
Save