Strongly-typed WeakReference

I was playing around with weak references recently and wanted to tidy up my code a little – there were casts everywhere – so I knocked up a derivation using generics. I'm sure someone somewhere already did this, but I thought I'd share it anyway, so here it is.

/// <summary>
/// Strongly-typed weak reference.
/// </summary>
/// <typeparam name="T">The type of object being referenced.</typeparam>
public class WeakReference<T> : WeakReference, ISerializable where T : class
{
    /// <summary>
    /// Initializes a new instance of the  <see cref="WeakReference<T>"/> class, referencing
    /// the specified object.
    /// </summary>
    /// <param name="target">An object to track.</param>
    public WeakReference(T target)
        : base(target)
    {

    }

    /// <summary>
    /// Initializes a new instance of the  <see cref="WeakReference<T>"/> class, referencing
    /// the specified object and using the specified resurrection tracking.
    /// </summary>
    /// <param name="target">An object to track.</param>
    /// <param name="trackResurrection">Indicates when to stop tracking the object. If <c>true</c>, the object is tracked
    /// after finalization; if <c>false</c>, the object is only tracked until finalization..</param>
    public WeakReference(T target, bool trackResurrection)
        : base(target, trackResurrection)
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="WeakReference<T>"/> class.
    /// </summary>
    /// <param name="info">An object that holds all the data needed to serialize or deserialize the current <see cref="T:System.WeakReference"/> object.</param>
    /// <param name="context">(Reserved) Describes the source and destination of the serialized stream specified by <paramref name="info"/>.</param>
    /// <exception cref="T:System.ArgumentNullException">
    /// 	<paramref name="info"/> is null. </exception>
    protected WeakReference(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }

    /// <summary>
    /// Gets or sets the object (the target) referenced by the current <see cref="T:System.WeakReference"/> object.
    /// </summary>
    /// <value></value>
    /// <returns>null if the object referenced by the current <see cref="T:System.WeakReference"/> object has been garbage collected; otherwise, a reference to the object referenced by the current <see cref="T:System.WeakReference"/> object.</returns>
    /// <exception cref="T:System.InvalidOperationException">The reference to the target object is invalid. This exception can be thrown while setting this property if the value is a null reference or if the object has been finalized during the set operation.</exception>
    public new T Target
    {
        get
        {
            return (T)base.Target;
        }

        set
        {
            base.Target = value;
        }
    }
}

There's not much to say about this class. I've replaced the Target property of the original WeakReference class with a strongly-typed one based on the type parameter, T. The other bits are just constructors to mirror those in the base class and it's job done.

Deserializing from a sequence of bytes

I was working on some file-based persistence today and found myself needing to load a string from an array of bytes that represented that string's characters. There is, of course, more than one way to skin this particular cat, but I took it as an opportunity to play around with extension methods.

The first way I found to do this was to have a method with an iterator block that took each pair of bytes and used the BitConverter.ToChar() method to yield a character.

public static IEnumerable<char> ToChars(this IEnumerable<byte> sequence)
{
    int counter = 0;
    byte[] bytes = new byte[2];

    foreach (var b in sequence)
    {
        bytes[counter++ % 2] = b;
        if (counter % 2 == 0)
        {
            yield return BitConverter.ToChar(bytes, 0);
        }
    }
}

Turning the results of this method into an array and using the appropriate string constructor meant job done. I realise that a little more work is needed to check we have a non-null sequence with an even number of bytes, but this is just illustrative. However, what if I had wanted a sequence of integers or doubles or some other type?

A more elegant solution would be to create a partitioning method that took the sequence of bytes and returned a sequence of smaller sequences.

public static IEnumerable<byte[]> Partition(this IEnumerable<byte> sequence, int partitionSize)
{
    bool any = false;

    int partitionIndex = 0;
    byte[] partition = new byte[partitionSize];
    foreach (var b in sequence)
    {
        any = true;
        partition[partitionIndex++] = b;

        if (partitionIndex >= partitionSize)
        {
            yield return partition;
            partitionIndex = 0;
        }
    }

    // We have a partial partition to yield.
    if (any && (partitionIndex != 0))
    {
        yield return partition;
    }
}

This time, we've got ourselves a sequence of byte arrays. To get the characters we would've got from the previous example, we have to perform a quick Select() call on the sequence.

