Skip to content

Commit df7ed54

Browse files
committed
add stringRepresentation() method to Object
1 parent 4a2f9f1 commit df7ed54

8 files changed

Lines changed: 46 additions & 1 deletion

File tree

EidosScribe/EidosHelpClasses.rtf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@
9090
\pard\pardeftab397\li547\ri720\sb60\sa60\partightenfactor0
9191
9292
\f4\fs20 \cf2 Prints the internal property structure of the receiving object; in particular, the element type of the object is printed, followed, on successive lines, by all of the properties supported by the object, their types, and a sample of their values.\
93+
\pard\pardeftab397\li720\fi-446\ri720\sb180\sa60\partightenfactor0
94+
95+
\f3\fs18 \cf2 \'96\'a0(string$)stringRepresentation(void)\
96+
\pard\pardeftab397\li547\ri720\sb60\sa60\partightenfactor0
97+
98+
\f4\fs20 \cf2 Returns a singleton
99+
\f3\fs18 string
100+
\f4\fs20 value that represents the receiving object. By default, this is simply the name of the class of the receiving object; however, many subclasses of
101+
\f3\fs18 Object
102+
\f4\fs20 provide a different string representation. The value returned by
103+
\f3\fs18 stringRepresentation()
104+
\f4\fs20 is the same string that would be printed by
105+
\f3\fs18 print()
106+
\f4\fs20 for the object, so
107+
\f3\fs18 stringRepresentation()
108+
\f4\fs20 allows the same representation to be used in other contexts such as
109+
\f3\fs18 paste()
110+
\f4\fs20 and
111+
\f3\fs18 cat()
112+
\f4\fs20 .\
93113
\pard\pardeftab720\ri720\sb360\sa60\partightenfactor0
94114
95115
\f0\b\fs22 \cf2 5.2 Class DataFrame\

QtSLiM/help/EidosHelpClasses.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<p class="p4">Returns the size of the receiving object.<span class="Apple-converted-space">  </span>This is equivalent to the <span class="s1">size()</span> (or <span class="s1">length()</span>) function; in other words, for any <span class="s1">object</span> <span class="s1">x</span>, the return value of the function call <span class="s1">size(x)</span> equals the return value of the class method call <span class="s1">x.size()</span>.<span class="Apple-converted-space">  </span>This method is provided solely for syntactic convenience.<span class="Apple-converted-space">  </span>Note that <span class="s1">+length()</span> is a synonym for <span class="s1">+size()</span>.</p>
2929
<p class="p3">– (void)str(void)</p>
3030
<p class="p4">Prints the internal property structure of the receiving object; in particular, the element type of the object is printed, followed, on successive lines, by all of the properties supported by the object, their types, and a sample of their values.</p>
31+
<p class="p3">– (string$)stringRepresentation(void)</p>
32+
<p class="p4">Returns a singleton <span class="s1">string</span> value that represents the receiving object.<span class="Apple-converted-space">  </span>By default, this is simply the name of the class of the receiving object; however, many subclasses of <span class="s1">Object</span> provide a different string representation.<span class="Apple-converted-space">  </span>The value returned by <span class="s1">stringRepresentation()</span> is the same string that would be printed by <span class="s1">print()</span> for the object, so <span class="s1">stringRepresentation()</span> allows the same representation to be used in other contexts such as <span class="s1">paste()</span> and <span class="s1">cat()</span>.</p>
3133
<p class="p1"><b>5.2<span class="Apple-converted-space">  </span>Class DataFrame</b></p>
3234
<p class="p3">(object&lt;DataFrame&gt;$)DataFrame(...)</p>
3335
<p class="p4">The <span class="s1">DataFrame</span> constructor can be called in the same ways as the constructor for <span class="s1">Dictionary</span> (its superclass): with no parameters to make an empty <span class="s1">DataFrame</span>, with key-value pairs, with a singleton <span class="s1">Dictionary</span> (or a subclass of <span class="s1">Dictionary</span>, like <span class="s1">DataFrame</span>) to make a copy, or with a singleton string in JSON format.<span class="Apple-converted-space">  </span>See the <span class="s1">Dictionary</span> class for further documentation.</p>

VERSIONS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ development head (in the master branch):
1616
revise recipe 6.1.2 (reading a recombination map from a file) to use readCSV() instead of readFile()
1717
extend the subset() method of DataFrame to accept NULL for rows/cols, to take entire columns or entire rows respectively, for usability
1818
extend readCSV() to allow sep="", meaning that the separator is "whitespace", as in R
19+
add a stringRepresentation() method on Object, to provide the same string representation printed by print()
1920

2021

2122
version 4.0 (Eidos version 3.0):

