RunArrays.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. **********************************************************************
  3. * Copyright (C) 2003-2008, International Business Machines
  4. * Corporation and others. All Rights Reserved.
  5. **********************************************************************
  6. */
  7. #ifndef __RUNARRAYS_H
  8. #define __RUNARRAYS_H
  9. #include "layout/LETypes.h"
  10. #include "layout/LEFontInstance.h"
  11. #include "unicode/utypes.h"
  12. #include "unicode/locid.h"
  13. /**
  14. * \file
  15. * \brief C++ API: base class for building classes which represent data that is associated with runs of text.
  16. */
  17. U_NAMESPACE_BEGIN
  18. /**
  19. * The initial size of an array if it is unspecified.
  20. *
  21. * @stable ICU 3.2
  22. */
  23. #define INITIAL_CAPACITY 16
  24. /**
  25. * When an array needs to grow, it will double in size until
  26. * it becomes this large, then it will grow by this amount.
  27. *
  28. * @stable ICU 3.2
  29. */
  30. #define CAPACITY_GROW_LIMIT 128
  31. /**
  32. * The <code>RunArray</code> class is a base class for building classes
  33. * which represent data that is associated with runs of text. This class
  34. * maintains an array of limit indices into the text, subclasses
  35. * provide one or more arrays of data.
  36. *
  37. * @stable ICU 3.2
  38. */
  39. class U_LAYOUTEX_API RunArray : public UObject
  40. {
  41. public:
  42. /**
  43. * Construct a <code>RunArray</code> object from a pre-existing
  44. * array of limit indices.
  45. *
  46. * @param limits is an array of limit indices. This array must remain
  47. * valid until the <code>RunArray</code> object is destroyed.
  48. *
  49. * @param count is the number of entries in the limit array.
  50. *
  51. * @stable ICU 3.2
  52. */
  53. inline RunArray(const le_int32 *limits, le_int32 count);
  54. /**
  55. * Construct an empty <code>RunArray</code> object. Clients can add limit
  56. * indices array using the <code>add</code> method.
  57. *
  58. * @param initialCapacity is the initial size of the limit indices array. If
  59. * this value is zero, no array will be allocated.
  60. *
  61. * @see add
  62. *
  63. * @stable ICU 3.2
  64. */
  65. RunArray(le_int32 initialCapacity);
  66. /**
  67. * The destructor; virtual so that subclass destructors are invoked as well.
  68. *
  69. * @stable ICU 3.2
  70. */
  71. virtual ~RunArray();
  72. /**
  73. * Get the number of entries in the limit indices array.
  74. *
  75. * @return the number of entries in the limit indices array.
  76. *
  77. * @stable ICU 3.2
  78. */
  79. inline le_int32 getCount() const;
  80. /**
  81. * Reset the limit indices array. This method sets the number of entries in the
  82. * limit indices array to zero. It does not delete the array.
  83. *
  84. * Note: Subclass arrays will also be reset and not deleted.
  85. *
  86. * @stable ICU 3.6
  87. */
  88. inline void reset();
  89. /**
  90. * Get the last limit index. This is the number of characters in
  91. * the text.
  92. *
  93. * @return the last limit index.
  94. *
  95. * @stable ICU 3.2
  96. */
  97. inline le_int32 getLimit() const;
  98. /**
  99. * Get the limit index for a particular run of text.
  100. *
  101. * @param run is the run. This is an index into the limit index array.
  102. *
  103. * @return the limit index for the run, or -1 if <code>run</code> is out of bounds.
  104. *
  105. * @stable ICU 3.2
  106. */
  107. inline le_int32 getLimit(le_int32 run) const;
  108. /**
  109. * Add a limit index to the limit indices array and return the run index
  110. * where it was stored. If the array does not exist, it will be created by
  111. * calling the <code>init</code> method. If it is full, it will be grown by
  112. * calling the <code>grow</code> method.
  113. *
  114. * If the <code>RunArray</code> object was created with a client-supplied
  115. * limit indices array, this method will return a run index of -1.
  116. *
  117. * Subclasses should not override this method. Rather they should provide
  118. * a new <code>add</code> method which takes a limit index along with whatever
  119. * other data they implement. The new <code>add</code> method should
  120. * first call this method to grow the data arrays, and use the return value
  121. * to store the data in their own arrays.
  122. *
  123. * @param limit is the limit index to add to the array.
  124. *
  125. * @return the run index where the limit index was stored, or -1 if the limit index cannt be stored.
  126. *
  127. * @see init
  128. * @see grow
  129. *
  130. * @stable ICU 3.2
  131. */
  132. le_int32 add(le_int32 limit);
  133. /**
  134. * ICU "poor man's RTTI", returns a UClassID for this class.
  135. *
  136. * @stable ICU 3.2
  137. */
  138. static inline UClassID getStaticClassID() { return (UClassID)&fgClassID; }
  139. /**
  140. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  141. *
  142. * @stable ICU 3.2
  143. */
  144. virtual inline UClassID getDynamicClassID() const { return getStaticClassID(); }
  145. protected:
  146. /**
  147. * Create a data array with the given initial size. This method will be
  148. * called by the <code>add</code> method if there is no limit indices
  149. * array. Subclasses which override this method must also call it from
  150. * the overriding method to create the limit indices array.
  151. *
  152. * @param capacity is the initial size of the data array.
  153. *
  154. * @see add
  155. *
  156. * @stable ICU 3.2
  157. */
  158. virtual void init(le_int32 capacity);
  159. /**
  160. * Grow a data array to the given initial size. This method will be
  161. * called by the <code>add</code> method if the limit indices
  162. * array is full. Subclasses which override this method must also call it from
  163. * the overriding method to grow the limit indices array.
  164. *
  165. * @param capacity is the initial size of the data array.
  166. *
  167. * @see add
  168. *
  169. * @stable ICU 3.2
  170. */
  171. virtual void grow(le_int32 capacity);
  172. /**
  173. * Set by the constructors to indicate whether
  174. * or not the client supplied the data arrays.
  175. * If they were supplied by the client, the
  176. * <code>add</code> method won't change the arrays
  177. * and the destructor won't delete them.
  178. *
  179. * @stable ICU 3.2
  180. */
  181. le_bool fClientArrays;
  182. private:
  183. /**
  184. * The address of this static class variable serves as this class's ID
  185. * for ICU "poor man's RTTI".
  186. */
  187. static const char fgClassID;
  188. le_int32 ensureCapacity();
  189. inline RunArray();
  190. inline RunArray(const RunArray & /*other*/);
  191. inline RunArray &operator=(const RunArray & /*other*/) { return *this; };
  192. const le_int32 *fLimits;
  193. le_int32 fCount;
  194. le_int32 fCapacity;
  195. };
  196. inline RunArray::RunArray()
  197. : UObject(), fClientArrays(FALSE), fLimits(NULL), fCount(0), fCapacity(0)
  198. {
  199. // nothing else to do...
  200. }
  201. inline RunArray::RunArray(const RunArray & /*other*/)
  202. : UObject(), fClientArrays(FALSE), fLimits(NULL), fCount(0), fCapacity(0)
  203. {
  204. // nothing else to do...
  205. }
  206. inline RunArray::RunArray(const le_int32 *limits, le_int32 count)
  207. : UObject(), fClientArrays(TRUE), fLimits(limits), fCount(count), fCapacity(count)
  208. {
  209. // nothing else to do...
  210. }
  211. inline le_int32 RunArray::getCount() const
  212. {
  213. return fCount;
  214. }
  215. inline void RunArray::reset()
  216. {
  217. fCount = 0;
  218. }
  219. inline le_int32 RunArray::getLimit(le_int32 run) const
  220. {
  221. if (run < 0 || run >= fCount) {
  222. return -1;
  223. }
  224. return fLimits[run];
  225. }
  226. inline le_int32 RunArray::getLimit() const
  227. {
  228. return getLimit(fCount - 1);
  229. }
  230. /**
  231. * The <code>FontRuns</code> class associates pointers to <code>LEFontInstance</code>
  232. * objects with runs of text.
  233. *
  234. * @stable ICU 3.2
  235. */
  236. class U_LAYOUTEX_API FontRuns : public RunArray
  237. {
  238. public:
  239. /**
  240. * Construct a <code>FontRuns</code> object from pre-existing arrays of fonts
  241. * and limit indices.
  242. *
  243. * @param fonts is the address of an array of pointers to <code>LEFontInstance</code> objects. This
  244. * array, and the <code>LEFontInstance</code> objects to which it points must remain
  245. * valid until the <code>FontRuns</code> object is destroyed.
  246. *
  247. * @param limits is the address of an array of limit indices. This array must remain valid until
  248. * the <code>FontRuns</code> object is destroyed.
  249. *
  250. * @param count is the number of entries in the two arrays.
  251. *
  252. * @stable ICU 3.2
  253. */
  254. inline FontRuns(const LEFontInstance **fonts, const le_int32 *limits, le_int32 count);
  255. /**
  256. * Construct an empty <code>FontRuns</code> object. Clients can add font and limit
  257. * indices arrays using the <code>add</code> method.
  258. *
  259. * @param initialCapacity is the initial size of the font and limit indices arrays. If
  260. * this value is zero, no arrays will be allocated.
  261. *
  262. * @see add
  263. *
  264. * @stable ICU 3.2
  265. */
  266. FontRuns(le_int32 initialCapacity);
  267. /**
  268. * The destructor; virtual so that subclass destructors are invoked as well.
  269. *
  270. * @stable ICU 3.2
  271. */
  272. virtual ~FontRuns();
  273. /**
  274. * Get the <code>LEFontInstance</code> object assoicated with the given run
  275. * of text. Use <code>RunArray::getLimit(run)</code> to get the corresponding
  276. * limit index.
  277. *
  278. * @param run is the index into the font and limit indices arrays.
  279. *
  280. * @return the <code>LEFontInstance</code> associated with the given text run.
  281. *
  282. * @see RunArray::getLimit
  283. *
  284. * @stable ICU 3.2
  285. */
  286. const LEFontInstance *getFont(le_int32 run) const;
  287. /**
  288. * Add an <code>LEFontInstance</code> and limit index pair to the data arrays and return
  289. * the run index where the data was stored. This method calls
  290. * <code>RunArray::add(limit)</code> which will create or grow the arrays as needed.
  291. *
  292. * If the <code>FontRuns</code> object was created with a client-supplied
  293. * font and limit indices arrays, this method will return a run index of -1.
  294. *
  295. * Subclasses should not override this method. Rather they should provide a new <code>add</code>
  296. * method which takes a font and a limit index along with whatever other data they implement.
  297. * The new <code>add</code> method should first call this method to grow the font and limit indices
  298. * arrays, and use the returned run index to store data their own arrays.
  299. *
  300. * @param font is the address of the <code>LEFontInstance</code> to add. This object must
  301. * remain valid until the <code>FontRuns</code> object is destroyed.
  302. *
  303. * @param limit is the limit index to add
  304. *
  305. * @return the run index where the font and limit index were stored, or -1 if the data cannot be stored.
  306. *
  307. * @stable ICU 3.2
  308. */
  309. le_int32 add(const LEFontInstance *font, le_int32 limit);
  310. /**
  311. * ICU "poor man's RTTI", returns a UClassID for this class.
  312. *
  313. * @stable ICU 3.2
  314. */
  315. static inline UClassID getStaticClassID() { return (UClassID)&fgClassID; }
  316. /**
  317. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  318. *
  319. * @stable ICU 3.2
  320. */
  321. virtual inline UClassID getDynamicClassID() const { return getStaticClassID(); }
  322. protected:
  323. virtual void init(le_int32 capacity);
  324. virtual void grow(le_int32 capacity);
  325. private:
  326. inline FontRuns();
  327. inline FontRuns(const FontRuns &other);
  328. inline FontRuns &operator=(const FontRuns & /*other*/) { return *this; };
  329. /**
  330. * The address of this static class variable serves as this class's ID
  331. * for ICU "poor man's RTTI".
  332. */
  333. static const char fgClassID;
  334. const LEFontInstance **fFonts;
  335. };
  336. inline FontRuns::FontRuns()
  337. : RunArray(0), fFonts(NULL)
  338. {
  339. // nothing else to do...
  340. }
  341. inline FontRuns::FontRuns(const FontRuns & /*other*/)
  342. : RunArray(0), fFonts(NULL)
  343. {
  344. // nothing else to do...
  345. }
  346. inline FontRuns::FontRuns(const LEFontInstance **fonts, const le_int32 *limits, le_int32 count)
  347. : RunArray(limits, count), fFonts(fonts)
  348. {
  349. // nothing else to do...
  350. }
  351. /**
  352. * The <code>LocaleRuns</code> class associates pointers to <code>Locale</code>
  353. * objects with runs of text.
  354. *
  355. * @stable ICU 3.2
  356. */
  357. class U_LAYOUTEX_API LocaleRuns : public RunArray
  358. {
  359. public:
  360. /**
  361. * Construct a <code>LocaleRuns</code> object from pre-existing arrays of locales
  362. * and limit indices.
  363. *
  364. * @param locales is the address of an array of pointers to <code>Locale</code> objects. This array,
  365. * and the <code>Locale</code> objects to which it points, must remain valid until
  366. * the <code>LocaleRuns</code> object is destroyed.
  367. *
  368. * @param limits is the address of an array of limit indices. This array must remain valid until the
  369. * <code>LocaleRuns</code> object is destroyed.
  370. *
  371. * @param count is the number of entries in the two arrays.
  372. *
  373. * @stable ICU 3.2
  374. */
  375. inline LocaleRuns(const Locale **locales, const le_int32 *limits, le_int32 count);
  376. /**
  377. * Construct an empty <code>LocaleRuns</code> object. Clients can add locale and limit
  378. * indices arrays using the <code>add</code> method.
  379. *
  380. * @param initialCapacity is the initial size of the locale and limit indices arrays. If
  381. * this value is zero, no arrays will be allocated.
  382. *
  383. * @see add
  384. *
  385. * @stable ICU 3.2
  386. */
  387. LocaleRuns(le_int32 initialCapacity);
  388. /**
  389. * The destructor; virtual so that subclass destructors are invoked as well.
  390. *
  391. * @stable ICU 3.2
  392. */
  393. virtual ~LocaleRuns();
  394. /**
  395. * Get the <code>Locale</code> object assoicated with the given run
  396. * of text. Use <code>RunArray::getLimit(run)</code> to get the corresponding
  397. * limit index.
  398. *
  399. * @param run is the index into the font and limit indices arrays.
  400. *
  401. * @return the <code>Locale</code> associated with the given text run.
  402. *
  403. * @see RunArray::getLimit
  404. *
  405. * @stable ICU 3.2
  406. */
  407. const Locale *getLocale(le_int32 run) const;
  408. /**
  409. * Add a <code>Locale</code> and limit index pair to the data arrays and return
  410. * the run index where the data was stored. This method calls
  411. * <code>RunArray::add(limit)</code> which will create or grow the arrays as needed.
  412. *
  413. * If the <code>LocaleRuns</code> object was created with a client-supplied
  414. * locale and limit indices arrays, this method will return a run index of -1.
  415. *
  416. * Subclasses should not override this method. Rather they should provide a new <code>add</code>
  417. * method which takes a locale and a limit index along with whatever other data they implement.
  418. * The new <code>add</code> method should first call this method to grow the font and limit indices
  419. * arrays, and use the returned run index to store data their own arrays.
  420. *
  421. * @param locale is the address of the <code>Locale</code> to add. This object must remain valid
  422. * until the <code>LocaleRuns</code> object is destroyed.
  423. *
  424. * @param limit is the limit index to add
  425. *
  426. * @return the run index where the locale and limit index were stored, or -1 if the data cannot be stored.
  427. *
  428. * @stable ICU 3.2
  429. */
  430. le_int32 add(const Locale *locale, le_int32 limit);
  431. /**
  432. * ICU "poor man's RTTI", returns a UClassID for this class.
  433. *
  434. * @stable ICU 3.2
  435. */
  436. static inline UClassID getStaticClassID() { return (UClassID)&fgClassID; }
  437. /**
  438. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  439. *
  440. * @stable ICU 3.2
  441. */
  442. virtual inline UClassID getDynamicClassID() const { return getStaticClassID(); }
  443. protected:
  444. virtual void init(le_int32 capacity);
  445. virtual void grow(le_int32 capacity);
  446. /**
  447. * @internal
  448. */
  449. const Locale **fLocales;
  450. private:
  451. inline LocaleRuns();
  452. inline LocaleRuns(const LocaleRuns &other);
  453. inline LocaleRuns &operator=(const LocaleRuns & /*other*/) { return *this; };
  454. /**
  455. * The address of this static class variable serves as this class's ID
  456. * for ICU "poor man's RTTI".
  457. */
  458. static const char fgClassID;
  459. };
  460. inline LocaleRuns::LocaleRuns()
  461. : RunArray(0), fLocales(NULL)
  462. {
  463. // nothing else to do...
  464. }
  465. inline LocaleRuns::LocaleRuns(const LocaleRuns & /*other*/)
  466. : RunArray(0), fLocales(NULL)
  467. {
  468. // nothing else to do...
  469. }
  470. inline LocaleRuns::LocaleRuns(const Locale **locales, const le_int32 *limits, le_int32 count)
  471. : RunArray(limits, count), fLocales(locales)
  472. {
  473. // nothing else to do...
  474. }
  475. /**
  476. * The <code>ValueRuns</code> class associates integer values with runs of text.
  477. *
  478. * @stable ICU 3.2
  479. */
  480. class U_LAYOUTEX_API ValueRuns : public RunArray
  481. {
  482. public:
  483. /**
  484. * Construct a <code>ValueRuns</code> object from pre-existing arrays of values
  485. * and limit indices.
  486. *
  487. * @param values is the address of an array of integer. This array must remain valid until
  488. * the <code>ValueRuns</code> object is destroyed.
  489. *
  490. * @param limits is the address of an array of limit indices. This array must remain valid until
  491. * the <code>ValueRuns</code> object is destroyed.
  492. *
  493. * @param count is the number of entries in the two arrays.
  494. *
  495. * @stable ICU 3.2
  496. */
  497. inline ValueRuns(const le_int32 *values, const le_int32 *limits, le_int32 count);
  498. /**
  499. * Construct an empty <code>ValueRuns</code> object. Clients can add value and limit
  500. * indices arrays using the <code>add</code> method.
  501. *
  502. * @param initialCapacity is the initial size of the value and limit indices arrays. If
  503. * this value is zero, no arrays will be allocated.
  504. *
  505. * @see add
  506. *
  507. * @stable ICU 3.2
  508. */
  509. ValueRuns(le_int32 initialCapacity);
  510. /**
  511. * The destructor; virtual so that subclass destructors are invoked as well.
  512. *
  513. * @stable ICU 3.2
  514. */
  515. virtual ~ValueRuns();
  516. /**
  517. * Get the integer value assoicated with the given run
  518. * of text. Use <code>RunArray::getLimit(run)</code> to get the corresponding
  519. * limit index.
  520. *
  521. * @param run is the index into the font and limit indices arrays.
  522. *
  523. * @return the integer value associated with the given text run.
  524. *
  525. * @see RunArray::getLimit
  526. *
  527. * @stable ICU 3.2
  528. */
  529. le_int32 getValue(le_int32 run) const;
  530. /**
  531. * Add an integer value and limit index pair to the data arrays and return
  532. * the run index where the data was stored. This method calls
  533. * <code>RunArray::add(limit)</code> which will create or grow the arrays as needed.
  534. *
  535. * If the <code>ValueRuns</code> object was created with a client-supplied
  536. * font and limit indices arrays, this method will return a run index of -1.
  537. *
  538. * Subclasses should not override this method. Rather they should provide a new <code>add</code>
  539. * method which takes an integer value and a limit index along with whatever other data they implement.
  540. * The new <code>add</code> method should first call this method to grow the font and limit indices
  541. * arrays, and use the returned run index to store data their own arrays.
  542. *
  543. * @param value is the integer value to add
  544. *
  545. * @param limit is the limit index to add
  546. *
  547. * @return the run index where the value and limit index were stored, or -1 if the data cannot be stored.
  548. *
  549. * @stable ICU 3.2
  550. */
  551. le_int32 add(le_int32 value, le_int32 limit);
  552. /**
  553. * ICU "poor man's RTTI", returns a UClassID for this class.
  554. *
  555. * @stable ICU 3.2
  556. */
  557. static inline UClassID getStaticClassID() { return (UClassID)&fgClassID; }
  558. /**
  559. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  560. *
  561. * @stable ICU 3.2
  562. */
  563. virtual inline UClassID getDynamicClassID() const { return getStaticClassID(); }
  564. protected:
  565. virtual void init(le_int32 capacity);
  566. virtual void grow(le_int32 capacity);
  567. private:
  568. inline ValueRuns();
  569. inline ValueRuns(const ValueRuns &other);
  570. inline ValueRuns &operator=(const ValueRuns & /*other*/) { return *this; };
  571. /**
  572. * The address of this static class variable serves as this class's ID
  573. * for ICU "poor man's RTTI".
  574. */
  575. static const char fgClassID;
  576. const le_int32 *fValues;
  577. };
  578. inline ValueRuns::ValueRuns()
  579. : RunArray(0), fValues(NULL)
  580. {
  581. // nothing else to do...
  582. }
  583. inline ValueRuns::ValueRuns(const ValueRuns & /*other*/)
  584. : RunArray(0), fValues(NULL)
  585. {
  586. // nothing else to do...
  587. }
  588. inline ValueRuns::ValueRuns(const le_int32 *values, const le_int32 *limits, le_int32 count)
  589. : RunArray(limits, count), fValues(values)
  590. {
  591. // nothing else to do...
  592. }
  593. U_NAMESPACE_END
  594. #endif