var sequenceOfChar = sequenceOfBytes
    .Partition(sizeof(char))
    .Select(x => BitConverter.ToChar(x, 0));

Of course, if we wanted a sequence of integers, the call would be a little different.

var sequenceOfInt32 = sequenceOfBytes
    .Partition(sizeof(int))
    .Select(x => BitConverter.ToInt32(x, 0));

There's a little more polish required to cope with null sequences and there's no guarantee that the last array in the partitioned sequence will have enough bytes for a full partition. Finally, in my examples this relies on the data being persisted in the order expected by the BitConverter calls, but you could manage that yourself depending on your own circumstances.

Is this useful to anyone else? Is there a better way to achieve the same goals?

Learning Poetry: Exercise 2

This is the second part in a series of posts documenting my efforts learning more about prosody:

In the last post, I explained how I was learning to be a better poet. I also included my attempts from the first exercise in Stephen Fry's book, The Ode Less Travelled: Unlocking the Poet Within. Now it is time for the fruits of the second exercise. I would love to hear your thoughts on my attempts – what works, what does not, where you think I've gone wrong. Perhaps you might get a copy of the book and have a go yourself. If you do, I'd really like to see your results.

The Exercise

Write five pairs of blank iambic pentameter in which the first line of each pair is end-stopped1 and there are no caesuras2, then write five pairs of blank iambic pentameter with the same meaning, but using enjambment3 and at least two caesuras.

The topics for each of the five pairs are:

  1. Precisely what you see outside your window.
  2. Precisely what you'd like to eat, right this minute.
  3. Precisely what you last remember dreaming about.
  4. Precisely what uncompleted chores are niggling at you.
  5. Precisely what you hate about your body.

The Results

End-stopped

  1. The blur of trees is racing out of sight,
    As speedily the train ploughs down the line.

  2. A pack of tasty chips from in my bag.
    The ones I bought last night inside the store.

  3. A crazed outlandish woman blocked my path,
    Demanding love and drinks from all my friends.

  4. I really must repair the door and step,
    And take the time to see the naked earth.

  5. My gut has grown from laziness and food,
    It hurts to walk upon my foot as well.

Using enjambment and caesuras

  1. The trees, in blurs of green that race beside
    the train, demark the path we travel on.

  2. Some chips, perhaps a drink of something, I bought
    selections from the store last night. Thank God.

  3. So drunk, the girl accosted me, she asked
    if anyone would like a kiss. We ran.

  4. The earth is bare, it waits for seeds, we might
    sew grass or herbs. And still the door needs work.

  5. From food, my gut has grown to fill the space
    beyond my pants. Yet still my foot, it aches.

  1. A single thought that finished with the line. []
  2. Pauses, which break up the flow. []
  3. Where the meaning runs on from one line to the next. []

Our new desk

IKEAEver since we moved into our house, we've had grand plans for our office (originally, the third bedroom). The plan was to use it as a reading room but that just never panned out. We had painted it when we moved in, then populated it with bookcases from IKEA, which in turn were populated with our books and CDs, but that was about it. Though we had a little tiny desk in there with a computer, all we really did was use the room to store things or hide other things when guests came over.

Given that this space was not really adding value to our lives, this year we decided to make a concerted effort to get the office into a more pleasing state with a view to actually using it as an office. So, we measured things, sketched a plan on the whiteboard in the kitchen and penned a list of tasks, including items to purchase.

Step one was to move some of the bookcases, so I did (though perhaps not at the pace my wife would've preferred). Once the bookcases and their contents were moved to the appropriate location as per our sketch, we were ready for the next step: the new desk. If moving the bookcases had been too slow for the missus then getting a new desk had been going in reverse. However, eventually we decided to head out to IKEA for inspiration and see what we could find.

Before the new desk
Before the new desk

I admit I wasn't expecting to find anything appropriate but after looking at all the desks and tables we could find in IKEA, we settled on the VIKA AMON black desk. We were originally going to get both the drawers and the storage leg, but the drawers only appear to come in white (I don't get why), so a change of plan was required. After examining all the other legs available in the VIKA range and discovering most options were out of stock and not quite what we wanted, we settled on the wooden legs.

The wooden legs
The wooden legs don't quite match our black office furniture

