Created
May 16, 2015 06:14
-
-
Save SilverIce/90c124aac6df17e5ef4c to your computer and use it in GitHub Desktop.
some crazy stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Node { | |
}; | |
template<class T> | |
struct ReadOnly { | |
T* _val; | |
friend class Writeable<T>; | |
public: | |
//ReadOnly(T& v) : _val(&v) {} | |
//ReadOnly(Writeable<T>& v) : _val(v.readonly()) {} | |
const T& value() const { assert(_val); return *_val; } | |
Writeable<T> writeable() { | |
Writeable<T> v = { _val }; | |
_val = nullptr; | |
return v; | |
} | |
}; | |
template<class T> | |
struct Writeable { | |
T* _val; | |
friend class ReadOnly<T>; | |
public: | |
// Can't be constructed from 'const T&' !!! | |
Writeable(T& v) : _val(v) {} | |
//Writeable(ReadOnly<T>& v) : _val(v.writeable()) {} | |
T& value() const { return *_val; } | |
ReadOnly<T> readonly() { | |
ReadOnly<T> v = { _val }; | |
_val = nullptr; | |
return v; | |
} | |
}; | |
void doConst(ReadOnly<Node> n) {} | |
void doMut(Writeable<Node> n) {} | |
static Writeable<Node> findMatchingNode(Writeable<Node> root) { | |
{ | |
doConst(root.readonly()); | |
} | |
doMut(root); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment