rixmap 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,297 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Rixmap::Image Spec
4
+ require_relative '../lib/rixmap'
5
+
6
+ describe Rixmap::Image do
7
+ context "256x256 Indexed Image" do
8
+ before do
9
+ @palette = Rixmap::Palette.new(256)
10
+ @image = Rixmap::Image.new(Rixmap::INDEXED, 256, 256, :palette => @palette)
11
+
12
+ # 初期化
13
+ n = 0
14
+ (1..16).each do |i|
15
+ (1..16).each do |j|
16
+ i2 = (i - 1) * 16
17
+ j2 = (j - 1) * 16
18
+ r = (255 - (Math.sqrt((255 - i2) ** 2 + j2 ** 2)))
19
+ g = (255 - (Math.sqrt((255 - i2) ** 2 + (255 - j2) ** 2)))
20
+ b = (255 - (Math.sqrt(i2 ** 2 + j2 ** 2)))
21
+ @palette[n] = [r, g, b, 255]
22
+ (((i - 1) * 16)...(i * 16)).each do |x|
23
+ (((j - 1) * 16)...(j * 16)).each do |y|
24
+ @image[x, y] = n
25
+ end
26
+ end
27
+ n += 1
28
+ end
29
+ end
30
+ end
31
+
32
+ it "mode should be INDEXED" do expect(@image.mode).to eq(Rixmap::INDEXED) end
33
+ it "is indexed" do expect(@image).to be_indexed end
34
+ it "is not grayscale" do expect(@image).not_to be_grayscale end
35
+ it "is not rgb" do expect(@image).not_to be_rgb end
36
+ it "width should be 256" do expect(@image.width).to eq(256) end
37
+ it "height should be 256" do expect(@image.height).to eq(256) end
38
+ it "palette should be same" do expect(@image.palette).to eq(@palette) end
39
+ it "size should be [256, 256]" do expect(@image.size).to eq([256, 256]) end
40
+ it "dim should be [256, 256, 1]" do expect(@image.dim).to eq([256, 256, 1]) end
41
+ it "all pixels are palette indexes" do
42
+ n = 0
43
+ (1..16).each do |i|
44
+ (1..16).each do |j|
45
+ (((i - 1) * 16)...(i * 16)).each do |x|
46
+ (((j - 1) * 16)...(j * 16)).each do |y|
47
+ expect(@image[x, y]).to eq(n)
48
+ end
49
+ end
50
+ n += 1
51
+ end
52
+ end
53
+ end
54
+ it "number of scanlines are same as height" do expect(@image.lines.size).to eq(@image.height) end
55
+ it "scanline type is Rixmap::Image::Line" do expect(@image.lines[0]).to be_kind_of(Rixmap::Image::Line) end
56
+ it "pixel type is FIXNUM" do expect(@image.pixels[0][0]).to be_kind_of(Fixnum) end
57
+ end
58
+
59
+ context "256x256 GRAYSCALE Image" do
60
+ before do
61
+ @image = Rixmap::Image.new(Rixmap::GRAYSCALE, 256, 256)
62
+
63
+ # 初期化
64
+ 256.times do |i|
65
+ 256.times do |j|
66
+ l = (255 - (Math.sqrt((255 - i) ** 2 + (255 - j) ** 2)))
67
+ @image[i, j] = l
68
+ end
69
+ end
70
+ end
71
+
72
+ it "mode should be GRAYSCALE" do expect(@image.mode).to eq(Rixmap::GRAYSCALE) end
73
+ it "is not indexed" do expect(@image).not_to be_indexed end
74
+ it "is grayscale" do expect(@image).to be_grayscale end
75
+ it "is not rgb" do expect(@image).not_to be_rgb end
76
+ it "width should be 256" do expect(@image.width).to eq(256) end
77
+ it "height should be 256" do expect(@image.height).to eq(256) end
78
+ it "palette should be nil" do expect(@image.palette).to be_nil end
79
+ it "size should be [256, 256]" do expect(@image.size).to eq([256, 256]) end
80
+ it "dim should be [256, 256, 1]" do expect(@image.dim).to eq([256, 256, 1]) end
81
+ it "all pixels are luminance" do
82
+ 256.times do |i|
83
+ 256.times do |j|
84
+ l = (255 - (Math.sqrt((255 - i) ** 2 + (255 - j) ** 2)))
85
+
86
+ # 比較用に丸め処理
87
+ dl = if l < 0
88
+ 0
89
+ elsif l > 255
90
+ 255
91
+ else
92
+ l.to_i
93
+ end
94
+
95
+ expect(@image[i, j]).to eq(dl)
96
+ end
97
+ end
98
+ end
99
+ it "number of scanlines are same as height" do expect(@image.lines.size).to eq(@image.height) end
100
+ it "scanline type is Rixmap::Image::Line" do expect(@image.lines[0]).to be_kind_of(Rixmap::Image::Line) end
101
+ it "pixel type is FIXNUM" do expect(@image.pixels[0][0]).to be_kind_of(Fixnum) end
102
+ end
103
+
104
+ context "256x256 GRAYALPHA Image" do
105
+ before do
106
+ @image = Rixmap::Image.new(Rixmap::GRAYALPHA, 256, 256)
107
+
108
+ # 初期化
109
+ 256.times do |i|
110
+ 256.times do |j|
111
+ l = (255 - (Math.sqrt((255 - i) ** 2 + (255 - j) ** 2)))
112
+ a = (255 - (Math.sqrt(i ** 2 + (255 - j) ** 2)))
113
+ @image[i, j] = [l, a]
114
+ end
115
+ end
116
+ end
117
+
118
+ it "mode should be GRAYALPHA" do expect(@image.mode).to eq(Rixmap::GRAYALPHA) end
119
+ it "is not indexed" do expect(@image).not_to be_indexed end
120
+ it "is grayscale" do expect(@image).to be_grayscale end
121
+ it "is not rgb" do expect(@image).not_to be_rgb end
122
+ it "width should be 256" do expect(@image.width).to eq(256) end
123
+ it "height should be 256" do expect(@image.height).to eq(256) end
124
+ it "palette should be nil" do expect(@image.palette).to be_nil end
125
+ it "size should be [256, 256]" do expect(@image.size).to eq([256, 256]) end
126
+ it "dim should be [256, 256, 2]" do expect(@image.dim).to eq([256, 256, 2]) end
127
+ it "all pixels are luminance" do
128
+ 256.times do |i|
129
+ 256.times do |j|
130
+ l = (255 - (Math.sqrt((255 - i) ** 2 + (255 - j) ** 2)))
131
+ a = (255 - (Math.sqrt(i ** 2 + (255 - j) ** 2)))
132
+
133
+ # 比較用に丸め処理
134
+ dl = if l < 0
135
+ 0
136
+ elsif l > 255
137
+ 255
138
+ else
139
+ l.to_i
140
+ end
141
+ da = if a < 0
142
+ 0
143
+ elsif a > 255
144
+ 255
145
+ else
146
+ a.to_i
147
+ end
148
+
149
+ expect(@image[i, j]).to eq([dl, da])
150
+ end
151
+ end
152
+ end
153
+ it "number of scanlines are same as height" do expect(@image.lines.size).to eq(@image.height) end
154
+ it "scanline type is Rixmap::Image::Line" do expect(@image.lines[0]).to be_kind_of(Rixmap::Image::Line) end
155
+ it "pixel type is Array" do expect(@image.pixels[0][0]).to be_kind_of(Array) end
156
+ end
157
+
158
+ context "256x256 RGB Image" do
159
+ before do
160
+ @image = Rixmap::Image.new(Rixmap::RGB, 256, 256)
161
+
162
+ # 初期化
163
+ 256.times do |i|
164
+ 256.times do |j|
165
+ r = (255 - (Math.sqrt((255 - i) ** 2 + j ** 2)))
166
+ g = (255 - (Math.sqrt((255 - i) ** 2 + (255 - j) ** 2)))
167
+ b = (255 - (Math.sqrt(i ** 2 + j ** 2)))
168
+ @image[i, j] = [r, g, b]
169
+ end
170
+ end
171
+ end
172
+
173
+ it "mode should be RGB" do expect(@image.mode).to eq(Rixmap::RGB) end
174
+ it "is not indexed" do expect(@image).not_to be_indexed end
175
+ it "is not grayscale" do expect(@image).not_to be_grayscale end
176
+ it "is rgb" do expect(@image).to be_rgb end
177
+ it "width should be 256" do expect(@image.width).to eq(256) end
178
+ it "height should be 256" do expect(@image.height).to eq(256) end
179
+ it "palette should be nil" do expect(@image.palette).to be_nil end
180
+ it "size should be [256, 256]" do expect(@image.size).to eq([256, 256]) end
181
+ it "dim should be [256, 256, 3]" do expect(@image.dim).to eq([256, 256, 3]) end
182
+ it "all pixels are [r, g, b] array" do
183
+ 256.times do |i|
184
+ 256.times do |j|
185
+ r = (255 - (Math.sqrt((255 - i) ** 2 + j ** 2)))
186
+ g = (255 - (Math.sqrt((255 - i) ** 2 + (255 - j) ** 2)))
187
+ b = (255 - (Math.sqrt(i ** 2 + j ** 2)))
188
+
189
+ # 比較用に丸め処理
190
+ dr = if r < 0
191
+ 0
192
+ elsif r > 255
193
+ 255
194
+ else
195
+ r.to_i
196
+ end
197
+ dg = if g < 0
198
+ 0
199
+ elsif g > 255
200
+ 255
201
+ else
202
+ g.to_i
203
+ end
204
+ db = if b < 0
205
+ 0
206
+ elsif b > 255
207
+ 255
208
+ else
209
+ b.to_i
210
+ end
211
+
212
+ expect(@image[i, j]).to eq([dr, dg, db])
213
+ end
214
+ end
215
+ end
216
+ it "number of scanlines are same as height" do expect(@image.lines.size).to eq(@image.height) end
217
+ it "scanline type is Rixmap::Image::Line" do expect(@image.lines[0]).to be_kind_of(Rixmap::Image::Line) end
218
+ it "pixel type is Array" do expect(@image.pixels[0][0]).to be_kind_of(Array) end
219
+ end
220
+
221
+ context "256x256 RGBA Image" do
222
+ before do
223
+ @image = Rixmap::Image.new(Rixmap::RGBA, 256, 256)
224
+
225
+ # 初期化
226
+ 256.times do |i|
227
+ 256.times do |j|
228
+ r = (255 - (Math.sqrt((255 - i) ** 2 + j ** 2)))
229
+ g = (255 - (Math.sqrt((255 - i) ** 2 + (255 - j) ** 2)))
230
+ b = (255 - (Math.sqrt(i ** 2 + j ** 2)))
231
+ a = (255 - (Math.sqrt(i ** 2 + (255 - j) ** 2)))
232
+ @image[i, j] = [r, g, b, a]
233
+ end
234
+ end
235
+ end
236
+
237
+ it "mode should be RGBA" do expect(@image.mode).to eq(Rixmap::RGBA) end
238
+ it "is not indexed" do expect(@image).not_to be_indexed end
239
+ it "is not grayscale" do expect(@image).not_to be_grayscale end
240
+ it "is rgb" do expect(@image).to be_rgb end
241
+ it "width should be 256" do expect(@image.width).to eq(256) end
242
+ it "height should be 256" do expect(@image.height).to eq(256) end
243
+ it "palette should be nil" do expect(@image.palette).to be_nil end
244
+ it "size should be [256, 256]" do expect(@image.size).to eq([256, 256]) end
245
+ it "dim should be [256, 256, 4]" do expect(@image.dim).to eq([256, 256, 4]) end
246
+ it "all pixels are [r, g, b, a] array" do
247
+ 256.times do |i|
248
+ 256.times do |j|
249
+ r = (255 - (Math.sqrt((255 - i) ** 2 + j ** 2)))
250
+ g = (255 - (Math.sqrt((255 - i) ** 2 + (255 - j) ** 2)))
251
+ b = (255 - (Math.sqrt(i ** 2 + j ** 2)))
252
+ a = (255 - (Math.sqrt(i ** 2 + (255 - j) ** 2)))
253
+
254
+ # 比較用に丸め処理
255
+ dr = if r < 0
256
+ 0
257
+ elsif r > 255
258
+ 255
259
+ else
260
+ r.to_i
261
+ end
262
+ dg = if g < 0
263
+ 0
264
+ elsif g > 255
265
+ 255
266
+ else
267
+ g.to_i
268
+ end
269
+ db = if b < 0
270
+ 0
271
+ elsif b > 255
272
+ 255
273
+ else
274
+ b.to_i
275
+ end
276
+ da = if a < 0
277
+ 0
278
+ elsif a > 255
279
+ 255
280
+ else
281
+ a.to_i
282
+ end
283
+
284
+ expect(@image[i, j]).to eq([dr, dg, db, da])
285
+ end
286
+ end
287
+ end
288
+ it "number of scanlines are same as height" do expect(@image.lines.size).to eq(@image.height) end
289
+ it "scanline type is Rixmap::Image::Line" do expect(@image.lines[0]).to be_kind_of(Rixmap::Image::Line) end
290
+ it "pixel type is Array" do expect(@image.pixels[0][0]).to be_kind_of(Array) end
291
+ end
292
+ end
293
+
294
+
295
+ #==============================================================================#
296
+ # $Id: image_spec.rb,v 36d5b09e38de 2014/03/30 11:13:09 chikuchikugonzalez $
297
+ # vim: set sts=2 ts=2 sw=2 expandtab:
data/spec/mode_spec.rb ADDED
@@ -0,0 +1,71 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Rixmap::Mode Spec
4
+ require_relative '../lib/rixmap'
5
+
6
+ describe Rixmap::Mode do
7
+ context "INDEXED" do
8
+ before do
9
+ @mode = Rixmap::INDEXED
10
+ end
11
+
12
+ it "depth should be 8" do expect(@mode.depth).to eq(8) end
13
+ it "is indexed type" do expect(@mode.indexed?).to be_true end
14
+ it "is not grayscale type" do expect(@mode.grayscale?).to be_false end
15
+ it "is not rgb type" do expect(@mode.rgb?).to be_false end
16
+ it "has no alpha channel" do expect(@mode.has_alpha?).to be_false end
17
+ end
18
+
19
+ context "GRAYSCALE" do
20
+ before do
21
+ @mode = Rixmap::GRAYSCALE
22
+ end
23
+
24
+ it "depth should be 8" do expect(@mode.depth).to eq(8) end
25
+ it "is not indexed type" do expect(@mode.indexed?).to be_false end
26
+ it "is grayscale type" do expect(@mode.grayscale?).to be_true end
27
+ it "is not rgb type" do expect(@mode.rgb?).to be_false end
28
+ it "has no alpha channel" do expect(@mode.has_alpha?).to be_false end
29
+ end
30
+
31
+ context "GRAYALPHA" do
32
+ before do
33
+ @mode = Rixmap::GRAYALPHA
34
+ end
35
+
36
+ it "depth should be 16" do expect(@mode.depth).to eq(16) end
37
+ it "is not indexed type" do expect(@mode.indexed?).to be_false end
38
+ it "is grayscale type" do expect(@mode.grayscale?).to be_true end
39
+ it "is not rgb type" do expect(@mode.rgb?).to be_false end
40
+ it "has alpha channel" do expect(@mode.has_alpha?).to be_true end
41
+ end
42
+
43
+ context "RGB" do
44
+ before do
45
+ @mode = Rixmap::RGB
46
+ end
47
+
48
+ it "depth should be 24" do expect(@mode.depth).to eq(24) end
49
+ it "is not indexed type" do expect(@mode.indexed?).to be_false end
50
+ it "is not grayscale type" do expect(@mode.grayscale?).to be_false end
51
+ it "is rgb type" do expect(@mode.rgb?).to be_true end
52
+ it "has no alpha channel" do expect(@mode.has_alpha?).to be_false end
53
+ end
54
+
55
+ context "RGBA" do
56
+ before do
57
+ @mode = Rixmap::RGBA
58
+ end
59
+
60
+ it "depth should be 32" do expect(@mode.depth).to eq(32) end
61
+ it "is not indexed type" do expect(@mode.indexed?).to be_false end
62
+ it "is not grayscale type" do expect(@mode.grayscale?).to be_false end
63
+ it "is rgb type" do expect(@mode.rgb?).to be_true end
64
+ it "has alpha channel" do expect(@mode.has_alpha?).to be_true end
65
+ end
66
+ end
67
+
68
+
69
+ #==============================================================================#
70
+ # $Id: mode_spec.rb,v 36d5b09e38de 2014/03/30 11:13:09 chikuchikugonzalez $
71
+ # vim: set sts=2 ts=2 sw=2 expandtab:
@@ -0,0 +1,77 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Rixmap::Palette Spec
4
+ require_relative '../lib/rixmap'
5
+
6
+ describe Rixmap::Palette do
7
+ context "Empty Palette" do
8
+ it "should be raise ArgumentError" do
9
+ expect { Rixmap::Palette.new(0) }.to raise_error(ArgumentError)
10
+ end
11
+ end
12
+
13
+ context "Default Palette" do
14
+ before do
15
+ @palette = Rixmap::Palette.new
16
+ end
17
+
18
+ it "size should be 256" do expect(@palette.size).to eq(256) end
19
+ it "colors are all black" do expect(@palette.to_a).to eq([Rixmap::Color.new(0, 0, 0, 255)] * 256) end
20
+ end
21
+
22
+ context "VGA Palette" do
23
+ before do
24
+ @palette = Rixmap::Palette::VGA.new
25
+ @colors = [
26
+ Rixmap::Color.new(255, 255, 255), # White
27
+ Rixmap::Color.new(192, 192, 192), # Silver
28
+ Rixmap::Color.new(128, 128, 192), # Gray
29
+ Rixmap::Color.new( 0, 0, 0), # Black
30
+ Rixmap::Color.new(255, 0, 0), # Red
31
+ Rixmap::Color.new(128, 0, 0), # Maroon
32
+ Rixmap::Color.new(255, 255, 0), # Yellow
33
+ Rixmap::Color.new(128, 128, 0), # Olive
34
+ Rixmap::Color.new( 0, 255, 0), # Green
35
+ Rixmap::Color.new( 0, 128, 0), # Lime
36
+ Rixmap::Color.new( 0, 255, 255), # Aqua
37
+ Rixmap::Color.new( 0, 128, 128), # Teal
38
+ Rixmap::Color.new( 0, 0, 255), # Blue
39
+ Rixmap::Color.new( 0, 0, 128), # Navy
40
+ Rixmap::Color.new(255, 0, 255), # Fucshi
41
+ Rixmap::Color.new(128, 0, 128) # Purple
42
+ ]
43
+ end
44
+
45
+ it "size should be 16" do expect(@palette.size).to eq(16) end
46
+ it "has fixed colors" do
47
+ @colors.each_with_index do |c, i|
48
+ expect(@palette[i]).to eq(c)
49
+ end
50
+ end
51
+ end
52
+
53
+ context "WebSafe Palette" do
54
+ before do
55
+ @palette = Rixmap::Palette::WebSafe.new
56
+ @levels = [0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF]
57
+ end
58
+
59
+ it "size should be 216" do expect(@palette.size).to eq(216) end
60
+ it "has fixed colors" do
61
+ 6.times do |ri|
62
+ 6.times do |gi|
63
+ 6.times do |bi|
64
+ n = ri * 36 + gi * 6 + bi
65
+ c = Rixmap::Color.new(@levels[ri], @levels[gi], @levels[bi])
66
+ expect(@palette[n]).to eq(c)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+
75
+ #==============================================================================#
76
+ # $Id: palette_spec.rb,v 36d5b09e38de 2014/03/30 11:13:09 chikuchikugonzalez $
77
+ # vim: set sts=2 ts=2 sw=2 expandtab:
@@ -0,0 +1,38 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2013 TANAKA Kenichi <chikuchikugonzalez@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+
24
+ 以下に定める条件に従い、本ソフトウェアおよび関連文書のファイル(以下「ソフト
25
+ ウェア」)の複製を取得するすべての人に対し、ソフトウェアを無制限に扱うことを無
26
+ 償で許可します。これには、ソフトウェアの複製を使用、複写、変更、結合、掲載、頒
27
+ 布、サブライセンス、および/または販売する権利、およびソフトウェアを提供する相
28
+ 手に同じことを許可する権利も無制限に含まれます。
29
+
30
+ 上記の著作権表示および本許諾表示を、ソフトウェアのすべての複製または重要な部分
31
+ に記載するものとします。
32
+
33
+ ソフトウェアは「現状のまま」で、明示であるか暗黙であるかを問わず、何らの保証も
34
+ なく提供されます。ここでいう保証とは、商品性、特定の目的への適合性、および権利
35
+ 非侵害についての保証も含みますが、それに限定されるものではありません。 作者ま
36
+ たは著作権者は、契約行為、不法行為、またはそれ以外であろうと、ソフトウェアに起
37
+ 因または関連し、あるいはソフトウェアの使用またはその他の扱いによって生じる一切
38
+ の請求、損害、その他の義務について何らの責任も負わないものとします。
@@ -0,0 +1,23 @@
1
+ # Chollas - Code Snipets Repository -
2
+ コード断片リポジトリです.
3
+ 「あ、これ使えそう」って思ったり、使いまわしたくなったコードを分割して管理します.
4
+
5
+ ## 必要要件
6
+ - C++11に対応したコンパイラ
7
+ - gcc 4.7 とか
8
+ - Visual Studio 2013 とか
9
+
10
+ ## ライセンスについて
11
+ `MIT`ライセンスを適用します.
12
+
13
+ ## 作者
14
+ _タナカ ケンイチ (TANAKA Kenichi) aka チクチーク・ゴンザレス (chikuchikugonzalez)_
15
+
16
+ ### 連絡先
17
+ #### E-mail
18
+ * mailto:chikuchikugonzalez@gmail.com
19
+ * mailto:chiku2gonzalez@live.jp
20
+
21
+ ## 名前の元
22
+ ウチワサボテンの英語名です.
23
+ `Opuntia`とも言う、っていうかそっちが正式っぽいですが`cholla`のほうがいいかなって.
@@ -0,0 +1,62 @@
1
+ // -*- coding: utf-8 -*-
2
+ /**
3
+ * 配置newを使用したメモリ確保実装.
4
+ */
5
+ #pragma once
6
+ #ifndef CHOLLAS_XNEW_HXX
7
+ #define CHOLLAS_XNEW_HXX
8
+ #include <stdexcept> // C++標準例外
9
+ #include <new> // 配置new
10
+ #include <cstdlib>
11
+
12
+ /*
13
+ * 名前空間作る
14
+ */
15
+ namespace chollas {
16
+ /**
17
+ * `std::free`でメモリブロックを解放するインスタンス破棄関数実装です.
18
+ * デストラクタを呼び出した上で、`std::free`を用いてメモリブロックを解放します.
19
+ *
20
+ * @param [class] CLASS メモリブロックにあるオブジェクトの型. デストラクタを呼び出すために使用します.
21
+ * @param [CLASS*] ptr 解放対象のオブジェクトポインタ
22
+ * @return [void]
23
+ */
24
+ template<class CLASS> static inline void xdelete(CLASS* ptr) {
25
+ if (ptr != NULL) {
26
+ try {
27
+ ptr->~CLASS();
28
+ std::free(ptr);
29
+ } catch (...) {
30
+ std::free(ptr);
31
+ throw; // 例外を再送出
32
+ }
33
+ }
34
+ }
35
+
36
+ /**
37
+ * `std::malloc`でメモリブロックを確保するインスタンス生成関数実装です.
38
+ * 確保されたメモリブロック上に配置newによってオブジェクトを構築します.
39
+ *
40
+ * @param [class] CLASS 対象クラスの型. コンストラクタを呼び出すのに使用します.
41
+ * @param [typename...] PARAMS コンストラクタ引数の型.
42
+ * @param [PARAMS] params コンストラクタに渡す引数リスト.
43
+ * @return [CLASS*] 確保されたインスタンスのポインタ. 確保できなかった場合は`NULL`になることもあります.
44
+ */
45
+ template<class CLASS, typename... PARAMS> static inline CLASS* xnew(PARAMS... params) {
46
+ CLASS* ptr = reinterpret_cast<CLASS*>(std::malloc(sizeof(CLASS)));
47
+ if (ptr != NULL) {
48
+ try {
49
+ return new(ptr) CLASS(params...);
50
+ } catch (...) {
51
+ std::free(ptr);
52
+ throw; // 例外を再throw
53
+ }
54
+ }
55
+ return ptr;
56
+ }
57
+ }
58
+
59
+ #endif // CHOLLAS_XNEW_HXX
60
+ //============================================================================//
61
+ // $Id$
62
+ // vim: set sts=4 ts=4 sw=4 expandtab filetype=cpp:
@@ -0,0 +1,42 @@
1
+ // -*- coding: utf-8 -*-
2
+ /**
3
+ * エンディアン関係のユーティリティ関数
4
+ */
5
+ #pragma once
6
+ #ifndef CHOLLAS_ENDIAN_HXX
7
+ #define CHOLLAS_ENDIAN_HXX
8
+
9
+ namespace chollas {
10
+
11
+ // TODO システムバイトオーダーチェックの実装
12
+
13
+ /**
14
+ * なんとなく名前空間を分離
15
+ */
16
+ namespace endian {
17
+ /**
18
+ * 参考: http://cppdiary.blog76.fc2.com/blog-entry-8.html
19
+ */
20
+ template<typename T> static inline T reverse(T value) {
21
+ size_t typeSize = sizeof(T);
22
+ if (typeSize == 1) {
23
+ // 1バイトは反転できない
24
+ return value;
25
+ }
26
+ T result = 0;
27
+ char* rp = reinterpret_cast<char*>(&result);
28
+ char* vp = reinterpret_cast<char*>(&value);
29
+ for (size_t i = 0; i < typeSize; i++) {
30
+ rp[i] = vp[(typeSize - 1) - i];
31
+ }
32
+ return result;
33
+ }
34
+
35
+
36
+ }
37
+ }
38
+
39
+ #endif // CHOLLAS_ENDIAN_HXX
40
+ //============================================================================//
41
+ // $Id$
42
+ // vim: set sts=4 ts=4 sw=4 expandtab filetype=cpp: