|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
It's a bit of misnomer, it returns a T& reference to the stored object (or throws an exception if the pointer is null). Both operations are on references. Naming is kinda taken from std:: reference_wrapper.
I think it would be wiser to implement it the second way, that only the reference wrapper gets modified. Less headaches with immutable types, threads, unintended consequences.
However, operator + might not return T. Damn. This can complicate things in the second case, with the plain operator +. However in that case I am not obligated to return ref<T> in the proxy. Only the += is problematic since I need to return ref<T>&.
I'll get some sleep over this, hopefully I will come up with a satisfying solution.
|
Basically, if operator overloading is used, anything can happen.
The innocent line a += 1; could be an overloaded += operator that initiates nuclear warfare.
Also, the lines a += 1 could increase a by 1 while a = a + 1 could decrease a by 10.
This is one of the reasons why java removed operator overloading completely (I don't agree with that decision, but it wasn't totally unreasonable).
If you use operator overloading or use classes that do it, always read the documentation (or in case of the usual lack of it, the code). Otherwise there is no way to really know what will happen. Avoid it as much as possible unless the benefits are far greater than the possible pitfalls for this specific scenario.
|
On September 03 2011 09:16 Neshapotamus wrote: EscPlan9, I dont understand your post at all. First off, if you have a WSDL, cant you generate a webservice proxy and use strongly typed object. If that approach doesnt work out and sounds like your getting XML, do you have the XSD for the XML. That way, you can serialize and deserialize the XML into a object. If you dont want to do it that way, can't you use LINQ to XML to and create a annonoymous object with the name for the property you would want.
Maybe, an example of what your trying to do and I can be more clear on taking the best approach.
Fair enough. I have no prior experience with WSDLs - I've just been working with them as if they are XML files. So to be frank, I do not understand the majority of your post either.
The previous developer (no longer with the company) used SvcUtil on the WSDL to generate the client proxy. I originally was tackling the problem through straight XML parsing methods, most recently XPATH. But I figure I really shouldn't be reinventing the wheel when he already figured out a reliable way of parsing the WSDL for the important information he needs. I just know going through his code gives me major headaches trying to understand what he's doing in the sections (no documentation, no comments, code works but is ugly as hell), but I digress...
I'll look into finding the schema (XSD) for the WSDLs. Though I do not yet understand what I should be getting from serializing/deserializing the XMLs (zero experience with that), or how to make use of that information.
As for the last point, I haven't touched LINQ yet. I know SQL and it seems fairly similar in syntax.
The current plan going forward: 1) Take more time to understand how SvcUtil was used on the WSDLs previously. 2) Take more time for planning and design once I understand the organization of the information returned from the SvcUtil generated client proxy. 3) Create new methods to send that information to the (WIP) GUI frontend and generate the tree structure.
A simplified dummy WSDL I created (from the top of my head while at home) that's similar to the WSDLs being analyzed:
<wsdlMe> <complexType name="Create"> <element name="Items" type="tns:Items" maxOccurs="unbounded"></element> <element name="Username" type="string"></element> </complexType> <complexType name="Items"> <element name="Description" type="string"></element> <element name="Quantity" type="int"></element> <element name="Price" type="decimal"></element> </complexType> <complexType name="CreateResponse"> <element name="Errors" type="tns:Errors" maxOccurs="unbounded"></element> </complexType> <complexType name="Errors"> <element name="Description" type="string"></element> </complexType> <complexType name="Finish"> <element name="Time" type="datetime"></element> </complexType> <complexType name="FinishResponse"> <element name="Errors" type="tns:Errors" maxOccurs="unbounded"></element> </complexType> <messages> <message name="CreateMsgIn" type="tns:Create"></message> <message name="CreateMsgOut" type="tns:CreateResponse"></message> <message name="FinishMsgIn" type="tns:Finish"></message> <message name="FinishMsgOut" type="tns:FinishResponse"></message> </messages>
<portType> <operations> <operation name="Create" type="tns:CreateMsg"></operation> <operation name="Finish" type="tns:FinishMsg"></operation> </operations> </portType> </wsdlMe>
The QA tester selects the operation they want to create a test case for. Then the WSDL is parsed (portType/operations/operation) to create the tree structure for the user. So they would see something like
(operation selected is Create) Create --CreateMsg ----CreateMsgIn ------Create --------Items -----------Description -----------Quantity -----------Price --------Username ----CreateMsgOut
... etc
I've been experimenting with using a Dictionary <string, TreeNode> to hold the information on the tree structure. Key = child-node-name(string) Value = parent-node (treenode)
So then when adding a treenode, it's something like
// create new node TreeNode newNode = new TreeNode(childNodeName); // get parent node TreeNode prevNode = dictionary[parentNodeName]; // add child to parent node within tree prevNode.Nodes.Add(newNode);
I'm pretty stressed that I've wasted a lot of time due to my inexperience here. Thankfully it isn't a high priority at the company? Either way, I've certainly come to appreciate taking more time for designing and planning ahead of time before coding.
(This post was more made to clarify what I'm working on, than to specifically ask questions. Feel free to comment on whatever anyways)
|
Actually, operator overloading, used correctly, is more obvious and less prone to errors than .plus() shenanigans, due to having a well-defined order of precedence without resorting to masses of parentheses. Still, as many (most?) advanced features of C++, it can be horribly, horribly misused as well.
A couple of really hairy combination is a) the combination of multiple inheritance and operator overloading and b) the combination of dynamic typing and operator overloading. Having well-defined types of your variables is absolutely crucial to proper usage of operator overloading.
As for the current problem, the second variant would seem more reasonable, but keep in mind that users may have selected x += y as opposed to x = x + y precisely because they want to change x the object, not x the reference. See [section 13.9 here for example.
It mostly comes down to: comment your damn code, and use const liberally to demonstrate which values change and which do not.
|
On September 05 2011 22:33 Morfildur wrote: This is one of the reasons why java removed operator overloading completely (I don't agree with that decision, but it wasn't totally unreasonable). Yeah, there are a few heavy abuses of operator overloading but that's no reason to forbid them completely. They are vital to numerical calculations (just as custom value types but that's another story...). They have well defined precedence and can greatly simplify expressions and make them much more transparent. Even if they don't allow full overloading, operators could be aliases for member functions, for example x + y could simply call x.add(y). I believe there is a Java precompiler that does such.
A method can be just as bad as an operator. Undocumented, in violation of contract, malicious. The common reasoning against operators can be applied to methods as well: an add() method might not have anything to do with addition, it could call launchNuclearMissile() just as easily as an operator. They should be treated like methods, with the same scrutiny applied to them. Complete with firing the programmer who abuses operators 
If you use operator overloading or use classes that do it, always read the documentation (or in case of the usual lack of it, the code). Otherwise there is no way to really know what will happen. Avoid it as much as possible unless the benefits are far greater than the possible pitfalls for this specific scenario.
Yup, that's the problem here. I do not have a definite class to just look at how it implements operators. Both binary and compound assignment operators might return T, T*, T&, const T& or even a completely unrelated type. The best approach in my case would be to limit the return types to a few correct cases, and use partial template specialization to override this best-guess behavior when it can't model a case properly.
|
today I run into a very strange bug with a java midlet app. I have a base class A and two derived class of A, say B and C.
the problem was with constructor.
A(int n) { foo = new Plop[n] }
B() { super(10); initFoos(); }
C() { super(12); initFoos(); }
---- Now the thing is when I would call initFoos for C, it would crash with out of bound after 10 elements on one device. And work properly on another device... Just wondering if someone really experienced have deeper knowledge of possible ambiguities. I have 10 years of work in the field, so I know better not to blame anything else than my code (in this case one of my workers.. ). We did a work around but I'm not happy about that.
|
From the sound of it, C.initFoos() does not give a flying fuck about foo.length (or even modifies foo).
Give us more code, it is difficult to tell the problem from a few lines.
|
Hey EscPlan9,
Seems like you need parse an XML file (in your case a WSDL) and get relevant information from it.
I dont really understand what WIP is but I am assuming its your front end (GUI) which has some type of tree control which can parse an object.
Here is my plan on how I would tackle your situation: http://schemas.xmlsoap.org/wsdl/ contains the XSD for any WSDL. Save your xsd
Use visual studio tools to create a class out of the XSD as follows: xsd.exe [fileName].xsd /c <--cmd line tool (this creates a C# class)
The xsd tool complained about "...", I needed to remove the "..." from the xsd to create the C# class
Add this class to your project.
Write this method as follows: public object XmlToObject(Type type, string fileName) { XmlSerializer ser = new XmlSerializer(type); FileStream myFileStream = new FileStream(fileName, FileMode.Open); object obj = ser.Deserialize(myFileStream); return obj; }
Now you have your XML(WSDL) as a strong typed object instead of XML. You wont need to parse XML and make references to any xml methods.
You need to convert this object into a Model that you want to send out. I would create a model class and just grab the information you need from the deserialized object.
Have another methods that would create your model(deserialized object) with your tree structure which you would send to your front end.
PM me if this is confusing in any way or I didn't understand you correctly.
|
I've got a dilemma about a design choice in my pointer / reference wrapper, the insight of an outsider would be welcome.
Should I proxy operator == and != to the underlying class, just as I do in the case of other operators, or should I use them for pointer comparison instead? Or perhaps both, the latter for pointers, references, rvalue references, reference wrappers, and the former for everything else? Though it sounds kinda dangerous to have two different semantics for a single operator.
I already have an "is" member function for pointer comparison, but it is kinda ugly. For example
ref<int> x = 1; ref<int> xcopy = x; ref<int> y = 2; cout << x.is(xcopy) << endl; cout << x.is(y) << endl; cout << x.is(null) << endl;
Perhaps I could define an "is" macro with some trick so the following is possible
cout << x is xcopy << endl; cout << x is y << endl; cout << x is null << endl;
For reference, operator = is NOT proxied to the underlying class, it just copies the address where it can. If the user wants assignment he uses
ref<SomeClass> x = new SomeClass(...); ref<SomeClass> y = new SomeClass(...); x.get() = y.get();
|
Challenge: Write a function A in your language of choice that takes an object argument O and adds a new function B to O defined as follows: given a function argument C, B sets every other function D in O (exclude B) in the object equal to the given function C applied to the original function D (the return value of C(D) is a function, of course).
Solution (javascript): + Show Spoiler + A = function(O) { var modified = new Array(); for (var D in O) { if (!Object.isFunction(O[D])) continue; modified.push(D); }; O.B = function(C) { modified.each(function(D){ O[D] = C(O[D]); }); return O; }; return O; }
I wrote something like this today as part of a functional modifier stack. So you declare A(O) to make an O with extensible methods. You can repeatedly stack functions by repeatedly calling O.B(C) to convert every D into C(D), C(C(D)), C(C(C(D))).... Here is a video of what a modifier stack looks like in Blender to get an idea of how useful this can be.
|
So you are essentially doing an O.D ∘ C composition for every D method of O, where C is a function.
That is not possible in C++ due to lack of type inspection. Even with a wrapper class like mine, you can not proxy methods with arbitrary name, so composition of all methods is not possible. Especially not dynamically.
But I believe you can make a class template A that in its constructor accepts o and c objects (of which are instances of template parameters O and C, an object and a functor respectively), and proxies all operators and a given set of methods of O that are known beforehand. Still no dynamically though.
Edit:
Yup, it is possible statically for a limited set of methods and operators, see this example for operator ():
#include <iostream> #include <functional> #include <utility>
using namespace std;
template <class O, class C> class A {
public:
A (O& o2, C& c2) : o (o2), c (c2) {}
template <class... Args> decltype(std::declval<O&&>()(std::declval<C&&>()(std::declval<Args&&>()...))) operator () (Args&&... args) { return o(c(std::forward<Args&&>(args)...)); }
protected:
O& o; C& c;
};
class O {
public:
int operator () (int x) { return 2 * x; }
};
class C {
public:
int operator () (int x) { return x + 1; }
};
int main (int argc, char* argv[]) { O o; C c; A<O, C> a(o, c);
cout << a(1) << endl; cout << a(10) << endl; }
|
Impressive, Frigo. I was hoping someone would post a C/C++ solution.
|
i would like to thank the people who helped my on my last problem, yes it was working properly, my bad D:
i have another problem :< im stuck and i hope you guys can help it compiles correctly but gives an error when i use it
> myprod n m = if m == 0 then 0 else (myprod n (m - 1)) + n
> myprod2 n m = if n == 1 then m else if n mod 2 == 1 then (myprod (n div 2) (m + m)) + m else (myprod (n div 2) (m + m))
this is a multiplication function. myprod is my old fcn, its very slow so i tried improving it by using binary multiplication.
myprod2 is what i have come up but it returns an error even though it compiles properly, the error is
no instance for (num ((a0->a0->a0->a10->a20)) arising from the literal '5' possible fix: add an instance declaration for (num ((a0->a0->a0->a10->a20))
and my problem is i dont know how to fix it =/
|
On September 08 2011 21:57 icystorage wrote:i would like to thank the people who helped my on my last problem, yes it was working properly, my bad D: i have another problem :< im stuck and i hope you guys can help it compiles correctly but gives an error when i use it > myprod n m = if m == 0 then 0 else (myprod n (m - 1)) + n
> myprod2 n m = if n == 1 then m else if n mod 2 == 1 then (myprod (n div 2) (m + m)) + m else (myprod (n div 2) (m + m))
this is a multiplication function. myprod is my old fcn, its very slow so i tried improving it by using binary multiplication. myprod2 is what i have come up but it returns an error even though it compiles properly, the error is no instance for (num ((a0->a0->a0->a10->a20)) arising from the literal '5' possible fix: add an instance declaration for (num ((a0->a0->a0->a10->a20)) and my problem is i dont know how to fix it =/
That's a compiler error actually. The problem is that when you use standard function names as infix operators, you need to encase them in backquotes, e.g., n `div` 2 is equivalent to div 2 n. Writing 1 div 2 is in effect saying "apply the div function (of type Integral a => a -> a -> a) to the constant 1 (of type Num a => a) and then take the result and apply the constant 2". Clearly 1 is not a function so the whole thing fails to type check.
EDIT: because of typeclasses, Haskell can give 1 div 2 a type
1 div 2 :: (Num a1, Num ((a -> a -> a) -> a1 -> t), Integral a) => t
Because it tries to unify the type of 1 with a function type that can take the div function as an argument. However, it fails when the typechecker tries to find an instantiation of the type variable a that satisfies the constraint. Hence the nasty compiler error that you see.
|
@Frigo I'm working with java so there is this kind of design i.e. == compares ref and .equals(x) method compares content.
What I don't like with this design is that I usually use object comparison more often than ref comparison. And I feel like the equals method clutters my code.
It's just about taste but if I were you I would use == to compare the referenced object and another method to compare reference. That's just my feelings using == and .equals()
|
w3schools should not be included in the OP; read this for more info.
|
On September 08 2011 23:53 FranzP wrote:@Frigo I'm working with java so there is this kind of design i.e. == compares ref and .equals(x) method compares content. What I don't like with this design is that I usually use object comparison more often than ref comparison. And I feel like the equals method clutters my code. It's just about taste but if I were you I would use == to compare the referenced object and another method to compare reference. That's just my feelings using == and .equals() 
.equals() also adds some problems to the language like x.equals(y); that throws an exception if x is null, == doesn't have that problem. That leads to wierd stuff like "somestring".equals(x); which is... not quite logical or intuitive.
|
On September 09 2011 00:23 Morfildur wrote:Show nested quote +On September 08 2011 23:53 FranzP wrote:@Frigo I'm working with java so there is this kind of design i.e. == compares ref and .equals(x) method compares content. What I don't like with this design is that I usually use object comparison more often than ref comparison. And I feel like the equals method clutters my code. It's just about taste but if I were you I would use == to compare the referenced object and another method to compare reference. That's just my feelings using == and .equals()  .equals() also adds some problems to the language like x.equals(y); that throws an exception if x is null, == doesn't have that problem. That leads to wierd stuff like "somestring".equals(x); which is... not quite logical or intuitive.
.equals itself is a nasty corner of the language due to the difference between primitive (value) and reference types. However, being able to call methods on any object, i.e., "hello".equals(x), should be expected in an object-oriented language where "everything is an object". It ought to be considered weird that you aren't able to call methods on primitive data (in Java at least, C# automatically boxes primitives so that you can do this --- at the cost of the actual boxing operation).
|
On September 08 2011 23:39 Kambing wrote:Show nested quote +On September 08 2011 21:57 icystorage wrote:i would like to thank the people who helped my on my last problem, yes it was working properly, my bad D: i have another problem :< im stuck and i hope you guys can help it compiles correctly but gives an error when i use it > myprod n m = if m == 0 then 0 else (myprod n (m - 1)) + n
> myprod2 n m = if n == 1 then m else if n mod 2 == 1 then (myprod (n div 2) (m + m)) + m else (myprod (n div 2) (m + m))
this is a multiplication function. myprod is my old fcn, its very slow so i tried improving it by using binary multiplication. myprod2 is what i have come up but it returns an error even though it compiles properly, the error is no instance for (num ((a0->a0->a0->a10->a20)) arising from the literal '5' possible fix: add an instance declaration for (num ((a0->a0->a0->a10->a20)) and my problem is i dont know how to fix it =/ That's a compiler error actually. The problem is that when you use standard function names as infix operators, you need to encase them in backquotes, e.g., n `div` 2 is equivalent to div 2 n. Writing 1 div 2 is in effect saying "apply the div function (of type Integral a => a -> a -> a) to the constant 1 (of type Num a => a) and then take the result and apply the constant 2". Clearly 1 is not a function so the whole thing fails to type check. EDIT: because of typeclasses, Haskell can give 1 div 2 a type 1 div 2 :: (Num a1, Num ((a -> a -> a) -> a1 -> t), Integral a) => t
Because it tries to unify the type of 1 with a function type that can take the div function as an argument. However, it fails when the typechecker tries to find an instantiation of the type variable a that satisfies the constraint. Hence the nasty compiler error that you see. *holds hand* thank you my friend... thank you... how could i be so stupid ._.
|
On September 08 2011 23:53 FranzP wrote:@Frigo I'm working with java so there is this kind of design i.e. == compares ref and .equals(x) method compares content. What I don't like with this design is that I usually use object comparison more often than ref comparison. And I feel like the equals method clutters my code. It's just about taste but if I were you I would use == to compare the referenced object and another method to compare reference. That's just my feelings using == and .equals() 
Thank you for your feedback. I asked my friend as well and concluded that indeed this is the correct approach.
Object comparison is done with == and !=, pointer comparison is done with an "is" macro that is defined as:
#define is .getPointer() ==
It seems to be working pretty well, it handles pointer comparison between reference wrappers as well, due to implicit conversion to pointer.
|
|
|
|