Click here to Skip to main content
15,883,894 members
Articles / Programming Languages / C++

RCF - Interprocess Communication for C++

Rate me:
Please Sign up or sign in to vote.
4.94/5 (147 votes)
25 Oct 2011CPOL20 min read 4.6M   8.4K   331  
A server/client IPC framework, using the C++ preprocessor as an IDL compiler.
//*****************************************************************************
// Copyright (c) 2003. All rights reserved.
// Developed by Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//*****************************************************************************

#ifndef INCLUDE_UTIL_SCAN_HPP
#define INCLUDE_UTIL_SCAN_HPP

#include <sstream>
#include <string>
#include <utility>
#include <vector>

namespace util {

    class Scan {
    public:
        Scan( std::string s, std::string format ) : i_(0), ok_(true) 
        {
            int n = 0;
            std::string::size_type pos = 0;
            std::string::size_type prev_pos = 0;
            std::string::size_type npos = std::string::npos;

            // Extract format string literals
            std::vector< std::pair<std::string, std::string> > literals;
            while(pos != npos) {
                n++;
                std::string specifier1 = makeSpecifier(n);
                std::string specifier2 = makeSpecifier(n+1);
                pos = format.find(specifier1, prev_pos);
                std::string literal1 = (pos == npos) ? format.substr(prev_pos) : format.substr(prev_pos, pos - prev_pos);
                if (pos != npos) pos += specifier1.length();
                prev_pos = pos;
                pos = format.find(specifier2, prev_pos);
                std::string literal2 = (pos == npos) ? format.substr(prev_pos) : format.substr(prev_pos, pos - prev_pos);
                literals.push_back( std::make_pair( literal1, literal2 ) );
            }

            // Extract arg string values
            pos = 0;
            for (std::vector< std::pair<std::string, std::string> >::iterator it = literals.begin(); it != literals.end(); it++) {
                if (pos != npos && pos == s.find( (*it).first, pos)) {
                    std::string::size_type a = pos + (*it).first.length();
                    std::string::size_type b = s.find( (*it).second, a );
                    values_.push_back( s.substr( a , (b == a) ? npos : b-a ) );
                    pos = b;
                }
                else {
                    ok_ = false;
                }
                    
            }
            
        }

        template<typename T> Scan &operator()(T &t)
        { 
            std::istringstream istr( getNextValue() );
            istr >> t;
            return *this;
        }
        
        operator bool() const
        { 
            return ok(); 
        }

        bool ok() const
        { 
            return ok_; 
        }

    private:

        std::string makeSpecifier( int n )
        {
            std::ostringstream ostr;
            ostr << "%" << n;
            return ostr.str();
        }

        std::string getNextValue()
        { 
            if (i_<values_.size()) 
                return values_[ i_++ ]; 
            else {
                ok_ = false;
                return "";
            }
        }

        std::vector<std::string> values_;
        unsigned int i_;
        bool ok_;
    };

} // namespace util


#endif // ! INCLUDE_UTIL_SCAN_HPP

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
Software developer, from Sweden and now living in Canberra, Australia, working on distributed C++ applications. When he is not programming, Jarl enjoys skiing and playing table tennis. He derives immense satisfaction from referring to himself in third person.

Comments and Discussions