The only problem with the legs was their appearance, but unlike the white drawers, the plain wood made this a reasonably easy fix. The office furniture is black/brown, including the desk top we selected so a quick trip to Home Depot was required to get a matching wood stain. The stain I selected was a Classic Black Minwax Polyshades stain and polyurethane in one with a satin finish. I admit that I mostly guessed at what colour might match the existing IKEA furniture. I also admit that I'm lazy, hence the stain and polyurethane in one.

I forwent the application of conditioner to the wood as I was applying an extremely dark stain to the wood. Over two days, I applied two coats of stain to the legs. In hindsight, three coats may have been better but any thin patches on the legs are not easily noticeable, so they can be our little secret.

Minwax stain and polyurethane
Minwax stain and polyurethane

After two coats, the legs were barely distinguishable from the desk that they were to support.

The stained legs and the desk top
The stained legs and the desk top

With the legs ready, it was time to clear some space in the office. So I dismantled the computer and moved everything out of the way to clear space for the new desk.

Space cleared, ready for the new desk
Space cleared, ready for the new desk

Some quick, effortless assembly later and the desk was all ready to install. The three legs attached to the desk as per the instructions provided with each leg. The remaining support was provided by the free-standing storage unit that we selected for housing our computer.

The desk, assembled and ready
The desk, assembled and ready

Once the desk was in place, it was clear we'd made a good choice as there was just enough room for the lamp in the corner. It was time to reassemble the computer and put it into place.

The finished work space
The finished work space

Proud of my efforts, I grabbed Chrissy to show her my handiwork. She was, of course, quick to point out that we still need to get lampshades and put our lamps on the new desk. Perhaps then, the office will finally be finished1.

  1. Just in time for us to turn it into a nursery? No, this is not an announcement. []

Learning Poetry: Exercise 1

This is the first part in a series of posts documenting my efforts learning more about prosody:

If you've been following my blog at all, you may have noticed that I have posted a poem or two. These attempts at prosody are remnants of songwriting attempts – lyrics that never gained a tune. Though I enjoy writing lyrics and, on the odd occasion pretending they're real poems, I've never taken the time to learn about the art of poetry. Because of this, much of what I write lacks the structure and care that would indicate or more learned authorship and I expect to some I may just come across as nothing but a poetaster1.

With that in mind, a couple of years ago I bought a book by Stephen Fry while I was on vacation in San Francisco. It's called The Ode Less Travelled: Unlocking The Poet Within. Allowing for the appropriate length of procrastination, I started reading it this weekend on my trip to Chicago for St.Patrick's Day and I've really been enjoying it. Each concept is introduced with examples and analysis before the reader is given an opportunity to put their newly acquired knowledge to work in simple exercises (Fry's view is that we're each capable of poetry if we only try).

I won't recreate the book here, it would be a poor facsimile, but I would like to present my attempts from each exercise. Whatever you think of my poetic prowess (or lack thereof), I hope it will be fun to follow along as I learn and improve my prosody. I'll begin with a brief explanation of the exercise and then provide my attempts2. As I don't intend to explain the terms in detail, you may want a copy of the book or a dictionary in order to understand the exercise.

The Exercise

Write 20 lines of blank3 verse in iambic pentameter4.

The Results

When Mrs. Wilson claimed she was a bitch,
Miss Chrissy said it was not really true.

Tonight, I slept inside an apple core.

The night is young and eager for some fun,
but what to do, I'm bored and losing time.

This exercise is rather dull for me.

The driver stopped to get another fare.

His face was low, without a look of love,
yet some might say he's clearly lost in thought.

I'm learning all about iambic lines.

My friends will all be quite impressed with this,
I know a term or two about the moon.

It burns to think she left me all alone.
Where will I find a girl as bright as her?

Another dog falls foul of Sergeant Crow.
The pound is where he locks them all away.

Tomorrow takes a darker turn for me.
The crows come home to roost and bury me.

A line or two of prosody to write.

I stole a pack of mints from Mrs. Brown.

  1. A word I learned from my new poetry professor, Stephen Fry. It means 'bad poet'. []
  2. Each exercise is actually introduced with some rather detailed instructions in the book that provide additional guidance and challenges than the summary I will provide. []
  3. Non-rhyming []
  4. Verse with the metre 'ti-tum ti-tum ti-tum ti-tum ti-tum', also known as the Heroic Line. []