C++ infrastructure

dev
Bill Sun 3 years ago
parent 20ed114766
commit 2bd7fdac52

4
.gitignore vendored

@ -29,3 +29,7 @@ test*.c*
*.k *.k
!header.k !header.k
!join.k !join.k
**/.vs
**/x64
*.user
*.filters

BIN
csv.so

Binary file not shown.

File diff suppressed because one or more lines are too long

@ -0,0 +1,3 @@
debug:
g++ -g3 -O0 server/server.cpp server/table.cpp -o a.out -Wall -Wextra -Wpedantic

@ -0,0 +1,6 @@
// Hint files help the Visual Studio IDE interpret Visual C++ identifiers
// such as names of functions and macros.
// For more information see https://go.microsoft.com/fwlink/?linkid=865984
#define Ops(o) template<typename T> vector_type<typename types::Coercion<_Ty, T>::type> operator##o(const vector_type<T>& r) { [[likely]] if (r.size == size) { return add(r); } else if (r.size == 1 || size == 1) { const bool lr = size == 1; const uint32_t& _size = lr ? r.size : size; const auto& _container = lr ? r.container : container; const auto& scalar = *(lr ? container : r.container); vector_type<typename types::Coercion<_Ty, T>::type> ret(_size); for (int i = 0; i < _size; ++i) ret[i] = _container[i] o scalar; return ret; } }
#define Op(o, x) template<typename T> vector_type<typename types::Coercion<_Ty, T>::type> inline x(const vector_type<T>& r) { vector_type<typename types::Coercion<_Ty, T>::type> ret(size); for (int i = 0; i < size; ++i) ret[i] = container[i] o r[i]; return ret; }
#define _Make_Ops(M) M(+, add) M(-, minus) M(*, multi) M(/, div) M(%, mod) M(&, and) M(|, or) M(^, xor)

@ -0,0 +1,9 @@
#ifndef _AQUERY_H
#define _AQUERY_H
#include "table.h"
class Context{
};
#endif

@ -0,0 +1,25 @@
#include <iostream>
#include "libaquery.h"
#ifdef _WIN32
#include "winhelper.h"
#else
#include <dlfcn.h>
#endif
typedef int (*code_snippet)(void*);
int main()
{
void* handle = dlopen("dll.so", RTLD_LAZY);
code_snippet c = static_cast<code_snippet>(dlsym(handle, "dlmain"));
printf("%d", c(0));
dlclose(handle);
vector_type<double> t1{ 1.2, 3.4, .2, 1e-5, 1, 3, 5, 4, 5};
vector_type<int> t2{ 2, 4, 3, 5, 0, 2, 6, 1, 2};
printf("x: ");
const auto& ret = t1 - t2;
for (const auto& x : ret) {
printf("%lf ", x);
}
puts("");
return 0;
}

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32210.308
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "server", "server.vcxproj", "{031352C2-AFBB-45AA-9518-DBC1F9EF2AF3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{031352C2-AFBB-45AA-9518-DBC1F9EF2AF3}.Debug|x64.ActiveCfg = Debug|x64
{031352C2-AFBB-45AA-9518-DBC1F9EF2AF3}.Debug|x64.Build.0 = Debug|x64
{031352C2-AFBB-45AA-9518-DBC1F9EF2AF3}.Debug|x86.ActiveCfg = Debug|Win32
{031352C2-AFBB-45AA-9518-DBC1F9EF2AF3}.Debug|x86.Build.0 = Debug|Win32
{031352C2-AFBB-45AA-9518-DBC1F9EF2AF3}.Release|x64.ActiveCfg = Release|x64
{031352C2-AFBB-45AA-9518-DBC1F9EF2AF3}.Release|x64.Build.0 = Release|x64
{031352C2-AFBB-45AA-9518-DBC1F9EF2AF3}.Release|x86.ActiveCfg = Release|Win32
{031352C2-AFBB-45AA-9518-DBC1F9EF2AF3}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {572EA821-8162-4161-9AC2-464C79F08B47}
EndGlobalSection
EndGlobal

@ -0,0 +1,160 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{031352c2-afbb-45aa-9518-dbc1f9ef2af3}</ProjectGuid>
<RootNamespace>server</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="table.cpp" />
<ClCompile Include="server.cpp" />
<ClCompile Include="vector_type.cpp" />
<ClCompile Include="winhelper.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="table.h" />
<ClInclude Include="libaquery.h" />
<ClInclude Include="types.h" />
<ClInclude Include="vector_type.hpp" />
<ClInclude Include="winhelper.h" />
</ItemGroup>
<ItemGroup>
<None Include="cpp.hint" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

@ -0,0 +1 @@
#include "table.h"

