Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Blob.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Runtime.InteropServices;
5 using SiliconStudio.Core;
6 using SiliconStudio.Core.IO;
7 
8 namespace SiliconStudio.Core.Storage
9 {
10  /// <summary>
11  /// Stores immutable binary content.
12  /// </summary>
13  public class Blob : ReferenceBase
14  {
15  private readonly ObjectDatabase objectDatabase;
16  private readonly IntPtr content;
17  private readonly int size;
18  private readonly ObjectId objectId;
19 
20  protected Blob(ObjectDatabase objectDatabase, ObjectId objectId)
21  {
22  this.objectDatabase = objectDatabase;
23  this.objectId = objectId;
24  }
25 
26  internal Blob(ObjectDatabase objectDatabase, ObjectId objectId, IntPtr content, int size)
27  : this(objectDatabase, objectId)
28  {
29  this.size = size;
30  this.content = Marshal.AllocHGlobal(size);
31  Utilities.CopyMemory(this.content, content, size);
32  }
33 
34  internal Blob(ObjectDatabase objectDatabase, ObjectId objectId, NativeStream stream)
35  : this(objectDatabase, objectId)
36  {
37  this.size = (int)stream.Length;
38  this.content = Marshal.AllocHGlobal(this.size);
39  stream.Read(this.content, this.size);
40  }
41 
42  /// <summary>
43  /// Gets the size.
44  /// </summary>
45  /// <value>
46  /// The size.
47  /// </value>
48  public int Size
49  {
50  get { return size; }
51  }
52 
53  /// <summary>
54  /// Gets the content.
55  /// </summary>
56  /// <value>
57  /// The content.
58  /// </value>
59  public IntPtr Content
60  {
61  get { return content; }
62  }
63 
64  /// <summary>
65  /// Gets the <see cref="ObjectId"/>.
66  /// </summary>
67  /// <value>
68  /// The <see cref="ObjectId"/>.
69  /// </value>
70  public ObjectId ObjectId
71  {
72  get { return objectId; }
73  }
74 
76  {
77  get { return objectDatabase; }
78  }
79 
80  /// <summary>
81  /// Gets a <see cref="NativeStream"/> over the <see cref="Content"/>.
82  /// </summary>
83  /// It will keeps a reference to the <see cref="Blob"/> until disposed.
84  /// <returns>A <see cref="NativeStream"/> over the <see cref="Content"/>.</returns>
86  {
87  return new BlobStream(this);
88  }
89 
90  /// <inheritdoc/>
91  protected override void Destroy()
92  {
93  objectDatabase.DestroyBlob(this);
94  Marshal.FreeHGlobal(this.content);
95  }
96  }
97 }
Stores immutable binary content.
Definition: Blob.cs:13
Blob(ObjectDatabase objectDatabase, ObjectId objectId)
Definition: Blob.cs:20
A read-only NativeMemoryStream that will properly keep references on its underlying Blob...
Definition: BlobStream.cs:11
override void Destroy()
Releases unmanaged and - optionally - managed resources
Definition: Blob.cs:91
Gives access to the object database.
Base class for a IReferencable class.
NativeStream GetContentStream()
Gets a NativeStream over the Content.
Definition: Blob.cs:85
A hash to uniquely identify data.
Definition: ObjectId.cs:13
A Stream with additional methods for native read and write operations using IntPtr.
Definition: NativeStream.cs:11
Only valid for a property / field that has a class or struct type. When restored, instead of recreati...
_In_ size_t _In_ size_t size
Definition: DirectXTexP.h:175