PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/dep/g3dlite/source/AnyVal.cpp

https://github.com/chucho/FaceCore
C++ | 1379 lines | 1020 code | 322 blank | 37 comment | 256 complexity | e567a4a837271e2ccfa5e981664190d9 MD5 | raw file
  1. /**
  2. @file AnyVal.cpp
  3. @author Morgan McGuire
  4. @maintainer Morgan McGuire
  5. @created 2006-06-11
  6. @edited 2008-07-14
  7. */
  8. #include "G3D/AnyVal.h"
  9. #include "G3D/Array.h"
  10. #include "G3D/stringutils.h"
  11. #include "G3D/Table.h"
  12. #include "G3D/Vector2.h"
  13. #include "G3D/Vector3.h"
  14. #include "G3D/Vector4.h"
  15. #include "G3D/Color1.h"
  16. #include "G3D/Color3.h"
  17. #include "G3D/Color4.h"
  18. #include "G3D/Matrix2.h"
  19. #include "G3D/Matrix3.h"
  20. #include "G3D/Matrix4.h"
  21. #include "G3D/Rect2D.h"
  22. #include "G3D/AABox.h"
  23. #include "G3D/CoordinateFrame.h"
  24. #include "G3D/Quat.h"
  25. #include "G3D/TextInput.h"
  26. #include "G3D/TextOutput.h"
  27. #include "G3D/BinaryInput.h"
  28. #include "G3D/BinaryOutput.h"
  29. namespace G3D {
  30. AnyVal AnyVal::fromFile(const std::string& filename) {
  31. TextInput t(filename);
  32. return AnyVal(t);
  33. }
  34. void AnyVal::load(const std::string& filename) {
  35. *this = fromFile(filename);
  36. }
  37. void AnyVal::save(const std::string& filename) const {
  38. TextOutput t(filename);
  39. serialize(t);
  40. t.commit();
  41. }
  42. AnyVal::AnyVal() : m_type(NIL), m_value(NULL), m_referenceCount(NULL) {
  43. }
  44. AnyVal::AnyVal(bool b) : m_type(BOOLEAN), m_value(new bool(b)), m_referenceCount(NULL) {
  45. }
  46. AnyVal::AnyVal(G3D::TextInput& t) : m_type(NIL), m_value(NULL), m_referenceCount(NULL) {
  47. deserialize(t);
  48. }
  49. /*AnyVal::AnyVal(G3D::BinaryInput& b) {
  50. deserialize(b);
  51. }
  52. */
  53. AnyVal::AnyVal(double v) : m_type(NUMBER), m_referenceCount(NULL) {
  54. m_value = new double(v);
  55. }
  56. AnyVal::AnyVal(int v) : m_type(NUMBER), m_referenceCount(NULL) {
  57. m_value = new double(v);
  58. }
  59. AnyVal::AnyVal(const Rect2D& v) : m_type(RECT2D), m_referenceCount(NULL) {
  60. m_value = new Rect2D(v);
  61. }
  62. AnyVal::AnyVal(const AABox& v) : m_type(AABOX), m_referenceCount(NULL) {
  63. m_value = new AABox(v);
  64. }
  65. AnyVal::AnyVal(const Vector2& v) : m_type(VECTOR2), m_referenceCount(NULL) {
  66. m_value = new Vector2(v);
  67. }
  68. AnyVal::AnyVal(const Vector3& v) : m_type(VECTOR3), m_referenceCount(NULL) {
  69. m_value = new Vector3(v);
  70. }
  71. AnyVal::AnyVal(const Vector4& v) : m_type(VECTOR4), m_referenceCount(NULL) {
  72. m_value = new Vector4(v);
  73. }
  74. AnyVal::AnyVal(const Color1& v) : m_type(COLOR1), m_referenceCount(NULL) {
  75. m_value = new Color1(v);
  76. }
  77. AnyVal::AnyVal(const Color3& v) : m_type(COLOR3), m_referenceCount(NULL) {
  78. m_value = new Color3(v);
  79. }
  80. AnyVal::AnyVal(const Color4& v) : m_type(COLOR4), m_referenceCount(NULL) {
  81. m_value = new Color4(v);
  82. }
  83. AnyVal::AnyVal(const std::string& v) : m_type(STRING), m_referenceCount(NULL) {
  84. m_value = new std::string(v);
  85. }
  86. AnyVal::AnyVal(const char* v) : m_type(STRING), m_referenceCount(NULL) {
  87. m_value = new std::string(v);
  88. }
  89. AnyVal::AnyVal(const Quat& v) : m_type(QUAT), m_referenceCount(NULL) {
  90. m_value = new Quat(v);
  91. }
  92. AnyVal::AnyVal(const CoordinateFrame& v) : m_type(COORDINATEFRAME), m_referenceCount(NULL) {
  93. m_value = new CoordinateFrame(v);
  94. }
  95. AnyVal::AnyVal(const Matrix2& v) : m_type(MATRIX2), m_referenceCount(NULL) {
  96. m_value = new Matrix2(v);
  97. }
  98. AnyVal::AnyVal(const Matrix3& v) : m_type(MATRIX3), m_referenceCount(NULL) {
  99. m_value = new Matrix3(v);
  100. }
  101. AnyVal::AnyVal(const Matrix4& v) : m_type(MATRIX4), m_referenceCount(NULL) {
  102. m_value = new Matrix4(v);
  103. }
  104. AnyVal::AnyVal(const AnyVal& c) : m_type(NIL), m_value(NULL), m_referenceCount(NULL) {
  105. *this = c;
  106. }
  107. AnyVal::AnyVal(Type arrayOrTable) : m_type(NIL), m_value(NULL), m_referenceCount(new int(1)) {
  108. // TODO: make AnyVal::createArray()
  109. switch (arrayOrTable) {
  110. case ARRAY:
  111. m_type = ARRAY;
  112. m_value = new Array<AnyVal>();
  113. break;
  114. case TABLE:
  115. m_type = TABLE;
  116. m_value = new Table<std::string, AnyVal>();
  117. break;
  118. default:
  119. debugAssertM(false, "Cannot construct AnyVal from constants except ARRAY or TABLE.");
  120. }
  121. }
  122. AnyVal::~AnyVal() {
  123. deleteValue();
  124. }
  125. void AnyVal::deleteValue() {
  126. if (m_referenceCount) {
  127. --(*m_referenceCount);
  128. if (*m_referenceCount <= 0) {
  129. delete m_referenceCount;
  130. m_referenceCount = NULL;
  131. // Pass through and delete the real object now
  132. } else {
  133. // Someone else is holding a reference, so we can't delete
  134. // the object.
  135. m_referenceCount = NULL;
  136. return;
  137. }
  138. }
  139. switch (m_type) {
  140. case NIL:
  141. // Nothing to do
  142. break;
  143. case NUMBER:
  144. delete (double*)m_value;
  145. break;
  146. case BOOLEAN:
  147. delete (bool*)m_value;
  148. break;
  149. case STRING:
  150. delete (std::string*)m_value;
  151. break;
  152. case RECT2D:
  153. delete (Rect2D*)m_value;
  154. break;
  155. case AABOX:
  156. delete (AABox*)m_value;
  157. break;
  158. case VECTOR2:
  159. delete (Vector2*)m_value;
  160. break;
  161. case VECTOR3:
  162. delete (Vector3*)m_value;
  163. break;
  164. case VECTOR4:
  165. delete (Vector4*)m_value;
  166. break;
  167. case MATRIX2:
  168. delete (Matrix2*)m_value;
  169. break;
  170. case MATRIX3:
  171. delete (Matrix3*)m_value;
  172. break;
  173. case MATRIX4:
  174. delete (Matrix4*)m_value;
  175. break;
  176. case QUAT:
  177. delete (Quat*)m_value;
  178. break;
  179. case COORDINATEFRAME:
  180. delete (CoordinateFrame*)m_value;
  181. break;
  182. case COLOR1:
  183. delete (Color1*)m_value;
  184. break;
  185. case COLOR3:
  186. delete (Color3*)m_value;
  187. break;
  188. case COLOR4:
  189. delete (Color4*)m_value;
  190. break;
  191. case ARRAY:
  192. delete (Array<AnyVal>*)m_value;
  193. break;
  194. case TABLE:
  195. delete (Table<std::string, AnyVal>*)m_value;
  196. break;
  197. default:
  198. debugAssertM(false, "Internal error: no destructor for this type.");
  199. }
  200. m_value = NULL;
  201. }
  202. AnyVal& AnyVal::operator=(const AnyVal& v) {
  203. deleteValue();
  204. m_type = v.m_type;
  205. m_referenceCount = v.m_referenceCount;
  206. if (isSharedType()) {
  207. ++(*m_referenceCount);
  208. m_value = v.m_value;
  209. } else {
  210. m_value = v.copyValue();
  211. }
  212. return *this;
  213. }
  214. void* AnyVal::copyValue() const {
  215. switch (m_type) {
  216. case NIL:
  217. return NULL;
  218. case NUMBER:
  219. return new double(*(double*)m_value);
  220. case BOOLEAN:
  221. return new bool(*(bool*)m_value);
  222. case STRING:
  223. return new std::string(*(std::string*)m_value);
  224. case RECT2D:
  225. return new Rect2D(*(Rect2D*)m_value);
  226. case AABOX:
  227. return new AABox(*(AABox*)m_value);
  228. case VECTOR2:
  229. return new Vector2(*(Vector2*)m_value);
  230. case VECTOR3:
  231. return new Vector3(*(Vector3*)m_value);
  232. case VECTOR4:
  233. return new Vector4(*(Vector4*)m_value);
  234. case MATRIX2:
  235. return new Matrix2(*(Matrix2*)m_value);
  236. case MATRIX3:
  237. return new Matrix3(*(Matrix3*)m_value);
  238. case MATRIX4:
  239. return new Matrix4(*(Matrix4*)m_value);
  240. case QUAT:
  241. return new Quat(*(Quat*)m_value);
  242. case COORDINATEFRAME:
  243. return new CoordinateFrame(*(CoordinateFrame*)m_value);
  244. case COLOR1:
  245. return new Color1(*(Color1*)m_value);
  246. case COLOR3:
  247. return new Color3(*(Color3*)m_value);
  248. case COLOR4:
  249. return new Color4(*(Color4*)m_value);
  250. case ARRAY:
  251. return new Array<AnyVal>(*(Array<AnyVal>*)m_value);
  252. case TABLE:
  253. return new Table<std::string, AnyVal>(*(Table<std::string, AnyVal>*)m_value);
  254. default:
  255. debugAssertM(false, "Internal error: no assignment operator for this type.");
  256. return NULL;
  257. }
  258. }
  259. AnyVal::Type AnyVal::type() const {
  260. return m_type;
  261. }
  262. static bool legalIdentifier(const std::string& s) {
  263. if (s.size() == 0) {
  264. return false;
  265. }
  266. if (! isLetter(s[0]) || (s[0] == '_')) {
  267. return false;
  268. }
  269. bool ok = true;
  270. for (unsigned int i = 1; i < s.size(); ++i) {
  271. ok &= isDigit(s[i]) || isLetter(s[i]) || (s[i] == '_');
  272. }
  273. return ok;
  274. }
  275. void AnyVal::serialize(G3D::TextOutput& t) const {
  276. switch (m_type) {
  277. case NIL:
  278. t.writeSymbol("Nil");
  279. break;
  280. case NUMBER:
  281. t.printf("%g", *(double*)m_value);
  282. break;
  283. case BOOLEAN:
  284. t.writeBoolean(*(bool*)m_value);
  285. break;
  286. case STRING:
  287. t.writeString(*(std::string*)m_value);
  288. break;
  289. case RECT2D:
  290. t.printf("R(%g, %g, %g, %g)", ((Rect2D*)m_value)->x0(), ((Rect2D*)m_value)->y0(),
  291. ((Rect2D*)m_value)->width(), ((Rect2D*)m_value)->height());
  292. break;
  293. case AABOX:
  294. t.printf("AAB(V3(%g, %g, %g), V3(%g, %g, %g))",
  295. aabox().low().x,
  296. aabox().low().y,
  297. aabox().low().z,
  298. aabox().high().x,
  299. aabox().high().y,
  300. aabox().high().z);
  301. break;
  302. case VECTOR2:
  303. t.printf("V2(%g, %g)", ((Vector2*)m_value)->x, ((Vector2*)m_value)->y);
  304. break;
  305. case VECTOR3:
  306. t.printf("V3(%g, %g, %g)", ((Vector3*)m_value)->x, ((Vector3*)m_value)->y, ((Vector3*)m_value)->z);
  307. break;
  308. case VECTOR4:
  309. t.printf("V4(%g, %g, %g, %g)", ((Vector4*)m_value)->x, ((Vector4*)m_value)->y, ((Vector4*)m_value)->z, ((Vector4*)m_value)->w);
  310. break;
  311. case MATRIX2:
  312. {
  313. const Matrix2& m = *(Matrix2*)m_value;
  314. t.printf("M2(\n");
  315. t.pushIndent();
  316. t.printf("%10.5f, %10.5f,\n%10.5f, %10.5f)",
  317. m[0][0], m[0][1],
  318. m[1][0], m[1][1]);
  319. t.popIndent();
  320. }
  321. break;
  322. case MATRIX3:
  323. {
  324. const Matrix3& m = *(Matrix3*)m_value;
  325. t.printf("M3(\n");
  326. t.pushIndent();
  327. t.printf("%10.5f, %10.5f, %10.5f,\n%10.5f, %10.5f, %10.5f,\n%10.5f, %10.5f, %10.5f)",
  328. m[0][0], m[0][1], m[0][2],
  329. m[1][0], m[1][1], m[1][2],
  330. m[2][0], m[2][1], m[2][2]);
  331. t.popIndent();
  332. }
  333. break;
  334. case MATRIX4:
  335. {
  336. const Matrix4& m = *(Matrix4*)m_value;
  337. t.printf("M4(\n");
  338. t.pushIndent();
  339. t.printf(
  340. "%10.5f, %10.5f, %10.5f, %10.5f,\n"
  341. "%10.5f, %10.5f, %10.5f, %10.5f,\n"
  342. "%10.5f, %10.5f, %10.5f, %10.5f,\n"
  343. "%10.5f, %10.5f, %10.5f, %10.5f)",
  344. m[0][0], m[0][1], m[0][2], m[0][3],
  345. m[1][0], m[1][1], m[1][2], m[1][3],
  346. m[2][0], m[2][1], m[2][2], m[2][3],
  347. m[3][0], m[3][1], m[3][2], m[3][3]);
  348. t.popIndent();
  349. }
  350. break;
  351. case QUAT:
  352. t.printf("Q(%g, %g, %g, %g)", ((Quat*)m_value)->x, ((Quat*)m_value)->y, ((Quat*)m_value)->z, ((Quat*)m_value)->w);
  353. break;
  354. case COORDINATEFRAME:
  355. {
  356. const CoordinateFrame& c = *(CoordinateFrame*)m_value;
  357. float x,y,z,yaw,pitch,roll;
  358. c.getXYZYPRDegrees(x,y,z,yaw,pitch,roll);
  359. t.printf("CF(V3(%g,%g,%g), %g, %g, %g)", x, y, z, yaw, pitch, roll);
  360. /*
  361. t.pushIndent();
  362. t.printf(
  363. "CF(\n%10.5f, %10.5f, %10.5f, %10.5f,\n"
  364. "%10.5f, %10.5f, %10.5f, %10.5f,\n"
  365. "%10.5f, %10.5f, %10.5f, %10.5f)",
  366. c.rotation[0][0], c.rotation[0][1], c.rotation[0][2], c.translation.x,
  367. c.rotation[1][0], c.rotation[1][1], c.rotation[1][2], c.translation.y,
  368. c.rotation[2][0], c.rotation[2][1], c.rotation[2][2], c.translation.z);
  369. t.popIndent();
  370. */
  371. }
  372. break;
  373. case COLOR1:
  374. t.printf("C1(%g)", ((Color1*)m_value)->value);
  375. break;
  376. case COLOR3:
  377. t.printf("C3(%g, %g, %g)", ((Color3*)m_value)->r, ((Color3*)m_value)->g, ((Color3*)m_value)->b);
  378. break;
  379. case COLOR4:
  380. t.printf("C4(%g, %g, %g, %g)", ((Color4*)m_value)->r, ((Color4*)m_value)->g, ((Color4*)m_value)->b, ((Color4*)m_value)->a);
  381. break;
  382. case ARRAY:
  383. {
  384. const Array<AnyVal>& a = *(Array<AnyVal>*)m_value;
  385. t.printf("[\n");
  386. t.pushIndent();
  387. for (int i = 0; i < a.size(); ++i) {
  388. a[i].serialize(t);
  389. if (i != a.size() - 1) {
  390. t.printf(", \n");
  391. }
  392. }
  393. t.printf("]");
  394. t.popIndent();
  395. }
  396. break;
  397. case TABLE:
  398. {
  399. const Table<std::string, AnyVal>& a = *(Table<std::string, AnyVal>*)m_value;
  400. t.printf("{\n");
  401. t.pushIndent();
  402. Table<std::string, AnyVal>::Iterator i = a.begin();
  403. const Table<std::string, AnyVal>::Iterator end = a.end();
  404. while (i != end) {
  405. // Quote names that are not legal C++ identifiers
  406. if (! legalIdentifier(i->key)) {
  407. t.printf("'%s' ", i->key.c_str());
  408. } else {
  409. t.writeSymbol(i->key);
  410. }
  411. t.printf("= ");
  412. i->value.serialize(t);
  413. if (i != end) {
  414. t.printf("\n");
  415. }
  416. ++i;
  417. }
  418. t.popIndent();
  419. t.printf("}");
  420. }
  421. break;
  422. default:
  423. debugAssertM(false, "Internal error: no serialize method for this type.");
  424. }
  425. }
  426. std::string AnyVal::toString() const {
  427. TextOutput t;
  428. serialize(t);
  429. std::string s;
  430. t.commitString(s);
  431. return s;
  432. }
  433. void AnyVal::deserialize(G3D::TextInput& t) {
  434. deleteValue();
  435. m_type = NIL;
  436. m_value = NULL;
  437. if (! t.hasMore()) {
  438. return;
  439. }
  440. switch (t.peek().type()) {
  441. case Token::END:
  442. // should never get here because of the hasMore check above
  443. return;
  444. break;
  445. case Token::NUMBER:
  446. m_type = NUMBER;
  447. m_value = new double(t.readNumber());
  448. break;
  449. case Token::STRING:
  450. m_type = STRING;
  451. m_value = new std::string(t.readString());
  452. break;
  453. case Token::NEWLINE:
  454. m_type = STRING;
  455. m_value = new std::string(t.readNewline());
  456. break;
  457. case Token::COMMENT:
  458. m_type = STRING;
  459. m_value = new std::string(t.readComment());
  460. break;
  461. case Token::BOOLEAN:
  462. m_type = BOOLEAN;
  463. m_value = new bool(t.readBoolean());
  464. break;
  465. case Token::SYMBOL:
  466. {
  467. std::string s = t.readSymbol();
  468. if (s == "NIL") {
  469. break;
  470. } else if (s == "true") {
  471. m_type = BOOLEAN;
  472. m_value = new bool(true);
  473. } else if (s == "false") {
  474. m_type = BOOLEAN;
  475. m_value = new bool(false);
  476. } else if (s == "R") {
  477. m_type = RECT2D;
  478. t.readSymbol("(");
  479. float x,y,w,h;
  480. x = (float)t.readNumber();
  481. t.readSymbol(",");
  482. y = (float)t.readNumber();
  483. t.readSymbol(",");
  484. w = (float)t.readNumber();
  485. t.readSymbol(",");
  486. h = (float)t.readNumber();
  487. t.readSymbol(")");
  488. m_value = new Rect2D(Rect2D::xywh(x, y, w, h));
  489. } else if (s == "AAB") {
  490. m_type = AABOX;
  491. Vector3 v[2];
  492. t.readSymbol("(");
  493. for (int i = 0; i < 2; ++i) {
  494. t.readSymbols("V3", "(");
  495. v[i].x = (float)t.readNumber();
  496. t.readSymbol(",");
  497. v[i].y = (float)t.readNumber();
  498. t.readSymbol(",");
  499. v[i].z = (float)t.readNumber();
  500. t.readSymbol(",");
  501. if (i == 0) {
  502. t.readSymbol(",");
  503. }
  504. }
  505. t.readSymbol(")");
  506. m_value = new AABox(v[0], v[1]);
  507. } else if (s == "V2") {
  508. t.readSymbol("(");
  509. Vector2 v;
  510. v.x = (float)t.readNumber();
  511. t.readSymbol(",");
  512. v.y = (float)t.readNumber();
  513. t.readSymbol(")");
  514. m_value = new Vector2(v);
  515. m_type = VECTOR2;
  516. } else if (s == "V3") {
  517. t.readSymbol("(");
  518. Vector3 v;
  519. v.x = (float)t.readNumber();
  520. t.readSymbol(",");
  521. v.y = (float)t.readNumber();
  522. t.readSymbol(",");
  523. v.z = (float)t.readNumber();
  524. t.readSymbol(")");
  525. m_value = new Vector3(v);
  526. m_type = VECTOR3;
  527. } else if (s == "V4") {
  528. t.readSymbol("(");
  529. Vector4 v;
  530. v.x = (float)t.readNumber();
  531. t.readSymbol(",");
  532. v.y = (float)t.readNumber();
  533. t.readSymbol(",");
  534. v.z = (float)t.readNumber();
  535. t.readSymbol(",");
  536. v.w = (float)t.readNumber();
  537. t.readSymbol(")");
  538. m_value = new Vector4(v);
  539. m_type = VECTOR4;
  540. } else if (s == "M2") {
  541. t.readSymbol("(");
  542. Matrix2 m;
  543. for (int r = 0; r < 2; ++r) {
  544. for (int c = 0; c < 2; ++c) {
  545. m[r][c] = (float)t.readNumber();
  546. if ((c != 1) || (r != 1)) {
  547. t.readSymbol(",");
  548. }
  549. }
  550. }
  551. t.readSymbol(")");
  552. m_value = new Matrix2(m);
  553. m_type = MATRIX2;
  554. } else if (s == "M3") {
  555. t.readSymbol("(");
  556. Matrix3 m;
  557. for (int r = 0; r < 3; ++r) {
  558. for (int c = 0; c < 3; ++c) {
  559. m[r][c] = (float)t.readNumber();
  560. if ((c != 2) || (r != 2)) {
  561. t.readSymbol(",");
  562. }
  563. }
  564. }
  565. t.readSymbol(")");
  566. m_value = new Matrix3(m);
  567. m_type = MATRIX3;
  568. } else if (s == "M4") {
  569. t.readSymbol("(");
  570. Matrix4 m;
  571. for (int r = 0; r < 4; ++r) {
  572. for (int c = 0; c < 4; ++c) {
  573. m[r][c] = (float)t.readNumber();
  574. if ((c != 3) || (r != 3)) {
  575. t.readSymbol(",");
  576. }
  577. }
  578. }
  579. t.readSymbol(")");
  580. m_value = new Matrix4(m);
  581. m_type = MATRIX4;
  582. } else if (s == "Q") {
  583. t.readSymbol("(");
  584. Quat q;
  585. q.x = (float)t.readNumber();
  586. t.readSymbol(",");
  587. q.y = (float)t.readNumber();
  588. t.readSymbol(",");
  589. q.z = (float)t.readNumber();
  590. t.readSymbol(",");
  591. q.w = (float)t.readNumber();
  592. t.readSymbol(")");
  593. m_value = new Quat(q);
  594. m_type = QUAT;
  595. } else if (s == "CF") {
  596. t.readSymbol("(");
  597. CoordinateFrame m;
  598. if (t.peek().type() == Token::SYMBOL) {
  599. // Angle format
  600. float x, y, z, yaw, roll, pitch;
  601. t.readSymbols("V3", "(");
  602. x = (float)t.readNumber();
  603. t.readSymbol(",");
  604. y = (float)t.readNumber();
  605. t.readSymbol(",");
  606. z = (float)t.readNumber();
  607. t.readSymbols(")", ",");
  608. yaw = (float)t.readNumber();
  609. t.readSymbol(",");
  610. pitch = (float)t.readNumber();
  611. roll = 0;
  612. if (t.peek().string() == ",") {
  613. t.readSymbol(",");
  614. roll = (float)t.readNumber();
  615. }
  616. m = CoordinateFrame::fromXYZYPRDegrees(x, y, z, yaw, pitch, roll);
  617. } else {
  618. // Matrix format
  619. for (int r = 0; r < 3; ++r) {
  620. for (int c = 0; c < 3; ++c) {
  621. m.rotation[r][c] = (float)t.readNumber();
  622. }
  623. m.translation[r] = (float)t.readNumber();
  624. if (r != 2) {
  625. t.readSymbol(",");
  626. }
  627. }
  628. }
  629. t.readSymbol(")");
  630. m_value = new CoordinateFrame(m);
  631. m_type = COORDINATEFRAME;
  632. } else if (s == "C1") {
  633. t.readSymbol("(");
  634. float v = (float)t.readNumber();
  635. t.readSymbol(")");
  636. m_value = new Color1(v);
  637. m_type = COLOR1;
  638. } else if (s == "C3") {
  639. t.readSymbol("(");
  640. Color3 c;
  641. c.r = (float)t.readNumber();
  642. t.readSymbol(",");
  643. c.g = (float)t.readNumber();
  644. t.readSymbol(",");
  645. c.b = (float)t.readNumber();
  646. t.readSymbol(")");
  647. m_value = new Color3(c);
  648. m_type = COLOR3;
  649. } else if (s == "C4") {
  650. t.readSymbol("(");
  651. Color4 c;
  652. c.r = (float)t.readNumber();
  653. t.readSymbol(",");
  654. c.g = (float)t.readNumber();
  655. t.readSymbol(",");
  656. c.b = (float)t.readNumber();
  657. t.readSymbol(",");
  658. c.a = (float)t.readNumber();
  659. t.readSymbol(")");
  660. m_value = new Color4(c);
  661. m_type = COLOR4;
  662. } else if (s == "[") {
  663. // Array
  664. m_type = ARRAY;
  665. m_value = new Array<AnyVal>();
  666. m_referenceCount = new int(1);
  667. Array<AnyVal>& a = *(Array<AnyVal>*)m_value;
  668. Token peek = t.peek();
  669. while ((peek.type() != Token::SYMBOL) || (peek.string() != "]")) {
  670. // Avoid copying large objects
  671. a.next().deserialize(t);
  672. peek = t.peek();
  673. if (peek.type() != Token::SYMBOL) {
  674. throw CorruptText("Expected ',' or ']'", peek);
  675. } else if (peek.string() == ",") {
  676. t.readSymbol(",");
  677. } else if (peek.string() != "]") {
  678. throw CorruptText("Missing ']'", peek);
  679. }
  680. }
  681. t.readSymbol("]");
  682. } else if (s == "{") {
  683. // Table
  684. m_type = TABLE;
  685. m_value = new Table<std::string, AnyVal>();
  686. m_referenceCount = new int(1);
  687. Table<std::string, AnyVal>& a = *(Table<std::string, AnyVal>*)m_value;
  688. Token peek = t.peek();
  689. while ((peek.type() != Token::SYMBOL) || (peek.string() != "}")) {
  690. std::string key;
  691. // Get the name
  692. if (peek.type() == Token::SYMBOL) {
  693. key = t.readSymbol();
  694. } else if (peek.extendedType() == Token::SINGLE_QUOTED_TYPE) {
  695. key = t.readString();
  696. } else {
  697. throw CorruptText("Expected name inside table", peek);
  698. }
  699. t.readSymbol("=");
  700. // Avoid copying large values
  701. a.set(key, AnyVal());
  702. a[key].deserialize(t);
  703. peek = t.peek();
  704. if ((peek.type() != Token::SYMBOL) && (peek.extendedType() != Token::SINGLE_QUOTED_TYPE)) {
  705. throw CorruptText("Missing expected name or '}'", peek);
  706. }
  707. }
  708. t.readSymbol("}");
  709. } else {
  710. throw CorruptText("Invalid value type.", t.peek());
  711. } // dispatch on symbol type
  712. } // scope
  713. break;
  714. }
  715. }
  716. AnyVal& AnyVal::operator[](const char* key) {
  717. return this->operator[]((std::string)key);
  718. }
  719. const AnyVal& AnyVal::operator[](const char* key) const {
  720. return this->operator[]((std::string)key);
  721. }
  722. AnyVal& AnyVal::operator[](const std::string& key) {
  723. if (m_type != TABLE) {
  724. throw WrongType(TABLE, m_type);
  725. }
  726. makeMutable();
  727. Table<std::string, AnyVal>& t = *(Table<std::string, AnyVal>*)m_value;
  728. if (! t.containsKey(key)) {
  729. t.set(key, AnyVal());
  730. }
  731. return t[key];
  732. }
  733. const AnyVal& AnyVal::operator[](const std::string& key) const {
  734. if (m_type != TABLE) {
  735. throw WrongType(TABLE, m_type);
  736. }
  737. const Table<std::string, AnyVal>& t = *(const Table<std::string, AnyVal>*)m_value;
  738. if (! t.containsKey(key)) {
  739. throw KeyNotFound(key);
  740. }
  741. return t[key];
  742. }
  743. void AnyVal::append(const AnyVal& v) {
  744. if (m_type != ARRAY) {
  745. throw WrongType(ARRAY, m_type);
  746. }
  747. makeMutable();
  748. Array<AnyVal>& a = *(Array<AnyVal>*)m_value;
  749. a.append(v);
  750. }
  751. void AnyVal::getKeys(Array<std::string>& keys) const {
  752. if (m_type != TABLE) {
  753. throw WrongType(TABLE, m_type);
  754. }
  755. const Table<std::string, AnyVal>& t = *(const Table<std::string, AnyVal>*)m_value;
  756. t.getKeys(keys);
  757. }
  758. int AnyVal::size() const {
  759. switch (m_type) {
  760. case TABLE:
  761. {
  762. const Table<std::string, AnyVal>& t = *(const Table<std::string, AnyVal>*)m_value;
  763. return t.size();
  764. }
  765. case ARRAY:
  766. {
  767. const Array<AnyVal>& a = *(Array<AnyVal>*)m_value;
  768. return a.size();
  769. }
  770. default:
  771. throw WrongType(ARRAY, m_type);
  772. }
  773. }
  774. AnyVal& AnyVal::operator[](int i) {
  775. if (m_type != ARRAY) {
  776. throw WrongType(ARRAY, m_type);
  777. }
  778. makeMutable();
  779. Array<AnyVal>& a = *(Array<AnyVal>*)m_value;
  780. if (i < 0) {
  781. throw IndexOutOfBounds(i, a.size());
  782. }
  783. if (a.size() <= i) {
  784. a.resize(i + 1);
  785. }
  786. return a[i];
  787. }
  788. const AnyVal& AnyVal::operator[](int i) const {
  789. if (m_type != ARRAY) {
  790. throw WrongType(ARRAY, m_type);
  791. }
  792. const Array<AnyVal>& a = *(Array<AnyVal>*)m_value;
  793. if (a.size() <= i || i < 0) {
  794. throw IndexOutOfBounds(i, a.size());
  795. }
  796. return a[i];
  797. }
  798. void AnyVal::makeMutable() {
  799. if (*m_referenceCount > 1) {
  800. // This is a shared instance
  801. --(*m_referenceCount);
  802. m_referenceCount = new int(1);
  803. m_value = copyValue();
  804. }
  805. }
  806. bool AnyVal::boolean() const {
  807. if (m_type != BOOLEAN) {
  808. throw WrongType(BOOLEAN, m_type);
  809. }
  810. return *(bool*)m_value;
  811. }
  812. bool AnyVal::boolean(bool defaultVal) const {
  813. if (m_type != BOOLEAN) {
  814. return defaultVal;
  815. }
  816. return *(bool*)m_value;
  817. }
  818. const std::string& AnyVal::string() const {
  819. if (m_type != STRING) {
  820. throw WrongType(STRING, m_type);
  821. }
  822. return *(std::string*)m_value;
  823. }
  824. const std::string& AnyVal::string(const std::string& defaultVal) const {
  825. if (m_type != STRING) {
  826. return defaultVal;
  827. } else {
  828. return *(std::string*)m_value;
  829. }
  830. }
  831. double AnyVal::number() const {
  832. if (m_type != NUMBER) {
  833. throw WrongType(NUMBER, m_type);
  834. }
  835. return *(double*)m_value;
  836. }
  837. double AnyVal::number(double defaultVal) const {
  838. if (m_type != NUMBER) {
  839. return defaultVal;
  840. } else {
  841. return *(double*)m_value;
  842. }
  843. }
  844. const Rect2D& AnyVal::rect2D() const {
  845. if (m_type != RECT2D) {
  846. throw WrongType(RECT2D, m_type);
  847. }
  848. return *(Rect2D*)m_value;
  849. }
  850. const Rect2D& AnyVal::rect2D(const Rect2D& defaultVal) const {
  851. if (m_type != RECT2D) {
  852. return defaultVal;
  853. } else {
  854. return *(Rect2D*)m_value;
  855. }
  856. }
  857. const AABox& AnyVal::aabox() const {
  858. if (m_type != AABOX) {
  859. throw WrongType(AABOX, m_type);
  860. }
  861. return *(AABox*)m_value;
  862. }
  863. const AABox& AnyVal::aabox(const AABox& defaultVal) const {
  864. if (m_type != AABOX) {
  865. return defaultVal;
  866. } else {
  867. return *(AABox*)m_value;
  868. }
  869. }
  870. const Color1& AnyVal::color1() const {
  871. if (m_type != COLOR1) {
  872. throw WrongType(COLOR1, m_type);
  873. }
  874. return *(Color1*)m_value;
  875. }
  876. const Color1& AnyVal::color1(const Color1& defaultVal) const {
  877. if (m_type != COLOR1) {
  878. return defaultVal;
  879. } else {
  880. return *(Color1*)m_value;
  881. }
  882. }
  883. const Color3& AnyVal::color3() const {
  884. if (m_type != COLOR3) {
  885. throw WrongType(COLOR3, m_type);
  886. }
  887. return *(Color3*)m_value;
  888. }
  889. const Color3& AnyVal::color3(const Color3& defaultVal) const {
  890. if (m_type != COLOR3) {
  891. return defaultVal;
  892. } else {
  893. return *(Color3*)m_value;
  894. }
  895. }
  896. const Color4& AnyVal::color4() const {
  897. if (m_type != COLOR4) {
  898. throw WrongType(COLOR4, m_type);
  899. }
  900. return *(Color4*)m_value;
  901. }
  902. const Color4& AnyVal::color4(const Color4& defaultVal) const {
  903. if (m_type != COLOR4) {
  904. return defaultVal;
  905. } else {
  906. return *(Color4*)m_value;
  907. }
  908. }
  909. const Vector2& AnyVal::vector2() const {
  910. if (m_type != VECTOR2) {
  911. throw WrongType(VECTOR2, m_type);
  912. }
  913. return *(Vector2*)m_value;
  914. }
  915. const Vector2& AnyVal::vector2(const Vector2& defaultVal) const {
  916. if (m_type != VECTOR2) {
  917. return defaultVal;
  918. } else {
  919. return *(Vector2*)m_value;
  920. }
  921. }
  922. const Vector3& AnyVal::vector3() const {
  923. if (m_type != VECTOR3) {
  924. throw WrongType(VECTOR3, m_type);
  925. }
  926. return *(Vector3*)m_value;
  927. }
  928. const Vector3& AnyVal::vector3(const Vector3& defaultVal) const {
  929. if (m_type != VECTOR3) {
  930. return defaultVal;
  931. } else {
  932. return *(Vector3*)m_value;
  933. }
  934. }
  935. const Vector4& AnyVal::vector4() const {
  936. if (m_type != VECTOR4) {
  937. throw WrongType(VECTOR4, m_type);
  938. }
  939. return *(Vector4*)m_value;
  940. }
  941. const Vector4& AnyVal::vector4(const Vector4& defaultVal) const {
  942. if (m_type != VECTOR4) {
  943. return defaultVal;
  944. } else {
  945. return *(Vector4*)m_value;
  946. }
  947. }
  948. const CoordinateFrame& AnyVal::coordinateFrame() const {
  949. if (m_type != COORDINATEFRAME) {
  950. throw WrongType(COORDINATEFRAME, m_type);
  951. }
  952. return *(CoordinateFrame*)m_value;
  953. }
  954. const CoordinateFrame& AnyVal::coordinateFrame(const CoordinateFrame& defaultVal) const {
  955. if (m_type != COORDINATEFRAME) {
  956. return defaultVal;
  957. } else {
  958. return *(CoordinateFrame*)m_value;
  959. }
  960. }
  961. const Matrix2& AnyVal::matrix2(const Matrix2& defaultVal) const {
  962. if (m_type != MATRIX2) {
  963. return defaultVal;
  964. } else {
  965. return *(Matrix2*)m_value;
  966. }
  967. }
  968. const Matrix2& AnyVal::matrix2() const {
  969. if (m_type != MATRIX2) {
  970. throw WrongType(MATRIX2, m_type);
  971. }
  972. return *(Matrix2*)m_value;
  973. }
  974. const Matrix3& AnyVal::matrix3(const Matrix3& defaultVal) const {
  975. if (m_type != MATRIX3) {
  976. return defaultVal;
  977. } else {
  978. return *(Matrix3*)m_value;
  979. }
  980. }
  981. const Matrix3& AnyVal::matrix3() const {
  982. if (m_type != MATRIX3) {
  983. throw WrongType(MATRIX3, m_type);
  984. }
  985. return *(Matrix3*)m_value;
  986. }
  987. const Matrix4& AnyVal::matrix4(const Matrix4& defaultVal) const {
  988. if (m_type != MATRIX4) {
  989. return defaultVal;
  990. } else {
  991. return *(Matrix4*)m_value;
  992. }
  993. }
  994. const Matrix4& AnyVal::matrix4() const {
  995. if (m_type != MATRIX4) {
  996. throw WrongType(MATRIX4, m_type);
  997. }
  998. return *(Matrix4*)m_value;
  999. }
  1000. const Quat& AnyVal::quat(const Quat& defaultVal) const {
  1001. if (m_type != QUAT) {
  1002. return defaultVal;
  1003. } else {
  1004. return *(Quat*)m_value;
  1005. }
  1006. }
  1007. const Quat& AnyVal::quat() const {
  1008. if (m_type != QUAT) {
  1009. throw WrongType(QUAT, m_type);
  1010. }
  1011. return *(Quat*)m_value;
  1012. }
  1013. const AnyVal& AnyVal::get(const std::string& key, const AnyVal& defaultVal) const {
  1014. if (m_type != TABLE) {
  1015. return defaultVal;
  1016. }
  1017. const Table<std::string, AnyVal>& t = *(const Table<std::string, AnyVal>*)m_value;
  1018. if (t.containsKey(key)) {
  1019. return t[key];
  1020. } else {
  1021. return defaultVal;
  1022. }
  1023. }
  1024. const AnyVal& AnyVal::get(const std::string& key) const {
  1025. if (m_type != TABLE) {
  1026. throw WrongType(TABLE, m_type);
  1027. }
  1028. const Table<std::string, AnyVal>& t = *(const Table<std::string, AnyVal>*)m_value;
  1029. if (t.containsKey(key)) {
  1030. return t[key];
  1031. } else {
  1032. throw KeyNotFound(key);
  1033. }
  1034. }
  1035. const AnyVal& AnyVal::get(int i, const AnyVal& defaultVal) const {
  1036. if (m_type != ARRAY) {
  1037. return defaultVal;
  1038. }
  1039. const Array<AnyVal>& a = *(const Array<AnyVal>*)m_value;
  1040. if ((i >= 0) && (i < a.size())) {
  1041. return a[i];
  1042. } else {
  1043. return defaultVal;
  1044. }
  1045. }
  1046. const AnyVal& AnyVal::get(int i) const {
  1047. if (m_type != ARRAY) {
  1048. throw WrongType(ARRAY, m_type);
  1049. }
  1050. const Array<AnyVal>& a = *(const Array<AnyVal>*)m_value;
  1051. if ((i >= 0) && (i < a.size())) {
  1052. return a[i];
  1053. } else {
  1054. throw IndexOutOfBounds(i, a.size());
  1055. }
  1056. }
  1057. }