Skip to content

Commit 1b12772

Browse files
committed
ext/intl: GregorianCalendar addressing TODO.
using internally solely smart pointer for internal ICU class.
1 parent 2645663 commit 1b12772

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

ext/intl/calendar/calendar_class.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ U_CFUNC Calendar *calendar_fetch_native_calendar(zend_object *object)
6161
{
6262
Calendar_object *co = php_intl_calendar_fetch_object(object);
6363

64-
return co->ucal;
64+
return co->ucal.get();
6565
}
6666

6767
U_CFUNC void calendar_object_construct(zval *object,
@@ -71,7 +71,7 @@ U_CFUNC void calendar_object_construct(zval *object,
7171

7272
CALENDAR_METHOD_FETCH_OBJECT_NO_CHECK; //populate to from object
7373
assert(co->ucal == NULL);
74-
co->ucal = calendar;
74+
co->ucal.reset(calendar);
7575
}
7676

7777
/* {{{ clone handler for Calendar */
@@ -90,7 +90,7 @@ static zend_object *Calendar_clone_obj(zend_object *object)
9090
if (UNEXPECTED(!newCalendar)) {
9191
zend_throw_error(NULL, "Failed to clone IntlCalendar");
9292
} else {
93-
co_new->ucal = newCalendar;
93+
co_new->ucal.reset(newCalendar);
9494
}
9595
} else {
9696
zend_throw_error(NULL, "Cannot clone uninitialized IntlCalendar");
@@ -143,7 +143,7 @@ static HashTable *Calendar_get_debug_info(zend_object *object, int *is_temp)
143143
debug_info = zend_new_array(8);
144144

145145
co = php_intl_calendar_fetch_object(object);
146-
cal = co->ucal;
146+
cal = co->ucal.get();
147147

148148
if (cal == NULL) {
149149
ZVAL_FALSE(&zv);
@@ -221,10 +221,7 @@ static void Calendar_objects_free(zend_object *object)
221221
{
222222
Calendar_object* co = php_intl_calendar_fetch_object(object);
223223

224-
if (co->ucal) {
225-
delete co->ucal;
226-
co->ucal = NULL;
227-
}
224+
co->ucal.reset();
228225
intl_error_reset(CALENDAR_ERROR_P(co));
229226

230227
zend_object_std_dtor(&co->zo);
@@ -237,7 +234,7 @@ static zend_object *Calendar_object_create(zend_class_entry *ce)
237234
Calendar_object* intern = (Calendar_object*)zend_object_alloc(sizeof(Calendar_object), ce);
238235

239236
zend_object_std_init(&intern->zo, ce);
240-
object_properties_init(&intern->zo, ce);
237+
object_properties_init(&intern->zo, ce);
241238
calendar_object_init(intern);
242239

243240
return &intern->zo;

ext/intl/calendar/calendar_class.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ typedef struct {
3333
intl_error err;
3434

3535
// ICU calendar
36+
#ifndef __cplusplus
3637
Calendar* ucal;
38+
#else
39+
std::unique_ptr<Calendar> ucal;
40+
#endif
3741

3842
zend_object zo;
3943
} Calendar_object;

ext/intl/calendar/calendar_methods.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ static void _php_intlcal_field_uec_ret_in32t_method(
227227

228228
CALENDAR_METHOD_FETCH_OBJECT;
229229

230-
int32_t result = (co->ucal->*func)(
230+
int32_t result = (co->ucal.get()->*func)(
231231
(UCalendarDateFields)field, CALENDAR_ERROR_CODE(co));
232232
INTL_METHOD_CHECK_STATUS(co, "Call to ICU method has failed");
233233

@@ -349,7 +349,7 @@ static void _php_intlcal_before_after(
349349
RETURN_THROWS();
350350
}
351351

352-
UBool res = (co->ucal->*func)(*when_co->ucal, CALENDAR_ERROR_CODE(co));
352+
UBool res = (co->ucal.get()->*func)(*when_co->ucal, CALENDAR_ERROR_CODE(co));
353353
INTL_METHOD_CHECK_STATUS(co, "intlcal_before/after: Error calling ICU method");
354354

355355
RETURN_BOOL((int)res);
@@ -610,7 +610,7 @@ static void _php_intlcal_field_ret_in32t_method(
610610

611611
CALENDAR_METHOD_FETCH_OBJECT;
612612

613-
int32_t result = (co->ucal->*func)((UCalendarDateFields)field);
613+
int32_t result = (co->ucal.get()->*func)((UCalendarDateFields)field);
614614
INTL_METHOD_CHECK_STATUS(co, "Call to ICU method has failed");
615615

616616
RETURN_LONG((zend_long)result);

ext/intl/calendar/gregoriancalendar_methods.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ using icu::StringPiece;
4646
}
4747

4848
static inline GregorianCalendar *fetch_greg(Calendar_object *co) {
49-
return static_cast<GregorianCalendar *>(co->ucal);
49+
return static_cast<GregorianCalendar *>(co->ucal.get());
5050
}
5151

5252
static bool set_gregorian_calendar_time_zone(GregorianCalendar *gcal, UErrorCode status)
@@ -204,7 +204,7 @@ static void _php_intlgregcal_constructor_body(
204204
}
205205
}
206206

207-
co->ucal = gcal.release();
207+
co->ucal = std::move(gcal);
208208
}
209209

210210
U_CFUNC PHP_FUNCTION(intlgregcal_create_instance)
@@ -256,7 +256,7 @@ U_CFUNC PHP_METHOD(IntlGregorianCalendar, createFromDate)
256256

257257
object_init_ex(return_value, GregorianCalendar_ce_ptr);
258258
co = Z_INTL_CALENDAR_P(return_value);
259-
co->ucal = gcal.release();
259+
co->ucal = std::move(gcal);
260260

261261
cleanup:
262262
zend_restore_error_handling(&error_handling);
@@ -304,8 +304,7 @@ U_CFUNC PHP_METHOD(IntlGregorianCalendar, createFromDateTime)
304304

305305
object_init_ex(return_value, GregorianCalendar_ce_ptr);
306306
co = Z_INTL_CALENDAR_P(return_value);
307-
// TODO: trying to get passed the ownership change step
308-
co->ucal = gcal.release();
307+
co->ucal = std::move(gcal);
309308

310309
cleanup:
311310
zend_restore_error_handling(&error_handling);

0 commit comments

Comments
 (0)