Commit 85abf746 authored by Christoffer Ackelman's avatar Christoffer Ackelman

Backported unique_ptr from C++11.

parent 17c471c9
......@@ -465,7 +465,9 @@ int XttTbl::command(char* input_str)
/* Read command file */
sts = readcmdfile(&command[1]);
if (sts == DCLI__NOFILE) {
message('E', "Unable to open file");
char tmp[200];
snprintf(tmp, 200, "Unable to open file \"%s\"", &command[1]);
message('E', tmp);
return DCLI__SUCCESS;
} else if (EVEN(sts))
return sts;
......@@ -486,7 +488,9 @@ int XttTbl::command(char* input_str)
/* Read command file */
sts = readcmdfile(&symbol_value[1]);
if (sts == DCLI__NOFILE) {
message('E', "Unable to open file");
char tmp[200];
snprintf(tmp, 200, "Unable to open file \"%s\"", &symbol_value[1]);
message('E', tmp);
return DCLI__SUCCESS;
} else if (EVEN(sts))
return sts;
......
#ifndef CO_UNIQUE_PTR
#define CO_UNIQUE_PTR
#include "co_debug.h"
// std::remove_reference
template<typename T> struct remove_reference { typedef T type; };
template<typename T> struct remove_reference<T&> { typedef T type; };
template<typename T> struct remove_reference<T&&> { typedef T type; };
// std::forward
template<typename T>
T&& forward(typename remove_reference<T>::type& x) {
return static_cast<T&&>(x);
}
template<typename T>
T&& forward(typename remove_reference<T>::type&& x) {
return static_cast<T&&>(x);
}
// std::move
template<typename T>
typename remove_reference<T>::type&& move(T&& x) {
return ((typename remove_reference<T>::type&&)x);
}
template<typename T>
class default_delete {
public:
void operator()(T* ptr) const {
delete ptr;
}
};
template<typename T>
class default_delete<T[]> {
public:
void operator()(T* ptr) const {
delete[] ptr;
}
};
// std::unique_ptr for single objects -> free memory with 'delete'
template<typename T, typename Deleter = default_delete<T>>
class unique_ptr {
public:
typedef unique_ptr<T, Deleter> this_type;
unique_ptr(T* p) : value(p) {
}
unique_ptr(T* p, Deleter d) : value(p), del(d) {
}
unique_ptr(this_type&& x) : value(x.release()), del(forward<Deleter>(x.get_deleter())) {
}
this_type& operator=(this_type&& x) {
reset(x.release());
del = move(forward<Deleter>(x.get_deleter()));
return *this;
}
~unique_ptr() {
reset();
}
void reset(T* p = NULL) {
if (p != value) {
get_deleter()(value);
value = p;
}
}
T* release() {
T* const tmp = value;
value = NULL;
return tmp;
}
T& operator*() const {
return *value;
}
T* operator->() const {
return value;
}
T* get() const {
return value;
}
Deleter& get_deleter() {
return del;
}
const Deleter& get_deleter() const {
return del;
}
protected:
T* value;
Deleter del;
unique_ptr(const this_type&);
unique_ptr& operator=(const this_type&);
unique_ptr& operator=(T* p);
};
// std::unique_ptr for arrays -> free memory with 'delete[]'
template<typename T, typename Deleter>
class unique_ptr<T[], Deleter> {
public:
typedef unique_ptr<T[], Deleter> this_type;
unique_ptr(T* p, Deleter d = default_delete<T[]>()) : value(p), del(d) {
}
unique_ptr(this_type&& x) : value(x.release()), del(forward<Deleter>(x.get_deleter())) {
}
this_type& operator=(this_type&& x) {
reset(x.release());
del = move(forward<Deleter>(x.get_deleter()));
return *this;
}
~unique_ptr() {
reset();
}
void reset(T* p = NULL) {
if (p != value) {
get_deleter()(value);
value = p;
}
}
T* release() {
T* const tmp = value;
value = NULL;
return tmp;
}
T& operator[](int i) const {
return value[i];
}
T* get() const {
return value;
}
Deleter& get_deleter() {
return del;
}
const Deleter& get_deleter() const {
return del;
}
protected:
T* value;
Deleter del;
unique_ptr(const this_type&);
unique_ptr& operator=(const this_type&);
unique_ptr& operator=(T* p);
};
#endif
\ No newline at end of file
This diff is collapsed.
......@@ -37,6 +37,8 @@
#ifndef flow_draw_qt_h
#define flow_draw_qt_h
#include "co_smart_ptr.h"
#include "flow_draw.h"
#include <QColor>
......@@ -205,9 +207,6 @@ public:
void set_click_sensitivity(FlowCtx* ctx, int value);
void set_image_clip_mask(
QPainter* painter, flow_tPixmap pixmap, int x, int y);
void set_white_background(FlowCtx* ctx);
int image_get_width(flow_tImImage image);
......@@ -233,7 +232,7 @@ public:
void* flow_ctx, int page_border, int* sts);
private:
QPainter* get_painter(int painter_type, int size, bool nav = false);
unique_ptr<QPainter> get_painter(int painter_type, int size, bool nav = false);
int rect_helper(FlowCtx* ctx, int painter_type, int size, int x, int y,
int width, int height, bool nav = false, bool fill = false);
......
This diff is collapsed.
......@@ -37,7 +37,7 @@
#ifndef glow_draw_qt_h
#define glow_draw_qt_h
#include <stack>
#include "co_smart_ptr.h"
#include "glow_customcolors.h"
#include "glow_draw.h"
......@@ -175,7 +175,7 @@ public:
int pixmap_height);
virtual void reset_background(GlowWind* w);
virtual void set_image_clip_mask(
QPainter* painter, glow_tPixmap pixmap, int x, int y);
unique_ptr<QPainter>& painter, glow_tPixmap pixmap, int x, int y);
virtual int set_clip_rectangle(
GlowWind* w, int ll_x, int ll_y, int ur_x, int ur_y);
......@@ -192,13 +192,13 @@ public:
virtual void buffer_background(DrawWind* w, GlowCtx* cctx);
virtual int print(char* filename, double x0, double x1, int end);
virtual int export_image(char* filename);
void set_clip(DrawWind* w, QPainter* painter);
void set_clip(DrawWind* w, unique_ptr<QPainter>& painter);
virtual void set_timer(GlowCtx* ctx, int time_ms,
void (*callback_func)(GlowCtx* ctx), void** id);
virtual void remove_timer(void* id);
int init_nav(QWidget* nav_widget);
QPoint* points_to_qt_points(glow_sPointX* points, int point_cnt);
QPoint* points_to_qt_points_curve(
unique_ptr<QPoint[]> points_to_qt_points(glow_sPointX* points, int point_cnt);
unique_ptr<QPoint[]> points_to_qt_points_curve(
GlowWind* w, glow_sPointX* points, int point_cnt, int* cnt);
int image_get_width(glow_tImImage image);
int image_get_height(glow_tImImage image);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment