Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
conr2d committed Jun 21, 2022
0 parents commit a1d32d3
Show file tree
Hide file tree
Showing 29 changed files with 1,158 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Language: Cpp
BasedOnStyle: LLVM
AlignAfterOpenBracket: DontAlign
AlignEscapedNewlines: DontAlign
AlignOperands: DontAlign
AlignTrailingComments: false
AllowAllArgumentsOnNextLine: false
AllowShortEnumsOnASingleLine: false
AllowShortBlocksOnASingleLine: Never
AllowShortFunctionsOnASingleLine: Empty
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
ColumnLimit: 120
CompactNamespaces: false
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
IncludeCategories:
- Regex: '^(<(eo|noir)/)'
SortPriority: 2
- Regex: '^(<catch2/)'
SortPriority: 1
- Regex: '^(<[_0-9A-Za-z]+/)'
SortPriority: 3
- Regex: '^(<[_0-9A-Za-z]+)'
SortPriority: 4
IndentGotoLabels: false
IndentPPDirectives: AfterHash
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: true
NamespaceIndentation: Inner
PackConstructorInitializers: NextLine
PointerAlignment: Left
SpaceAfterTemplateKeyword: false
SpaceBeforeCtorInitializerColon: false
SpacesBeforeTrailingComments: 1
SpacesInLineCommentPrefix:
Minimum: 0
UseTab: Never
61 changes: 61 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# Mac OS
.DS_Store

# Build
build

# CLion
.idea
cmake-build-*

# Clangd
.cache
compile_commands.json

# Vim
.vim
.vimrc
*.swp

# VSCode
.vscode

# Doxygen
html

# Protobuf
*.pb.h
*.pb.cc
73 changes: 73 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
cmake_minimum_required(VERSION 3.16)

project(eo)

set(CMAKE_CXX_STANDARD 20)
add_compile_definitions(BOOST_ASIO_HAS_STD_INVOKE_RESULT)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 11.0)
message(FATAL_ERROR "GCC version 11 or later is required")
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.0)
message(FATAL_ERROR "Clang version 10 or later is required")
endif()
endif()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_BINARY_DIR})
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_BINARY_DIR})

if(UNIX AND NOT APPLE)
execute_process(COMMAND ping conan.io -c 2 -w 1000 RESULT_VARIABLE NO_CONNECTION OUTPUT_QUIET ERROR_QUIET)
elseif(APPLE)
execute_process(COMMAND ping conan.io -c 2 -W 1000 RESULT_VARIABLE NO_CONNECTION OUTPUT_QUIET ERROR_QUIET)
endif()

if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/conan.cmake" AND NOT NO_CONNECTION)
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/0.18.1/conan.cmake" "${CMAKE_CURRENT_BINARY_DIR}/conan.cmake" TLS_VERIFY ON)
endif()

include(conan)

set(CONAN_PACKAGES
fmt/8.1.1
scope-lite/0.2.0
)
if(UNIX AND NOT APPLE)
set(CONAN_PACKAGES
${CONAN_PACKAGES}
boost/1.78.0
)
endif()

conan_cmake_configure(REQUIRES ${CONAN_PACKAGES} GENERATORS cmake_find_package)

set(PROJECT_BUILD_TYPE ${CMAKE_BUILD_TYPE})
set(CMAKE_BUILD_TYPE Release)
conan_cmake_autodetect(CONAN_SETTINGS)
set(CMAKE_BUILD_TYPE ${PROJECT_BUILD_TYPE})

# HACK: remove c++ standard version from conan settings to use prebuilt binaries
string(REPLACE ";compiler.cppstd=20" "" CONAN_SETTINGS "${CONAN_SETTINGS}")
if(NOT NO_CONNECTION)
conan_cmake_install(PATH_OR_REFERENCE . BUILD missing REMOTE conancenter SETTINGS ${CONAN_SETTINGS})
endif()

if(APPLE)
if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(BREW_ROOT "/usr/local/opt")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64")
set(BREW_ROOT "/opt/homebrew/opt")
endif()
set(BOOST_ROOT "${BREW_ROOT}/boost" CACHE STRING "boost root directory")
set(OPENSSL_ROOT_DIR "${BREW_ROOT}/openssl" CACHE STRING "openssl root directory")
endif()

find_package(Boost REQUIRED)
find_package(fmt REQUIRED)
find_package(scope-lite REQUIRED)

add_subdirectory(src)
add_subdirectory(examples)
6 changes: 6 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
link_libraries(eo::main)

add_executable(hello-world hello-world.cpp)
add_executable(channels channels.cpp)
add_executable(channel-directions channel-directions.cpp)
add_executable(waitgroups waitgroups.cpp)
43 changes: 43 additions & 0 deletions examples/channel-directions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// https://gobyexample.com/channel-directions
//
// package main
//
// import "fmt"
//
// func ping(pings chan<- string, msg string) {
// pings <- msg
// }
//
// func pong(pings <-chan string, pongs chan<- string) {
// msg := <-pings
// pongs <- msg
// }
//
// func main() {
// pings := make(chan string, 1)
// pongs := make(chan string, 1)
// ping(pings, "passed message")
// pong(pings, pongs)
// fmt.Println(<-pongs)
// }

#include <eo/fmt.h>

using namespace eo;

func<> ping(chan<std::string>& pings, const std::string& msg) {
co_await (pings << msg);
}

func<> pong(chan<std::string>& pings, chan<std::string>& pongs) {
auto msg = co_await *pings;
co_await (pongs << msg);
}

func<> eo_main() {
auto pings = make_chan<std::string>(1);
auto pongs = make_chan<std::string>(1);
co_await ping(pings, "passed message");
co_await pong(pings, pongs);
fmt::println(co_await *pongs);
}
29 changes: 29 additions & 0 deletions examples/channels.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://gobyexample.com/channel
//
// package main
//
// import "fmt"
//
// func main() {
// messages := make(chan string)
//
// go func() { messages <- "ping" }()
//
// msg := <-messages
// fmt.Println(msg)
// }

#include <eo/fmt.h>

using namespace eo;

func<> eo_main() {
auto messages = make_chan<std::string>();

go([&]() -> func<> { co_await (messages << "ping"); });

auto msg = co_await *messages;
fmt::println(msg);

co_return;
}
19 changes: 19 additions & 0 deletions examples/hello-world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// https://gobyexample.com/hello-world
//
// package main
//
// import "fmt"
//
// func main() {
// fmt.Println("hello world")
// }

#include <eo/fmt.h>

using namespace eo;

func<> eo_main() {
fmt::println("hello world");

co_return;
}
63 changes: 63 additions & 0 deletions examples/waitgroups.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// https://gobyexample.com/waitgroups
//
// package main
//
// import (
// "fmt"
// "sync"
// "time"
// )
//
// func worker(id int) {
// fmt.Printf("Worker %d starting\n", id)
//
// time.Sleep(time.Second)
// fmt.Printf("Worker %d done\n", id)
// }
//
// func main() {
// var wg sync.WaitGroup
//
// for i := 1; i <= 5; i++ {
// wg.Add(1)
//
// i := i
//
// go func() {
// defer wg.Done()
// worker(i)
// }()
// }
//
// wg.Wait()
// }

#include <eo/fmt.h>
#include <eo/sync.h>
#include <eo/time.h>

using namespace eo;

func<> worker(int id) {
fmt::print("Worker {:d} starting\n", id);

co_await time::sleep(std::chrono::seconds(1));
fmt::print("Worker {:d} done\n", id);
}

func<> eo_main() {
auto wg = std::make_shared<sync::WaitGroup>();

for (auto i = 1; i <= 5; i++) {
wg->add(1);

go([wg, i]() -> func<> {
eo_defer([&]() { wg->done(); });
co_await worker(i);
});
}

wg->wait();

co_return;
}
41 changes: 41 additions & 0 deletions format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

if [[ $# -eq 0 ]];
then
paths="src"
else
paths="$@"
fi

# clang-format-13 wraps enum brace even though its option is set to false.
# Use clang-format-14 shipped with npm `clang-format` package
if [[ "$OSTYPE" == "darwin"* ]];
then
arch=$(uname -m)
if [[ "$arch" == "x86_64" ]];
then
formatter=/usr/local/lib/node_modules/clang-format/index.js
elif [[ "$arch" == "arm64" ]];
then
formatter=/opt/homebrew/lib/node_modules/clang-format/index.js
else
echo "ERROR: unsupported architecture( $arch )"
exit 1
fi
else
formatter=clang-format
fi

for path in $paths
do
if [[ -d "$path" ]]
then
find "$path" \( -name "*.h" -o -name "*.cpp" \) -exec $formatter -i --style=file {} \;
else
$formatter -i --style=file "$path"
fi
if [[ $? -ne 0 ]];
then
exit $?
fi
done
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set(PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

add_subdirectory(eo)
Loading

0 comments on commit a1d32d3

Please sign in to comment.