diff --git a/m-shared-ptr.h b/m-shared-ptr.h index fecd18a7..e5bce36c 100644 --- a/m-shared-ptr.h +++ b/m-shared-ptr.h @@ -623,6 +623,7 @@ fattr void M_F(name, _write_read2_unlock)(shared_t *out, const shared_t *src1, c M_IF_METHOD(INIT, oplist)( extern shared_t *M_F(name, _new)(void); , ) \ M_IF_METHOD(INIT_SET, oplist)( extern shared_t *M_F(name, _new_copy)(const shared_t *); , ) \ M_IF_METHOD(SET, oplist)( extern void M_F(name, _copy)(shared_t *, const shared_t *); , ) \ +M_IF_METHOD2(INIT_SET, TYPE, oplist)( extern shared_t *M_F(name, _new_from)(M_GET_TYPE oplist const); , ) \ extern shared_t *M_F(name, _acquire)(shared_t *); \ extern void M_F(name, _release)(shared_t *); \ extern void M_F(name, _set)(shared_t **, shared_t *); \ @@ -663,6 +664,21 @@ M_IF_METHOD(INIT_SET, oplist)( \ M_F(name, _read_unlock)(src); \ M_F(name, _init_lock)(out); \ return out; \ + } \ + \ + M_IF_METHOD(TYPE,oplist)( , \ + fattr shared_t *M_F(name, _new_from)(type const src); ) \ + fattr shared_t *M_F(name, _new_from)(type const src) \ + { \ + shared_t *out = M_CALL_NEW(oplist, shared_t); \ + if (M_UNLIKELY_NOMEM( out == NULL)) { \ + M_MEMORY_FULL(sizeof (shared_t)); \ + abort(); \ + } \ + M_ON_EXCEPTION( M_CALL_FREE(oplist, out) ) \ + M_CALL_INIT_SET(oplist, out->data, src); \ + M_F(name, _init_lock)(out); \ + return out; \ } \ , ) \ \ diff --git a/tests/test-mshared-ptr.c b/tests/test-mshared-ptr.c index c37063ec..31ce9e94 100644 --- a/tests/test-mshared-ptr.c +++ b/tests/test-mshared-ptr.c @@ -15,6 +15,27 @@ SHARED_PTR_DECL_AS(shared_double, SharedDouble, M_BASIC_OPLIST) SHARED_PTR_DEF_EXTERN_AS(shared_double, SharedDouble, double, M_BASIC_OPLIST) +static void test_double(void) +{ + SharedDouble *p = shared_double_new(); + SharedDouble *q = shared_double_new_from(2.0); + SharedDouble *r = shared_double_new_from(1.0); + SharedDouble *rr = shared_double_new_from(3.0); + shared_double_add(p, q, r); + assert(shared_double_equal_p(p, rr)); + shared_double_sub(p, q, r); + assert(shared_double_equal_p(p, r)); + shared_double_mul(p, q, r); + assert(shared_double_equal_p(p, q)); + shared_double_div(p, q, r); + assert(shared_double_equal_p(p, q)); + shared_double_reset(p); + shared_double_release(p); + shared_double_release(q); + shared_double_release(r); + shared_double_release(rr); +} + // TEST WITH STRING SHARED_PTR_DECL(shared_string, STRING_OPLIST) @@ -286,19 +307,12 @@ static void test_thread(void) m_thread_join(idx); } -static void test_double(void) -{ - SharedDouble *d = shared_double_new(); - shared_double_reset(d); - shared_double_clear(d); -} - int main(void) { test_string(); + test_double(); test_array(); test_array_string(); test_thread(); - test_double(); exit(0); }