|
Boost Users : |
Subject: [Boost-users] vpath so far...
From: Dan Bloomquist (danb_at_[hidden])
Date: 2010-05-07 17:37:20
Hi,
I have ordered a new copy of /C++/ Template /Metaprogramming.
/I am trying to do this stuff the Boost way and now I have a pretty good
handle on using regex.
Other than this stuff needs to be wrapped in proper namespaces, I was
just wondering how it looks.
Thanks, Dan.
~~
This is what the usage would look like, this class has the workings of a
singleton. The name map might be a class the exposes just the string
pair to the internal parser so more than just a string name can be
stored in the map. In my case I also carry a set of flags in my old code.
~~~ main.cpp ~~~ the test, it works so far...
#include <vpath.hpp>
using namespace boost::filesystem; // for ease of testing
using namespace boost::xpressive;
int main(int argc, char *argv[])
{
//this should be a vpath type?
std::pair< std::string, std::string > strTS( "$(", ")" );
//uses shared pointer for create and forget usage, and scope of
creation is not relevant
sp_virtual_filesystem_map vtable= sp_virtual_filesystem_map( new
virtual_filesystem_map );
{ //test, initialize in local scope
vpath init( strTS, vtable );
}
//the table can be modified any time or replaced with 'set_vnames
(*vtable)[ "PATH" ]= "test_path";
(*vtable)[ "TEST" ]= "test.txt";
//the test
vpath testPath( "C:\\$(PATH)\\class\\User_2_2\\$(TEST)" );
std::cout << testPath << std::endl;
//prints: 'c:/test_path/class/User_2_2/test.txt'
}
~~~~ the vpath.hpp //I have a list of includes in stdafx.h so may have
missed one or two here
#include <boost/filesystem/operations.hpp>
#include <boost/regex.hpp>
#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/regex_actions.hpp>
#include <boost/shared_ptr.hpp>
#include <map>
template<class String, class Traits> class basic_vpath;
typedef basic_vpath< std::string, boost::filesystem::path_traits > vpath;
typedef std::map< std::string, std::string > virtual_filesystem_map;
typedef boost::shared_ptr< virtual_filesystem_map >
sp_virtual_filesystem_map;
template<class String, class Traits> class basic_vpath
:public boost::filesystem::basic_path< String, Traits >
{
//app scope singleton
static sp_virtual_filesystem_map spVNamesPaths;
//app scope singleton
static boost::xpressive::sregex signature;
void parse_path( ) {
String str=( regex_replace( string( ), signature, ref(
*spVNamesPaths )[ s1 ] ) );
clear( );
operator/= ( str );
}
public:
//should probably parse before assign to path
basic_vpath( const string_type & s )
:basic_path( s ) {
if( ! spVNamesPaths )
return;
parse_path( );
}
basic_vpath( const value_type * s )
:basic_path( s ) {
if( ! spVNamesPaths )
return;
parse_path( );
}
//initialize, the singleton constructor
basic_vpath( std::pair< String, String >& sig,
sp_virtual_filesystem_map spMap ) {
set_signature( sig );
set_vnames( spMap );
}
void set_signature( std::pair< String, String >& sig ) {
signature= sig.first >> (s1 = +_w) >> sig.second;
}
void set_vnames( sp_virtual_filesystem_map spVNames ) {
spVNamesPaths= spVNames;
}
};
~~~~~ need a vpath.cpp for the persistent members
#include "stdafx.h"// use precompiled headers for development
#include <vpath.hpp>
sp_virtual_filesystem_map vpath::spVNamesPaths;
boost::xpressive::sregex vpath::signature;
~~~~ stdafx.h// for test
#include <boost/filesystem/operations.hpp>
#include <boost/regex.hpp>
#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/regex_actions.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream> // for std::cout
#include <map>
#include <stdio.h>
#include <ostream>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net