librime: common.h Source File

| librime 1.2 Rime Input Method Engine, the core library

common.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2011-03-14 GONG Chen <chen.sst@gmail.com>
6//
7#ifndef RIME_COMMON_H_
8#define RIME_COMMON_H_
9
10#include <rime/build_config.h>
11
12#include <filesystem>
13#include <functional>
14#include <list>
15#include <map>
16#include <memory>
17#include <set>
18#include <string>
19#include <utility>
20#include <vector>
21#include <deque>
22#define BOOST_BIND_NO_PLACEHOLDERS
23#include <boost/signals2/connection.hpp>
24#include <boost/signals2/signal.hpp>
25#include <boost/unordered_map.hpp>
26#include <boost/unordered_set.hpp>
27
28#ifdef RIME_ENABLE_LOGGING
29#include <glog/logging.h>
30#else
31#include "no_logging.h"
32#endif // RIME_ENABLE_LOGGING
33
34// call a pointer to member function on this
35#define RIME_THIS_CALL(f) (this->*(f))
36
37#define RIME_THIS_CALL_AS(T, f) ((T*)this->*(f))
38
39namespace rime {
40
41using std::deque;
42using std::function;
43using std::list;
44using std::make_pair;
45using std::make_unique;
46using std::map;
47using std::pair;
48using std::set;
49using std::string;
50using std::vector;
51
52template <class Key, class T>
53 using hash_map = boost::unordered_map<Key, T>;
54template <class T>
55 using hash_set = boost::unordered_set<T>;
56
57template <class T>
58 using the = std::unique_ptr<T>;
59template <class T>
60 using an = std::shared_ptr<T>;
61template <class T>
62 using of = an<T>;
63template <class T>
64 using weak = std::weak_ptr<T>;
65
66template <class X, class Y>
67 inline an<X> As(const an<Y>& ptr) {
68 return std::dynamic_pointer_cast<X>(ptr);
69}
70
71template <class X, class Y>
72 inline bool Is(const an<Y>& ptr) {
73 return bool(As<X, Y>(ptr));
74}
75
76template <class T, class... Args>
77 inline an<T> New(Args&&... args) {
78 return std::make_shared<T>(std::forward<Args>(args)...);
79}
80
81using boost::signals2::connection;
82using boost::signals2::signal;
83
84 class path : public std::filesystem::path {
85 using fs_path = std::filesystem::path;
86
87 public:
88 path() : fs_path() {}
89 path(const fs_path& p) : fs_path(p) {}
90 path(fs_path&& p) : fs_path(std::move(p)) {}
91#ifdef _WIN32
92 // convert utf-8 string to native encoding path.
93 explicit path(const std::string& utf8_path)
94 : fs_path(std::filesystem::u8path(utf8_path)) {}
95 explicit path(const char* utf8_path)
96 : fs_path(std::filesystem::u8path(utf8_path)) {}
97#else
98 // disable implicit conversion from string to path for development purpose.
99 explicit path(const std::string& utf8_path) : fs_path(utf8_path) {}
100 explicit path(const char* utf8_path) : fs_path(utf8_path) {}
101#endif
102
103 path& operator/=(const path& p) { return *this = fs_path::operator/=(p); }
104 path& operator/=(const fs_path& p) { return *this = fs_path::operator/=(p); }
105 // convert UTF-8 encoded string to native encoding, then append.
106 path& operator/=(const std::string& p) { return *this /= path(p); }
107 path& operator/=(const char* p) { return *this /= path(p); }
108
109 friend path operator/(const path& lhs, const path& rhs) {
110 return path(lhs) /= rhs;
111 }
112 friend path operator/(const path& lhs, const fs_path& rhs) {
113 return path(lhs) /= rhs;
114 }
115 friend path operator/(const fs_path& lhs, const path& rhs) {
116 return path(lhs) /= rhs;
117 }
118 // convert UTF-8 encoded string to native encoding, then append.
119 friend path operator/(const path& lhs, const std::string& rhs) {
120 return path(lhs) /= path(rhs);
121 }
122 friend path operator/(const path& lhs, const char* rhs) {
123 return path(lhs) /= path(rhs);
124 }
125 friend path operator/(const fs_path& lhs, const std::string& rhs) {
126 return path(lhs) /= path(rhs);
127 }
128 friend path operator/(const fs_path& lhs, const char* rhs) {
129 return path(lhs) /= path(rhs);
130 }
131#ifdef RIME_ENABLE_LOGGING
132 friend std::ostream& operator<<(std::ostream& os, const path& p) {
133 return os << p.u8string();
134 }
135#endif
136};
137
138} // namespace rime
139
140#endif // RIME_COMMON_H_
no_logging.h
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::weak
std::weak_ptr< T > weak
Definition common.h:64
rime::operator<<
std::ostream & operator<<(std::ostream &stream, const Reference &reference)
Definition config_compiler.cc:12
rime::As
an< X > As(const an< Y > &ptr)
Definition common.h:67
rime::hash_map
boost::unordered_map< Key, T > hash_map
Definition common.h:53
rime::the
std::unique_ptr< T > the
Definition common.h:58
rime::Is
bool Is(const an< Y > &ptr)
Definition common.h:72
rime::hash_set
boost::unordered_set< T > hash_set
Definition common.h:55
rime::of
an< T > of
Definition common.h:62
rime::New
an< T > New(Args &&... args)
Definition common.h:77
rime::filesystem
Definition fs.h:7
rime::path
Definition common.h:84
rime::path::operator/
friend path operator/(const fs_path &lhs, const std::string &rhs)
Definition common.h:125
rime::path::operator/
friend path operator/(const fs_path &lhs, const path &rhs)
Definition common.h:115
rime::path::operator/=
path & operator/=(const fs_path &p)
Definition common.h:104
rime::path::operator/
friend path operator/(const fs_path &lhs, const char *rhs)
Definition common.h:128
rime::path::operator/
friend path operator/(const path &lhs, const fs_path &rhs)
Definition common.h:112
rime::path::operator/
friend path operator/(const path &lhs, const path &rhs)
Definition common.h:109
rime::path::operator/
friend path operator/(const path &lhs, const char *rhs)
Definition common.h:122
rime::path::operator/=
path & operator/=(const char *p)
Definition common.h:107
rime::path::operator/
friend path operator/(const path &lhs, const std::string &rhs)
Definition common.h:119
rime::path::operator/=
path & operator/=(const std::string &p)
Definition common.h:106
rime::path::path
path(fs_path &&p)
Definition common.h:90
rime::path::path
path(const std::string &utf8_path)
Definition common.h:99
rime::path::path
path(const char *utf8_path)
Definition common.h:100
rime::path::path
path(const fs_path &p)
Definition common.h:89
rime::path::path
path()
Definition common.h:88
rime::path::operator/=
path & operator/=(const path &p)
Definition common.h:103