FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpptcl/cpptcl.cc at master · flightaware/cpptcl · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
flightaware
/
cpptcl
Public
Notifications
You must be signed in to change notification settings
Fork
12
Star
58
Code
Issues
1
Pull requests
1
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
cpptcl
/
cpptcl.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
879 lines (680 loc) · 22.7 KB
Breadcrumbs
cpptcl
/
cpptcl.cc
Copy path
File metadata and controls
879 lines (680 loc) · 22.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
//
Copyright (C) 2004-2006, Maciej Sobczak
//
Copyright (C) 2017-2019, FlightAware LLC
//
//
Permission to copy, use, modify, sell and distribute this software
//
is granted provided this copyright notice appears in all copies.
//
This software is provided "as is" without express or implied
//
warranty, and with no claim as to its suitability for any purpose.
//
#
include
<
iterator
>
#
include
<
map
>
#
include
<
memory
>
#
include
<
sstream
>
#
include
"
cpptcl/cpptcl.h
"
using
namespace
Tcl
;
using
namespace
Tcl
::details
;
using
namespace
std
;
result::result
(Tcl_Interp *interp) : interp_(interp) {}
result::
operator
bool
()
const
{
Tcl_Obj *obj =
Tcl_GetObjResult
(interp_);
int
val, cc;
cc =
Tcl_GetBooleanFromObj
(interp_, obj, &val);
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp_);
}
return
static_cast
<
bool
>(val);
}
result::
operator
double
()
const
{
Tcl_Obj *obj =
Tcl_GetObjResult
(interp_);
double
val;
int
cc =
Tcl_GetDoubleFromObj
(interp_, obj, &val);
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp_);
}
return
val;
}
result::
operator
int
()
const
{
Tcl_Obj *obj =
Tcl_GetObjResult
(interp_);
int
val, cc;
cc =
Tcl_GetIntFromObj
(interp_, obj, &val);
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp_);
}
return
val;
}
result::
operator
long
()
const
{
Tcl_Obj *obj =
Tcl_GetObjResult
(interp_);
long
val;
int
cc;
cc =
Tcl_GetLongFromObj
(interp_, obj, &val);
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp_);
}
return
val;
}
result::
operator
string
()
const
{
Tcl_Obj *obj =
Tcl_GetObjResult
(interp_);
return
Tcl_GetString
(obj);
}
result::
operator
object
()
const
{
return
object
(
Tcl_GetObjResult
(interp_)); }
void
details::set_result
(Tcl_Interp *interp,
bool
b) {
Tcl_SetObjResult
(interp,
Tcl_NewBooleanObj
(b)); }
void
details::set_result
(Tcl_Interp *interp,
int
i) {
Tcl_SetObjResult
(interp,
Tcl_NewIntObj
(i)); }
void
details::set_result
(Tcl_Interp *interp,
long
i) {
Tcl_SetObjResult
(interp,
Tcl_NewLongObj
(i)); }
void
details::set_result
(Tcl_Interp *interp,
double
d) {
Tcl_SetObjResult
(interp,
Tcl_NewDoubleObj
(d)); }
void
details::set_result
(Tcl_Interp *interp, string
const
&s) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(s.
data
(),
static_cast
<
int
>(s.
size
()))); }
void
details::set_result
(Tcl_Interp *interp,
void
*p) {
ostringstream ss;
ss <<
'
p
'
<< p;
string
s
(ss.
str
());
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(s.
data
(),
static_cast
<
int
>(s.
size
())));
}
void
details::set_result
(Tcl_Interp *interp, object
const
&o) {
Tcl_SetObjResult
(interp, o.
get_object
()); }
void
details::check_params_no
(
int
objc,
int
required,
const
std::string &message) {
if
(objc < required) {
throw
tcl_error
(message);
}
}
object
details::get_var_params
(Tcl_Interp *interp,
int
objc, Tcl_Obj *
CONST
objv[],
int
from, policies
const
&pol) {
object o;
if
(pol.
variadic_
) {
check_params_no
(objc, from, pol.
usage_
);
o.
assign
(objv + from, objv + objc);
}
else
{
check_params_no
(objc, from +
1
, pol.
usage_
);
o.
assign
(objv[from]);
}
o.
set_interp
(interp);
return
o;
}
namespace
//
anonymous
{
//
map of polymorphic callbacks
typedef
map<string, shared_ptr<callback_base>> callback_interp_map;
typedef
map<Tcl_Interp *, callback_interp_map> callback_map;
callback_map callbacks;
callback_map constructors;
//
map of call policies
typedef
map<string, policies> policies_interp_map;
typedef
map<Tcl_Interp *, policies_interp_map> policies_map;
policies_map call_policies;
//
map of object handlers
typedef
map<string, shared_ptr<class_handler_base>> class_interp_map;
typedef
map<Tcl_Interp *, class_interp_map> class_handlers_map;
class_handlers_map class_handlers;
//
helper for finding call policies - returns true when found
bool
find_policies
(Tcl_Interp *interp, string
const
&cmdName, policies_interp_map::iterator &piti) {
policies_map::iterator pit = call_policies.
find
(interp);
if
(pit == call_policies.
end
()) {
return
false
;
}
piti = pit->
second
.
find
(cmdName);
return
piti != pit->
second
.
end
();
}
extern
"
C
"
int
object_handler
(ClientData cd, Tcl_Interp *interp,
int
objc, Tcl_Obj *
CONST
objv[]);
//
helper function for post-processing call policies
//
for both free functions (isMethod == false)
//
and class methods (isMethod == true)
void
post_process_policies
(Tcl_Interp *interp, policies &pol, Tcl_Obj *
CONST
objv[],
bool
isMethod) {
//
check if it is a factory
if
(!pol.
factory_
.
empty
()) {
class_handlers_map::iterator it = class_handlers.
find
(interp);
if
(it == class_handlers.
end
()) {
throw
tcl_error
(
"
Factory was registered for unknown class.
"
);
}
class_interp_map::iterator oit = it->
second
.
find
(pol.
factory_
);
if
(oit == it->
second
.
end
()) {
throw
tcl_error
(
"
Factory was registered for unknown class.
"
);
}
class_handler_base *chb = oit->
second
.
get
();
//
register a new command for the object returned
//
by this factory function
//
if everything went OK, the result is the address of the
//
new object in the 'pXXX' form
//
- the new command will be created with this name
Tcl_CreateObjCommand
(interp,
Tcl_GetString
(
Tcl_GetObjResult
(interp)), object_handler,
static_cast
<ClientData>(chb),
0
);
}
//
process all declared sinks
//
- unregister all object commands that envelopes the pointers
for
(vector<
int
>::iterator s = pol.
sinks_
.
begin
(); s != pol.
sinks_
.
end
(); ++s) {
if
(isMethod ==
false
) {
//
example: if there is a declared sink at parameter 3,
//
and the Tcl command was:
//
% fun par1 par2 PAR3 par4
//
then the index 3 correctly points into the objv array
int
index = *s;
Tcl_DeleteCommand
(interp,
Tcl_GetString
(objv[index]));
}
else
{
//
example: if there is a declared sink at parameter 3,
//
and the Tcl command was:
//
% $p method par1 par2 PAR3 par4
//
then the index 3 needs to be incremented
//
in order correctly point into the 4th index of objv array
int
index = *s +
1
;
Tcl_DeleteCommand
(interp,
Tcl_GetString
(objv[index]));
}
}
}
//
actual functions handling various callbacks
//
generic callback handler
extern
"
C
"
int
callback_handler
(ClientData, Tcl_Interp *interp,
int
objc, Tcl_Obj *
CONST
objv[]) {
callback_map::iterator it = callbacks.
find
(interp);
if
(it == callbacks.
end
()) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(
"
Trying to invoke non-existent callback (wrong interpreter?)
"
, -
1
));
return
TCL_ERROR
;
}
string
cmdName
(
Tcl_GetString
(objv[
0
]));
callback_interp_map::iterator iti = it->
second
.
find
(cmdName);
if
(iti == it->
second
.
end
()) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(
"
Trying to invoke non-existent callback (wrong cmd name?)
"
, -
1
));
return
TCL_ERROR
;
}
policies_map::iterator pit = call_policies.
find
(interp);
if
(pit == call_policies.
end
()) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(
"
Trying to invoke callback with no known policies
"
, -
1
));
return
TCL_ERROR
;
}
policies_interp_map::iterator piti;
if
(
find_policies
(interp, cmdName, piti) ==
false
) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(
"
Trying to invoke callback with no known policies
"
, -
1
));
return
TCL_ERROR
;
}
policies &pol = piti->
second
;
try
{
iti->
second
->
invoke
(interp, objc, objv, pol);
post_process_policies
(interp, pol, objv,
false
);
}
catch
(exception
const
&e) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(
const_cast
<
char
*>(e.
what
()), -
1
));
return
TCL_ERROR
;
}
catch
(...) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(
"
Unknown error.
"
, -
1
));
return
TCL_ERROR
;
}
return
TCL_OK
;
}
//
generic "object" command handler
extern
"
C
"
int
object_handler
(ClientData cd, Tcl_Interp *interp,
int
objc, Tcl_Obj *
CONST
objv[]) {
//
here, client data points to the singleton object
//
which is responsible for managing commands for
//
objects of a given type
class_handler_base *chb =
reinterpret_cast
<class_handler_base *>(cd);
//
the command name has the form 'pXXX' where XXX is the address
//
of the "this" object
string
const
str
(
Tcl_GetString
(objv[
0
]));
istringstream
ss
(str);
char
dummy;
void
*p;
ss >> dummy >> p;
try
{
string
methodName
(
Tcl_GetString
(objv[
1
]));
policies &pol = chb->
get_policies
(methodName);
chb->
invoke
(p, interp, objc, objv, pol);
post_process_policies
(interp, pol, objv,
true
);
}
catch
(exception
const
&e) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(
const_cast
<
char
*>(e.
what
()), -
1
));
return
TCL_ERROR
;
}
catch
(...) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(
"
Unknown error.
"
, -
1
));
return
TCL_ERROR
;
}
return
TCL_OK
;
}
//
generic "constructor" command
extern
"
C
"
int
constructor_handler
(ClientData cd, Tcl_Interp *interp,
int
objc, Tcl_Obj *
CONST
objv[]) {
//
here, client data points to the singleton object
//
which is responsible for managing commands for
//
objects of a given type
class_handler_base *chb =
reinterpret_cast
<class_handler_base *>(cd);
callback_map::iterator it = constructors.
find
(interp);
if
(it == constructors.
end
()) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(
"
Trying to invoke non-existent callback (wrong interpreter?)
"
, -
1
));
return
TCL_ERROR
;
}
string
className
(
Tcl_GetString
(objv[
0
]));
callback_interp_map::iterator iti = it->
second
.
find
(className);
if
(iti == it->
second
.
end
()) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(
"
Trying to invoke non-existent callback (wrong class name?)
"
, -
1
));
return
TCL_ERROR
;
}
policies_interp_map::iterator piti;
if
(
find_policies
(interp, className, piti) ==
false
) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(
"
Trying to invoke callback with no known policies
"
, -
1
));
return
TCL_ERROR
;
}
policies &pol = piti->
second
;
try
{
iti->
second
->
invoke
(interp, objc, objv, pol);
//
if everything went OK, the result is the address of the
//
new object in the 'pXXX' form
//
- we can create a new command with this name
Tcl_CreateObjCommand
(interp,
Tcl_GetString
(
Tcl_GetObjResult
(interp)), object_handler,
static_cast
<ClientData>(chb),
0
);
}
catch
(exception
const
&e) {
Tcl_SetResult
(interp,
const_cast
<
char
*>(e.
what
()),
TCL_VOLATILE
);
return
TCL_ERROR
;
}
catch
(...) {
Tcl_SetObjResult
(interp,
Tcl_NewStringObj
(
"
Unknown error.
"
, -
1
));
return
TCL_ERROR
;
}
return
TCL_OK
;
}
}
//
namespace
Tcl::details::no_init_type Tcl::no_init;
policies &
policies::factory
(string
const
&name) {
factory_ = name;
return
*
this
;
}
policies &
policies::sink
(
int
index) {
sinks_.
push_back
(index);
return
*
this
;
}
policies &
policies::variadic
() {
variadic_ =
true
;
return
*
this
;
}
policies &
policies::usage
(string
const
&message) {
usage_ =
std::string
(
"
Usage:
"
) + message;
return
*
this
;
}
policies
Tcl::factory
(string
const
&name) {
return
policies
().
factory
(name); }
policies
Tcl::sink
(
int
index) {
return
policies
().
sink
(index); }
policies
Tcl::variadic
() {
return
policies
().
variadic
(); }
policies
Tcl::usage
(string
const
&message) {
return
policies
().
usage
(message); }
class_handler_base::class_handler_base
() {
//
default policies for the -delete command
policies_[
"
-delete
"
] =
policies
();
}
void
class_handler_base::register_method
(string
const
&name, shared_ptr<object_cmd_base> ocb, policies
const
&p) {
methods_[name] = ocb;
policies_[name] = p;
}
policies &
class_handler_base::get_policies
(string
const
&name) {
policies_map_type::iterator it = policies_.
find
(name);
if
(it == policies_.
end
()) {
throw
tcl_error
(
"
Trying to use non-existent policy:
"
+ name);
}
return
it->
second
;
}
object::object
() : interp_(
0
) {
obj_ =
Tcl_NewObj
();
Tcl_IncrRefCount
(obj_);
}
object::object
(
bool
b) : interp_(
0
) {
obj_ =
Tcl_NewBooleanObj
(b);
Tcl_IncrRefCount
(obj_);
}
object::object
(
char
const
*buf,
size_t
size) : interp_(
0
) {
obj_ =
Tcl_NewByteArrayObj
(
reinterpret_cast
<
unsigned
char
const
*>(buf),
static_cast
<
int
>(size));
Tcl_IncrRefCount
(obj_);
}
object::object
(
double
d) : interp_(
0
) {
obj_ =
Tcl_NewDoubleObj
(d);
Tcl_IncrRefCount
(obj_);
}
object::object
(
int
i) : interp_(
0
) {
obj_ =
Tcl_NewIntObj
(i);
Tcl_IncrRefCount
(obj_);
}
object::object
(
long
l) : interp_(
0
) {
obj_ =
Tcl_NewLongObj
(l);
Tcl_IncrRefCount
(obj_);
}
object::object
(
char
const
*s) : interp_(
0
) {
obj_ =
Tcl_NewStringObj
(s, -
1
);
Tcl_IncrRefCount
(obj_);
}
object::object
(string
const
&s) : interp_(
0
) {
obj_ =
Tcl_NewStringObj
(s.
data
(),
static_cast
<
int
>(s.
size
()));
Tcl_IncrRefCount
(obj_);
}
object::object
(Tcl_Obj *o,
bool
shared) : interp_(
0
) {
init
(o, shared); }
object::object
(object
const
&other,
bool
shared) : interp_(other.get_interp()) {
init
(other.
obj_
, shared); }
void
object::init
(Tcl_Obj *o,
bool
shared) {
if
(shared) {
obj_ = o;
}
else
{
obj_ =
Tcl_DuplicateObj
(o);
}
Tcl_IncrRefCount
(obj_);
}
object::~object
() {
Tcl_DecrRefCount
(obj_); }
object &
object::assign
(
bool
b) {
Tcl_SetBooleanObj
(obj_, b);
return
*
this
;
}
object &
object::resize
(
size_t
size) {
Tcl_SetByteArrayLength
(obj_,
static_cast
<
int
>(size));
return
*
this
;
}
object &
object::assign
(
char
const
*buf,
size_t
size) {
Tcl_SetByteArrayObj
(obj_,
reinterpret_cast
<
unsigned
char
const
*>(buf),
static_cast
<
int
>(size));
return
*
this
;
}
object &
object::assign
(
double
d) {
Tcl_SetDoubleObj
(obj_, d);
return
*
this
;
}
object &
object::assign
(
int
i) {
Tcl_SetIntObj
(obj_, i);
return
*
this
;
}
object &
object::assign
(
long
l) {
Tcl_SetLongObj
(obj_, l);
return
*
this
;
}
object &
object::assign
(
char
const
*s) {
Tcl_SetStringObj
(obj_, s, -
1
);
return
*
this
;
}
object &
object::assign
(string
const
&s) {
Tcl_SetStringObj
(obj_, s.
data
(),
static_cast
<
int
>(s.
size
()));
return
*
this
;
}
object &
object::assign
(object
const
&other) {
object
(other).
swap
(*
this
);
return
*
this
;
}
object &
object::assign
(Tcl_Obj *o) {
object
(o).
swap
(*
this
);
return
*
this
;
}
object &
object::swap
(object &other) {
std::swap
(obj_, other.
obj_
);
std::swap
(interp_, other.
interp_
);
return
*
this
;
}
template
<>
bool
object::get<
bool
>(interpreter &i)
const
{
int
retVal;
int
res =
Tcl_GetBooleanFromObj
(i.
get
(), obj_, &retVal);
if
(res !=
TCL_OK
) {
throw
tcl_error
(i.
get
());
}
return
static_cast
<
bool
>(retVal);
}
template
<> vector<
char
> object::get<vector<
char
>>(interpreter &)
const
{
size_t
size;
char
const
*buf =
get
(size);
return
vector<
char
>(buf, buf + size);
}
template
<>
double
object::get<
double
>(interpreter &i)
const
{
double
retVal;
int
res =
Tcl_GetDoubleFromObj
(i.
get
(), obj_, &retVal);
if
(res !=
TCL_OK
) {
throw
tcl_error
(i.
get
());
}
return
retVal;
}
template
<>
int
object::get<
int
>(interpreter &i)
const
{
int
retVal;
int
res =
Tcl_GetIntFromObj
(i.
get
(), obj_, &retVal);
if
(res !=
TCL_OK
) {
throw
tcl_error
(i.
get
());
}
return
retVal;
}
template
<>
long
object::get<
long
>(interpreter &i)
const
{
long
retVal;
int
res =
Tcl_GetLongFromObj
(i.
get
(), obj_, &retVal);
if
(res !=
TCL_OK
) {
throw
tcl_error
(i.
get
());
}
return
retVal;
}
template
<>
char
const
*object::get<
char
const
*>(interpreter &)
const
{
return
get
(); }
template
<> string object::get<string>(interpreter &)
const
{
Tcl_Size len;
char
const
*buf =
Tcl_GetStringFromObj
(obj_, &len);
return
string
(buf, buf + len);
}
string
object::asString
()
const
{
return
get<string>(); }
int
object::asInt
()
const
{
return
get<
int
>(); }
bool
object::asBool
()
const
{
return
get<
bool
>(); }
long
object::asLong
()
const
{
return
get<
long
>(); }
double
object::asDouble
()
const
{
return
get<
double
>(); }
char
const
*
object::get
()
const
{
return
Tcl_GetString
(obj_); }
char
const
*
object::get
(
size_t
&size)
const
{
Tcl_Size len;
unsigned
char
*buf =
Tcl_GetByteArrayFromObj
(obj_, &len);
size = len;
return
const_cast
<
char
const
*>(
reinterpret_cast
<
char
*>(buf));
}
size_t
object::size
(interpreter &i)
const
{
Tcl_Size len;
int
res =
Tcl_ListObjLength
(i.
get
(), obj_, &len);
if
(res !=
TCL_OK
) {
throw
tcl_error
(i.
get
());
}
return
static_cast
<
size_t
>(len);
}
object
object::at
(
size_t
index, interpreter &i)
const
{
Tcl_Obj *o;
int
res =
Tcl_ListObjIndex
(i.
get
(), obj_,
static_cast
<
int
>(index), &o);
if
(res !=
TCL_OK
) {
throw
tcl_error
(i.
get
());
}
if
(o ==
NULL
) {
throw
tcl_error
(
"
Index out of range.
"
);
}
return
object
(o);
}
object &
object::append
(object
const
&o, interpreter &i) {
int
res =
Tcl_ListObjAppendElement
(i.
get
(), obj_, o.
obj_
);
if
(res !=
TCL_OK
) {
throw
tcl_error
(i.
get
());
}
return
*
this
;
}
object &
object::append_list
(object
const
&o, interpreter &i) {
int
res =
Tcl_ListObjAppendList
(i.
get
(), obj_, o.
obj_
);
if
(res !=
TCL_OK
) {
throw
tcl_error
(i.
get
());
}
return
*
this
;
}
object &
object::replace
(
size_t
index,
size_t
count, object
const
&o, interpreter &i) {
int
res =
Tcl_ListObjReplace
(i.
get
(), obj_,
static_cast
<
int
>(index),
static_cast
<
int
>(count),
1
, &(o.
obj_
));
if
(res !=
TCL_OK
) {
throw
tcl_error
(i.
get
());
}
return
*
this
;
}
object &
object::replace_list
(
size_t
index,
size_t
count, object
const
&o, interpreter &i) {
Tcl_Size objc;
Tcl_Obj **objv;
int
res =
Tcl_ListObjGetElements
(i.
get
(), o.
obj_
, &objc, &objv);
if
(res !=
TCL_OK
) {
throw
tcl_error
(i.
get
());
}
res =
Tcl_ListObjReplace
(i.
get
(), obj_,
static_cast
<
int
>(index),
static_cast
<
int
>(count), objc, objv);
if
(res !=
TCL_OK
) {
throw
tcl_error
(i.
get
());
}
return
*
this
;
}
void
object::set_interp
(Tcl_Interp *interp) { interp_ = interp; }
Tcl_Interp *
object::get_interp
()
const
{
return
interp_; }
Tcl::interpreter *interpreter::defaultInterpreter =
nullptr
;
interpreter::interpreter
() {
interp_ =
Tcl_CreateInterp
();
owner_ =
true
;
if
(defaultInterpreter) {
throw
tcl_error
(
"
expecting a single interpreter
"
);
}
}
interpreter::interpreter
(Tcl_Interp *interp,
bool
owner) {
interp_ = interp;
owner_ = owner;
if
(!defaultInterpreter) {
if
(
Tcl_InitStubs
(interp,
"
8.6
"
,
0
) ==
NULL
) {
throw
tcl_error
(
"
Failed to initialize stubs
"
);
}
//
Make a copy
defaultInterpreter =
new
interpreter
(*
this
);
}
}
interpreter::interpreter
(
const
interpreter &i) : interp_(i.interp_), owner_(i.owner_) {}
interpreter::~interpreter
() {
if
(owner_) {
//
clear all callback info belonging to this interpreter
clear_definitions
(interp_);
Tcl_DeleteInterp
(interp_);
}
}
#
if
TCL_MAJOR_VERSION < 9
void
interpreter::make_safe
() {
int
cc =
Tcl_MakeSafe
(interp_);
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp_);
}
}
#
endif
result
interpreter::eval
(string
const
&script) {
int
cc =
Tcl_Eval
(interp_, script.
c_str
());
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp_);
}
return
result
(interp_);
}
result
interpreter::eval
(istream &s) {
string
str
(istreambuf_iterator<
char
>(s.
rdbuf
()), istreambuf_iterator<
char
>());
return
eval
(str);
}
result
interpreter::eval
(object
const
&o) {
int
cc =
Tcl_EvalObjEx
(interp_, o.
get_object
(),
0
);
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp_);
}
return
result
(interp_);
}
result
interpreter::getVar
(string
const
&variableName, string
const
&indexName) {
object n =
object
(variableName.
c_str
());
object i =
object
(indexName.
c_str
());
Tcl_Obj * obj =
Tcl_ObjGetVar2
(interp_, n.
get_object
(), i.
get_object
(),
0
);
if
(obj ==
NULL
) {
throw
tcl_error
(interp_);
}
else
{
Tcl_SetObjResult
(interp_, obj);
}
return
result
(interp_);
}
result
interpreter::getVar
(string
const
&variableName) {
object n =
object
(variableName.
c_str
());
Tcl_Obj * obj =
Tcl_ObjGetVar2
(interp_, n.
get_object
(),
nullptr
,
0
);
if
(obj ==
NULL
) {
throw
tcl_error
(interp_);
}
else
{
Tcl_SetObjResult
(interp_, obj);
}
return
result
(interp_);
}
bool
interpreter::exists
(string
const
&variableName, string
const
&indexName) {
object n =
object
(variableName.
c_str
());
object i =
object
(indexName.
c_str
());
Tcl_Obj * obj =
Tcl_ObjGetVar2
(interp_, n.
get_object
(), i.
get_object
(),
0
);
return
(obj !=
NULL
);
}
bool
interpreter::exists
(string
const
&variableName) {
object n =
object
(variableName.
c_str
());
Tcl_Obj * obj =
Tcl_ObjGetVar2
(interp_, n.
get_object
(),
nullptr
,
0
);
return
(obj !=
NULL
);
}
void
interpreter::pkg_provide
(string
const
&name, string
const
&version) {
int
cc =
Tcl_PkgProvide
(interp_, name.
c_str
(), version.
c_str
());
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp_);
}
}
void
interpreter::create_namespace
(string
const
&name) {
if
(
Tcl_CreateNamespace
(interp_, name.
c_str
(),
0
,
0
) ==
0
) {
throw
tcl_error
(interp_);
}
}
void
interpreter::create_alias
(string
const
&cmd, interpreter &targetInterp, string
const
&targetCmd) {
int
cc =
Tcl_CreateAlias
(interp_, cmd.
c_str
(), targetInterp.
interp_
, targetCmd.
c_str
(),
0
,
0
);
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp_);
}
}
void
interpreter::clear_definitions
(Tcl_Interp *interp) {
//
delete all callbacks that were registered for given interpreter
{
callback_map::iterator it = callbacks.
find
(interp);
if
(it == callbacks.
end
()) {
//
no callbacks defined for this interpreter
return
;
}
callback_interp_map &imap = it->
second
;
for
(callback_interp_map::iterator it2 = imap.
begin
(); it2 != imap.
end
(); ++it2) {
Tcl_DeleteCommand
(interp, it2->
first
.
c_str
());
}
callbacks.
erase
(interp);
}
//
delete all constructors
{
callback_map::iterator it = constructors.
find
(interp);
if
(it == constructors.
end
()) {
//
no callbacks defined for this interpreter
return
;
}
callback_interp_map &imap = it->
second
;
for
(callback_interp_map::iterator it2 = imap.
begin
(); it2 != imap.
end
(); ++it2) {
Tcl_DeleteCommand
(interp, it2->
first
.
c_str
());
}
callbacks.
erase
(interp);
}
//
delete all call policies
call_policies.
erase
(interp);
//
delete all object handlers
//
(we have to assume that all living objects were destroyed,
//
otherwise Bad Things will happen)
class_handlers.
erase
(interp);
}
void
interpreter::add_function
(string
const
&name, shared_ptr<callback_base> cb, policies
const
&p) {
Tcl_CreateObjCommand
(interp_, name.
c_str
(), callback_handler,
0
,
0
);
callbacks[interp_][name] = cb;
call_policies[interp_][name] = p;
}
void
interpreter::add_class
(string
const
&name, shared_ptr<class_handler_base> chb) { class_handlers[interp_][name] = chb; }
void
interpreter::add_constructor
(string
const
&name, shared_ptr<class_handler_base> chb, shared_ptr<callback_base> cb, policies
const
&p) {
Tcl_CreateObjCommand
(interp_, name.
c_str
(), constructor_handler,
static_cast
<ClientData>(chb.
get
()),
0
);
constructors[interp_][name] = cb;
call_policies[interp_][name] = p;
}
int
tcl_cast<
int
>::from(Tcl_Interp *interp, Tcl_Obj *obj,
bool
) {
int
res;
int
cc =
Tcl_GetIntFromObj
(interp, obj, &res);
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp);
}
return
res;
}
long
tcl_cast<
long
>::from(Tcl_Interp *interp, Tcl_Obj *obj,
bool
) {
long
res;
int
cc =
Tcl_GetLongFromObj
(interp, obj, &res);
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp);
}
return
res;
}
bool
tcl_cast<
bool
>::from(Tcl_Interp *interp, Tcl_Obj *obj,
bool
) {
int
res;
int
cc =
Tcl_GetBooleanFromObj
(interp, obj, &res);
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp);
}
return
res !=
0
;
}
double
tcl_cast<
double
>::from(Tcl_Interp *interp, Tcl_Obj *obj,
bool
) {
double
res;
int
cc =
Tcl_GetDoubleFromObj
(interp, obj, &res);
if
(cc !=
TCL_OK
) {
throw
tcl_error
(interp);
}
return
res;
}
string tcl_cast<string>::from(Tcl_Interp *, Tcl_Obj *obj,
bool
) {
return
Tcl_GetString
(obj); }
char
const
*tcl_cast<
char
const
*>::from(Tcl_Interp *, Tcl_Obj *obj,
bool
) {
return
Tcl_GetString
(obj); }
object tcl_cast<object>::from(Tcl_Interp *interp, Tcl_Obj *obj,
bool
) {
object
o
(obj);
o.
set_interp
(interp);
return
o;
}
Back
|
FazBrowse Home
|
New Git URL