@ -0,0 +1,31 @@
#ifndef _TABLE_H
#define _TABLE_H
#include "types.h"
#include "vector_type.hpp"
template <class T>
class vector_type;
#ifdef _MSC_VER
namespace types {
enum Type_t;
}
#endif
template<typename Ty>
class ColRef : public vector_type<Ty>
{
const char* name;
types::Type_t ty;
ColRef(const char* name) : name(name) {}
};
class TableInfo {
const char* name;
ColRef<void>* colrefs;
uint32_t n_cols;
};
#endif

@ -0,0 +1,72 @@
#ifndef _TYPES_H
#define _TYPES_H
#include <typeinfo>
#include <functional>
#include <cstdint>
#include <type_traits>
#include "table.h"
namespace types {
enum Type_t {
AINT, AFLOAT, ASTR, ADOUBLE, ALDOUBLE, ALONG, ASHORT, ADATE, ATIME, ACHAR, NONE,
AUINT, AULONG, AUSHORT, AUCHAR
};
// TODO: deal with data/time <=> str/uint conversion
struct date_t {
uint32_t val;
date_t(const char* d) {
}
const char* toString() const{
return "";
}
};
struct time_t {
uint32_t val;
time_t(const char* d) {
}
const char* toString() const {
return "";
}
};
template <typename T>
struct Types {
typedef T type;
constexpr Types() noexcept = default;
#define ConnectTypes(f, x, y) \
f(int, AINT) \
f(float, AFLOAT) \
f(const char*, ASTR) \
f(double, ADOUBLE) \
f(long double, ALDOUBLE) \
f(long, ALONG) \
f(short, ASHORT) \
f(date_t, ADATE) \
f(time_t, ATIME) \
f(unsigned short, AUSHORT) \
f(unsigned long, AULONG) \
f(unsigned int, AUINT) \
f(unsigned char, ACHAR) \
f(void, NONE)
constexpr Type_t getType() const {
#define TypeConnect(x, y) if(typeid(T) == typeid(x)) return y; else
ConnectTypes(TypeConnect, x, y)
return NONE;
}
};
#define Cond(c, x, y) typename std::conditional<c, x, y>::type
#define Comp(o) (sizeof(T1) o sizeof(T2))
#define Same(x, y) (std::is_same_v<x, y>)
#define _U(x) std::is_unsigned<x>::value
#define Fp(x) std::is_floating_point<x>::value
template <class T1, class T2>
struct Coercion {
using t1 = Cond(Comp(<=), Cond(Comp(==), Cond(Fp(T1), T1, Cond(Fp(T2), T2, Cond(_U(T1), T2, T1))), T2), T1);
using t2 = Cond(Same(T1, T2), T1, Cond(Same(T1, const char*) || Same(T2, const char*), const char*, void));
using type = Cond(Same(t2, void), Cond(Same(T1, date_t) && Same(T2, time_t) || Same(T1, time_t) && Same(T2, time_t), void, t1), t2);
};
}
#endif // !_TYPES_H

@ -0,0 +1 @@
#include "vector_type.hpp"

