ãã¤ã³ãæ¹ä¿®ä¸
çµæ§ä½åº¦ãæ¸ãç´ãã¦ãã¾ãé¢æ°ã®ãã¤ã³ãå¨ããªãã§ãããã¾ãæ¸ãç´ãã¦ã¾ãã
ããã¾ã§ã®è¨è¿°æ¹æ³ã§ãæ¸ããã®ã§ããã次ã®ãããªæ°è¨æ³ã追å ãããã¨ã«ãªãã¾ããã
XTAL_BIND(Vector){
Xdef_method(normalize); // ãã®ãããªããã£ã¨ç°¡åãªè¨è¿°æ¹æ³ã®è¿½å
Xdef_method(length);
}
ã¾ããå¼æ°ã®æå¾ãArguments*ãã¾ãã¯ãconst ArgumentsPtr&ãã¨ãããã¨ã§ãç°¡åã«å¯å¤å¼æ°ãåãåããããã«ãªãã¾ããã
æ¨æºã©ã¤ãã©ãªã®ãã¤ã³ã
ãã¨ãã°luaã§ã¯ãæ°å¦ç³»ã®é¢æ°ãluaãã使ãããã¨ããå ´åã
lua_State *L = lua_open(); luaopen_math(L);
ã¨ããããã«ãluaopen_math()ãå¼ã¶å¿
è¦ãããã¾ããããã¯squirrelã§ãããã ã£ãã¯ãã§ãã
使ããªãã©ã¤ãã©ãªã§ã¡ã¢ãªãå§è¿«ããªãããã«ã§ããããã§ãããã¡ãã£ã¨é¢åã«ãæãã¾ãã
Xtalã§ã¯å¿ è¦ãªã¨ãã«èªåçã«ãã¤ã³ããããæ©æ§ãå ¥ãã¾ããã
f: math::sin(1); // ããã§mathã®ã¡ã³ãããã¤ã³ãããã
Xtalã§ã¯ã使ããªãã¨ãã¯ãã¤ã³ããããªããã使ãéã¯ä½ãC++å´ãããããã¨ãªã使ããã¨ãã§ããããã§ãã
ããã¯ãã¦ã¼ã¶ã¼ãä½ã£ãã©ã¤ãã©ãªãåæ§ã§ãã
XTAL_PREBIND(UserClass){ // PREBINDã§ã¯ãã³ã³ã¹ãã©ã¯ã¿ãç¶æ¿é¢ä¿ãè¨è¿° it->inherit(cpp_class<UserBaseClass>()); // UserBaseClassãç¶æ¿ããã it->def_ctor(ctor<UserClass>()); // ã³ã³ã¹ãã©ã¯ã¿ãç»é² } XTAL_BIND(UserClass){ // ã¡ã³ããç»é² it->def_method("aaa", &UserClass::aaa); it->def_method("bbb", &UserClass::bbb) it->def_method("ccc", &UserClass::ccc) it->def_method("ddd", &UserClass::ddd) it->def_method("eee", &UserClass::eee) it->def_method("fff", &UserClass::fff) }
ã¨æ¸ãã¦ããã°ãã¯ã©ã¹ã®ã¡ã½ãããå¼ã³åºãã¨ããªã©ã«èªåçã«XTAL_BINDãå®è¡ãããã¡ã³ããç»é²ããã¾ãã
XTAL_PREBINDã¯ç¶æ¿é¢ä¿ã®ãã§ãã¯ããªãããéãã¾ãã¯ã¤ã³ã¹ã¿ã³ã¹ã®çæããããéã«å¼ã³åºããã¾ãã
evalé¢æ°è¿½å
ãããã¬ã®å®è£ ã®ããã«ããã¬ã¼ã¯ãã¤ã³ãã«ã¦åæ¢ä¸ã«å¼ãè©ä¾¡ããæ©è½ããå¿ è¦ã ã£ãã®ã§å®è£ ãã¦ããã®ã§ãããã¤ãã§ã«builtin::evalé¢æ°ã¨ãã¦è¿½å ãããã¨ã«ãã¾ããã
foo: 100; eval("foo + 10").p; //=> 110;
ã¾ããæ°¸ãã使ç¨ä¸å¯è½ã¨ãªã£ã¦ããixããevalã使ããã¨ã§ã·ã³ãã«ã«å®è£ ããªãããã¨ãã§ããã®ã§ã復活ãããã¨ã«ãªãã¾ããã
16byteã¢ã©ã¤ã¡ã³ããæã¤ã¡ã³ããæã¤ãªãã¸ã§ã¯ãã«å¯¾å¿
ããã¾ã§xnew
æ®éã®newãåæ§ã«ãé¢ä¿ãªãé
ç½®ãã¾ãã
ãªã®ã§ã¢ã©ã¤ã¡ã³ãã®åé¡ã¯ã©ããããããªãã ãããã¨æã£ã¦ããã®ã§ããã
ã²ã¼ã ã«ããã¦ã¯ã16byteã¢ã©ã¤ã¡ã³ããæã¤ãã¨ã¯ãããããããã¨ãã£ããã¨ãããã対å¿ãããã¨ã«ãã¾ããã
ã©ããã£ã¦å¯¾å¿ãããããã®ä¸é±éæ©ãã§ãã£ã¨å®è£ ã§ãã¾ããã
xnewã§çæããããªãã¸ã§ã¯ãã¯ã16byteã¢ã©ã¤ã¡ã³ããæã¤ã¡ã³ããããå ´å16byteã¢ã©ã¤ã¡ã³ããä¿éãã¾ãã
æããªããªãã¸ã§ã¯ãã«é¢ãã¦ã¯ããã¾ã§ã©ããã§ãã
#include <xmmintrin.h> struct Vec128{ __m128 a; }; struct Spr{ Vec128 v; }; XTAL_BIND(Spr){ it->def_var("v", &Spr::v); } void test(){ Spr* p = new Spr(); _mm_add_ps(p->v.a, p->v.a); // ããã¯å±éº SmartPtr<Spr> s = xnew<Spr>(); _mm_add_ps(s->v.a, s->v.a); // xnewã§ç¢ºä¿ããå ´åã¯å¤§ä¸å¤« }
-
-
- -
-
ä»åã®å®è£ ã®ä½æ³¢
AllocatorLibã¯æ¬¡ã®ãããªã¤ã³ã¿ã¼ãã§ã¤ã¹ã¨å¤æ´ã¨ãªãã¾ããã
class AllocatorLib{ public: virtual ~AllocatorLib(){} virtual void* malloc(std::size_t size); virtual void free(void* p, std::size_t size); virtual void* malloc_align(std::size_t size, std::size_t alignment); virtual void free_align(void* p, std::size_t size, std::size_t alignment); virtual void out_of_memory(){} };
malloc_align, free_alignãå¢ãã¦ãã¾ãã
ãã ãããããã¯å®è£
ããªãã¦ããã¾ãã¾ãããããã©ã«ãã®å®è£
ã§mallocã使ã£ã¦æ´åããã¡ã¢ãªãè¿ãããã«ãªã£ã¦ãã¾ãã
memalignãªã©ããã£ã¨å¹çã®ããé¢æ°ãããå ´åã«ãªã¼ãã¼ã©ã¤ããã¦ãã ããã
- -
ã¾ããRefCountingBase, Baseã¨ããåºåºã¯ã©ã¹ãä¿®æ£ãã¾ããã
ä»®æ³é¢æ°ã¯ãã¹ã¦on_ã¨ããæ¥é ãã¤ããååã«ãªãã¼ã ããéä»®æ³é¢æ°ã¨ãã¾ããã
ãã¨ãã°virtual void rawmember();ã¯void on_rawmember();ã¨ãªãã¾ãã
ãã¹ãã©ã¯ã¿ãéä»®æ³ã¨ããä»®æ³é¢æ°ãæé¤ãã¾ããã
ãã ããç¹å¥ãªä»çµã¿ãå°å
¥ããããããããã¯ä»®æ³é¢æ°ã¨åæ§ã«ãªã¼ãã¼ã©ã¤ãã§ãã¾ãã
ã¡ã¢ãªã¢ãã±ã¼ã¿æ¸ãã
ãããè»è¼ªã®åçºæã ã¨ãããã¨ã¯æ¿ç¥ãã¦ãã!
Xtalå ¨ä½ãã¾ãåçºæã¨ãããããã§ããã©ã
Xtalã¯ã¦ã¼ã¶ã¼ãèªç±ã«malloc, freeé¢æ°ãç»é²ã§ããããã«ãªã£ã¦ããã§ããã
ãã¡ã¢ãªç¢ºä¿é¢æ°ãç¨æãã¦ç»é²ããã®ããã©ããããåç´ã«ã¡ã¢ãªã®ããããããã¾ã§ä½¿ã£ã¦ã»ãããããã¨ãããã¨ãããã¨æããã§ãã
ãã®ããã«ã
ã»ã¯ã©ã¹ä¸ã¤ã§å®çµãã¦ãã
ã»ã¡ã¢ãªé åãæå®ããã ã
ã»ã°ãã¼ãã«å¤æ°ãªã©ã使ç¨ããªã
ã»ã¾ãããããã¨ã¨ã«ããã·ã³ãã«
ãããªã©ã¤ãã©ãªãä½ããã¨ãããã¨ã§ä½ãã¾ããã
ãã£ã³ã¯ã®ç®¡çã¯èµ¤é»æ¨ã§è¡ã£ã¦ãã¾ãã
è¦æ±ããããµã¤ãºã«ãã£ã¨ãè¿ããã¾ãã¢ãã¬ã¹å¤ãå°ãããã£ã³ã¯ãæ¤ç´¢ãã¾ãã
管çé åã®ãµã¤ãºã¯ãããªã¼ãã£ã³ã¯ã®ã¨ãã¯16byte(ã¨ããããã¤ã³ã¿4ã¤å)ã使ç¨ãã£ã³ã¯æã¯8byte(ã¨ããããã¤ã³ã¿äºã¤å)ã§ãã
赤é»æ¨ã®ãã¼ãã¨ãã¦ã®è²ã¨ã使ç¨ä¸ãã©ããã®ãã©ã°ã¯ãåãã£ã³ã¯ãæããã¤ã³ã¿ã«åãè¾¼ãã§ã¾ãã
æ§è½è©ä¾¡ãã¦ãã¾ãã§ãã¡ãªãç´ ç´ã«dlmallocçµã¿è¾¼ããâ¦ã
ããããã¡ã¢ãªã¢ãã±ã¼ã¿ã®æ§è½è©ä¾¡ã£ã¦ã©ãè¨ã£ããå¹æçãªãã ããï¼
以ä¸ã½ã¼ã¹ã ç¶ããèªã
Rubyã§ãã¤ããªãã¼ã¿ãèªã¿æ¸ããã
Rubyã§ãã¤ããªãã¼ã¿ãèªã¿æ¸ãããã®ã«ã¯ãpack, unpack使ããããªãã§ããã
åããããRuby使ãå§ãã¦ããä½å¹´ãçµã¤ãã§ããã©ãpack, unpackã£ã¦ãã¤ã¾ã§ãã£ã¦ãè¦ããããªããã
使ãã¨ãªããæ±ãç ©éãªã³ã¼ãã«ãªã£ã¦ãããã§ããããããªãã¨ã£ã¦ãªãã§ããï¼
ãªããã©ã¤ãã©ãªãªãããªã¼ã¨ã°ã°ãã¾ãã¦ããã¡ãã§ç´¹ä»ããã¦ããã©ã¤ãã©ãªãä¸éãè¦ã¦ã¿ã¾ããã
http://d.hatena.ne.jp/kenhys/20070522/1179848262
ã»binaryparser
ã»calibre-binaryreader
ã»bindata
ã»BitStructEx
ãããããªããã©ããè¤éã¨ãããããã¾ãã¡ç´æçãããªãã¨ããæ°ããã¾ãã
èªåã®çæ³ã®ä½¿ãæ¹ã®å½¢ã¯æ¬¡ã®ãããªæãã§ãã
include(BinaryData) # float3è¦ç´ ã®ãã¯ãã«æ§é ä½å®ç¾© Vector = struct{ # Vectorã®æåã®è¦ç´ ã¯xã§ãfloat 32 bitã®åã¨ããæå³ var x: F32 # Vectorã®äºã¤ç®ã®è¦ç´ ã¯yã§ãfloat 32 bitã®åã¨ããæå³ var y: F32 # ã¾ãï¼ã¿ã¦ï¼ãã®zè¦ç´ ã®å®ç¾©ãï¼ã¾ãã§JavaScriptã®å®ç¾©ã¿ããã ãï¼ var z: F32 } # 3x3ã®ãããªãã¯ã¹æ§é ä½å®ç¾© Matrix = struct{ # Matrixã®æåã®è¦ç´ ã¯rowsã§ãã¦ã¼ã¶ã¼ãå®ç¾©ããVectoræ§é ä½ã®ä¸ã¤ã®è¦ç´ ãæã¤é åã¨ããæå³ var rows: Vector[3] # ã¦ã¼ã¶ã¼ãå®ç¾©ããæ§é ä½ããã¹ããã¦ããããé åã¨ãã¦ä½¿ããã¨ãããã¨ãï¼ } # Model = struct{ # é ç¹ã®æ°ãinteger 32 bitåã¨ãã¦æ㤠var vertex_count: I32 # ä¸ã®vertex_countãé·ãã¨ãã¦ä½¿ããã®ãï¼ var vertices: Vector[:vertex_count] var matrix: Matrix } # ã¢ãã«æ§é ä½ã®ã¤ã³ã¹ã¿ã³ã¹çæ model = Model.new # å¤ãæ ¼ç´ããã®ããç´æçãªè¨æ³ãªã®ãï¼ model.vertex_count = 2; model.vertices[0].x = 1 model.vertices[0].y = 2 model.vertices[0].z = 3 model.vertices[1].x = 4 model.vertices[1].y = 5 model.vertices[1].z = 6 model.matrix.rows[0].x = 10 model.matrix.rows[1].y = 20 model.matrix.rows[2].z = 30 sio = StringIO.new # ãã¤ããªã¨ãã¦æ¸ãè¾¼ã Model.write_to_stream(sio, model) #ããã§sioã«ã¯Cè¨èªã§æ¬¡ã®ãããªæãã§æ¸ãã¦åºåãããã¤ããªã¨åããã©ã¼ãããã§å ¥ã£ã¦ã # struct Vector{ float x, y, z; }; # struct Matrix{ Vector rows[3]; }; # struct Model{ int vertex_count; Vector vertices[vertex_count]; Matrix matrix; }; # Model model = {...}; # fwrite(fp, &model, 1, sizeof(model)); # ã¹ããªã¼ã ãä¸çªæåã«æ»ã sio.pos = 0 # ãã¤ããªããèªã¿è¾¼ãã§å¾©å ãã model_deserialized = Model.read_from_stream(sio) # æåååãã¦åºå print model_deserializedæåååãããããªã³ãçµæ
{:vertex_count=>2, :vertices=>[{:x=>1.0, :y=>2.0, :z=>3.0}, {:x=>4.0, :y=>5.0, :z=>6.0}], :matrix=> {:rows=> [{:x=>10.0, :y=>0.0, :z=>0.0}, {:x=>0.0, :y=>20.0, :z=>0.0}, {:x=>0.0, :y=>0.0, :z=>30.0}]}}ã§ããããªçæ³ãªã©ã¤ãã©ãªã®ä¸å°ãä½ã£ã¦ã¿ã¾ããã
ç¶ããèªã
æååãªãã©ã«ã渡ããã¦çæãããStringãªãã¸ã§ã¯ãã¯ãã¡ã¢ãªç¢ºä¿ãèµ·ãããªãããã«ãã
ãã¨ãã¨æååã®é·ããsizeof(int)-1ã®ãã®ã¯ãå³å¤ã«ãªãããã«ãã¦ã¡ã¢ãªç¢ºä¿ãèµ·ããªããã工夫ãã¦ãã¾ãããã
ä»åãæååãªãã©ã«ã渡ããã¦çæãããStringãªãã¸ã§ã¯ããå³å¤ã«ãªãããã«ãã¾ããã
void foo(){ StringPtr str = xnew<String>(XTAL_STRING("This is a pen.")); // å¤æ°strã«ç´æ¥æååãªãã©ã«ã¸ã®ãã¤ã³ã¿ãåãè¾¼ã¾ãã¦ãã AnyPtr a = XTAL_STRING("Hello, World"); // å¤æ°aã«ç´æ¥æååãªãã©ã«ã¸ã®ãã¤ã³ã¿ãåãè¾¼ã¾ãã¦ãã }