Skip to content

Commit

Permalink
Merge pull request #595 from Tencent/dev
Browse files Browse the repository at this point in the history
support golang
  • Loading branch information
lingol authored Dec 17, 2020
2 parents 4a87d26 + d8416b5 commit f81e077
Show file tree
Hide file tree
Showing 12 changed files with 1,537 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Android/MMKV/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.4.10'
ext.kotlin_version = '1.4.21'

repositories {
google()
Expand Down
6 changes: 1 addition & 5 deletions Core/MMKVLog_Android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ using namespace std;

constexpr auto APP_NAME = "MMKV";

extern const char *_getFileName(const char *path);

static android_LogPriority MMKVLogLevelDesc(MMKVLogLevel level) {
switch (level) {
case MMKVLogDebug:
Expand All @@ -47,7 +45,7 @@ static android_LogPriority MMKVLogLevelDesc(MMKVLogLevel level) {
}
}

void _MMKVLogWithLevel(MMKVLogLevel level, const char *file, const char *func, int line, const char *format, ...) {
void _MMKVLogWithLevel(MMKVLogLevel level, const char *filename, const char *func, int line, const char *format, ...) {
if (level >= g_currentLogLevel) {
string message;
char buffer[16];
Expand All @@ -68,8 +66,6 @@ void _MMKVLogWithLevel(MMKVLogLevel level, const char *file, const char *func, i
va_end(args);
}

auto filename = _getFileName(file);

if (g_logHandler) {
g_logHandler(level, filename, line, func, message);
} else {
Expand Down
7 changes: 7 additions & 0 deletions POSIX/golang/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Core/
Makefile
libmmkv.*
*.cbp
Testing/
tencent.com/
install_manifest.txt
73 changes: 73 additions & 0 deletions POSIX/golang/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#
# Tencent is pleased to support the open source community by making
# MMKV available.
#
# Copyright (C) 2020 THL A29 Limited, a Tencent company.
# All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.8.0)

project(mmkv)

IF(APPLE)
add_compile_definitions(FORCE_POSIX)
ENDIF()

add_subdirectory(../../Core Core)

This comment has been minimized.

Copy link
@DeaconBr

DeaconBr Apr 1, 2022

delete line 31


# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
mmkv

# Sets the library as a shared library.
STATIC

# Provides a relative path to your source file(s).
golang-bridge.h
golang-bridge.cpp
)

target_include_directories(mmkv PUBLIC
${CMAKE_CURRENT_SOURCE_DIR})

set_target_properties(mmkv PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( mmkv
core
pthread
)

# Install source file & library to tencent.com/mmkv dir

install(FILES mmkv.go callback.go go.mod mmkv_test.go golang-bridge.h golang-bridge.cpp
DESTINATION "tencent.com/mmkv")

install(FILES libmmkv.a Core/libcore.a
DESTINATION "tencent.com/mmkv/lib")

119 changes: 119 additions & 0 deletions POSIX/golang/callback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Tencent is pleased to support the open source community by making
* MMKV available.
*
* Copyright (C) 2020 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// MMKV is a cross-platform key-value storage framework developed by WeChat.
package mmkv

/*
#include "golang-bridge.h"
#include <stdlib.h>
*/
import "C"

// the callback type of Logger
type LogHandler func(level int, file string, line int, function string, message string)

var gLogHandler LogHandler

// register a log callback
func RegisterLogHandler(logHandler LogHandler) {
gLogHandler = logHandler

C.setWantsLogRedirect(C.bool(true))
}

// unregister a log callback
func UnRegisterLogHandler() {
gLogHandler = nil

C.setWantsLogRedirect(C.bool(false))
}

//export myLogHandler
func myLogHandler(level int, file string, line int, function string, message string) {
if gLogHandler != nil {
gLogHandler(level, file, line, function, message)
}
}

const (
OnErrorDiscard = iota // When there's an error, MMKV will discard everything by default.
OnErrorRecover // When there's an error, MMKV will try to recover as much data as possible.
)

const (
MMKVCRCCheckFail = iota
MMKVFileLength
)

// the callback type of error handler
// error is either MMKVCRCCheckFail or MMKVFileLength
// return OnErrorDiscard (default) or OnErrorRecover
type ErrorHandler func(mmapID string, error int) int

var gErrorHandler ErrorHandler

// register a error callback
func RegisterErrorHandler(errorHandler ErrorHandler) {
gErrorHandler = errorHandler

C.setWantsErrorHandle(C.bool(true))
}

// unregister a error callback
func UnRegisterErrorHandler() {
gErrorHandler = nil

C.setWantsErrorHandle(C.bool(false))
}

//export myErrorHandler
func myErrorHandler(mmapID string, error int) int {
if gErrorHandler != nil {
return gErrorHandler(mmapID, error)
}
return OnErrorDiscard
}

// the type of content change handler
type ContentChangeHandler func(mmapID string)

var gContentChangeHandler ContentChangeHandler

// register a content change callback
func RegisterContentChangeHandler(contentChangeHandler ContentChangeHandler) {
gContentChangeHandler = contentChangeHandler

C.setWantsContentChangeHandle(C.bool(true))
}

// unregister a content change callback
func UnRegisterContentChangeHandler() {
gContentChangeHandler = nil

C.setWantsContentChangeHandle(C.bool(false))
}

//export myContentChangeHandler
func myContentChangeHandler(mmapID string) {
if gContentChangeHandler != nil {
gContentChangeHandler(mmapID)
}
}
3 changes: 3 additions & 0 deletions POSIX/golang/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module tencent.com/mmkv

go 1.15
Loading

0 comments on commit f81e077

Please sign in to comment.