00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(_XRB_DATAFILEVALUE_HPP_)
00012 #define _XRB_DATAFILEVALUE_HPP_
00013
00014 #include "xrb.hpp"
00015
00016 #include <map>
00017 #include <stdio.h>
00018 #include <string>
00019 #include <vector>
00020
00021 #include "xrb_util.hpp"
00022 #include "xrb_indentformatter.hpp"
00023
00024 namespace Xrb
00025 {
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 class DataFileBoolean;
00202 class DataFileInteger;
00203 class DataFileFloat;
00204 class DataFileCharacter;
00205 class DataFileString;
00206 class DataFileArray;
00207 class DataFileStructure;
00208
00209 enum DataFileElementType
00210 {
00211 DAT_BOOLEAN = 0,
00212 DAT_SINT32,
00213 DAT_UINT32,
00214 DAT_FLOAT,
00215 DAT_CHARACTER,
00216 DAT_STRING,
00217 DAT_KEY_PAIR,
00218 DAT_ARRAY,
00219 DAT_STRUCTURE,
00220 DAT_NO_TYPE,
00221
00222 DAT_COUNT
00223 };
00224
00225 enum NumericSign
00226 {
00227 NEGATIVE = 0,
00228 POSITIVE = 1
00229 };
00230
00231 std::string const &DataFileElementTypeString (DataFileElementType data_file_element_type);
00232
00268 class DataFileValue
00269 {
00270 public:
00271
00272 virtual ~DataFileValue () { }
00273
00274 virtual DataFileElementType ElementType () const = 0;
00275 inline DataFileValue const *PathElement (std::string const &path) const { return SubpathElement(path, 0); }
00276
00277
00278 bool PathElementBoolean (std::string const &path) const throw (std::string);
00279 Sint32 PathElementSint32 (std::string const &path) const throw (std::string);
00280 Uint32 PathElementUint32 (std::string const &path) const throw (std::string);
00281 Float PathElementFloat (std::string const &path) const throw (std::string);
00282 char PathElementCharacter (std::string const &path) const throw (std::string);
00283 std::string const &PathElementString (std::string const &path) const throw (std::string);
00284 DataFileArray const *PathElementArray (std::string const &path) const throw (std::string);
00285 DataFileStructure const *PathElementStructure (std::string const &path) const throw (std::string);
00286
00287 virtual void Print (IndentFormatter &formatter) const = 0;
00288 virtual void PrintAST (IndentFormatter &formatter) const = 0;
00289
00290 protected:
00291
00292 virtual DataFileValue const *SubpathElement (
00293 std::string const &path,
00294 Uint32 start) const = 0;
00295
00296
00297
00298 friend class DataFileKeyPair;
00299 friend class DataFileArray;
00300 friend class DataFileStructure;
00301 };
00302
00303
00304
00305
00306
00307 class DataFileLeafValue : public DataFileValue
00308 {
00309 public:
00310
00311 DataFileLeafValue ()
00312 :
00313 DataFileValue()
00314 { }
00315 virtual ~DataFileLeafValue () = 0;
00316
00317 protected:
00318
00319 virtual DataFileValue const *SubpathElement (
00320 std::string const &path,
00321 Uint32 start) const;
00322 };
00323
00324
00325
00326
00327
00328 class DataFileBoolean : public DataFileLeafValue
00329 {
00330 public:
00331
00332 DataFileBoolean (bool value)
00333 :
00334 DataFileLeafValue(),
00335 m_value(value)
00336 { }
00337
00338 inline bool Value () const { return m_value; }
00339
00340 virtual DataFileElementType ElementType () const { return DAT_BOOLEAN; }
00341
00342 virtual void Print (IndentFormatter &formatter) const
00343 {
00344 formatter.BeginLine("%s", BOOL_TO_STRING(m_value));
00345 }
00346 virtual void PrintAST (IndentFormatter &formatter) const;
00347
00348 private:
00349
00350 bool const m_value;
00351 };
00352
00353
00354
00355
00356
00357 class DataFileSint32 : public DataFileLeafValue
00358 {
00359 public:
00360
00361 DataFileSint32 (Uint32 value)
00362 :
00363 DataFileLeafValue(),
00364 m_value(value)
00365 { }
00366
00367 inline Sint32 Value () const { return m_value; }
00368
00369 virtual DataFileElementType ElementType () const { return DAT_SINT32; }
00370
00371 virtual void Print (IndentFormatter &formatter) const { formatter.BeginLine("%+d", m_value); }
00372 virtual void PrintAST (IndentFormatter &formatter) const;
00373
00374 private:
00375
00376 Sint32 m_value;
00377 };
00378
00379
00380
00381
00382
00383 class DataFileUint32 : public DataFileLeafValue
00384 {
00385 public:
00386
00387 DataFileUint32 (Uint32 value)
00388 :
00389 DataFileLeafValue(),
00390 m_value(value)
00391 { }
00392
00393 inline Uint32 Value () const { return m_value; }
00394
00395 virtual DataFileElementType ElementType () const { return DAT_UINT32; }
00396
00397 virtual void Print (IndentFormatter &formatter) const { formatter.BeginLine("%u", m_value); }
00398 virtual void PrintAST (IndentFormatter &formatter) const;
00399
00400 private:
00401
00402 Uint32 m_value;
00403 };
00404
00405
00406
00407
00408
00409 class DataFileFloat : public DataFileLeafValue
00410 {
00411 public:
00412
00413 DataFileFloat (Float value)
00414 :
00415 DataFileLeafValue(),
00416 m_value(value)
00417 { }
00418
00419 inline Float Value () const { return m_value; }
00420
00421 virtual DataFileElementType ElementType () const { return DAT_FLOAT; }
00422
00423 void Sign (NumericSign sign);
00424
00425 virtual void Print (IndentFormatter &formatter) const
00426 {
00427 formatter.BeginLine("%e", m_value);
00428 }
00429 virtual void PrintAST (IndentFormatter &formatter) const;
00430
00431 private:
00432
00433 Float m_value;
00434 };
00435
00436
00437
00438
00439
00440 class DataFileCharacter : public DataFileLeafValue
00441 {
00442 public:
00443
00444 DataFileCharacter (char value)
00445 :
00446 DataFileLeafValue(),
00447 m_value(value)
00448 { }
00449
00450 inline char Value () const { return m_value; }
00451
00452 virtual DataFileElementType ElementType () const { return DAT_CHARACTER; }
00453
00454 virtual void Print (IndentFormatter &formatter) const
00455 {
00456 formatter.BeginLine("%s", Util::CharacterLiteral(m_value).c_str());
00457 }
00458 virtual void PrintAST (IndentFormatter &formatter) const;
00459
00460 private:
00461
00462 char const m_value;
00463 };
00464
00465
00466
00467
00468
00469 class DataFileString : public DataFileLeafValue
00470 {
00471 public:
00472
00473 DataFileString ()
00474 :
00475 DataFileLeafValue()
00476 { }
00477 DataFileString (std::string const &value)
00478 :
00479 DataFileLeafValue(),
00480 m_value(value)
00481 { }
00482
00483 inline std::string const &Value () const { return m_value; }
00484
00485 inline void AppendString (std::string const &string) { m_value += string; }
00486 inline void AppendCharacter (char const character) { m_value += character; }
00487
00488 virtual DataFileElementType ElementType () const { return DAT_STRING; }
00489
00490 virtual void Print (IndentFormatter &formatter) const;
00491 virtual void PrintAST (IndentFormatter &formatter) const;
00492
00493 private:
00494
00495 std::string m_value;
00496 };
00497
00498
00499
00500
00501
00502 class DataFileContainer : public DataFileValue
00503 {
00504 public:
00505
00506 DataFileContainer ()
00507 :
00508 DataFileValue()
00509 { }
00510
00511 void SetPathElementBoolean (std::string const &path, bool value) throw(std::string);
00512 void SetPathElementSint32 (std::string const &path, Sint32 value) throw(std::string);
00513 void SetPathElementUint32 (std::string const &path, Uint32 value) throw(std::string);
00514 void SetPathElementFloat (std::string const &path, Float value) throw(std::string);
00515 void SetPathElementCharacter (std::string const &path, char value) throw(std::string);
00516 void SetPathElementString (std::string const &path, std::string const &value) throw(std::string);
00517
00518 protected:
00519
00520 enum NodeType
00521 {
00522 NT_LEAF = 0,
00523 NT_ARRAY,
00524 NT_STRUCTURE
00525 };
00526
00527 static NodeType ParentElementNodeType (std::string const &path, Uint32 start) throw(std::string);
00528
00529 virtual void SetSubpathElement (
00530 std::string const &path,
00531 Uint32 start,
00532 DataFileLeafValue *value) throw(std::string) = 0;
00533
00534 friend class DataFileKeyPair;
00535 friend class DataFileArray;
00536 friend class DataFileStructure;
00537 };
00538
00539
00540
00541
00542
00543 class DataFileKeyPair : public DataFileContainer
00544 {
00545 public:
00546
00547 DataFileKeyPair (std::string const &key, DataFileValue *value)
00548 :
00549 DataFileContainer(),
00550 m_key(key),
00551 m_value(value)
00552 {
00553 ASSERT1(m_key.length() > 0);
00554 }
00555 virtual ~DataFileKeyPair ()
00556 {
00557 delete m_value;
00558 }
00559
00560 inline std::string const &GetKey () const { return m_key; }
00561 inline DataFileValue *Value () const { return m_value; }
00562
00563 virtual DataFileElementType ElementType () const { return DAT_KEY_PAIR; }
00564
00565 virtual void Print (IndentFormatter &formatter) const;
00566 virtual void PrintAST (IndentFormatter &formatter) const;
00567
00568 protected:
00569
00570 virtual DataFileValue const *SubpathElement (
00571 std::string const &path,
00572 Uint32 start) const;
00573
00574 virtual void SetSubpathElement (
00575 std::string const &path,
00576 Uint32 start,
00577 DataFileLeafValue *value) throw(std::string);
00578
00579 private:
00580
00581 std::string const m_key;
00582 DataFileValue *m_value;
00583 };
00584
00585
00586
00587
00588
00589 class DataFileArray : public DataFileContainer
00590 {
00591 public:
00592
00593 DataFileArray ()
00594 :
00595 DataFileContainer()
00596 { }
00597 virtual ~DataFileArray ();
00598
00599 bool ShouldBeFormattedInline () const;
00600 DataFileElementType ArrayElementType () const;
00601 DataFileElementType UltimateArrayElementType () const;
00602 Uint32 DimensionCount () const;
00603 inline Uint32 ElementCount () const { return m_element_vector.size(); }
00604 inline DataFileValue *Element (Uint32 index) const
00605 {
00606 return index < m_element_vector.size() ? m_element_vector[index] : NULL;
00607 }
00608
00609 void AppendValue (DataFileValue *value);
00610
00611 virtual DataFileElementType ElementType () const { return DAT_ARRAY; }
00612
00613 virtual void Print (IndentFormatter &formatter) const;
00614 virtual void PrintAST (IndentFormatter &formatter) const;
00615
00616 protected:
00617
00618 virtual DataFileValue const *SubpathElement (
00619 std::string const &path,
00620 Uint32 start) const;
00621
00622 virtual void SetSubpathElement (
00623 std::string const &path,
00624 Uint32 start,
00625 DataFileLeafValue *value) throw(std::string);
00626
00627 private:
00628
00629 std::string DimensionAndTypeString () const;
00630 static bool DoesMatchDimensionAndType (DataFileArray const *array0, DataFileArray const *array1);
00631
00632 typedef std::vector<DataFileValue *> ElementVector;
00633
00634 ElementVector m_element_vector;
00635 };
00636
00637
00638
00639
00640
00641 class DataFileStructure : public DataFileContainer
00642 {
00643 public:
00644
00645 DataFileStructure ()
00646 :
00647 DataFileContainer()
00648 { }
00649 virtual ~DataFileStructure ();
00650
00651 DataFileValue const *Value (std::string const &key) const;
00652
00653 void AddKeyPair (std::string const &key, DataFileValue *value);
00654 void AddKeyPair (DataFileKeyPair *key_value_pair);
00655
00656 virtual DataFileElementType ElementType () const { return DAT_STRUCTURE; }
00657
00658 virtual void Print (IndentFormatter &formatter) const;
00659 virtual void PrintAST (IndentFormatter &formatter) const;
00660
00661 protected:
00662
00663 virtual DataFileValue const *SubpathElement (
00664 std::string const &path,
00665 Uint32 start) const;
00666
00667 virtual void SetSubpathElement (
00668 std::string const &path,
00669 Uint32 start,
00670 DataFileLeafValue *value) throw(std::string);
00671
00672 private:
00673
00674 static bool IsValidKey (std::string const &key);
00675
00676 typedef std::map<std::string, DataFileKeyPair *> MemberMap;
00677
00678 MemberMap m_member_map;
00679 };
00680
00681 }
00682
00683 #endif // !defined(_XRB_DATAFILEVALUE_HPP_)
00684