1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 module capnproto.MessageBuilder;
23 
24 import java.nio.ByteBuffer;
25 
26 import capnproto.AnyPointer;
27 import capnproto.BuilderArena;
28 import capnproto.ReaderArena;
29 import capnproto.ReaderOptions;
30 import capnproto.SegmentBuilder;
31 
32 final class MessageBuilder
33 {
34 public: //Methods.
35 	this()
36 	{
37 		this.arena = new BuilderArena(BuilderArena.SUGGESTED_FIRST_SEGMENT_WORDS, BuilderArena.SUGGESTED_ALLOCATION_STRATEGY);
38 	}
39 	
40 	this(int firstSegmentWords)
41 	{
42 		this.arena = new BuilderArena(firstSegmentWords, BuilderArena.SUGGESTED_ALLOCATION_STRATEGY);
43 	}
44 	
45 	this(int firstSegmentWords, BuilderArena.AllocationStrategy allocationStrategy)
46 	{
47 		this.arena = new BuilderArena(firstSegmentWords, allocationStrategy);
48 	}
49 	
50 	T.Builder getRoot(T)()
51 	{
52 		return this.getRootInternal().getAs!T();
53 	}
54 	
55 	void setRoot(T, U)(U reader)
56 	{
57 		this.getRootInternal().setAs!T(reader);
58 	}
59 	
60 	T.Builder initRoot(T)()
61 	{
62 		return this.getRootInternal().initAs!T();
63 	}
64 	
65 	ByteBuffer[] getSegmentsForOutput()
66 	{
67 		return this.arena.getSegmentsForOutput();
68 	}
69 
70 private: //Methods.
71 	AnyPointer.Builder getRootInternal()
72 	{
73 		auto rootSegment = &this.arena.segments.data[0];
74 		if(rootSegment.currentSize() == 0)
75 		{
76 			auto location = rootSegment.allocate(1);
77 			if(location == SegmentBuilder.FAILED_ALLOCATION)
78 				throw new Error("Could not allocate root pointer.");
79 			if(location != 0)
80 				throw new Error("First allocated word of new segment was not at offset 0.");
81 			return AnyPointer.Builder(rootSegment, cast(int)location);
82 		}
83 		return AnyPointer.Builder(rootSegment, 0);
84 	}
85 
86 private: //Variables.
87 	BuilderArena arena;
88 }