On Jul 23, 2010, at 9:24 38AM, Stéphane Ducasse wrote:
>> I really like Assets. The whole framework is small, flexible, useful and
>> easy to understand.
> what is assets?
>
> Stef
A really small and neat external resource-management framework in VisualWorks.
Its relation to once is being the original use case.
I've attached the class comment from VW, which explains how it works pretty
well :)
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf320
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural
\f0\fs24 \cf0 I (and subclasses) am a library of of assets (images, icons, strings, etc), to be used by an application. My methods which return Smallalk objects, can be tagged to specify a file "links" external files in as Smalltalk objects during development time. The method used to turn the file contents into a Smalltalk object is a negotiation between the file's type (extension) and methods provided by the receiver.\
\
\
Implementation Details:\
Asset classes are singletons. There is never a need to create instances of them. Assets are retrieved by simply sending the name of the asset to the them.\
\
A preliminary asset method might look like this:\
\
myIcon\
<file: 'somePath.png'>\
\
After it has been "integrated, it would look something like:\
\
myIcon\
<file: 'somePath.png'>\
<md5sum: #[1 2 3 4 5 6 7 8]>\
\
^[CachedImage on: (Image extent: 2...@2 depth: 1 bitsPerPixel: 1 palette: (Graphics.MappedPalette withColors: (#(#(#\{Graphics.ColorValue\} #scaledRed:scaledGreen:scaledBlue: #(0 0 0)) ) collect: [:each | (each at: 1) value perform: (each at: 2) withArguments: (each at: 3)])) usingBits: #[0 0 0 0 0 0 0 0])] once\
\
The following is an explanation of the various parts:\
\
- File Specifier: (<file: 'somePath.png'>)\
The first tag is used to link the method with an external resource. The external resource is specified by the argument which is path to a file. The method that is used to "integrate" the external source file into a reproducible Smalltak object, is determined by the file's extension type. In this case, 'png'. Each import type needs to have a method called import_<extension>: if it is to integrate that file type. For example, the one used in this case is import_png: By default, there is support for gif, jpeg, jpg, bmp, and png. You can add to this set or override them, in either your own subclass or by extending the base Assets class.\
\
- Remembered Hash: (<md5sum: #[1 2 3 4 5 6 7 8]>)\
The second tag (i.e. md5sum:) shows up after we have "integrated" the external file with the method. It is a snapshot of the md5sum of the external file at the time of integration. It can then be used to determine at future points which assets need to be reintegrated because the hash of their external files no longer matches that of the method.\
\
- Once Block: (^[....] once)\
Though the return value of an asset method is an object generated by importing from an external resouce file, it is considered a must that after that fact, the method be able to reproduce the object without the resource file. This is necessary because applications shouldn't require big directories full of files. Applications must be able to be loaded from Store or Parcel without external file references.\
\
The code to recreate the object goes in the block. The message once is special. It is like value, in that returns the value of the block. But the value returned is that as rememebered from the first time the block was sent this message. For example, if the contents of the block were to create a new OpaqueImage of moderate size, each time we asked for this asset, performance would be dismal. It would a waste of both cpu cycles and memory. This technique is more than 100 times faster than the former 'ListIconLibrary visualFor: aSymbol' technique.\
}
There's also this screencast, which shows off the RB integration, and caching
speedup due to once use:
http://www.cincomsmalltalk.com/userblogs/travis/files/assetsOne.mov
and this blogpost, explaining once:
http://www.cincomsmalltalk.com/userblogs/travis/blogView?entry=3346567529
Cheers,
Henry
_______________________________________________
Pharo-project mailing list
[email protected]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project