Skip to content

Commit

Permalink
first stab
Browse files Browse the repository at this point in the history
  • Loading branch information
heapwolf committed Mar 31, 2015
0 parents commit d80c1eb
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.sw*
build/
deps/
test/test

24 changes: 24 additions & 0 deletions Makefile
@@ -0,0 +1,24 @@

DEPS ?= gyp libuv

all: build test

.PHONY: deps
deps: $(DEPS)

gyp: deps/gyp
deps/gyp:
git clone --depth 1 https://chromium.googlesource.com/external/gyp.git ./deps/gyp

libuv: deps/libuv
deps/libuv:
git clone --depth 1 git://github.com/libuv/libuv.git ./deps/libuv

build: $(DEPS)
deps/gyp/gyp --depth=. -Goutput_dir=./out -Icommon.gypi --generator-output=./build -Dlibrary=static_library -Duv_library=static_library -f make

.PHONY: test
test: ./test/test.cc
make -C ./build/ test
cp ./build/out/Release/test ./test/test

26 changes: 26 additions & 0 deletions README.md
@@ -0,0 +1,26 @@
# SYNOPSIS

# USAGE

## AS A MODULE USING `BALE`.
```cpp
import Buffer "buffer"
Buffer buf(10);
```
## AS A REGLUAR C++ LIBRARY.
Build with `buffer.cc` as a dependency and include the header file.
```cpp
#include "buffer.h"
Buffer buf(10);
```

# API

## CONSTRUCTOR

## INSTANCE METHODS

## STATIC METHODS

44 changes: 44 additions & 0 deletions buffer.h
@@ -0,0 +1,44 @@
#ifndef NODEUV_BUFFER_H
#define NODEUV_BUFFER_H

#define NODEUV_MIN(a, b) ((a) < (b) ? (a) : (b))

#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <functional>

extern "C" {
#include "uv.h"
}

namespace nodeuv {

using namespace std;

class Buffer {

public:

uv_buf_t data;

string toString();
int length();
int copy(Buffer b);
int copy(Buffer b, int target_start);
int copy(Buffer b, int target_start, int source_start);
int copy(Buffer b, int target_start, int source_start, int source_end);

Buffer();
Buffer(int size);
Buffer(char* str);
Buffer(string str);
Buffer(const Buffer &buf);
Buffer& operator= (const Buffer &buf);
};
}

#endif

