>>Single Point of Failure >>

§ Home § CV § Blog Photography Divagações §

Calendar

May 2012
M T W T F S S
« Apr    
 123456
78910111213
14151617181920
21222324252627
28293031  


Popular Tags


Recent Entries



Categories


Archives


Using Type-Erasure to store heterogenous properties

Ξ April 5th, 2012 | → 0 Comments |
Uncategorized ||


The objective was to implement a Property class composed of a key and a value. The class is designed to store heterogenous data, implemeting a non-template class to allow storing properties in a standard container.

Property p("zero", 0),

auto key = p.key();
auto value = p.value< int >();
auto tname = p.type();

Let’s start over with the simple stuff: the class declaration and some public types.

class Property
{
    public: // public types

    typedef std::string     key_t;
    typedef std::string     type_t;

To provide internal storage for the property value, define a Placeholder abstract class.

private:

    class Placeholder
    {
    public:

        Placeholder(const key_t& key) : key_(key) {}

        virtual ~Placeholder() {};

        virtual key_t key() { return key_; }
        virtual type_t type() const = 0;

    private:

        key_t key_;
    };

The Property class will store a pointer to Placeholder base class. This class provides for type-erasure since it does not mention the actual value type. The value is stored in the derived templated PlaceHolderStorage class.

    template < typename T >
    class PlaceholderStorage : public Placeholder
    {
    public:
        PlaceholderStorage(const key_t& key, const T& value):
            Placeholder(key), value_(value)
        {}

        virtual ~PlaceholderStorage() {}

        virtual type_t type() const
        {
            return typename_of< T >();
        };

        virtual const T& value()
        {
            return value_;
        }

    private:
        T value_;
    };

The default constructor of property created an integer value 0 and “default” as key. Default construction is only available to allow for usage with map standard container. Construction is also enabled by a template with the type of the storage data.

public:

    Property() :
        property_( new PlaceholderStorage< int >("default", 0) )
    {}

    template < typename T >
    Property(const key_t& key, const T& value) :
        property_( new PlaceholderStorage< T >(key, value) )
    {}

The Property allows access to the key and to the value. Access to the value requires a template type. A PropertyTypeInvalid exception is thrown if the requested type is not according to the stored value.

public:

    key_t key() const
    {
        return property_->key();
    }

    type_t type() const
    {
        return property_->type();
    }

    template < typename T >
    T value() const
    {
        PlaceholderStorage< T > *derived
            = dynamic_cast<
                PlaceholderStorage< T >* >(property_.get());

        if(derived == 0x0)
            throw PropertyTypeInvalid(
                std::string("Invalid property type, accessing \"")
                                + typename_of< T >()
                                + "\" value, but type is \""
                                + property_->type() + "\"");

        return derived->value();
    }

Internally, the Property stores the Placeholder for the data in a shared pointer.

private:

    std::shared_ptr< Placeholder > property_;
};

To access the complete example, access abyss source code.

 


Home, Sick, Alone…

Ξ June 4th, 2010 | → 0 Comments |
Music, Uncategorized |, , |


I might be home alone, taking care of this cold and the running nose, but found this on the internet and had to share.

 


More Pragmatic

Ξ May 7th, 2010 | → 0 Comments |
Uncategorized |, , , |


Andrew Hunt is becoming one of my favorite authors. The first book I read from Andy was “The Pragmatic Programmer“, which is a book full of insights on the life philosophy to become an expert programmer. Now I got my hands in “Pragmatic Thinking & Learning - Refactor Your Wetware” and it is another excellent book. This book is not only for software developers, it is intended for a wider audience and provides lots of tips to improve the way you think - it certainly made me think about the way I put by “mind” into the job.
I strongly recommend it…

 


Procrastination…

Ξ November 10th, 2009 | → 0 Comments |
Uncategorized |, , |


This must be based on myself… Lev Ylmaz doesn’t know me, but he certainly did a great job depicting me, specially when I’m on vacations! By the way, I’m on vacations.


And there’s a lot of other funny videos. These are my other favorites: [1], [2] and [3].

 


Frak BSG, this is Amazing!

Ξ August 8th, 2009 | → 0 Comments |
TV, Uncategorized |, , , , |


Awesome is the best word to describe the new version of Battle Star Galactica (BSG). Although being a sci-fi fan, I have to admit that I never had the change to see the “old” version of this TV series (I didn’t say huge fan). The BSG marathon to reach the end of season 2 was almost necessary, in fact, after a couple of episodes I was totally hooked! And the season finale!… Amazing!

Battle Star Galactica Cover

The suspense left in the air by the end of season 2 was so spectacular that I started immediately to watch season 3. I only hope that it continues to be this great until the end…

As a final note: ‘Frak’ all ‘Toasters’… Personally, I like the Model Six’s shapes. :)

Cylon Model 6

 


Blog About…

Ξ April 10th, 2009 | → 0 Comments |
Uncategorized |, , , |


I’m a Gmail user, and only use other mail servers for work purposes because it’s company policy. My messages tend to be neatly tagged before being archived, but there are always a couple of them that just seem to stay in the Inbox forever. Some of them are already tagged and ready to go, but they seem to escape… for some reason.

One of the reasons relates to the existence of a special tag. When something interesting to write about arrives it is tagged with the ‘blogabout‘ tag. In order to archive all the ‘blogabout‘ messages here some links to their content. Hope you enjoy it.

- Profiteroles: A recipe for a marvelous desert… from a worth following portuguese recipes blog.

- 100 Questions: From a very interesting blog that I follow on a daily basis.

- Smarter Men Have More Sperm: … sometimes I wonder if all science is good science. Obviously I’m a nearsighted person, without a long run vision of science, but can someone tell me where is the importance of studying the relation between the amount of sperm produced by a man and his intelligence.

 


Campinos

Ξ August 17th, 2008 | → 0 Comments |
Life, Photo, Uncategorized |, |


Campinos are hard working men (from Ribatejo, Portugal) that work the cattle in the fields, also called Lezirias. My village has an annual traditional festival, in which they show the traditional ways of rounding up the cattle.

 


deadline

Ξ December 31st, 1969 | → 0 Comments |
Uncategorized ||



          deadline
                 \ded-lin\ n (1864) a line drawn within or arround a prison that a prisioner passes at risk of being shot

in Merrian-Webster Online Dictionary       

 


On the nightstand...



Entries | Comments