Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to make a "synthetic" lvalue?

Reply
Thread Tools

How to make a "synthetic" lvalue?

 
 
Roland Schwarz
Guest
Posts: n/a
 
      05-19-2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to test python extension modules during 'make check' / 'make distcheck'? Mark Asbach Python 1 11-03-2006 02:29 AM
How does one make use of sub files to make DVD? Bun Mui DVD Video 0 05-21-2006 03:24 PM
How does one make use of sub files to make DVD? Bun Mui Computer Support 0 05-21-2006 03:09 PM
Anyone know how to make XP make a sound when typing? Like old dumb terminals? Fire Computer Support 6 02-17-2004 12:56 AM



Advertisments