472,794 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 software developers and data experts.

How to make a "synthetic" lvalue?

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, intstore;

// 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<intfoo("foo");
int bar;
int baz;
remote_lval<intfoobar("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

Jun 27 '08 #1
0 960

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: JKop | last post by:
When you want to store an integer in C++, you use an integral type, eg. int main() { unsigned char amount_legs_dog = 4; } In writing portable C++ code, there should be only two factors that...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
30
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.