To cache lately opened segments in memory is a good idea. Suppose that you will cache last 10 opened segments. Create a singleton class that will hold a dictionary that maps string to segment object. Each time you need to read/write from any segment you will check if this singleton includes it already (by some id - its name for example). If yes - you will get its reference/pointer and read/write to it. Otherwise, you will open a new segment and store it in this singleton.
Singleton is a class that may have only one instance, usually created on first usage. See next link http://forums.codeguru.com/showthread.php?423850-how-to-implement-a-singleton-in-c-c. I would do it like this.
In header file:
class Singleton {
public:
static Singleton* instance();
void write (myData data, unsigned long id);
void select(unsigned long id);
void select(time_t insertionTime);
private:
Singleton();
static Singleton* m_singleton; // singleton instance
managed_shared_memory m_segment;
};
In cpp file: Singleton* Singleton::m_singleton= NULL;
Singleton::Singleton()
: segment(open_only, "MySharedMemory")
{
// do init stuff
}
Singleton* Singleton::instance()
{
if (m_singleton== NULL)
m_singleton = new Singleton();
return m_singleton;
}
void Singleton::write (myData data, unsigned long id)
{
//Find the vector using the c-string name
MyVector *myvector = m_segment.find<MyVector>("MyVector").first;
// add code here
}
void Singleton::select(unsigned long id)
{
// your code here
}
void Singleton::select(time_t insertionTime)
{
// your code here
}
Usage in the implementor of write_interface:
Singleton::instance().write (data, id);
The solution only ensures one instance since program starts until it ends.