Commit 9785f2d3 authored by Kirill Smelkov's avatar Kirill Smelkov

cxx: New package

Start new package that provides C++ amendments to be used by libgolang
and its users. Current functionality: dict and set that wrap
std::unordered_map and std::unordered_set into ergonomic interface.

The code originates from wendelin.core:
https://lab.nexedi.com/kirr/wendelin.core/blob/5a045ed1/wcfs/internal/wcfs_misc.h#L214-257

Pyx/nogil only.
parent a245ab56
include COPYING README.rst CHANGELOG.rst tox.ini pyproject.toml trun
include golang/libgolang.h
include golang/runtime/libgolang.cpp
include golang/cxx.h
include golang/errors.h
include golang/errors.cpp
include golang/sync.h
......
#ifndef _NXD_LIBGOLANG_CXX_H
#define _NXD_LIBGOLANG_CXX_H
// Copyright (C) 2019 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
// Package cxx provides C++ amendments to be used by libgolang and its users.
#include <unordered_map>
#include <unordered_set>
#include <tuple>
// golang::cxx::
namespace golang {
namespace cxx {
using std::tuple;
// dict wraps unordered_map into ergonomic interface.
template<typename Key, typename Value>
struct dict : std::unordered_map<Key, Value> {
// has returns whether dict has element k.
bool has(const Key &k) const {
const dict &d = *this;
return d.find(k) != d.end();
}
// get implements `d[k] -> (v, ok)`.
tuple<Value, bool> get(const Key &k) const {
const dict &d = *this;
auto _ = d.find(k);
if (_ == d.end())
return make_tuple(Value(), false);
return make_tuple(_->second, true);
}
// pop implements `d[k] -> (v, ok); del d[k]`.
tuple<Value, bool> pop(const Key &k) {
dict &d = *this;
auto _ = d.find(k);
if (_ == d.end())
return make_tuple(Value(), false);
Value v = _->second;
d.erase(_);
return make_tuple(v, true);
}
};
// set wraps unordered_set into ergonomic interface.
template<typename Key>
struct set : std::unordered_set<Key> {
// has returns whether set has element k.
bool has(const Key &k) const {
const set &s = *this;
return s.find(k) != s.end();
}
};
}} // golang::cxx::
#endif // _NXD_LIBGOLANG_CXX_H
# cython: language_level=2
# Copyright (C) 2019 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
# it under the terms of the GNU General Public License version 3, or (at your
# option) any later version, as published by the Free Software Foundation.
#
# You can also Link and Combine this program with other software covered by
# the terms of any of the Free Software licenses or any of the Open Source
# Initiative approved licenses and Convey the resulting work. Corresponding
# source of such a combination shall include the source code for all other
# software used.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
"""Package cxx provides C++ amendments to be used by libgolang and its users."""
cimport libcpp.unordered_map
cimport libcpp.unordered_set
cdef extern from "<golang/cxx.h>" namespace "golang::cxx" nogil:
cppclass dict[Key, Value] (libcpp.unordered_map.unordered_map[Key, Value]):
bint has(Key k) const
# TODO get
# TODO pop
cppclass set[Key] (libcpp.unordered_set.unordered_set[Key]):
bint has(Key k) const
......@@ -166,6 +166,8 @@ def Extension(name, sources, **kw):
dependv.append('%s/golang/libgolang.h' % pygo)
dependv.append('%s/golang/_golang.pxd' % pygo)
dependv.append('%s/golang/__init__.pxd' % pygo)
dependv.append('%s/golang/cxx.h' % pygo)
dependv.append('%s/golang/cxx.pxd' % pygo)
dependv.append('%s/golang/errors.h' % pygo)
dependv.append('%s/golang/errors.pxd' % pygo)
dependv.append('%s/golang/_errors.pxd' % pygo)
......
......@@ -198,6 +198,7 @@ setup(
'golang/time.cpp'],
depends = [
'golang/libgolang.h',
'golang/cxx.h',
'golang/errors.h',
'golang/sync.h',
'golang/time.h'],
......
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