-
Notifications
You must be signed in to change notification settings - Fork 333
/
dispatchkit.hpp
1234 lines (1001 loc) · 36.2 KB
/
dispatchkit.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is distributed under the BSD License.
// See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com
#ifndef CHAISCRIPT_DISPATCHKIT_HPP_
#define CHAISCRIPT_DISPATCHKIT_HPP_
#include <typeinfo>
#include <string>
#include <map>
#include <set>
#include <stdexcept>
#include <vector>
#include <iostream>
#include <deque>
#include <list>
#include <algorithm>
#include "boxed_value.hpp"
#include "type_info.hpp"
#include "proxy_functions.hpp"
#include "proxy_constructors.hpp"
#include "dynamic_object.hpp"
#include "../chaiscript_threading.hpp"
/// \namespace chaiscript::dispatch
/// \brief Classes and functions specific to the runtime dispatch side of ChaiScript. Some items may be of use to the end user.
namespace chaiscript
{
namespace exception
{
/**
* Exception thrown in the case that an object name is invalid because it is a reserved word
*/
class reserved_word_error : public std::runtime_error
{
public:
reserved_word_error(const std::string &t_word) CHAISCRIPT_NOEXCEPT
: std::runtime_error("Reserved word not allowed in object name: " + t_word), m_word(t_word)
{
}
virtual ~reserved_word_error() CHAISCRIPT_NOEXCEPT {}
std::string word() const
{
return m_word;
}
private:
std::string m_word;
};
/**
* Exception thrown in the case that an object name is invalid because it contains illegal characters
*/
class illegal_name_error : public std::runtime_error
{
public:
illegal_name_error(const std::string &t_name) throw()
: std::runtime_error("Reserved name not allowed in object name: " + t_name), m_name(t_name)
{
}
virtual ~illegal_name_error() throw() {}
std::string name() const
{
return m_name;
}
private:
std::string m_name;
};
/**
* Exception thrown in the case that an object name is invalid because it already exists in current context
*/
class name_conflict_error : public std::runtime_error
{
public:
name_conflict_error(const std::string &t_name) throw()
: std::runtime_error("Name already exists in current context " + t_name), m_name(t_name)
{
}
virtual ~name_conflict_error() throw() {}
std::string name() const
{
return m_name;
}
private:
std::string m_name;
};
/**
* Exception thrown in the case that a non-const object was added as a shared object
*/
class global_non_const : public std::runtime_error
{
public:
global_non_const() CHAISCRIPT_NOEXCEPT
: std::runtime_error("a global object must be const")
{
}
virtual ~global_non_const() CHAISCRIPT_NOEXCEPT {}
};
}
/// \brief Holds a collection of ChaiScript settings which can be applied to the ChaiScript runtime.
/// Used to implement loadable module support.
class Module
{
public:
Module &add(const Type_Info &ti, const std::string &name)
{
m_typeinfos.push_back(std::make_pair(ti, name));
return *this;
}
Module &add(const Dynamic_Cast_Conversion &d)
{
m_conversions.push_back(d);
return *this;
}
Module &add(const Proxy_Function &f, const std::string &name)
{
m_funcs.push_back(std::make_pair(f, name));
return *this;
}
Module &add_global_const(const Boxed_Value &t_bv, const std::string &t_name)
{
if (!t_bv.is_const())
{
throw chaiscript::exception::global_non_const();
}
m_globals.push_back(std::make_pair(t_bv, t_name));
return *this;
}
//Add a bit of chaiscript to eval during module implementation
Module &eval(const std::string &str)
{
m_evals.push_back(str);
return *this;
}
Module &add(const std::shared_ptr<Module> &m)
{
m->apply(*this, *this);
return *m;
}
template<typename Eval, typename Engine>
void apply(Eval &t_eval, Engine &t_engine) const
{
apply(m_typeinfos.begin(), m_typeinfos.end(), t_engine);
apply(m_funcs.begin(), m_funcs.end(), t_engine);
apply_eval(m_evals.begin(), m_evals.end(), t_eval);
apply_single(m_conversions.begin(), m_conversions.end(), t_engine);
apply_globals(m_globals.begin(), m_globals.end(), t_engine);
}
~Module()
{
}
private:
std::vector<std::pair<Type_Info, std::string> > m_typeinfos;
std::vector<std::pair<Proxy_Function, std::string> > m_funcs;
std::vector<std::pair<Boxed_Value, std::string> > m_globals;
std::vector<std::string> m_evals;
std::vector<Dynamic_Cast_Conversion> m_conversions;
template<typename T, typename InItr>
void apply(InItr begin, InItr end, T &t) const
{
while (begin != end)
{
try {
t.add(begin->first, begin->second);
} catch (const chaiscript::exception::name_conflict_error &) {
/// \todo Should we throw an error if there's a name conflict
/// while applying a module?
}
++begin;
}
}
template<typename T, typename InItr>
void apply_globals(InItr begin, InItr end, T &t) const
{
while (begin != end)
{
t.add_global_const(begin->first, begin->second);
++begin;
}
}
template<typename T, typename InItr>
void apply_single(InItr begin, InItr end, T &t) const
{
while (begin != end)
{
t.add(*begin);
++begin;
}
}
template<typename T, typename InItr>
void apply_eval(InItr begin, InItr end, T &t) const
{
while (begin != end)
{
t.eval(*begin);
++begin;
}
}
};
/// Convenience typedef for Module objects to be added to the ChaiScript runtime
typedef std::shared_ptr<Module> ModulePtr;
namespace detail
{
/**
* A Proxy_Function implementation that is able to take
* a vector of Proxy_Functions and perform a dispatch on them. It is
* used specifically in the case of dealing with Function object variables
*/
class Dispatch_Function : public dispatch::Proxy_Function_Base
{
public:
Dispatch_Function(const std::vector<Proxy_Function> &t_funcs)
: Proxy_Function_Base(build_type_infos(t_funcs)),
m_funcs(t_funcs)
{
}
virtual bool operator==(const dispatch::Proxy_Function_Base &rhs) const CHAISCRIPT_OVERRIDE
{
try {
const Dispatch_Function &dispatchfun = dynamic_cast<const Dispatch_Function &>(rhs);
return m_funcs == dispatchfun.m_funcs;
} catch (const std::bad_cast &) {
return false;
}
}
virtual ~Dispatch_Function() {}
virtual std::vector<Const_Proxy_Function> get_contained_functions() const CHAISCRIPT_OVERRIDE
{
return std::vector<Const_Proxy_Function>(m_funcs.begin(), m_funcs.end());
}
virtual int get_arity() const CHAISCRIPT_OVERRIDE
{
typedef std::vector<Proxy_Function> function_vec;
auto begin = m_funcs.begin();
const function_vec::const_iterator end = m_funcs.end();
if (begin != end)
{
int arity = (*begin)->get_arity();
++begin;
while (begin != end)
{
if (arity != (*begin)->get_arity())
{
// The arities in the list do not match, so it's unspecified
return -1;
}
++begin;
}
return arity;
}
return -1; // unknown arity
}
virtual bool call_match(const std::vector<Boxed_Value> &vals, const Dynamic_Cast_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
{
auto begin = m_funcs.begin();
auto end = m_funcs.end();
while (begin != end)
{
if ((*begin)->call_match(vals, t_conversions))
{
return true;
} else {
++begin;
}
}
return false;
}
virtual std::string annotation() const CHAISCRIPT_OVERRIDE
{
return "Multiple method dispatch function wrapper.";
}
protected:
virtual Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Dynamic_Cast_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
{
return dispatch::dispatch(m_funcs.begin(), m_funcs.end(), params, t_conversions);
}
private:
std::vector<Proxy_Function> m_funcs;
static std::vector<Type_Info> build_type_infos(const std::vector<Proxy_Function> &t_funcs)
{
typedef std::vector<Proxy_Function> function_vec;
auto begin = t_funcs.begin();
const function_vec::const_iterator end = t_funcs.end();
if (begin != end)
{
std::vector<Type_Info> type_infos = (*begin)->get_param_types();
++begin;
bool sizemismatch = false;
while (begin != end)
{
std::vector<Type_Info> param_types = (*begin)->get_param_types();
if (param_types.size() != type_infos.size())
{
sizemismatch = true;
}
for (size_t i = 0; i < type_infos.size() && i < param_types.size(); ++i)
{
if (!(type_infos[i] == param_types[i]))
{
type_infos[i] = detail::Get_Type_Info<Boxed_Value>::get();
}
}
++begin;
}
assert(type_infos.size() > 0 && " type_info vector size is < 0, this is only possible if something else is broken");
if (sizemismatch)
{
type_infos.resize(1);
}
return type_infos;
}
return std::vector<Type_Info>();
}
};
}
namespace detail
{
/**
* Main class for the dispatchkit. Handles management
* of the object stack, functions and registered types.
*/
class Dispatch_Engine
{
public:
typedef std::map<std::string, chaiscript::Type_Info> Type_Name_Map;
typedef std::map<std::string, Boxed_Value> Scope;
typedef std::deque<Scope> StackData;
typedef std::shared_ptr<StackData> Stack;
struct State
{
std::map<std::string, std::vector<Proxy_Function> > m_functions;
std::map<std::string, Proxy_Function> m_function_objects;
std::map<std::string, Boxed_Value> m_global_objects;
Type_Name_Map m_types;
std::set<std::string> m_reserved_words;
State &operator=(const State &) = default;
};
Dispatch_Engine()
: m_stack_holder(this),
m_place_holder(std::shared_ptr<dispatch::Placeholder_Object>(new dispatch::Placeholder_Object()))
{
}
~Dispatch_Engine()
{
}
/// \brief casts an object while applying any Dynamic_Conversion available
template<typename Type>
typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv) const
{
return chaiscript::boxed_cast<Type>(bv, &m_conversions);
}
/**
* Add a new conversion for upcasting to a base class
*/
void add(const Dynamic_Cast_Conversion &d)
{
m_conversions.add_conversion(d);
}
/**
* Add a new named Proxy_Function to the system
*/
void add(const Proxy_Function &f, const std::string &name)
{
validate_object_name(name);
add_function(f, name);
}
/**
* Set the value of an object, by name. If the object
* is not available in the current scope it is created
*/
void add(const Boxed_Value &obj, const std::string &name)
{
validate_object_name(name);
StackData &stack = get_stack_data();
for (int i = static_cast<int>(stack.size())-1; i >= 0; --i)
{
std::map<std::string, Boxed_Value>::const_iterator itr = stack[i].find(name);
if (itr != stack[i].end())
{
stack[i][name] = obj;
return;
}
}
add_object(name, obj);
}
/**
* Adds a named object to the current scope
*/
void add_object(const std::string &name, const Boxed_Value &obj)
{
StackData &stack = get_stack_data();
validate_object_name(name);
Scope &scope = stack.back();
auto itr = scope.find(name);
if (itr != stack.back().end())
{
throw chaiscript::exception::name_conflict_error(name);
} else {
stack.back().insert(std::make_pair(name, obj));
}
}
/**
* Adds a new global shared object, between all the threads
*/
void add_global_const(const Boxed_Value &obj, const std::string &name)
{
validate_object_name(name);
if (!obj.is_const())
{
throw chaiscript::exception::global_non_const();
}
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_global_object_mutex);
if (m_state.m_global_objects.find(name) != m_state.m_global_objects.end())
{
throw chaiscript::exception::name_conflict_error(name);
} else {
m_state.m_global_objects.insert(std::make_pair(name, obj));
}
}
/**
* Adds a new global (non-const) shared object, between all the threads
*/
void add_global(const Boxed_Value &obj, const std::string &name)
{
validate_object_name(name);
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_global_object_mutex);
if (m_state.m_global_objects.find(name) != m_state.m_global_objects.end())
{
throw chaiscript::exception::name_conflict_error(name);
} else {
m_state.m_global_objects.insert(std::make_pair(name, obj));
}
}
/**
* Adds a new scope to the stack
*/
void new_scope()
{
StackData &stack = get_stack_data();
stack.push_back(Scope());
}
/**
* Pops the current scope from the stack
*/
void pop_scope()
{
StackData &stack = get_stack_data();
if (stack.size() > 1)
{
stack.pop_back();
} else {
throw std::range_error("Unable to pop global stack");
}
}
/// Pushes a new stack on to the list of stacks
void new_stack()
{
Stack s(new Stack::element_type());
s->push_back(Scope());
m_stack_holder->stacks.push_back(s);
}
void pop_stack()
{
m_stack_holder->stacks.pop_back();
}
/// \returns the current stack
Stack get_stack() const
{
return m_stack_holder->stacks.back();
}
/**
* Searches the current stack for an object of the given name
* includes a special overload for the _ place holder object to
* ensure that it is always in scope.
*/
Boxed_Value get_object(const std::string &name) const
{
// Is it a placeholder object?
if (name == "_")
{
return m_place_holder;
}
StackData &stack = get_stack_data();
// Is it in the stack?
for (int i = static_cast<int>(stack.size())-1; i >= 0; --i)
{
std::map<std::string, Boxed_Value>::const_iterator stackitr = stack[i].find(name);
if (stackitr != stack[i].end())
{
return stackitr->second;
}
}
// Is the value we are looking for a global?
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_global_object_mutex);
auto itr = m_state.m_global_objects.find(name);
if (itr != m_state.m_global_objects.end())
{
return itr->second;
}
}
// If all that failed, then check to see if it's a function
return get_function_object(name);
}
/**
* Registers a new named type
*/
void add(const Type_Info &ti, const std::string &name)
{
add_global_const(const_var(ti), name + "_type");
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
m_state.m_types.insert(std::make_pair(name, ti));
}
/**
* Returns the type info for a named type
*/
Type_Info get_type(const std::string &name) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
auto itr = m_state.m_types.find(name);
if (itr != m_state.m_types.end())
{
return itr->second;
}
throw std::range_error("Type Not Known");
}
/**
* Returns the registered name of a known type_info object
* compares the "bare_type_info" for the broadest possible
* match
*/
std::string get_type_name(const Type_Info &ti) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
for (const auto & elem : m_state.m_types)
{
if (elem.second.bare_equal(ti))
{
return elem.first;
}
}
return ti.bare_name();
}
/**
* Return all registered types
*/
std::vector<std::pair<std::string, Type_Info> > get_types() const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
return std::vector<std::pair<std::string, Type_Info> >(m_state.m_types.begin(), m_state.m_types.end());
}
/**
* Return a function by name
*/
std::vector< Proxy_Function >
get_function(const std::string &t_name) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
const std::map<std::string, std::vector<Proxy_Function> > &funs = get_functions_int();
auto itr
= funs.find(t_name);
if (itr != funs.end())
{
return itr->second;
} else {
return std::vector<Proxy_Function>();
}
}
/// \returns a function object (Boxed_Value wrapper) if it exists
/// \throws std::range_error if it does not
Boxed_Value get_function_object(const std::string &t_name) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
const std::map<std::string, Proxy_Function> &funs = get_function_objects_int();
auto itr = funs.find(t_name);
if (itr != funs.end())
{
return const_var(itr->second);
} else {
throw std::range_error("Object not found: " + t_name);
}
}
/**
* Return true if a function exists
*/
bool function_exists(const std::string &name) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
const std::map<std::string, std::vector<Proxy_Function> > &functions = get_functions_int();
return functions.find(name) != functions.end();
}
/// \returns All values in the local thread state, added through the add() function
std::map<std::string, Boxed_Value> get_locals() const
{
StackData &stack = get_stack_data();
Scope &scope = stack.front();
return scope;
}
/// \brief Sets all of the locals for the current thread state.
///
/// \param[in] t_locals The map<name, value> set of variables to replace the current state with
///
/// Any existing locals are removed and the given set of variables is added
void set_locals(const std::map<std::string, Boxed_Value> &t_locals)
{
StackData &stack = get_stack_data();
Scope &scope = stack.front();
scope = t_locals;
}
///
/// Get a map of all objects that can be seen from the current scope in a scripting context
///
std::map<std::string, Boxed_Value> get_scripting_objects() const
{
Stack_Holder &s = *m_stack_holder;
// We don't want the current context, but one up if it exists
StackData &stack = (s.stacks.size()==1)?(*(s.stacks.back())):(*s.stacks[s.stacks.size()-2]);
std::map<std::string, Boxed_Value> retval;
// note: map insert doesn't overwrite existing values, which is why this works
for (auto itr = stack.rbegin(); itr != stack.rend(); ++itr)
{
retval.insert(itr->begin(), itr->end());
}
// add the global values
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_global_object_mutex);
retval.insert(m_state.m_global_objects.begin(), m_state.m_global_objects.end());
}
return retval;
}
///
/// Get a map of all functions that can be seen from a scripting context
///
std::map<std::string, Boxed_Value> get_function_objects() const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
const std::map<std::string, Proxy_Function> &funs = get_function_objects_int();
std::map<std::string, Boxed_Value> objs;
for (const auto & fun : funs)
{
objs.insert(std::make_pair(fun.first, const_var(fun.second)));
}
return objs;
}
/**
* Get a vector of all registered functions
*/
std::vector<std::pair<std::string, Proxy_Function > > get_functions() const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
std::vector<std::pair<std::string, Proxy_Function> > rets;
const std::map<std::string, std::vector<Proxy_Function> > &functions = get_functions_int();
for (const auto & function : functions)
{
for (const auto & internal_func : function.second)
{
rets.push_back(std::make_pair(function.first, internal_func));
}
}
return rets;
}
void add_reserved_word(const std::string &name)
{
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
m_state.m_reserved_words.insert(name);
}
const Dynamic_Cast_Conversions &conversions() const
{
return m_conversions;
}
Boxed_Value call_function(const std::string &t_name, const std::vector<Boxed_Value> ¶ms) const
{
std::vector<Proxy_Function> functions = get_function(t_name);
return dispatch::dispatch(functions.begin(), functions.end(), params, m_conversions);
}
Boxed_Value call_function(const std::string &t_name) const
{
return call_function(t_name, std::vector<Boxed_Value>());
}
Boxed_Value call_function(const std::string &t_name, const Boxed_Value &p1) const
{
std::vector<Boxed_Value> params;
params.push_back(p1);
return call_function(t_name, params);
}
Boxed_Value call_function(const std::string &t_name, const Boxed_Value &p1, const Boxed_Value &p2) const
{
std::vector<Boxed_Value> params;
params.push_back(p1);
params.push_back(p2);
return call_function(t_name, params);
}
/**
* Dump object info to stdout
*/
void dump_object(const Boxed_Value &o) const
{
std::cout << (o.is_const()?"const ":"") << type_name(o) << std::endl;
}
/**
* Dump type info to stdout
*/
void dump_type(const Type_Info &type) const
{
std::cout << (type.is_const()?"const ":"") << get_type_name(type);
}
/**
* Dump function to stdout
*/
void dump_function(const std::pair<const std::string, Proxy_Function > &f) const
{
std::vector<Type_Info> params = f.second->get_param_types();
std::string annotation = f.second->annotation();
if (annotation.size() > 0) {
std::cout << annotation;
}
dump_type(params.front());
std::cout << " " << f.first << "(";
for (std::vector<Type_Info>::const_iterator itr = params.begin() + 1;
itr != params.end();
)
{
dump_type(*itr);
++itr;
if (itr != params.end())
{
std::cout << ", ";
}
}
std::cout << ") " << std::endl;
}
/**
* Returns true if a call can be made that consists of the first parameter
* (the function) with the remaining parameters as its arguments.
*/
Boxed_Value call_exists(const std::vector<Boxed_Value> ¶ms)
{
if (params.size() < 1)
{
throw chaiscript::exception::arity_error(static_cast<int>(params.size()), 1);
}
Const_Proxy_Function f = this->boxed_cast<Const_Proxy_Function>(params[0]);
return Boxed_Value(f->call_match(std::vector<Boxed_Value>(params.begin() + 1, params.end()), m_conversions));
}
/**
* Dump all system info to stdout
*/
void dump_system() const
{
std::cout << "Registered Types: " << std::endl;
std::vector<std::pair<std::string, Type_Info> > types = get_types();
for (std::vector<std::pair<std::string, Type_Info> >::const_iterator itr = types.begin();
itr != types.end();
++itr)
{
std::cout << itr->first << ": ";
std::cout << itr->second.bare_name();
std::cout << std::endl;
}
std::cout << std::endl;
std::vector<std::pair<std::string, Proxy_Function > > funcs = get_functions();
std::cout << "Functions: " << std::endl;
for (std::vector<std::pair<std::string, Proxy_Function > >::const_iterator itr = funcs.begin();
itr != funcs.end();
++itr)
{
dump_function(*itr);
}
std::cout << std::endl;
}
/**
* return true if the Boxed_Value matches the registered type by name
*/
bool is_type(const Boxed_Value &r, const std::string &user_typename) const
{
try {
if (get_type(user_typename).bare_equal(r.get_type_info()))
{
return true;
}
} catch (const std::range_error &) {
}
try {
const dispatch::Dynamic_Object &d = boxed_cast<const dispatch::Dynamic_Object &>(r);
return d.get_type_name() == user_typename;
} catch (const std::bad_cast &) {
}
return false;
}
std::string type_name(const Boxed_Value &obj) const
{
return get_type_name(obj.get_type_info());
}
State get_state()
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l2(m_global_object_mutex);
return m_state;
}
void set_state(const State &t_state)
{
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l2(m_global_object_mutex);
m_state = t_state;
}
void save_function_params(const std::vector<Boxed_Value> &t_params)
{
Stack_Holder &s = *m_stack_holder;
s.call_params.insert(s.call_params.begin(), t_params.begin(), t_params.end());
}
void new_function_call()
{
++m_stack_holder->call_depth;
}
void pop_function_call()
{
Stack_Holder &s = *m_stack_holder;
--s.call_depth;