Source

Datenwerk / src / datenwerkpp / item.cpp

Full commit
/*
Copyright (C) 2011 by Leonard Ritter <contact@leonard-ritter.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <string.h>
#include "shared.hpp"

namespace dwk {

////////////////////////////////////////////////////////////////////////////////

item_type Item::get_type() const
{
	return item_type(dw_item_get_type(handle));
}

Item Item::parent() const
{
	return dw_item_parent(handle);
}

void Item::unhinge() const
{
	dw_item_unhinge(handle);
}

Item Item::get_root() const
{
	return dw_item_get_root(handle);
}

bool Item::is_related(const Item &item) const
{
	return dw_item_is_related(this->handle, item.c_ptr());
}

Item Item::copy() const
{
	return Item(new_reference(dw_item_copy(handle)));
}

void Item::incref()
{
	handle = dw_item_incref(handle);
}

void Item::decref()
{
	handle = dw_item_decref(handle);
}

int Item::refcount() const
{
	return dw_item_refcount(handle);
}

std::string Item::get_path(const Item &root/* = None*/) const
{
	char path[1024];
	if (dw_item_get_path(root.c_ptr(), handle, path, 1024))
		return path;
	assert(0);
	return "";
}

Item Item::from_path(const std::string &path) const
{
	return dw_item_from_path(handle, path.c_str());
}

bool Item::to_file(const std::string &filepath)
{
    return dw_item_to_file(handle, filepath.c_str());
}

static size_t file_io_read(void *ptr, size_t size, size_t count, void *ctx)
{
    FileIO *self = (FileIO *)ctx;
    return self->read(ptr, size, count);
}

static size_t file_io_write(const void *ptr, size_t size, size_t count, void *ctx)
{
    FileIO *self = (FileIO *)ctx;
    return self->write(ptr, size, count);
}

static int file_io_seek(void *ctx, long int offset, int origin)
{
    FileIO *self = (FileIO *)ctx;
    return self->seek(offset, origin);
}

static long int file_io_tell(void *ctx)
{
    FileIO *self = (FileIO *)ctx;
    return self->tell();
}    

static void init_file_io(dw_file_io_t &cio, FileIO &io)
{
    memset(&cio, 0, sizeof(dw_file_io_t));
    cio.ctx = &io;
    cio.read = &file_io_read;
    cio.write = &file_io_write;
    cio.seek = &file_io_seek;
    cio.tell = &file_io_tell;
}

bool Item::dump_file(FileIO &io, ItemToURIFunc item2uri, void *ctx)
{
    dw_file_io_t cio;
    init_file_io(cio, io);
    
    return dw_dump_file(&cio, handle, (dw_item_to_uri_t)item2uri, ctx);
}

std::string Item::get_debug_string() const
{
	char path[1024];
	if (dw_get_item_debug_string(handle, path, 1024))
		return path;
	assert(0);
	return "";
}

TypedSignal<EventItemParent> Item::get_signal_parent() const
{
	return dw_item_get_signal_parent(handle);
}

Item::Item(const Item &item)
	: handle(item.handle)
{
	if (handle)
		incref();
}

Item::Item(dw_item_t *handle)
{
	this->handle = handle;
	if (this->handle)
		incref();
}

Item::Item(const new_reference &ref)
	: handle(ref.handle)
{
}

Item::~Item()
{
	if (handle)
		decref();
}

dw_item_t *Item::c_ptr() const
{
	return this->handle;
}

Item &Item::operator =(const Item &item)
{
	if (handle)
		decref();
	handle = item.handle;
	if (handle)
		incref();
}

Item::Item()
	: handle(NULL)
{
}

bool Item::operator ==(const Item &item) const
{
	return handle == item.handle;
}

bool Item::operator !=(const Item &item) const
{
	return handle != item.handle;
}

bool Item::valid() const
{
	return handle != NULL;
}

////////////////////////////////////////////////////////////////////////////////

int get_item_count()
{
	return dw_get_item_count();
}

std::string get_item_type_string(item_type type)
{
	return dw_get_item_type_string(dw_item_type(type));
}

Item load_file(FileIO &io, URIToItemFunc uri2item, void *ctx)
{
    dw_file_io_t cio;
    init_file_io(cio, io);
    
    return Item(new_reference(dw_load_file(&cio, (dw_uri_to_item_t)uri2item, ctx)));
}

Item item_from_file(const std::string &filepath)
{
    return Item(new_reference(dw_item_from_file(filepath.c_str())));
}

////////////////////////////////////////////////////////////////////////////////

const Item None = Item((dw_item_t *)NULL);

} // namespace dwk