Share Library (C++)
File detail
Source code
#ifndef SHARECTL_H
#define SHARECTL_H
/**
* @file sharectl.h
* @brief Command-line utility manipulating with shared segment
* @author Kamil Dudka, xdudka00@gmail.com
* @date 2007-05-15
* @ingroup sharectl
*/
#include "sharelib.h"
#include <string>
#include <iostream>
// #include <map>
namespace Share {
/**
* @brief Parser for commnad-line arguments of sharectl utility
* @ingroup sharectl
*/
class ArgParser {
public:
/**
* @brief commnad-line switches enumeration
*/
typedef enum {
NONE = 0, // No switch
HELP, // Display help
CREATE, // --create
CREATE_AND_WAIT, // --create-and-wait
NOTIFY_ATTACH, // --notify-attach
NOTIFY_DETACH, // --notify-detach
UNLINK, // --unlinnk
FORCE_DESTORY, // --force-destroy
ALLOC, // --alloc
FREE // --free
} Action;
/**
* @param argc Synonymous argument from main() function.
* @param argv Synonymous argument from main() function.
* @throw ShareException Library-specific exception derived from std::bad_alloc
*/
ArgParser (int argc, char *argv[]) throw (ShareException);
/**
* @return Return true if new segment should be created.
*/
bool isNewSegment();
/**
* @return Return segment name parsed from command-lnie.
*/
const char* name();
/**
* @return Return size argument parsed from command-lnie.
*/
size_t size();
/**
* @return Return action(switch) argument parsed from command-lnie.
*/
Action action();
private:
std::string name_;
size_t size_;
Action action_;
};
/**
* @brief Command-lnie switches mapping from string to enum.
* @ingroup sharectl
*/
class ArgSwitchChecker {
typedef ArgParser::Action TAction;
// STL map
// typedef std::map<std::string, TAction> TMap;
// Share library Map
typedef Share::Map<std::string, TAction> TMap;
public:
ArgSwitchChecker();
/**
* Map string to enum
* @param str String to map.
* @return Return switch enumeration or ArgParser::NONE, if not found.
*/
TAction checkSwitch (std::string str);
private:
TMap map_;
};
class SharedSegment; // Platform-independent shared segment representation
class SegmentHeader; // Object placed at begin of shared segment
/**
* @brief Wrapper around SharedSegment class for sharectl
* @ingroup sharectl
*/
class SharedSegmentWrapper {
public:
SharedSegmentWrapper();
~SharedSegmentWrapper();
/**
* Create new shared segment.
* @param name Desired shared segment name.
* @param size Desired shared segment size.
* @throw ShareException Library-specific exception derived from std::bad_alloc
*/
void create (const char *name, size_t size) throw (ShareException);
/**
* Attach to existing shared segment.
* @param name Name of shared segment to attach to
* @throw ShareException Library-specific exception derived from std::bad_alloc
*/
void attach (const char *name) throw (ShareException);
/**
* @return Return address of attached shared segment
*/
SegmentHeader* atAddr() throw (ShareException);
/**
* Attempt to destroy shared segment. No checks are being performed!!
*/
void destroy();
private:
SharedSegment *segment_;
};
/**
* @brief Wrapper around SegmentHeader class for sharectl
* @ingroup sharectl
*/
class SegmentDebuger {
public:
/**
* @param shareManager ShareManager object to work with.
*/
SegmentDebuger (SegmentHeader *shareManager);
/**
* Call relevant ShareManager method.
* @param action Expected values are NOTIFY_ATTACH, NOTIFY_DETACH or UNLINK
*/
void refCountAction (ArgParser::Action action);
/**
* @return Return if shared segment should be destroyed due to share protocol
*/
bool shouldBeDestroyed();
/**
* Attempt to allocate block of memory in shared segment
* @param size Size of block to allocate.
*/
void alloc(size_t size);
/**
* Attempt to free bloc kof memory in shared segment
* @param relativePointer Relative pointer to block to free.
*/
void free(size_t relativePointer);
private:
SegmentHeader *segmentManager_;
friend std::ostream& operator<< (std::ostream &, SegmentDebuger&);
};
/// Stream SegmentHeader statistics to output stream
std::ostream& operator<< (std::ostream &, SegmentDebuger&);
}
#endif // SHARECTL_H