Skip to content
Snippets Groups Projects
Commit fd39dd93 authored by Recolic Keghart's avatar Recolic Keghart
Browse files

made rlib header only.

parent 9fd25a21
No related branches found
No related tags found
No related merge requests found
...@@ -9,30 +9,21 @@ PREFIX ?= /usr ...@@ -9,30 +9,21 @@ PREFIX ?= /usr
def: compile_library def: compile_library
compile_library:
$(CXX) $(CXXFLAGS) -c libr.cc -I . -o libr.o
$(AR) $(ARFLAGS) libr.a libr.o
install_header: install_header:
[ ! -d $(PREFIX)/include/rlib ] || rm -rf $(PREFIX)/include/rlib [ ! -d $(PREFIX)/include/rlib ] || rm -rf $(PREFIX)/include/rlib
cp -r . $(PREFIX)/include/rlib cp -r . $(PREFIX)/include/rlib
rm -rf $(PREFIX)/include/rlib/test $(PREFIX)/include/rlib/.git rm -rf $(PREFIX)/include/rlib/test $(PREFIX)/include/rlib/.git
install_library: compile_library
cp libr.a $(PREFIX)/lib/
install_cmake: install_library install_cmake: install_library
[ ! -d $(PREFIX)/lib/cmake/rlib ] || rm -rf $(PREFIX)/lib/cmake/rlib [ ! -d $(PREFIX)/lib/cmake/rlib ] || rm -rf $(PREFIX)/lib/cmake/rlib
[ ! -d $(PREFIX)/lib/cmake ] || cp -r cmake $(PREFIX)/lib/cmake/rlib [ ! -d $(PREFIX)/lib/cmake ] || cp -r cmake $(PREFIX)/lib/cmake/rlib
install: install_header install_library install_cmake install: install_header install_cmake
uninstall: uninstall:
rm -rf $(PREFIX)/include/rlib $(PREFIX)/lib/cmake/rlib rm -rf $(PREFIX)/include/rlib $(PREFIX)/lib/cmake/rlib
rm -f $(PREFIX)/lib/libr.a
clean: clean:
rm *.o *.a
.PHONY: test .PHONY: test
......
...@@ -14,6 +14,5 @@ if(MSYS OR MINGW) ...@@ -14,6 +14,5 @@ if(MSYS OR MINGW)
add_definitions(-DRLIB_MINGW_DISABLE_TLS) add_definitions(-DRLIB_MINGW_DISABLE_TLS)
endif() endif()
add_library(r STATIC ../libr.cc)
include_directories(..) include_directories(..)
# use this file if you want to install rlib # use this file if you want to install rlib
set(rlib_INCLUDE_DIRS ${PREFIX}/include) set(rlib_INCLUDE_DIRS ${PREFIX}/include)
set(rlib_LIBRARIES ${PREFIX}/lib/libr.a)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# using clang # using clang
......
#include <rlib/log.hpp> //log_level_t
#include <rlib/stream.hpp>
#include <sstream>
#if (RLIB_CXX_STD >= 2017) && (RLIB_COMPILER_ID != CC_MSVC)
#warning library should not be compiled under C++17. Or The library wont work for c++14 users.
#endif
namespace rlib {
namespace impl {
// If libr.cc is built under C++17, and other header files are included in C++14 project,
// Then something wrong will happen. So DO NOT ignore the following definitions. NEVER.
//#if RLIB_CXX_STD < 2017
bool enable_endl_flush = true;
int max_predefined_log_level = (int)log_level_t::DEBUG;
NullStreamBuf null_streambuf;
//#endif
//#ifndef RLIB_MINGW_DISABLE_TLS
//#if RLIB_CXX_STD < 2017
thread_local std::stringstream _format_string_helper_ss;
thread_local std::stringstream to_string_by_sstream_ss;
//#endif
//#endif
}
//#if RLIB_CXX_STD < 2017
std::ostream null_stream(&impl::null_streambuf);
//#endif
}
...@@ -36,11 +36,10 @@ namespace rlib { ...@@ -36,11 +36,10 @@ namespace rlib {
// Allow extension. // Allow extension.
enum class log_level_t : int { FATAL = 1, ERROR, WARNING, INFO, VERBOSE, DEBUG }; enum class log_level_t : int { FATAL = 1, ERROR, WARNING, INFO, VERBOSE, DEBUG };
namespace impl { namespace impl {
#if RLIB_CXX_STD < 2017 inline int &max_predefined_log_level() {
extern int max_predefined_log_level; static int instance = (int)log_level_t::DEBUG;
#else return instance;
inline int max_predefined_log_level = (int)log_level_t::DEBUG; }
#endif
} }
/* /*
How to update log_level_t: How to update log_level_t:
...@@ -109,10 +108,10 @@ namespace rlib { ...@@ -109,10 +108,10 @@ namespace rlib {
} }
// Warning: this method is not thread-safe. // Warning: this method is not thread-safe.
log_level_t register_log_level(const std::string &name) { log_level_t register_log_level(const std::string &name) {
if(impl::max_predefined_log_level == INT_MAX) if(impl::max_predefined_log_level() == INT_MAX)
throw std::overflow_error("At most {}(INT_MAX) log_level is allowed."_format(INT_MAX)); throw std::overflow_error("At most {}(INT_MAX) log_level is allowed."_format(INT_MAX));
++ impl::max_predefined_log_level; ++ impl::max_predefined_log_level();
log_level_t new_level = (log_level_t)impl::max_predefined_log_level; log_level_t new_level = (log_level_t)impl::max_predefined_log_level();
custom_log_level_names.push_back({new_level, name}); custom_log_level_names.push_back({new_level, name});
return new_level; return new_level;
} }
......
...@@ -96,25 +96,24 @@ namespace rlib { ...@@ -96,25 +96,24 @@ namespace rlib {
// implementations below -------------------------------- // implementations below --------------------------------
namespace impl { namespace impl {
#if RLIB_CXX_STD < 2017 inline bool &enable_endl_flush() {
extern bool enable_endl_flush; static bool instance = true;
#else return instance;
inline bool enable_endl_flush = true; }
#endif
} }
inline bool sync_with_stdio(bool sync = true) noexcept { inline bool sync_with_stdio(bool sync = true) noexcept {
return std::ios::sync_with_stdio(sync); return std::ios::sync_with_stdio(sync);
} }
inline bool enable_endl_flush(bool enable = true) noexcept { inline bool enable_endl_flush(bool enable = true) noexcept {
return impl::enable_endl_flush = enable; return impl::enable_endl_flush() = enable;
} }
// Implements. // Implements.
template < class CharT, class Traits > template < class CharT, class Traits >
inline std::basic_ostream<CharT, Traits>& endl(std::basic_ostream<CharT, Traits>& os) { inline std::basic_ostream<CharT, Traits>& endl(std::basic_ostream<CharT, Traits>& os) {
os << RLIB_IMPL_ENDLINE; os << RLIB_IMPL_ENDLINE;
if(impl::enable_endl_flush) if(impl::enable_endl_flush())
os.flush(); os.flush();
return os; return os;
} }
......
...@@ -10,16 +10,15 @@ namespace rlib { ...@@ -10,16 +10,15 @@ namespace rlib {
public: public:
int overflow(int c) { return c; } int overflow(int c) { return c; }
}; };
#if RLIB_CXX_STD < 2017
extern NullStreamBuf null_streambuf; inline NullStreamBuf & null_streambuf() {
#else static NullStreamBuf instance;
inline NullStreamBuf null_streambuf; return instance;
#endif }
} }
#if RLIB_CXX_STD < 2017 inline std::ostream &null_stream() {
extern std::ostream null_stream; static std::ostream instance(impl::null_streambuf());
#else return instance;
inline std::ostream null_stream(&impl::null_streambuf); }
#endif
} }
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <sstream> #include <sstream>
#include <type_traits> #include <type_traits>
// Intel C++ compiler has a pending bug for `thread_local inline` variable.
#if RLIB_COMPILER_ID == CC_ICC #if RLIB_COMPILER_ID == CC_ICC
#define RLIB_IMPL_SSTREAM_DISABLE_TLS #define RLIB_IMPL_SSTREAM_DISABLE_TLS
#endif #endif
...@@ -36,14 +37,14 @@ namespace rlib { ...@@ -36,14 +37,14 @@ namespace rlib {
// literals::_format, format_string, string::format // literals::_format, format_string, string::format
namespace impl { namespace impl {
#ifndef RLIB_IMPL_SSTREAM_DISABLE_TLS #ifndef RLIB_IMPL_SSTREAM_DISABLE_TLS
#if RLIB_CXX_STD < 2017 inline std::stringstream &to_string_by_sstream_ss() {
// Intel C++ compiler has a pending bug for `thread_local inline` variable. static thread_local std::stringstream instance;
thread_local extern std::stringstream to_string_by_sstream_ss; return instance;
thread_local extern std::stringstream _format_string_helper_ss; }
#else inline std::stringstream &_format_string_helper_ss() {
thread_local inline std::stringstream to_string_by_sstream_ss; static thread_local std::stringstream instance;
thread_local inline std::stringstream _format_string_helper_ss; return instance;
#endif }
#endif #endif
template <typename VarT> template <typename VarT>
std::string to_string_by_sstream(VarT &thing) { std::string to_string_by_sstream(VarT &thing) {
...@@ -52,7 +53,7 @@ namespace rlib { ...@@ -52,7 +53,7 @@ namespace rlib {
// Also fix mingw bug. But much slower! // Also fix mingw bug. But much slower!
std::stringstream ss; std::stringstream ss;
#else #else
auto &ss = to_string_by_sstream_ss; auto &ss = to_string_by_sstream_ss();
ss.str(std::string()); ss.str(std::string());
#endif #endif
ss << thing; ss << thing;
...@@ -65,7 +66,7 @@ namespace rlib { ...@@ -65,7 +66,7 @@ namespace rlib {
// Fix intel C++ bug https://software.intel.com/en-us/forums/intel-c-compiler/topic/784136 // Fix intel C++ bug https://software.intel.com/en-us/forums/intel-c-compiler/topic/784136
std::stringstream ss; std::stringstream ss;
#else #else
auto &ss = _format_string_helper_ss; // cached stringstream is much quicker. auto &ss = _format_string_helper_ss(); // cached stringstream is much quicker.
ss.str(std::string()); ss.str(std::string());
#endif #endif
size_t pos = 0, prev_pos = 0; size_t pos = 0, prev_pos = 0;
......
...@@ -24,8 +24,8 @@ MODULES=string meta trait stdio sio scope_guard ...@@ -24,8 +24,8 @@ MODULES=string meta trait stdio sio scope_guard
XTRA_FLAGS ?= XTRA_FLAGS ?=
CXXFLAGS=-I. -I../.. $(XTRA_FLAGS) CXXFLAGS=-I. -I../.. $(XTRA_FLAGS)
STD ?= 14 STD ?= 14
FLAGS11=-std=c++11 rlib/libr.a FLAGS11=-std=c++11
FLAGS14=-std=c++14 rlib/libr.a FLAGS14=-std=c++14
FLAGS17=-std=c++17 FLAGS17=-std=c++17
ifeq ($(STD),11) ifeq ($(STD),11)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment