Another pass at C++ification.
This commit is contained in:
@@ -46,6 +46,8 @@ Sources = Split("""
|
|||||||
md5.c
|
md5.c
|
||||||
util.c
|
util.c
|
||||||
thread.c
|
thread.c
|
||||||
|
oocontext.cpp
|
||||||
|
oostanza.cpp
|
||||||
""")
|
""")
|
||||||
|
|
||||||
Headers = Split("""
|
Headers = Split("""
|
||||||
|
|||||||
@@ -110,13 +110,14 @@ xmpp_conn_t * xmpp_conn_clone(xmpp_conn_t * const conn)
|
|||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
void xmpp_conn_release(xmpp_conn_t * const conn)
|
int xmpp_conn_release(xmpp_conn_t * const conn)
|
||||||
{
|
{
|
||||||
xmpp_ctx_t *ctx;
|
xmpp_ctx_t *ctx;
|
||||||
xmpp_connlist_t *item, *prev;
|
xmpp_connlist_t *item, *prev;
|
||||||
xmpp_handlist_t *hlitem, *thli;
|
xmpp_handlist_t *hlitem, *thli;
|
||||||
hash_iterator_t *iter;
|
hash_iterator_t *iter;
|
||||||
const char *key;
|
const char *key;
|
||||||
|
int released = 0;
|
||||||
|
|
||||||
if (conn->ref > 1)
|
if (conn->ref > 1)
|
||||||
conn->ref--;
|
conn->ref--;
|
||||||
@@ -196,7 +197,10 @@ void xmpp_conn_release(xmpp_conn_t * const conn)
|
|||||||
if (conn->pass) xmpp_free(ctx, conn->pass);
|
if (conn->pass) xmpp_free(ctx, conn->pass);
|
||||||
if (conn->stream_id) xmpp_free(ctx, conn->stream_id);
|
if (conn->stream_id) xmpp_free(ctx, conn->stream_id);
|
||||||
xmpp_free(ctx, conn);
|
xmpp_free(ctx, conn);
|
||||||
|
released = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return released;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *xmpp_conn_get_jid(const xmpp_conn_t * const conn)
|
const char *xmpp_conn_get_jid(const xmpp_conn_t * const conn)
|
||||||
|
|||||||
33
src/ctx.c
33
src/ctx.c
@@ -57,10 +57,27 @@ int xmpp_version_check(int major, int minor)
|
|||||||
|
|
||||||
/* define the global default allocator, logger and context here */
|
/* define the global default allocator, logger and context here */
|
||||||
|
|
||||||
|
/* wrap stdlib routines to deal with userdata pointer */
|
||||||
|
static void *_malloc(const size_t size, void * const userdata)
|
||||||
|
{
|
||||||
|
return malloc(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void _free(void *p, void * const userdata)
|
||||||
|
{
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *_realloc(void *p, const size_t size, void * const userdata)
|
||||||
|
{
|
||||||
|
return realloc(p, size);
|
||||||
|
}
|
||||||
|
|
||||||
static xmpp_mem_t xmpp_default_mem = {
|
static xmpp_mem_t xmpp_default_mem = {
|
||||||
malloc, /* use the stdlib routines by default */
|
_malloc, /* use the thinly wrapped stdlib routines by default */
|
||||||
free,
|
_free,
|
||||||
realloc
|
_realloc,
|
||||||
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char * const _xmpp_log_level_name[4] = {"DEBUG", "INFO", "WARN", "ERROR"};
|
static const char * const _xmpp_log_level_name[4] = {"DEBUG", "INFO", "WARN", "ERROR"};
|
||||||
@@ -103,18 +120,18 @@ static xmpp_log_t xmpp_default_log = { NULL, NULL };
|
|||||||
|
|
||||||
void *xmpp_alloc(const xmpp_ctx_t * const ctx, const size_t size)
|
void *xmpp_alloc(const xmpp_ctx_t * const ctx, const size_t size)
|
||||||
{
|
{
|
||||||
return ctx->mem->alloc(size);
|
return ctx->mem->alloc(size, ctx->mem->userdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
void xmpp_free(const xmpp_ctx_t * const ctx, void *p)
|
void xmpp_free(const xmpp_ctx_t * const ctx, void *p)
|
||||||
{
|
{
|
||||||
ctx->mem->free(p);
|
ctx->mem->free(p, ctx->mem->userdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *xmpp_realloc(const xmpp_ctx_t * const ctx, void *p,
|
void *xmpp_realloc(const xmpp_ctx_t * const ctx, void *p,
|
||||||
const size_t size)
|
const size_t size)
|
||||||
{
|
{
|
||||||
return ctx->mem->realloc(p, size);
|
return ctx->mem->realloc(p, size, ctx->mem->userdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* logger */
|
/* logger */
|
||||||
@@ -190,9 +207,9 @@ xmpp_ctx_t *xmpp_ctx_new(const xmpp_mem_t * const mem,
|
|||||||
xmpp_ctx_t *ctx = NULL;
|
xmpp_ctx_t *ctx = NULL;
|
||||||
|
|
||||||
if (mem == NULL)
|
if (mem == NULL)
|
||||||
ctx = xmpp_default_mem.alloc(sizeof(xmpp_ctx_t));
|
ctx = xmpp_default_mem.alloc(sizeof(xmpp_ctx_t), NULL);
|
||||||
else
|
else
|
||||||
ctx = mem->alloc(sizeof(xmpp_ctx_t));
|
ctx = mem->alloc(sizeof(xmpp_ctx_t), mem->userdata);
|
||||||
|
|
||||||
if (ctx != NULL) {
|
if (ctx != NULL) {
|
||||||
if (mem != NULL)
|
if (mem != NULL)
|
||||||
|
|||||||
@@ -139,7 +139,8 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
|
|||||||
|
|
||||||
/* select errored */
|
/* select errored */
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
xmpp_error(ctx, "xmpp", "event watcher internal error");
|
xmpp_error(ctx, "xmpp", "event watcher internal error %d",
|
||||||
|
sock_error());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -120,8 +120,9 @@ copy_error:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* free a stanza object and it's contents */
|
/* free a stanza object and it's contents */
|
||||||
void xmpp_stanza_release(xmpp_stanza_t * const stanza)
|
int xmpp_stanza_release(xmpp_stanza_t * const stanza)
|
||||||
{
|
{
|
||||||
|
int released = 0;
|
||||||
xmpp_stanza_t *child, *tchild;
|
xmpp_stanza_t *child, *tchild;
|
||||||
|
|
||||||
/* release all children */
|
/* release all children */
|
||||||
@@ -139,7 +140,10 @@ void xmpp_stanza_release(xmpp_stanza_t * const stanza)
|
|||||||
if (stanza->attributes) hash_release(stanza->attributes);
|
if (stanza->attributes) hash_release(stanza->attributes);
|
||||||
if (stanza->data) xmpp_free(stanza->ctx, stanza->data);
|
if (stanza->data) xmpp_free(stanza->ctx, stanza->data);
|
||||||
xmpp_free(stanza->ctx, stanza);
|
xmpp_free(stanza->ctx, stanza);
|
||||||
|
released = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return released;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* small helper function */
|
/* small helper function */
|
||||||
|
|||||||
25
strophe.h
25
strophe.h
@@ -57,16 +57,17 @@ xmpp_ctx_t *xmpp_ctx_new(const xmpp_mem_t * const mem,
|
|||||||
void xmpp_ctx_free(xmpp_ctx_t * const ctx);
|
void xmpp_ctx_free(xmpp_ctx_t * const ctx);
|
||||||
|
|
||||||
struct _xmpp_mem_t {
|
struct _xmpp_mem_t {
|
||||||
void *(*alloc)(const size_t size);
|
void *(*alloc)(const size_t size, void * const userdata);
|
||||||
void (*free)(void *p);
|
void (*free)(void *p, void * const userdata);
|
||||||
void *(*realloc)(void *p, const size_t size);
|
void *(*realloc)(void *p, const size_t size, void * const userdata);
|
||||||
|
void *userdata;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
XMPP_LEVEL_DEBUG,
|
XMPP_LEVEL_DEBUG,
|
||||||
XMPP_LEVEL_INFO,
|
XMPP_LEVEL_INFO,
|
||||||
XMPP_LEVEL_WARN,
|
XMPP_LEVEL_WARN,
|
||||||
XMPP_LEVEL_ERROR
|
XMPP_LEVEL_ERROR
|
||||||
} xmpp_log_level_t;
|
} xmpp_log_level_t;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@@ -81,9 +82,9 @@ typedef void (*xmpp_log_handler)(void * const userdata,
|
|||||||
const char * const msg);
|
const char * const msg);
|
||||||
|
|
||||||
struct _xmpp_log_t {
|
struct _xmpp_log_t {
|
||||||
xmpp_log_handler handler;
|
xmpp_log_handler handler;
|
||||||
void * userdata;
|
void *userdata;
|
||||||
/* mutex_t lock; */
|
/* mutex_t lock; */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* return a default logger filtering at a given level */
|
/* return a default logger filtering at a given level */
|
||||||
@@ -143,7 +144,7 @@ typedef void (*xmpp_conn_handler)(xmpp_conn_t * const conn,
|
|||||||
|
|
||||||
xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx);
|
xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx);
|
||||||
xmpp_conn_t * xmpp_conn_clone(xmpp_conn_t * const conn);
|
xmpp_conn_t * xmpp_conn_clone(xmpp_conn_t * const conn);
|
||||||
void xmpp_conn_release(xmpp_conn_t * const conn);
|
int xmpp_conn_release(xmpp_conn_t * const conn);
|
||||||
|
|
||||||
const char *xmpp_conn_get_jid(const xmpp_conn_t * const conn);
|
const char *xmpp_conn_get_jid(const xmpp_conn_t * const conn);
|
||||||
void xmpp_conn_set_jid(xmpp_conn_t * const conn, const char * const jid);
|
void xmpp_conn_set_jid(xmpp_conn_t * const conn, const char * const jid);
|
||||||
@@ -216,7 +217,7 @@ xmpp_stanza_t *xmpp_stanza_clone(xmpp_stanza_t * const stanza);
|
|||||||
xmpp_stanza_t * xmpp_stanza_copy(const xmpp_stanza_t * const stanza);
|
xmpp_stanza_t * xmpp_stanza_copy(const xmpp_stanza_t * const stanza);
|
||||||
|
|
||||||
/** free a stanza object and it's contents */
|
/** free a stanza object and it's contents */
|
||||||
void xmpp_stanza_release(xmpp_stanza_t * const stanza);
|
int xmpp_stanza_release(xmpp_stanza_t * const stanza);
|
||||||
|
|
||||||
/** marshall a stanza into text for transmission or display **/
|
/** marshall a stanza into text for transmission or display **/
|
||||||
int xmpp_stanza_to_text(xmpp_stanza_t *stanza,
|
int xmpp_stanza_to_text(xmpp_stanza_t *stanza,
|
||||||
|
|||||||
111
strophepp.h
111
strophepp.h
@@ -6,29 +6,83 @@
|
|||||||
namespace XMPP {
|
namespace XMPP {
|
||||||
class Context {
|
class Context {
|
||||||
private:
|
private:
|
||||||
xmpp_ctx_t *ctx;
|
xmpp_mem_t m_mem;
|
||||||
|
xmpp_log_t m_log;
|
||||||
|
xmpp_ctx_t *m_ctx;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Context();
|
Context();
|
||||||
virtual ~Context();
|
virtual ~Context();
|
||||||
|
|
||||||
virtual void *alloc(const size_t size) = 0;
|
virtual void *alloc(const size_t size);
|
||||||
virtual void *realloc(void *p, const size_t size) = 0;
|
virtual void *realloc(void *p, const size_t size);
|
||||||
virtual void free(void *p) = 0;
|
virtual void free(void *p);
|
||||||
virtual void log(xmpp_log_handler handler, void * const userdata) = 0;
|
virtual void log(const xmpp_log_level_t level,
|
||||||
|
const char * const area,
|
||||||
|
const char * const msg);
|
||||||
|
|
||||||
|
xmpp_ctx_t *getContext();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void *callAlloc(const size_t size, void *userdata);
|
static void *callAlloc(const size_t size, void * const userdata);
|
||||||
static void *callRealloc(void *p, const size_t size, void *userdata);
|
static void *callRealloc(void *p, const size_t size,
|
||||||
static void *callFree(void *p, void *userdata);
|
void * const userdata);
|
||||||
}
|
static void callFree(void *p, void * const userdata);
|
||||||
|
static void callLog(void * const userdata,
|
||||||
|
const xmpp_log_level_t level,
|
||||||
|
const char * const area,
|
||||||
|
const char * const msg);
|
||||||
|
};
|
||||||
|
|
||||||
|
class Stanza {
|
||||||
|
private:
|
||||||
|
Context *m_ctx;
|
||||||
|
xmpp_stanza_t *m_stanza;
|
||||||
|
|
||||||
|
void *operator new(size_t size, Context *ctx);
|
||||||
|
void operator delete(void *p, Context *ctx);
|
||||||
|
Stanza(Context *ctx);
|
||||||
|
virtual ~Stanza();
|
||||||
|
|
||||||
|
public:
|
||||||
|
void release();
|
||||||
|
Stanza *clone();
|
||||||
|
Stanza *copy();
|
||||||
|
|
||||||
|
int toText(const char ** const buf, size_t * const buflen);
|
||||||
|
Stanza *getChildren();
|
||||||
|
Stanza *getChildByName(const char * const name);
|
||||||
|
Stanza *getNext();
|
||||||
|
char *getAttribute(const char * const name);
|
||||||
|
char *getNamespace();
|
||||||
|
char *getText();
|
||||||
|
char *getName();
|
||||||
|
void addChild(Stanza *child);
|
||||||
|
void setNamespace(const char * const ns);
|
||||||
|
void setAttribute(const char * const key, const char * const value);
|
||||||
|
void setName(const char * const name);
|
||||||
|
void setText(const char * const text);
|
||||||
|
void setText(const char * const text, const size_t size);
|
||||||
|
char *getType();
|
||||||
|
char *getId();
|
||||||
|
char *getTo();
|
||||||
|
char *getFrom();
|
||||||
|
void setType(const char * const type);
|
||||||
|
void setId(const char * const id);
|
||||||
|
void setTo(const char * const to);
|
||||||
|
void setFrom(const char * const from);
|
||||||
|
};
|
||||||
|
|
||||||
class Connection {
|
class Connection {
|
||||||
private:
|
private:
|
||||||
|
Context *m_ctx;
|
||||||
xmpp_conn_t *conn;
|
xmpp_conn_t *conn;
|
||||||
|
|
||||||
public:
|
void *operator new(size_t size, Context *ctx);
|
||||||
Connection(Context *ctx);
|
Connection(Context *ctx);
|
||||||
|
|
||||||
|
public:
|
||||||
|
static Connection *create(Context *ctx);
|
||||||
virtual ~Connection();
|
virtual ~Connection();
|
||||||
Connection *clone();
|
Connection *clone();
|
||||||
void operator delete(void *p);
|
void operator delete(void *p);
|
||||||
@@ -57,42 +111,7 @@ namespace XMPP {
|
|||||||
const char * const id,
|
const char * const id,
|
||||||
void * const userdata);
|
void * const userdata);
|
||||||
void deleteIdHandler(xmpp_handler handler);
|
void deleteIdHandler(xmpp_handler handler);
|
||||||
}
|
};
|
||||||
|
|
||||||
class Stanza {
|
|
||||||
private:
|
|
||||||
xmpp_stanza_t *stanza;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Stanza(Context *ctx);
|
|
||||||
virtual ~Stanza();
|
|
||||||
void operator delete(void *p);
|
|
||||||
Stanza *clone();
|
|
||||||
Stanza *copy();
|
|
||||||
|
|
||||||
int toText(const char ** const buf, size_t * const buflen);
|
|
||||||
Stanza *getChildren();
|
|
||||||
Stanza *getChildByName(const char * const name);
|
|
||||||
Stanza *getNext();
|
|
||||||
char *getAttribute(const char * const name);
|
|
||||||
char *getNamespace();
|
|
||||||
char *getText();
|
|
||||||
char *getName();
|
|
||||||
void addChild(Stanza *child);
|
|
||||||
void setNamespace(const char * const ns);
|
|
||||||
void setAttribute(const char * const key, const char * const value);
|
|
||||||
void setName(const char * const name);
|
|
||||||
void setText(const char * const text);
|
|
||||||
void setText(const char * const text, const size_t size);
|
|
||||||
char *getType();
|
|
||||||
char *getId();
|
|
||||||
char *getTo();
|
|
||||||
char *getFrom();
|
|
||||||
void setType(const char * const type);
|
|
||||||
void setId(const char * const id);
|
|
||||||
void setTo(const char * const to);
|
|
||||||
void setFrom(const char * const from);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* __LIBSTROPHE_STROPHEPP_H__ */
|
#endif /* __LIBSTROPHE_STROPHEPP_H__ */
|
||||||
|
|||||||
@@ -24,19 +24,19 @@ static int mem_alloc_called = 0;
|
|||||||
static int mem_free_called = 0;
|
static int mem_free_called = 0;
|
||||||
static int mem_realloc_called = 0;
|
static int mem_realloc_called = 0;
|
||||||
|
|
||||||
void *my_alloc(const size_t size)
|
void *my_alloc(const size_t size, void * const userdata)
|
||||||
{
|
{
|
||||||
mem_alloc_called++;
|
mem_alloc_called++;
|
||||||
return malloc(size);
|
return malloc(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void my_free(void *p)
|
void my_free(void *p, void * const userdata)
|
||||||
{
|
{
|
||||||
mem_free_called++;
|
mem_free_called++;
|
||||||
return free(p);
|
return free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *my_realloc(void *p, const size_t size)
|
void *my_realloc(void *p, const size_t size, void * const userdata)
|
||||||
{
|
{
|
||||||
mem_realloc_called++;
|
mem_realloc_called++;
|
||||||
return realloc(p, size);
|
return realloc(p, size);
|
||||||
|
|||||||
Reference in New Issue
Block a user