<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.simplicity &#187; DataStore</title>
	<atom:link href="http://www.dotsimplicity.net/tag/datastore/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotsimplicity.net</link>
	<description>Simple, reliable, simplicity. A software discussion blog</description>
	<lastBuildDate>Sun, 04 Jul 2010 09:44:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Component Oriented Entities &#8211; Updated (and some more&#8230;)</title>
		<link>http://www.dotsimplicity.net/2009/04/component-oriented-entities-updated-and-some-more/</link>
		<comments>http://www.dotsimplicity.net/2009/04/component-oriented-entities-updated-and-some-more/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 20:24:17 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software Idea]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[DataStore]]></category>
		<category><![CDATA[Entities]]></category>

		<guid isPermaLink="false">http://www.dotsimplicity.net/?p=193</guid>
		<description><![CDATA[The code I posted in the article Component Oriented Entities has been updated. The code in that article contained some documentation mistakes. Thanks to Nick these mistakes have been corrected. Besides that I&#8217;ve also added some new methods (Entity::remove*) which were somehow forgotten in the first implementation. And of course the code has also been [...]]]></description>
			<content:encoded><![CDATA[<p>The code I posted in the article <a title="Component Oriented Entities" href="http://www.dotsimplicity.net/?p=181">Component Oriented Entities</a> has been updated. The code in that article contained some documentation mistakes. Thanks to Nick these mistakes have been corrected. Besides that I&#8217;ve also added some new methods (Entity::remove*) which were somehow forgotten in the first implementation. And of course the code has also been slightly improved. Unfortunately the Event Management part of the implementation still hasn&#8217;t been included. Still I would like to encourage people who are using the code or parts of it to update their own code.</p>
<p>Also, a sneak peek on what I&#8217;m currently working on.</p>
<p><span id="more-193"></span></p>
<p>DataStore.h</p>
<pre class="brush: cpp;">

#ifndef __DATASTORE_H__
#define __DATASTORE_H__

// Include files
#include &quot;../dependencies.h&quot;
#include &quot;DataStack.h&quot;

// Forward declarations
class DataStack;

// DataStore class.
class DataStore
{
public:

    // Initialisation and deinitialisation
    DataStore();
    ~DataStore();

    void init();
    void clear();

    // Methods
    bool addDataStack(DataStack *stack);
    DataStack* createDataStack(const std::string &amp;name);

    DataStack* getDataStack(const unsigned int id);
    DataStack* getDataStack(const std::string &amp;name);

    void removeAll();
    bool removeDataStack(DataStack *stack);
    bool removeDataStack(const unsigned int);
    bool removeDataStack(const std::string &amp;name);

private:

    // Normal members
    std::vector&lt;DataStack*&gt; mStacks;

};

#endif
</pre>
<p>DataStore.cpp</p>
<pre class="brush: cpp;">

// Include files
#include &quot;DataStore.h&quot;

// DataStore class
// DataStore constructor.
DataStore::DataStore()
{
    init();
}

// DataStore deconstructor.
DataStore::~DataStore()
{
    clear();
}

// Initialises the Data Store.
void DataStore::init()
{
}

// Clears the Data Store.
void DataStore::clear()
{
    removeAll();
}

// Adds the given Data Stack to the Data Store.
bool DataStore::addDataStack(DataStack *stack)
{
    // Did we get a valid pointer?
    if(stack == NULL)
      return false;

    // Does a Data Stack with the same name exist?
    if(getDataStack(stack-&gt;getName()))
      return false;

    // Add the Data Stack to the vector.
    mStacks.push_back(stack);

    return true;
}

// Creates a Data Stack with the given name.
DataStack* DataStore::createDataStack(const std::string &amp;name)
{
    // Does a Data Stack with the same name exist?
    if(getDataStack(name))
      return NULL;

    // Create the Data Stack and add it to the vector.
    DataStack *stack = new DataStack(name);
    mStacks.push_back(stack);

    return stack;
}

// Gets the Data Stack with the given id.
DataStack* DataStore::getDataStack(const unsigned int id)
{
    for(unsigned int i = 0; i &lt; mStacks.size(); i++)
    {
       if(mStacks[i]-&gt;getID() == id)
         return mStacks[i];
    }

    return NULL;
}

// Gets the Data Stack with the given name.
DataStack* DataStore::getDataStack(const std::string &amp;name)
{
    for(unsigned int i = 0; i &lt; mStacks.size(); i++)
    {
       if(mStacks[i]-&gt;getName() == name)
         return mStacks[i];
    }

    return NULL;
}

// Removes all Data Stacks from this Data Store.
void DataStore::removeAll()
{
    // Clear the Data Stacks.
    for(unsigned int i = 0; i &lt; mStacks.size(); i++)
      delete mStacks[i];

    mStacks.clear();
}

// Removes the given Data Stack.
bool DataStore::removeDataStack(DataStack *stack)
{
    // Did we get a valid pointer?
    if(stack == NULL)
      return false;

    // Try to remove the Data Stack.
    vector&lt;DataStack*&gt;::iterator it;

    for(it = mStacks.begin(); it &lt; mStacks.end(); it++)
    {
        DataStack *dstack = *it;

        if(dstack == stack)
        {
           delete dstack;
           mStacks.erase(it);
           return true;
        }
    }

    // We couldn't find the Data Stack and thus couldn't remove it.
    return false;

}

// Removes the given Data Stack with the given id.
bool DataStore::removeDataStack(const unsigned int id)
{
    // Try to remove the Data Stack.
    vector&lt;DataStack*&gt;::iterator it;

    for(it = mStacks.begin(); it &lt; mStacks.end(); it++)
    {
        DataStack *dstack = *it;

        if(dstack-&gt;getID() == id)
        {
           delete dstack;
           mStacks.erase(it);
           return true;
        }
    }

    // We couldn't find the Data Stack and thus couldn't remove it.
    return false;
}

// Removes the given Data Stack with the given name.
bool DataStore::removeDataStack(const std::string &amp;name)
{
    // Try to remove the Data Stack.
    vector&lt;DataStack*&gt;::iterator it;

    for(it = mStacks.begin(); it &lt; mStacks.end(); it++)
    {
        DataStack *dstack = *it;

        if(dstack-&gt;getName() == name)
        {
           delete dstack;
           mStacks.erase(it);
           return true;
        }
    }

    // We couldn't find the Data Stack and thus couldn't remove it.
    return false;
}

// End of File
</pre>
<p>DataStack.h</p>
<pre class="brush: cpp;">

#ifndef __DATASTACK_H__
#define __DATASTACK_H__

// Include files
#include &quot;../dependencies.h&quot;
#include &quot;DataStore.h&quot;

// Forward declarations.
class DataStore;

// DataStack class.
class DataStack
{

    friend class DataStore;

public:

    // Initialisation and deinitialisation
    DataStack(const std::string &amp;name);
    ~DataStack();

    void init();
    void clear();

    // Methods
    unsigned int getID() const;
    const std::string&amp; getName() const;

    unsigned int getSize() const;

    // Sets the variable with the given name.
    template&lt;typename T&gt;
    bool setVar(const std::string &amp;name, const T &amp;value)
    {
        std::stringstream ss;
        ss &lt;&lt; value;

        if(ss.good())
        {
           mVars[name] = ss.str();
           return true;
        }

        else return false;
    };

    // Gets the variable with the given name.
    template&lt;typename T&gt;
    T getVar(const std::string &amp;name)
    {
        std::map&lt;std::string, std::string&gt;::iterator it;
        it = mVars.find(name);

        if(it != mVars.end())
        {
           std::stringstream ss(it-&gt;second);

           T t;
           ss &gt;&gt; t;
           return t;
        }

        else return NULL;
    };

    void removeAll();
    bool removeVar(const std::string &amp;name);

    //bool save(const std::string &amp;filename); TODO
    //bool load(const std::string &amp;filename); TODO

private:

    // Static members
    static unsigned int mIDCount;

    // Normal members
    unsigned int mID;
    std::string mName;

    std::map&lt;std::string, std::string&gt; mVars;
};

#endif
</pre>
<p>DataStack.cpp</p>
<pre class="brush: cpp;">

// Include files.
#include &quot;DataStack.h&quot;

// DataStack class
// Static variables.
unsigned int DataStack::mIDCount = 0;

// DataStack constructor.
DataStack::DataStack(const std::string &amp;name)
: mName(name)
{
    mID = mIDCount++;
}

// DataStack deconstructor.
DataStack::~DataStack()
{
    clear();
}

// Initialises the Data Stack.
void DataStack::init()
{
}

// Clears the Data Stack.
void DataStack::clear()
{
    removeAll();
}

// Gets the ID of the Data Stack.
unsigned int DataStack::getID() const
{
    return mID;
}

// Gets the name of the Data Stack.
const std::string&amp; DataStack::getName() const
{
    return mName;
}

// Gets the number of variables on the Data Stack.
unsigned int DataStack::getSize() const
{
    return mVars.size();
}

// Removes all variables from this Data Stack.
void DataStack::removeAll()
{
    mVars.clear();
}

// Removes the variable with the given name.
bool DataStack::removeVar(const std::string &amp;name)
{
    std::map&lt;std::string, std::string&gt;::iterator it;
    it = mVars.find(name);

    if(it != mVars.end())
    {
       mVars.erase(it);
       return true;
    }

    else return false;
}

// End of File
</pre>
<p>Remember that all of the above code has been placed in the public domain and that I’ll take no responsibility or liability of any kind for any use that you may make of this code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotsimplicity.net/2009/04/component-oriented-entities-updated-and-some-more/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