@ -0,0 +1,220 @@
/*
* Bill Sun 2022
*/
#ifndef _VECTOR_TYPE
#define _VECTOR_TYPE
#include <cstring>
#include <cstdlib>
#include <cstdint>
#include <algorithm>
#include <initializer_list>
#include <vector>
#include <stdarg.h>
#include "types.h"
#pragma pack(push, 1)
template <typename _Ty>
class vector_type {
public:
void inline _copy(vector_type<_Ty>& vt) {
this->size = vt.size;
this->capacity = vt.capacity;
this->container = (_Ty*)malloc(size * sizeof(_Ty));
memcpy(container, vt.container, sizeof(_Ty) * size);
}
void inline _move(vector_type<_Ty>&& vt) {
this->size = vt.size;
this->capacity = vt.capacity;
this->container = vt.container;
vt.size = vt.capacity = 0;
vt.container = 0;
}
public:
_Ty* container;
uint32_t size, capacity;
typedef _Ty* iterator_t;
vector_type(uint32_t size) : size(size), capacity(size) {
container = (_Ty*)malloc(size * sizeof(_Ty));
}
constexpr vector_type(std::initializer_list<_Ty> _l) {
size = capacity = _l.size();
_Ty* container = this->container = (_Ty*)malloc(sizeof(_Ty) * _l.size());
for (const auto& l : _l) {
*(container++) = l;
}
}
constexpr vector_type() noexcept = default;
constexpr vector_type(vector_type<_Ty>& vt) noexcept {
_copy(vt);
}
constexpr vector_type(vector_type<_Ty>&& vt) noexcept {
_move(std::move(vt));
}
vector_type<_Ty> operator =(vector_type<_Ty>& vt) {
_copy(vt);
return *this;
}
vector_type<_Ty> operator =(vector_type<_Ty>&& vt) {
_move(std::move(vt));
return *this;
}
void emplace_back(_Ty _val) {
if (size >= capacity) { // geometric growth
capacity += 1 + capacity >> 1;
_Ty* n_container = (_Ty*)malloc(capacity * sizeof(_Ty));
memcpy(n_container, container, sizeof(_Ty) * size);
free(container);
container = n_container;
}
container[size++] = _val;
}
iterator_t erase(iterator_t _it) {
#ifdef DEBUG
// Do bound checks
if (!(size && capicity && container &&
_it >= container && (_it - container) < size))
return 0;
#endif
iterator_t curr = _it + 1, end = _it + --size;
while (curr < end)
*(curr - 1) = *(curr++);
return _it;
}
iterator_t begin() const {
return container;
}
iterator_t end() const {
return container + size;
}
iterator_t find(const _Ty item) const {
iterator_t curr = begin(), _end = end();
while (curr != _end && *(curr++) != item);
return curr;
}
_Ty& operator[](const std::size_t _i) const {
return container[_i];
}
void shrink_to_fit() {
if (size && capacity != size) {
capacity = size;
_Ty* _container = (_Ty*)malloc(sizeof(_Ty) * size);
memcpy(_container, container, sizeof(_Ty) * size);
free(container);
container = _container;
}
}
_Ty& back() {
return container[size - 1];
}
void qpop() {
size = size ? size - 1 : size;
}
void pop() {
if (size) {
--size;
if (capacity > (size << 1))
{
_Ty* new_container = (_Ty*)malloc(sizeof(_Ty) * size);
memcpy(new_container, container, sizeof(_Ty) * size);
free(container);
container = new_container;
capacity = size;
}
}
}
_Ty pop_back() {
return container[--size];
}
void merge(vector_type<_Ty>& _other) {
if (capacity < this->size + _other.size)
{
_Ty* new_container = (_Ty*)malloc(sizeof(_Ty) * (_other.size + this->size));
capacity = this->size + _other.size;
memcpy(new_container, container, sizeof(_Ty) * this->size);
memcpy(new_container + this->size, _other.container, sizeof(_Ty) * _other.size);
free(container);
container = new_container;
}
else
memcpy(container + this->size, _other.container, sizeof(_Ty) * _other.size);
size = this->size + _other.size;
}
template<class _Iter>
void merge(_Iter begin, _Iter end) {
unsigned int dist = static_cast<unsigned int>(std::distance(begin, end));
if (capacity < this->size + dist) {
_Ty* new_container = (_Ty*)malloc(sizeof(_Ty) * (dist + this->size));
capacity = this->size + dist;
memcpy(new_container, container, sizeof(_Ty) * this->size);
free(container);
container = new_container;
}
for (int i = 0; i < dist; ++i) {
container[i + this->size] = *(begin + i);
}
size = this->size + dist;
}
~vector_type() {
if (capacity > 0) free(container);
container = 0; size = capacity = 0;
}
#define Op(o, x) \
template<typename T>\
vector_type<typename types::Coercion<_Ty, T>::type> inline x(const vector_type<T>& r) const {\
vector_type<typename types::Coercion<_Ty, T>::type> ret(size);\
for (int i = 0; i < size; ++i)\
ret[i] = container[i] o r[i];\
return ret;\
}
#define Opeq(o, x) \
template<typename T>\
vector_type<typename types::Coercion<_Ty, T>::type> inline x##eq(const vector_type<T>& r) {\
for (int i = 0; i < size; ++i)\
container[i] = container[i] o##= r[i];\
return *this;\
}
#define Ops(o, x) \
template<typename T>\
vector_type<typename types::Coercion<_Ty, T>::type> operator o (const vector_type<T>& r) const {\
[[likely]] if (r.size == size) {\
return x(r);\
}\
}
#define Opseq(o, x) \
template<typename T>\
vector_type<typename types::Coercion<_Ty, T>::type> operator o##= (const vector_type<T>& r) {\
[[likely]] if (r.size == size) {\
return x##eq(r);\
}\
}
#define _Make_Ops(M) \
M(+, add) \
M(-, minus) \
M(*, multi) \
M(/, div) \
M(%, mod)
_Make_Ops(Op)
_Make_Ops(Opeq)
_Make_Ops(Ops)
_Make_Ops(Opseq)
};
#pragma pack(pop)
#endif

@ -0,0 +1,18 @@
#include "winhelper.h"
#include <Windows.h>
void* dlopen(const char* lib, int)
{
return LoadLibraryA(lib);
}
void* dlsym(void* handle, const char* proc)
{
return GetProcAddress(static_cast<HMODULE>(handle), proc);
}
int dlclose(void* handle)
{
return FreeLibrary(static_cast<HMODULE>(handle));
}

@ -0,0 +1,7 @@
#ifndef _WINHELPER_H
#define _WINHELPER_H
#define RTLD_LAZY 1
void* dlopen(const char*, int);
void* dlsym(void*, const char*);
int dlclose(void*);
#endif
Loading…
Cancel
Save