How to make a "synthetic" lvalue?

Discussion in 'C++' started by Roland Schwarz, May 19, 2008.

  1. Most probably what I am asking for already has been
    answered somewhere, still I was not able to find:-(

    I want to encapsulate a class ( a primitive type for
    the beginning ) to behave like a lvalue, but instead
    of living in process memory being forwarded to some
    remote store.

    I believe an lvalue needs to be able to be convertible
    to its wrapped type on reading, and providing an
    assign function (operator =) for writing.

    The following snippet illustrates the idea:

    #include <iostream>
    #include <map>
    #include <string>

    // a model of the store on the remote end
    std::map<std::string, int> store;

    // the call to the remote store sets a value
    void set(std::string key, int val)
    {
    store[key] = val;
    }

    // this call to the remote store reads a value
    int get(std::string key)
    {
    return store[key];
    }

    // the template wraps a native type
    template<class T>
    class remote_lval
    {
    public:
    remote_lval(std::string key)
    : key_(key)
    {}

    operator T()
    {
    return get(key_);
    }

    T operator = (T val)
    {
    set(key_, val);
    return val;
    }

    private:
    const std::string key_;
    };

    int main
    (
    int argc
    , char* argv[]
    )
    {
    remote_lval<int> foo("foo");
    int bar;
    int baz;
    remote_lval<int> foobar("foobar");
    remote_lval<int>& hmm(foobar);

    foobar = baz = foo = bar = 42;

    std::cout << foobar << ", " << baz << ", " << foo << ", " << bar <<
    ", " << hmm << std::endl;

    foobar = foo + baz;

    std::cout << foobar << ", " << baz << ", " << foo << ", " << bar <<
    ", " << hmm << std::endl;

    return 0;
    }

    Altough the above code apparently works, I am not sure if I am
    overseeing something important. I intend to extend the idea to
    make the key_ a pos_type into a stream and be able to treat file
    space like a memory space. I know that there will be issues of
    caching and concurrency, but for the beginning I want to get the
    lval wrapper right.

    If anyone thinks what I am trying to do is a bad idea, I would be very
    glad to learn the reasoning "why" it is a bad idea, before going
    any further.

    Thank you for your kind attention.

    Roland aka. speedsnail
     
    Roland Schwarz, May 19, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kavya
    Replies:
    9
    Views:
    541
    Dik T. Winter
    Oct 28, 2006
  2. synthetic properties

    , Sep 2, 2008, in forum: Python
    Replies:
    2
    Views:
    703
    Bruno Desthuilliers
    Sep 2, 2008
  3. Replies:
    11
    Views:
    715
    James Kuyper
    Sep 22, 2008
  4. Julian Mehnle
    Replies:
    0
    Views:
    259
    Julian Mehnle
    Jul 17, 2003
  5. Peter Michaux

    Synthetic click on link doesn't follow link

    Peter Michaux, Jun 4, 2007, in forum: Javascript
    Replies:
    2
    Views:
    189
    -Lost
    Jun 5, 2007
Loading...

Share This Page