eidos/eidos_class_Object.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ EidosValue_SP EidosObject::ExecuteInstanceMethod(EidosGlobalStringID p_method_id
116116
#pragma unused(p_arguments, p_interpreter)
117117
switch (p_method_id)
118118
{
119-
case gEidosID_str: return ExecuteMethod_str(p_method_id, p_arguments, p_interpreter);
119+
case gEidosID_str: return ExecuteMethod_str(p_method_id, p_arguments, p_interpreter);
120+
case gEidosID_stringRepresentation: return ExecuteMethod_stringRepresentation(p_method_id, p_arguments, p_interpreter);
120121

121122
default:
122123
{
@@ -234,6 +235,19 @@ EidosValue_SP EidosObject::ExecuteMethod_str(EidosGlobalStringID p_method_id, co
234235
return gStaticEidosValueVOID;
235236
}
236237

238+
// ********************* – (string$)stringRepresentation(void)
239+
//
240+
EidosValue_SP EidosObject::ExecuteMethod_stringRepresentation(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter)
241+
{
242+
#pragma unused (p_method_id, p_arguments, p_interpreter)
243+
244+
std::ostringstream oss;
245+
246+
Print(oss);
247+
248+
return EidosValue_SP(new (gEidosValuePool->AllocateChunk()) EidosValue_String_singleton(oss.str()));
249+
}
250+
237251
EidosValue_SP EidosObject::ContextDefinedFunctionDispatch(const std::string &p_function_name, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter)
238252
{
239253
#pragma unused(p_function_name, p_arguments, p_interpreter)
@@ -520,6 +534,7 @@ const std::vector<EidosMethodSignature_CSP> *EidosClass::Methods(void) const
520534
methods->emplace_back((EidosClassMethodSignature *)(new EidosClassMethodSignature(gEidosStr_size, kEidosValueMaskInt | kEidosValueMaskSingleton)));
521535
methods->emplace_back((EidosClassMethodSignature *)(new EidosClassMethodSignature(gEidosStr_length, kEidosValueMaskInt | kEidosValueMaskSingleton)));
522536
methods->emplace_back((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gEidosStr_str, kEidosValueMaskVOID)));
537+
methods->emplace_back((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gEidosStr_stringRepresentation, kEidosValueMaskString | kEidosValueMaskSingleton)));
523538

524539
std::sort(methods->begin(), methods->end(), CompareEidosCallSignatures);
525540
}

eidos/eidos_class_Object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class EidosObject
7878

7979
virtual EidosValue_SP ExecuteInstanceMethod(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
8080
EidosValue_SP ExecuteMethod_str(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
81+
EidosValue_SP ExecuteMethod_stringRepresentation(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
8182

8283
// EidosContext is a typedef for EidosObject at present, so this class is the superclass of the Context
8384
// object. If that gets complicated we'll probably want to make a new EidosContext class to formalize things,

eidos/eidos_globals.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,6 +2702,7 @@ const std::string &gEidosStr_length = EidosRegisteredString("length", gEidosID_l
27022702
const std::string &gEidosStr_methodSignature = EidosRegisteredString("methodSignature", gEidosID_methodSignature);
27032703
const std::string &gEidosStr_propertySignature = EidosRegisteredString("propertySignature", gEidosID_propertySignature);
27042704
const std::string &gEidosStr_str = EidosRegisteredString("str", gEidosID_str);
2705+
const std::string &gEidosStr_stringRepresentation = EidosRegisteredString("stringRepresentation", gEidosID_stringRepresentation);
27052706

27062707
// strings for EidosTestElement
27072708
const std::string &gEidosStr__TestElement = EidosRegisteredString("_TestElement", gEidosID__TestElement);

eidos/eidos_globals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ extern const std::string &gEidosStr_length;
881881
extern const std::string &gEidosStr_methodSignature;
882882
extern const std::string &gEidosStr_propertySignature;
883883
extern const std::string &gEidosStr_str;
884+
extern const std::string &gEidosStr_stringRepresentation;
884885

885886
extern const std::string &gEidosStr__TestElement;
886887
extern const std::string &gEidosStr__yolk;
@@ -1001,6 +1002,7 @@ enum _EidosGlobalStringID : uint32_t
10011002
gEidosID_methodSignature,
10021003
gEidosID_propertySignature,
10031004
gEidosID_str,
1005+
gEidosID_stringRepresentation,
10041006

10051007
gEidosID__TestElement,
10061008
gEidosID__yolk,

eidos/eidos_test_functions_other.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,9 @@ void _RunClassTests(std::string temp_path)
939939
EidosAssertScriptSuccess_VOID("matrix(_Test(7)).str();");
940940
EidosAssertScriptSuccess_VOID("matrix(c(_Test(7), _Test(8), _Test(9))).str();");
941941

942+
// stringRepresentation()
943+
EidosAssertScriptSuccess_SV("matrix(rep(_Test(7), 3)).stringRepresentation();", {"_TestElement", "_TestElement", "_TestElement"});
944+
EidosAssertScriptSuccess_S("Dictionary('a', 1:3, 'b', 5:6).stringRepresentation();", "{a=1 2 3;b=5 6;}");
942945

943946
// Test EidosDictionaryUnretained properties and methods, using EidosDictionaryRetained
944947
// since there's no way to instantiate an EidosDictionaryUnretained directly

0 commit comments

Comments
 (0)