xot 0.1.11 → 0.1.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE +21 -0
- data/README.md +1 -1
- data/Rakefile +8 -13
- data/VERSION +1 -1
- data/ext/xot/extconf.rb +2 -3
- data/ext/xot/tester.cpp +55 -7
- data/include/xot.h +5 -3
- data/include/xot/debug.h +6 -1
- data/include/xot/defs.h +2 -3
- data/include/xot/exception.h +18 -6
- data/include/xot/noncopyable.h +30 -0
- data/include/xot/pimpl.h +10 -18
- data/include/xot/ref.h +25 -58
- data/include/xot/string.h +9 -0
- data/include/xot/time.h +3 -1
- data/include/xot/util.h +68 -13
- data/lib/xot.rb +5 -1
- data/lib/xot/bit_flag.rb +28 -15
- data/lib/xot/bit_flag_accessor.rb +78 -0
- data/lib/xot/bit_util.rb +19 -0
- data/lib/xot/const_symbol_accessor.rb +59 -0
- data/lib/xot/extconf.rb +8 -7
- data/lib/xot/module.rb +4 -19
- data/lib/xot/rake.rb +302 -127
- data/lib/xot/rake/alias_task.rb +34 -0
- data/lib/xot/rake/escalation.rb +32 -0
- data/lib/xot/rake/util.rb +255 -0
- data/lib/xot/setter.rb +2 -1
- data/lib/xot/universal_accessor.rb +40 -0
- data/src/debug.cpp +3 -3
- data/src/string.cpp +33 -2
- data/src/time.cpp +16 -8
- data/test/test_bit_flag.rb +1 -10
- data/test/test_bit_flag_accessor.rb +64 -0
- data/test/test_bit_util.rb +18 -0
- data/test/test_const_symbol_accessor.rb +56 -0
- data/test/test_universal_accessor.rb +51 -0
- data/xot.gemspec +3 -6
- metadata +28 -45
- data/task/ext.rake +0 -76
- data/task/gem.rake +0 -51
- data/task/lib.rake +0 -92
- data/task/mac.rake +0 -45
- data/task/test.rake +0 -35
data/include/xot/string.h
CHANGED
@@ -38,6 +38,15 @@ namespace Xot
|
|
38
38
|
|
39
39
|
String (const char* str);
|
40
40
|
|
41
|
+
template <typename ITERATOR>
|
42
|
+
String (ITERATOR begin, ITERATOR end) : Super(begin, end) {}
|
43
|
+
|
44
|
+
String upcase () const;
|
45
|
+
|
46
|
+
String downcase () const;
|
47
|
+
|
48
|
+
String strip () const;
|
49
|
+
|
41
50
|
operator const char* () const;
|
42
51
|
|
43
52
|
friend String operator + (const String& lhs, const String& rhs);
|
data/include/xot/time.h
CHANGED
data/include/xot/util.h
CHANGED
@@ -6,6 +6,7 @@
|
|
6
6
|
|
7
7
|
#include <stdint.h>
|
8
8
|
#include <math.h>
|
9
|
+
#include <assert.h>
|
9
10
|
#include <xot/defs.h>
|
10
11
|
#include <xot/time.h>
|
11
12
|
|
@@ -26,46 +27,100 @@ namespace Xot
|
|
26
27
|
double random (double min_, double max_);
|
27
28
|
|
28
29
|
|
30
|
+
template <typename T = uint>
|
31
|
+
inline constexpr T
|
32
|
+
bit (int nth, T base = 0x1)
|
33
|
+
{
|
34
|
+
return (T) (base << nth);
|
35
|
+
}
|
36
|
+
|
37
|
+
template <typename T>
|
38
|
+
inline constexpr T
|
39
|
+
clip (T minval, T maxval, T value)
|
40
|
+
{
|
41
|
+
return value > maxval ? maxval : (value < minval ? minval : value);
|
42
|
+
}
|
43
|
+
|
44
|
+
|
45
|
+
inline constexpr double
|
46
|
+
deg2rad (double degree)
|
47
|
+
{
|
48
|
+
return degree / 180.0 * M_PI;
|
49
|
+
}
|
50
|
+
|
51
|
+
inline constexpr double
|
52
|
+
rad2deg (double radian)
|
53
|
+
{
|
54
|
+
return radian / M_PI * 180.0;
|
55
|
+
}
|
56
|
+
|
57
|
+
|
29
58
|
template <typename T>
|
30
|
-
inline
|
59
|
+
inline void
|
60
|
+
add_flag (T* pvalue, uint flag)
|
31
61
|
{
|
32
|
-
|
62
|
+
assert(pvalue);
|
63
|
+
|
64
|
+
*pvalue |= flag;
|
33
65
|
}
|
34
66
|
|
35
67
|
template <typename T>
|
36
|
-
inline
|
68
|
+
inline void
|
69
|
+
remove_flag (T* pvalue, uint flag)
|
37
70
|
{
|
38
|
-
|
71
|
+
assert(pvalue);
|
72
|
+
|
73
|
+
*pvalue &= ~flag;
|
39
74
|
}
|
40
75
|
|
76
|
+
template <typename T>
|
77
|
+
inline bool
|
78
|
+
has_flag (T value, uint flag)
|
79
|
+
{
|
80
|
+
if (flag == 0) return false;
|
81
|
+
return (value & flag) == flag;
|
82
|
+
}
|
41
83
|
|
42
84
|
template <typename T>
|
43
|
-
inline
|
85
|
+
inline bool
|
86
|
+
check_and_remove_flag (T* pvalue, uint flag)
|
44
87
|
{
|
45
|
-
|
88
|
+
assert(pvalue);
|
89
|
+
|
90
|
+
bool has = has_flag(*pvalue, flag);
|
91
|
+
remove_flag(pvalue, flag);
|
92
|
+
return has;
|
46
93
|
}
|
47
94
|
|
48
95
|
|
96
|
+
static const uintptr_t POINTER_FLAG = 0x1;
|
97
|
+
|
49
98
|
template <typename T>
|
50
|
-
inline T*
|
99
|
+
inline T*
|
100
|
+
set_pointer_flag (T* pointer, bool flag = true)
|
51
101
|
{
|
52
102
|
uintptr_t intval = *(uintptr_t*) &pointer;
|
53
|
-
if (flag)
|
54
|
-
|
103
|
+
if (flag)
|
104
|
+
intval |= POINTER_FLAG;
|
105
|
+
else
|
106
|
+
intval &= ~POINTER_FLAG;
|
107
|
+
|
55
108
|
return *(T**) &intval;
|
56
109
|
}
|
57
110
|
|
58
111
|
template <typename T>
|
59
|
-
inline const T*
|
112
|
+
inline const T*
|
113
|
+
set_pointer_flag (const T* pointer, bool flag = true)
|
60
114
|
{
|
61
115
|
return set_pointer_flag(const_cast<T*>(pointer), flag);
|
62
116
|
}
|
63
117
|
|
64
118
|
template <typename T>
|
65
|
-
inline bool
|
119
|
+
inline bool
|
120
|
+
get_pointer_flag (const T* pointer)
|
66
121
|
{
|
67
|
-
uintptr_t intval = *(uintptr_t*) &pointer;
|
68
|
-
return intval &
|
122
|
+
const uintptr_t& intval = *(uintptr_t*) &pointer;
|
123
|
+
return intval & POINTER_FLAG;
|
69
124
|
}
|
70
125
|
|
71
126
|
|
data/lib/xot.rb
CHANGED
@@ -3,7 +3,11 @@
|
|
3
3
|
|
4
4
|
require 'xot/module'
|
5
5
|
require 'xot/bit_flag'
|
6
|
+
require 'xot/bit_flag_accessor'
|
7
|
+
require 'xot/bit_util'
|
6
8
|
require 'xot/block_util'
|
9
|
+
require 'xot/const_symbol_accessor'
|
7
10
|
require 'xot/hookable'
|
8
|
-
require 'xot/setter'
|
9
11
|
require 'xot/invoker'
|
12
|
+
require 'xot/setter'
|
13
|
+
require 'xot/universal_accessor'
|
data/lib/xot/bit_flag.rb
CHANGED
@@ -1,48 +1,61 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
|
3
3
|
|
4
|
+
require 'xot/bit_util'
|
5
|
+
|
6
|
+
|
4
7
|
module Xot
|
5
8
|
|
6
9
|
|
7
10
|
class BitFlag
|
8
11
|
|
9
|
-
|
10
|
-
|
12
|
+
include BitUtil
|
13
|
+
|
14
|
+
alias make_bit bit
|
15
|
+
|
16
|
+
def initialize (auto: false, none: 0, **flags, &block)
|
17
|
+
@bit2sym, @sym2bit, @auto, @next = {}, {none: none, no: none}, auto, 1
|
18
|
+
flags.each {|sym, value| flag sym, value}
|
11
19
|
BlockUtil.instance_eval_or_block_call self, &block if block
|
12
20
|
end
|
13
21
|
|
14
|
-
def flag (symbol, bit)
|
22
|
+
def flag (symbol, value = nil, bit: nil)
|
23
|
+
bit = value || make_bit(bit) || (@auto ? @next : nil)
|
24
|
+
|
25
|
+
raise ArgumentError if !bit
|
26
|
+
raise "flag: symbol #{symbol.inspect} or bit #{bit} is already registered." if
|
27
|
+
@sym2bit.key?(symbol) || @bit2sym.key?(bit)
|
28
|
+
|
29
|
+
single_bit = bit.to_s(2).count('1') == 1
|
30
|
+
|
15
31
|
@sym2bit[symbol] = bit
|
16
|
-
@bit2sym[bit] = symbol
|
32
|
+
@bit2sym[bit] = symbol if single_bit
|
33
|
+
@next = bit << 1 if single_bit && bit >= @next
|
34
|
+
|
35
|
+
bit
|
17
36
|
end
|
18
37
|
|
19
38
|
def bits2symbols (bits)
|
20
39
|
array = []
|
21
40
|
bits.to_s(2).reverse.each_char.with_index do |char, index|
|
22
41
|
next unless char == '1'
|
23
|
-
|
24
|
-
|
25
|
-
raise "unknown bit for flag." unless symbol
|
42
|
+
symbol = @bit2sym[bit index]
|
43
|
+
raise "unknown bit #{index} for flag." unless symbol
|
26
44
|
array << symbol
|
27
45
|
end
|
28
46
|
array
|
29
47
|
end
|
30
48
|
|
31
49
|
def symbols2bits (*symbols)
|
32
|
-
symbols.
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.bit (*args)
|
36
|
-
bits = 0
|
37
|
-
args.each {|n| bits |= 0x1 << n}
|
38
|
-
bits
|
50
|
+
symbols.flatten.reduce(0) {|value, symbol| value | sym2bit(symbol)}
|
39
51
|
end
|
40
52
|
|
41
53
|
private
|
42
54
|
|
43
55
|
def sym2bit (symbol)
|
44
56
|
bit = @sym2bit[symbol]
|
45
|
-
|
57
|
+
bit = flag symbol if !bit && @auto
|
58
|
+
raise "unknown symbol #{symbol.inspect} for flag." unless bit
|
46
59
|
bit
|
47
60
|
end
|
48
61
|
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
|
4
|
+
require 'xot/bit_flag'
|
5
|
+
require 'xot/block_util'
|
6
|
+
|
7
|
+
|
8
|
+
module Xot
|
9
|
+
|
10
|
+
|
11
|
+
module BitFlagAccessor
|
12
|
+
|
13
|
+
def bit_flag_accessor (name, bit_flag = nil, **flags, &block)
|
14
|
+
bf = define_bit_flag name, bit_flag, flags, block
|
15
|
+
define_bit_flag_writer name, bf
|
16
|
+
define_bit_flag_reader name, bf
|
17
|
+
end
|
18
|
+
|
19
|
+
def bit_flag_writer (name, bit_flag = nil, **flags, &block)
|
20
|
+
define_bit_flag_writer name, define_bit_flag(name, bit_flag, flags, block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def bit_flag_reader (name, bit_flag = nil, **flags, &block)
|
24
|
+
define_bit_flag_reader name, define_bit_flag(name, bit_flag, flags, block)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def define_bit_flag (name, bit_flag, flags, block)
|
30
|
+
bit_flag ||= Xot::BitFlag.new **flags
|
31
|
+
Xot::BlockUtil.instance_eval_or_block_call bit_flag, &block if block
|
32
|
+
|
33
|
+
define_singleton_method "#{name}_flag".intern do
|
34
|
+
bit_flag
|
35
|
+
end
|
36
|
+
|
37
|
+
bit_flag
|
38
|
+
end
|
39
|
+
|
40
|
+
def define_bit_flag_writer (name, bit_flag)
|
41
|
+
writer = "#{name}=".intern
|
42
|
+
setter = "bf_set_#{name}__".intern
|
43
|
+
|
44
|
+
alias_method setter, writer
|
45
|
+
private setter
|
46
|
+
|
47
|
+
define_method writer do |*symbols|
|
48
|
+
__send__ setter, bit_flag.symbols2bits(symbols)
|
49
|
+
end
|
50
|
+
|
51
|
+
name
|
52
|
+
end
|
53
|
+
|
54
|
+
def define_bit_flag_reader (name, bit_flag)
|
55
|
+
reader = name.intern
|
56
|
+
getter = "bf_get_#{name}__".intern
|
57
|
+
|
58
|
+
alias_method getter, reader
|
59
|
+
private getter
|
60
|
+
|
61
|
+
define_method reader do
|
62
|
+
bit_flag.bits2symbols __send__(getter)
|
63
|
+
end
|
64
|
+
|
65
|
+
name
|
66
|
+
end
|
67
|
+
|
68
|
+
end# BitFlagAccessor
|
69
|
+
|
70
|
+
|
71
|
+
end# Xot
|
72
|
+
|
73
|
+
|
74
|
+
class Module
|
75
|
+
|
76
|
+
include Xot::BitFlagAccessor
|
77
|
+
|
78
|
+
end# Module
|
data/lib/xot/bit_util.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
|
4
|
+
module Xot
|
5
|
+
|
6
|
+
|
7
|
+
module ConstSymbolAccessor
|
8
|
+
|
9
|
+
def const_symbol_accessor (name, **symbol2const)
|
10
|
+
const_symbol_writer name, **symbol2const
|
11
|
+
const_symbol_reader name, **symbol2const
|
12
|
+
end
|
13
|
+
|
14
|
+
def const_symbol_writer (name, **symbol2const)
|
15
|
+
writer = "#{name}=".intern
|
16
|
+
setter = "cs_set_#{name}__".intern
|
17
|
+
|
18
|
+
alias_method setter, writer
|
19
|
+
private setter
|
20
|
+
|
21
|
+
define_method writer do |symbol|
|
22
|
+
const = symbol2const[symbol]
|
23
|
+
raise ArgumentError unless const
|
24
|
+
__send__ setter, const
|
25
|
+
end
|
26
|
+
|
27
|
+
name
|
28
|
+
end
|
29
|
+
|
30
|
+
def const_symbol_reader (name, **symbol2const)
|
31
|
+
reader = name.intern
|
32
|
+
getter = "cs_get_#{name}__".intern
|
33
|
+
|
34
|
+
alias_method getter, reader
|
35
|
+
private getter
|
36
|
+
|
37
|
+
const2symbol = symbol2const.reduce({}) {|h, (k, v)| h[v] = k; h}
|
38
|
+
|
39
|
+
define_method reader do
|
40
|
+
const = __send__ getter
|
41
|
+
symbol = const2symbol[const]
|
42
|
+
raise "'#{const}' is unknown value." unless symbol
|
43
|
+
symbol
|
44
|
+
end
|
45
|
+
|
46
|
+
name
|
47
|
+
end
|
48
|
+
|
49
|
+
end# ConstSymbolAccessor
|
50
|
+
|
51
|
+
|
52
|
+
end# Xot
|
53
|
+
|
54
|
+
|
55
|
+
class Module
|
56
|
+
|
57
|
+
include Xot::ConstSymbolAccessor
|
58
|
+
|
59
|
+
end# Module
|
data/lib/xot/extconf.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
|
3
3
|
|
4
|
-
require 'xot/rake'
|
4
|
+
require 'xot/rake/util'
|
5
5
|
require 'xot/block_util'
|
6
6
|
|
7
7
|
|
@@ -12,11 +12,11 @@ module Xot
|
|
12
12
|
|
13
13
|
include Xot::Rake
|
14
14
|
|
15
|
-
attr_reader :modules, :defs, :
|
15
|
+
attr_reader :modules, :defs, :inc_dirs, :lib_dirs, :headers, :libs, :local_libs, :frameworks
|
16
16
|
|
17
17
|
def initialize (*modules, &block)
|
18
18
|
@modules = modules.map {|m| m.const_get :Module}
|
19
|
-
@defs, @
|
19
|
+
@defs, @inc_dirs, @lib_dirs, @headers, @libs, @local_libs, @frameworks =
|
20
20
|
([[]] * 7).map &:dup
|
21
21
|
Xot::BlockUtil.instance_eval_or_block_call self, &block if block
|
22
22
|
end
|
@@ -36,15 +36,16 @@ module Xot
|
|
36
36
|
|
37
37
|
local_libs << (clang? ? 'c++' : 'stdc++')
|
38
38
|
|
39
|
-
$CPPFLAGS =
|
40
|
-
$CFLAGS =
|
41
|
-
$
|
39
|
+
$CPPFLAGS = make_cppflags $CPPFLAGS, defs, inc_dirs
|
40
|
+
$CFLAGS = make_cflags $CFLAGS + ' -x c++'
|
41
|
+
$CXXFLAGS = make_cflags $CXXFLAGS + ' -x c++' if $CXXFLAGS
|
42
|
+
$LDFLAGS = make_ldflags $LDFLAGS, lib_dirs, frameworks
|
42
43
|
$LOCAL_LIBS << local_libs.map {|s| " -l#{s}"}.join
|
43
44
|
end
|
44
45
|
|
45
46
|
def create_makefile (*args)
|
46
47
|
modules.each do |m|
|
47
|
-
dir_config m.name.downcase, m.
|
48
|
+
dir_config m.name.downcase, m.inc_dir, m.lib_dir
|
48
49
|
end
|
49
50
|
|
50
51
|
exit 1 unless headers.all? {|s| have_header s}
|
data/lib/xot/module.rb
CHANGED
@@ -6,6 +6,8 @@ module Xot
|
|
6
6
|
|
7
7
|
module Module
|
8
8
|
|
9
|
+
module_function
|
10
|
+
|
9
11
|
def name ()
|
10
12
|
super.split('::')[-2]
|
11
13
|
end
|
@@ -15,10 +17,10 @@ module Xot
|
|
15
17
|
end
|
16
18
|
|
17
19
|
def root_dir (path = '')
|
18
|
-
File.expand_path "
|
20
|
+
File.expand_path "../../#{path}", __dir__
|
19
21
|
end
|
20
22
|
|
21
|
-
def
|
23
|
+
def inc_dir ()
|
22
24
|
root_dir 'include'
|
23
25
|
end
|
24
26
|
|
@@ -26,23 +28,6 @@ module Xot
|
|
26
28
|
root_dir 'lib'
|
27
29
|
end
|
28
30
|
|
29
|
-
def task_dir ()
|
30
|
-
root_dir 'task'
|
31
|
-
end
|
32
|
-
|
33
|
-
def load_tasks (*names)
|
34
|
-
if names.empty?
|
35
|
-
Dir["#{task_dir}/**/*.rake"].each {|path| load path}
|
36
|
-
else
|
37
|
-
names.each do |name|
|
38
|
-
path = "#{task_dir}/#{name}.rake"
|
39
|
-
load path if File.exist? path
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
extend self
|
45
|
-
|
46
31
|
end# Module
|
47
32
|
|
48
33
|
|