29 changes: 29 additions & 0 deletions build.gyp
@@ -0,0 +1,29 @@
{
'includes': [ 'common.gypi' ],
'targets': [
{
'target_name': 'nodeuv-buffer',
'product_name': 'nodeuv-buffer',
'type': 'static_library',
'sources': [
'buffer.h',
'./src/buffer.cc'
],
'dependencies': [
'./deps/libuv/uv.gyp:libuv',
],
},
{
'target_name': 'test',
'type': 'executable',
'sources': [
'./src/buffer.cc',
'./test/test.cc',
],
'dependencies': [
'./deps/libuv/uv.gyp:libuv',
'nodeuv-buffer',
],
}
]
}
83 changes: 83 additions & 0 deletions common.gypi
@@ -0,0 +1,83 @@
{
'variables': {
'conditions': [
['OS == "mac"', {
'target_arch%': 'x64'
}, {
'target_arch%': 'ia32'
}]
]
},
'target_defaults': {
'default_configuration': 'Release',
'conditions': [
['OS == "mac"', {
'defines': [ 'DARWIN' ]
}],
['OS == "linux"', {
'defines': [ 'LINUX' ]
}],
['OS == "win"', {
'defines': [ 'WIN32' ]
}],
['OS == "mac" and target_arch == "x64"', {
'xcode_settings': {
'ARCHS': [ 'x86_64' ]
},
}]
],
'configurations': {
'Debug': {
'cflags': [ '-g', '-O0', '-std=c++1y' ],
'defines': [ 'DEBUG' ],
'xcode_settings': {
'OTHER_CPLUSPLUSFLAGS' : [ '-std=c++1y' ],
'OTHER_LDFLAGS': ['-std=c++1y'],
'OTHER_CFLAGS': [ '-g', '-O0', '-Wall' ]
}
},
'Release': {
'cflags': [
'-O3',
'-std=c++1y',
'-fstrict-aliasing',
'-fomit-frame-pointer',
'-fdata-sections',
'-ffunction-sections',
'-fPIC',
],
'defines': [ 'NDEBUG' ],
'xcode_settings': {
'ALWAYS_SEARCH_USER_PATHS': 'NO',
'GCC_CW_ASM_SYNTAX': 'NO',
'GCC_DYNAMIC_NO_PIC': 'NO',
'GCC_ENABLE_PASCAL_STRINGS': 'NO',
'GCC_INLINES_ARE_PRIVATE_EXTERN': 'YES',
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES',
'GCC_THREADSAFE_STATICS': 'NO',
'GCC_WARN_ABOUT_MISSING_NEWLINE': 'YES',
'PREBINDING': 'NO',
'USE_HEADERMAP': 'NO',
'OTHER_CPLUSPLUSFLAGS' : [
'-O3',
'-std=c++1y',
'-fstrict-aliasing',
'-fomit-frame-pointer',
'-fdata-sections',
'-ffunction-sections',
'-fPIC',
],
'OTHER_CFLAGS': [
'-O3',
'-fstrict-aliasing',
'-fomit-frame-pointer',
'-fdata-sections',
'-ffunction-sections',
'-fPIC',
]
}
}
}
}
}

71 changes: 71 additions & 0 deletions src/buffer.cc
@@ -0,0 +1,71 @@
#include "../buffer.h"

namespace nodeuv {

using namespace std;

string Buffer::toString() {
string s;
s.assign(data.base, data.len);
return s;
}

int Buffer::length() {
return data.len;
}

int Buffer::copy(Buffer b) {
return this->copy(b, 0, 0, b.length());
}

int Buffer::copy(Buffer b, int target_start) {
return this->copy(b, target_start, 0, b.length());
}

int Buffer::copy(Buffer b, int target_start, int source_start) {
return this->copy(b, target_start, source_start, b.length());
}

int Buffer::copy(Buffer b, int target_start, int source_start, int source_end) {

size_t obj_length = b.length();
size_t target_length = this->length();
char* target_data = this->data.base;

if (target_start >= target_length || source_start >= source_end) {
return 0;
}

if (source_start > obj_length) {
throw runtime_error("out of range index");
}

uint32_t to_copy = NODEUV_MIN(NODEUV_MIN(source_end - source_start,
target_length - target_start),
obj_length - source_start);

memmove(this->data.base + target_start, b.data.base + source_start, to_copy);
return to_copy;
}

Buffer::Buffer() {
}

Buffer::Buffer(int size) {
data.base = (char*) malloc(size);
data.len = size;
}

Buffer::Buffer(string str) {
data = uv_buf_init((char*) str.c_str(), str.length());
}

Buffer::Buffer(char* str) {
data = uv_buf_init(str, strlen(str));
}

Buffer::Buffer(const Buffer &buf) {
data = buf.data;
}
}

26 changes: 26 additions & 0 deletions test/test.cc
@@ -0,0 +1,26 @@
#include "../buffer.h"
#include <istream>

using namespace nodeuv;

#define ASSERT(message, ...) do { \
if(!(__VA_ARGS__)) { \
std::cerr << "FAIL: " << message << std::endl; \
} \
else { \
std::cout << "OK: " << message << std::endl; \
} \
} while(0);

int main() {

//
// sanity test.
//
ASSERT("sanity: true is false", true == false);
ASSERT("sanity: true is true", true == true);



}

0 comments on commit d80c1eb

Please sign in to comment.