forked from jeniyat/StackOverflowNER
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPosts_Small.xml
More file actions
4 lines (3 loc) · 5.6 KB
/
Copy pathPosts_Small.xml
File metadata and controls
4 lines (3 loc) · 5.6 KB
1
2
3
4
<row Id="13347179" PostTypeId="1" AcceptedAnswerId="13347433" CreationDate="2012-11-12T16:09:49.260" Score="0" ViewCount="1505" Body="<p>There are many folks out there that claim their singleton implementation to be robust and general because it uses metaprogramming constructs.</p>

<p>My goal is to enforce a singleton policy onto a derived class so that I do not have to explicitly (manually) declare the derived class' constructors as private. I think there's a way to naively add the instance static variable and the getter as a policy by making the templated singleton a friend of the class you derive. But that's not at all elegant.</p>

<p>I started with this code, that, among other things, is given as being a correct (i.e. complete) design of a singleton, while it is clearly allowing for multiple instances:</p>

<pre><code>template &lt;class CWrappedClass&gt;
class CSingleton
{
protected:
 static CWrappedClass* ms_instance;
private:
 CSingleton(){}
 CSingleton(const CSingleton&amp; ) {}
 CSingleton&amp; operator = (const CSingleton&amp;) {}

public:
 static CWrappedClass&amp; GetInstance()
 {
 if (ms_instance == NULL)
 ms_instance = new CWrappedClass;
 return *ms_instance;
 }
};

template &lt;class CWrappedClass&gt;
CWrappedClass* CSingleton&lt;CWrappedClass&gt;::ms_instance = NULL;
</code></pre>

<p>And a singleton client of this "policy", using CRTP:</p>

<pre><code>class CThing : public CSingleton&lt;CThing&gt;
{
 // friend class CSingleton&lt;CThing&gt;; // only if ctor is private!
public:
 void DoNothing()
 {
 std::cout&lt;&lt;" Nothing \n";
 }
 CThing()
 {
 std::cout&lt;&lt;" single ";
 }
};
</code></pre>

<p><strong>NOTE</strong> this is not a correct implementation of a CRTP Singleton policy, it's merely part of the question!</p>

<p>The code <em>won't</em> compile as is. The base singleton policy class has its constructor declared private, so it can't support derived instances unless the child is a friend of this class. People usually make the constructors protected, which means there's nothing to keep a user from making derived class non-singletonian.</p>

<p><strong>Problem/Question</strong>: Is there any mechanism to enforce a singleton policy without having to make the derived class' constructors private manually?</p>
" OwnerUserId="1256811" LastEditorUserId="1256811" LastEditDate="2012-11-12T16:19:32.020" LastActivityDate="2012-11-12T16:25:38.137" Title="Templated Singleton Policy using CRTP in C++" Tags="<c++><templates><design-patterns><singleton>" AnswerCount="1" CommentCount="5" FavoriteCount="1" />
<row Id="13352832" PostTypeId="1" AcceptedAnswerId="13353144" CreationDate="2012-11-12T22:45:07.810" Score="0" ViewCount="796" Body="<p>I've got this as XML:</p>

<pre><code>...
&lt;product&gt;
&lt;id&gt;1&lt;/id&gt;
&lt;defaultImage&gt;test.jpg&lt;/defaultImage&gt;
&lt;/product&gt;
...
</code></pre>

<p>I've got this as php:</p>

<pre><code>$testcase = 'defaultimage';
$xml = simplexml_load_file('./temp/'.$i.'.xml');
foreach ($xml-&gt;children() as $child) {
 $child-&gt;$testcase;
}
</code></pre>

<p>Now the problem is this, I'm forced to have <code>$testcase</code> in a lowercase form (defaultimage) BUT in the XML file the name of the child is: <code>defaultImage</code> (note the uppercase I)</p>

<p>Question: How can I handle all the children as lowercases?</p>
" OwnerUserId="1501285" LastEditorUserId="363262" LastEditDate="2012-11-12T23:29:08.180" LastActivityDate="2012-11-12T23:29:08.180" Title="PHP simplexml children to lowercase" Tags="<php><simplexml><lowercase>" AnswerCount="1" CommentCount="0" />
<row Id="1533" PostTypeId="2" ParentId="1528" CreationDate="2008-08-04T19:19:33.013" Score="16" Body="<p>While you cannot prevent usage of those inherited members to my knowledge, you should be able to hide them from IntelliSense using the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.editorbrowsableattribute.aspx" rel="noreferrer">EditorBrowsableAttribute</a>:</p>

<pre><code>Using System.ComponentModel;

[EditorBrowsable(EditorBrowsableState.Never)]
private string MyHiddenString = "Muahahahahahahahaha";
</code></pre>

<p><em>Edit:</em> Just saw this in the documentation comments, which makes it kinda useless for this purpose:</p>

<blockquote>
 <p>There is a prominent note that states that this attribute "does not suppress members from a class in the same assembly". That is true but not complete. Actually, the attribute does not suppress members from a class in the same solution.</p>
</blockquote>
" OwnerUserId="91" LastEditorUserId="91" LastEditDate="2009-11-19T17:51:07.720" LastActivityDate="2009-11-19T17:51:07.720" CommentCount="0" />